【发布时间】:2017-01-01 18:23:53
【问题描述】:
我正在做一个学校项目,我必须在 MVP 架构模式中编写一个 android 项目。我也开始对 Google Dagger 2 进行一些研究。现在我只是想检查数据库中是否存在用户。众所周知,问题在于传递上下文。我寻找一个简洁的答案,但我找不到任何东西。我就是这样做的。
使用 Dagger 2 作为上下文的代码:
public class DaggerApplication extends Application {
DaggerComponent daggerComponent;
@Override
public void onCreate() {
super.onCreate();
daggerComponent = DaggerComponent.builder().daggerModule(new DaggerModule(this)).build();
daggerComponent.inject(this);
}
public DaggerComponent getAppComponent(){
return daggerComponent;
}
}
@Module
public class DaggerModule {
private final DaggerApplication daggerApplication;
public DaggerModule(DaggerApplication daggerApplication){
this.daggerApplication = daggerApplication;
}
@Provides
@Singleton
Context providesApplicationContext() {
return this.daggerApplication;
}
@Provides
@Singleton
SharedPreferences providesSharedPreferences(Context context) {
return context.getSharedPreferences("My_Pref", Context.MODE_PRIVATE);
}
}
@Singleton
@Component
(modules = {DaggerModule.class})
public interface DaggerComponent {
void inject(DaggerApplication daggerApplication);
void inject(SigninActivityFragment signinActivityFragment);
}
然后我在片段中获取我的上下文,如下所示:
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
((DaggerApplication)getActivity().getApplication()).getAppComponent().inject(this);
accountPresenter = new AccountPresenter(this,new MyDatabaseSource(context));
}
MyDatabaseSource 需要此上下文才能使数据库正常工作。然后这个 MyDatabaseSource 打开数据库从数据库中获取用户并关闭它。像这样的:
public class MyDatabaseSource implements MyModel {
public MyDatabaseSource(@NonNull Context context) {
checkNotNull(context);
myLocalDatabaseOpenHelper = new MyLocalDatabaseOpenHelper(context);
}
//Implementing MyModel functions here
}
这就是我需要上下文的原因。
public class MyLocalDatabaseOpenHelper extends SQLiteOpenHelper {
private static final String MY_LOCAL_DATABASE_NAME = "MyUserDatabase";
private static final int MY_LOCAL_DATABASE_VERSION = 1;
public MyLocalDatabaseOpenHelper(Context context) {
super(context, MY_LOCAL_DATABASE_NAME, null, MY_LOCAL_DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
//Created my database here
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Not required as at version 1
}
@Override
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Not required as at version 1
}
}
我创建了 MyDatabaseSource 实现的接口并将其命名为 MyModel。
代码如下:
public interface MyModel {
void insertUser(User user);
void deleteUser(int index);
void updateUser(User user);
ArrayList <User> getUsers();
User getUser(int index);
}
然后我的演示者拥有了成为我的视图和模型之间的中间人所需的一切。
这是我的演示者代码:
public AccountPresenter(AccountView accountView, MyModel myModel) {
this.accountView = accountView;
this.myModel = myModel;
}
public void onSignInButtonClicked() {
String username = accountView.getUsername();
String password = accountView.getPassword();
if (username.isEmpty()) {
accountView.setUsernameErrorMessage(R.string.activity_signin_username_error);
}
if (password.isEmpty()) {
accountView.setPasswordErrorMessage(R.string.activity_signin_password_error);
}
ArrayList<User> userArrayList = myModel.getUsers();
if (userArrayList.size() != 0) {
for (int i = 0; i < userArrayList.size(); i++) {
if (username.equals(userArrayList.get(i).getUserName())) {
if (password.equals(userArrayList.get(i).getUserPassword())) {
accountView.showUserExists(R.string.activity_signin_user_exists_toast);
} else {
accountView.showIncorrectPassword(R.string.activity_signin_incorrect_password_toast);
}
} else {
accountView.showUserDoesNotExist(R.string.activity_signin_user_does_not_exist_toast);
}
}
} else {
accountView.showNoUserExists(R.string.activity_signin_no_exists_toast);
}
}
我想知道这是否是使用 Google Dagger 2 实现 MVP 的正确方法。我个人觉得应该有另一种实现方法;将上下文传递给演示者超出了使用 Google Dagger 的目的。我刚开始研究 Google Dagger 2 和 MVP 模式。因此,任何建议都会有所帮助。我只是希望它是正确的。不想丢分。 :)
任何建议和意见都会有所帮助。 :)
【问题讨论】:
-
你可能会发现这篇文章很有用Why Activities in Android are not UI Elements