title: Android MVP + Google Dagger 2 + SQLite
body: <p>I am working on a school project where I have to code an android project in MVP architectural pattern. I started doing a bit of research on Google Dagger 2 as well. For now I am simply trying to check if a user exists in the database or not. The problem, as everyone knows is passing the context. I looked for a concise answer but I couldn't find anything. This is how I tried to do it.</p>
  
  <p>Code for using Dagger 2 for context:</p>
  
  <pre><code>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);
  }
  </code></pre>
  
  <p>I then get my context in the fragment like this:</p>
  
  <pre><code>@Override
      public void onActivityCreated(@Nullable Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
          ((DaggerApplication)getActivity().getApplication()).getAppComponent().inject(this);
          accountPresenter = new AccountPresenter(this,new MyDatabaseSource(context));
      }
  </code></pre>
  
  <p>The MyDatabaseSource needs this context for the database to work. Then this MyDatabaseSource opens the database gets the user from the database and closes it. Something like this:</p>
  
  <pre><code>public class MyDatabaseSource implements MyModel {
      public MyDatabaseSource(@NonNull Context context) {
          checkNotNull(context);
          myLocalDatabaseOpenHelper = new MyLocalDatabaseOpenHelper(context);
      }
      //Implementing MyModel functions here
  }
  </code></pre>
  
  <p>This is why I need the context.</p>
  
  <pre><code>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
      }
  }
  </code></pre>
  
  <p>I created the interface that MyDatabaseSource implements and I called it MyModel.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>public interface MyModel {
  
      void insertUser(User user);
      void deleteUser(int index);
      void updateUser(User user);
      ArrayList &lt;User&gt; getUsers();
      User getUser(int index);
  
  }
  </code></pre>
  
  <p>Then my presenter has everything it needs in-order to become the middle man between my view and model.</p>
  
  <p>This is my presenter code:</p>
  
  <pre><code>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&lt;User&gt; userArrayList = myModel.getUsers();
          if (userArrayList.size() != 0) {
              for (int i = 0; i &lt; 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);
          }
      }
  </code></pre>
  
  <p>I would like to know if this is the correct way of implementing MVP using Google Dagger 2. I personally feel like there should be another way to implement this; passing context to the presenter beats the purpose of using Google Dagger. I just started researching on Google Dagger 2 and MVP pattern. So any suggestions will be helpful. I just want it to be right. Don't want to loose a grade. :)</p>
  
  <p>Any suggestions and advice will be helpful. :)</p>