7

i plan to use clean architecture with MVP.
I am beginning a android project using the clean architecture approach that uncle bob endorses. i've downloaded a template project that is kind of like a starter template to kickstart you when using clean architecute approach. The git hub is here: https://github.com/dmilicic/Android-Clean-Boilerplate.git

So we will have 3 layers; domain, presentation and threading per the template.

My question is about a login activity i am designing. I am creating a "sign in with google" button. but i am not sure where to put the googleAPIClient,googleSignInOptions and googleSignInResult calls. and after i get the google account authenticated i then pass it to firebaseAuth to log the user into my site so thats another API call as well that im not sure how it works. To know how to use a google account to log in to firebase application you can check here: https://www.androidtutorialpoint.com/firebase/android-firebase-authentication-tutorial-using-firebase-google-login/

so lets me explain why im having trouble with the template. lets start tracing users steps assuming he wants to sign in using google account:

  1. User hits the "sign in with google" button. that should trigger the UI to ask the presenter to begin google sign in attempt. so at this point should i make a interactor (use-case) for the presenter to use or should i initialize the googleAPICient directly in the presenter ? The concern is the google sign-in Api works by creating an intent and passing it a googleApiClient as a parameter. you then start that Intent using startActivityForResult. So thats all android code , so should it not be in the presentation layer then and specifically in the activity itself ? so it seems i have no choice but to call the google sign stuff from the view/Activity itself. correct ?

Then after i get the activity result from that call i was planning to log into firebase like this:

//this block of code is in the activity
@Override     
public void onActivityResult(int requestCode, int resultCode, Intent data) {         
    super.onActivityResult(requestCode, resultCode, data);           
    // Result returned from launching the Intent from 
    GoogleSignInApi.getSignInIntent(...);
    if (requestCode == RC_SIGN_IN) {
       GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
    if (result.isSuccess()) {
       // Google Sign In was successful, authenticate with Firebase
       googleSignInAccount account = result.getSignInAccount();
       //******* make the presenter log into firebase not the view
       presenter.firebaseAuthWithGoogle(account);              
    }
    else {
       // Google Sign In failed, update UI appropriately
       // ...
    }
}

Then in the presenter code :

//this code will be in the presenter class itself, should it be in in a interactor instead ?

private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
    Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());
    AuthCredential credential = 
    GoogleAuthProvider.getCredential(acct.getIdToken(), null);
    Auth.signInWithCredential(credential).addOnCompleteListener(this, new OnCompleteListener() {
          @Override
          public void onComplete(@NonNull Task task) {
              Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());
              if (!task.isSuccessful()) {
                  Log.w(TAG, "signInWithCredential", task.getException());
             }
    });
}
            
René Link
  • 48,224
  • 13
  • 108
  • 140
j2emanue
  • 60,549
  • 65
  • 286
  • 456
  • 1
    Following the MVP architecture, it means you'll have to avoid android code in presenter at all costs. That means you are correct in calling the signin code in activity – Aroniez May 16 '17 at 13:43
  • 2
    As of now 199 views but no answers. All the MVP tutorials have very simple examples where it's easy to avoid placing Android code in the Presenter. But what happens if we want to `StartActivityForResult`? Or pass `Parcelable` data through the Presenter to the Model layer? Or use Android `Bitmap`? There are so many Android API calls. I can place them all in the View but then that becomes more than just a UI handler and the Presenter is practically empty. – Simon Hutton Nov 02 '17 at 20:19
  • 1
    I'm having the same dilema. As I see, `GoogleApiClient` needs an Android `Context` that means it has to be built in your outer layer and if you are using dependency injection, it can be injected in your activity as a singleton when you need it. But I am not fully sure and I can be wrong. – Ch4vi Nov 29 '19 at 12:05

0 Answers0