I want to apply design pattern to my application. I think to divide it by this categories:
Where:
- LoginListener : implements the listener for the activity
- LoginNetwork : is the class where i want to execute the firebase code
- LoginNetworkListener : is the class where i want to insert the code for the firebase listener
- LoginLogic : is the class where i insert the code for initialize the googleSignIn for my application ( and the request permission )
- LoginController : there isn't code here
- LoginSignleton : is Singleton for all data necessary between the other Login classes.
This is my code: LoginActiivity:
public class LoginActivity extends AppCompatActivity {
private final static String TAG = "Login Activity";
private final static int RC_SIGN_IN = 9000;
private Button continueNoLogin;
private SignInButton googleLogin;
private LoginNetwork loginNetwork;
private LoginSingleton loginSingleton;
private LoginLogic loginLogic;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
FirebaseApp.initializeApp(this);
continueNoLogin = (Button) findViewById(R.id.login_unregistered);
googleLogin = (SignInButton) findViewById(R.id.login_registered_google);
loginNetwork = new LoginNetwork();
loginSingleton = LoginSingleton.getInstance();
loginLogic = new LoginLogic(this);
}
@Override
protected void onStart() {
super.onStart();
LoginListener loginListener = new LoginListener();
continueNoLogin.setOnClickListener(loginListener);
googleLogin.setOnClickListener(loginListener);
}
public void startLoginResultActivity(){
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(LoginSingleton.getInstance().getGoogleApiClient());
startActivityForResult(signInIntent,RC_SIGN_IN);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d(TAG,"OnActivityResult");
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
Log.d(TAG,"RC_SIGN_IN");
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if (result.isSuccess()) {
Log.d(TAG,"google signin successfull");
// Google Sign In was successful, authenticate with Firebase
} else {
Log.d(TAG,"SignInWithGoogle Failed " + result.getStatus());
// Google Sign In failed, update UI appropriately
}
}
}
}
LoginListener:
public class LoginListener implements View.OnClickListener,GoogleApiClient.OnConnectionFailedListener {
private final static String TAG = "LoginListener";
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.login_unregistered:
break;
case R.id.login_registered_google:
Log.d(TAG,"Ho ricevuto l'intent");
LoginActivity loginActivity = new LoginActivity();
loginActivity.startLoginResultActivity();
break;
}
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
}
LoginSingleton:
public class LoginSingleton {
private GoogleApiClient googleApiClient;
private GoogleSignInOptions googleSignInOptions;
private static LoginSingleton ourInstance = new LoginSingleton();
public static LoginSingleton getInstance() {
return ourInstance;
}
private LoginSingleton() {
}
public void setGoogleApiClient(GoogleApiClient googleApiClient) {
this.googleApiClient = googleApiClient;
}
public GoogleApiClient getGoogleApiClient() {
return googleApiClient;
}
public void setGoogleSignInOptions(GoogleSignInOptions googleSignInOptions) {
this.googleSignInOptions = googleSignInOptions;
}
public GoogleSignInOptions getGoogleSignInOptions() {
return googleSignInOptions;
}
}
LoginNetwork:
public class LoginNetwork {
private final static String TAG = "LoginNetwork";
private FirebaseAuth firebaseAuth;
public LoginNetwork(){
// Init FirebaseAuth
firebaseAuth = FirebaseAuth.getInstance();
// generate listener
LoginNetworkListener loginNetworkListener = new LoginNetworkListener();
firebaseAuth.addAuthStateListener(loginNetworkListener);
}
}
LoginNetworkListener:
public class LoginNetworkListener implements FirebaseAuth.AuthStateListener {
private final static String TAG = "LoginNetworkListener";
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if (firebaseAuth.getCurrentUser() != null){
Log.d(TAG,firebaseAuth.getCurrentUser().toString()+"is signedIn");
} else {
Log.d(TAG,"User is signedOut");
}
}
}
LoginLogic:
public class LoginLogic {
private final static String TAG = "LoginLogic";
private GoogleApiClient googleApiClient;
private GoogleSignInOptions googleSignInOptions;
public LoginLogic(AppCompatActivity activity){
LoginListener loginListener = new LoginListener();
/*
* Configure Google Signin
*/
LoginSingleton.getInstance().setGoogleSignInOptions(new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken("571158992107-7bifs7qrivnqpkk77gmmoko9rkg73c0d.apps.googleusercontent.com").requestEmail().build());
LoginSingleton.getInstance().setGoogleApiClient(new GoogleApiClient.Builder(activity).enableAutoManage(activity,loginListener).build());
}
}
When i start the application it give me this error:
FATAL EXCEPTION: main Process: it.unisalento.progetto.software.cunamameli.dafb, PID: 24354 java.lang.RuntimeException: Unable to start activity ComponentInfo{http://ift.tt/2dGLObD}: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process it.unisalento.progetto.software.cunamameli.dafb. Make sure to call FirebaseApp.initializeApp(Context) first. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2456)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2539)
at android.app.ActivityThread.access$900(ActivityThread.java:159)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1384)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:152)
at android.app.ActivityThread.main(ActivityThread.java:5507)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process it.unisalento.progetto.software.cunamameli.dafb. Make sure to call FirebaseApp.initializeApp(Context) first.
at com.google.firebase.FirebaseApp.getInstance(Unknown Source)
at com.google.firebase.auth.FirebaseAuth.getInstance(Unknown Source)
at it.unisalento.progetto.software.cunamameli.dafb.loginPack.LoginNetwork.<init>(LoginNetwork.java:19)
at it.unisalento.progetto.software.cunamameli.dafb.loginPack.LoginActivity.onCreate(LoginActivity.java:35)
at android.app.Activity.performCreate(Activity.java:6304)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2409)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2539)
at android.app.ActivityThread.access$900(ActivityThread.java:159)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1384)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:152)
at android.app.ActivityThread.main(ActivityThread.java:5507)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Where:
LoginNetwork.java:19 is -> firebaseAuth = FirebaseAuth.getInstance();
and
LoginActivity.java:35 is -> loginNetwork = new LoginNetwork();
I don't understand the where is the error. Please help me. Thanks a lot!
Aucun commentaire:
Enregistrer un commentaire