私はMVPアーキテクチャーパターンでアンドロイドプロジェクトをコーディングする必要がある学校プロジェクトに取り組んでいます。私はGoogle Dagger 2についても少しの研究を始めました。今のところ私は単にユーザーがデータベースに存在するかどうかをチェックしようとしています。誰もが知っているように、問題は文脈を渡すことです。私は簡潔な答えを探しましたが、何も見つかりませんでした。これが私のやり方です。コンテキストにダガー2を使用するためのAndroid MVP + Google Dagger 2 + SQLite
コード:
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
}
これは私にコンテキストが必要な理由です。
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のダガー2を使用してMVPを実装するための正しい方法であれば、これを実現するための別の方法があるはずのように、私は個人的に感じて知っていただきたいと思います。プレゼンターにコンテキストを渡すことは、Google Daggerを使用する目的よりも優れています。私はちょうどGoogle Dagger 2とMVPのパターンについて研究を始めました。だから、どんな提案も役に立ちます。私はちょうどそれが正しいと思う。グレードを失いたくない。 :)
ご意見やアドバイスをお寄せください。 :)プレゼンターにコンテキストを渡す
この記事は役に立つと思われる[AndroidでのアクティビティがUI要素でない理由](http://www.techyourchance.com/activities-android/) – tchelidze