@@ -250,6 +250,13 @@ private void changePassword(String oldPassword, String newPassword) {
250250 }
251251 }
252252
253+ private Completable unlinkProvider (@ NonNull FirebaseUser user , @ NonNull Button button , @ NonNull Provider provider , @ StringRes int providerName ) {
254+ return user .unlink (provider .getProviderId ())
255+ .andThen (user .reload ())
256+ .observeOn (AndroidSchedulers .mainThread ())
257+ .doOnComplete (() -> setButtonText (button , provider , providerName ));
258+ }
259+
253260 private void setupSignInAnonymouslyButton (FirebaseAuth firebaseAuth ) {
254261 setupButton (firebaseAuth ,
255262 signInAnonymouslyButton ,
@@ -332,11 +339,7 @@ public void onSuccess(LoginResult loginResult) {
332339 v -> {
333340 FirebaseUser user = firebaseAuth .getCurrentUser ();
334341 if (user != null && user .isSignedInWith (Provider .FACEBOOK )) {
335- onDestroy .add (user
336- .unlink (Provider .FACEBOOK .getProviderId ())
337- .andThen (user .reload ())
338- .observeOn (AndroidSchedulers .mainThread ())
339- .doOnComplete (() -> setButtonText (signInWithFacebookButton , Provider .FACEBOOK , R .string .facebook ))
342+ onDestroy .add (unlinkProvider (user , facebookLoginButton , Provider .FACEBOOK , R .string .facebook )
340343 .subscribe (() -> {}, RxUtil .ON_ERROR_LOG_V3 ));
341344 } else {
342345 facebookLoginButton .performClick ();
@@ -353,28 +356,35 @@ public void onSuccess(LoginResult loginResult) {
353356 private void setupSignInWithGoogleButton (FirebaseAuth firebaseAuth ) {
354357 setupButton (firebaseAuth ,
355358 signInWithGoogleButton ,
356- v -> onDestroy .add (RxJavaBridge
357- .toV3Disposable (new RxInlineActivityResult (this )
358- .request (googleSignInClient .getSignInIntent ())
359- .map (result -> {
360- Intent data = result .getData ();
361- return GoogleSignIn .getSignedInAccountFromIntent (data )
362- .getResult ();
363- })
364- .flatMapSingle (account -> {
365- String token = account .getIdToken ();
366- IdpAuthCredential credential = GoogleAuthProvider .getCredential (token );
367- return RxJavaBridge .toV2Single (firebaseAuth .signInWithCredential (credential ));
368- })
369- .doOnError (e -> {
370- dialog (e );
371- googleSignInClient .signOut ();
372- })
373- .subscribe (io .reactivex .internal .functions .Functions .emptyConsumer (), RxUtil .ON_ERROR_LOG_V2 ))),
359+ v -> {
360+ FirebaseUser user = firebaseAuth .getCurrentUser ();
361+ if (user != null && user .isSignedInWith (Provider .GOOGLE )) {
362+ onDestroy .add (unlinkProvider (user , signInWithGoogleButton , Provider .GOOGLE , R .string .google )
363+ .subscribe (() -> {}, RxUtil .ON_ERROR_LOG_V3 ));
364+ } else {
365+ onDestroy .add (RxJavaBridge
366+ .toV3Disposable (new RxInlineActivityResult (this )
367+ .request (googleSignInClient .getSignInIntent ())
368+ .map (result -> {
369+ Intent data = result .getData ();
370+ return GoogleSignIn .getSignedInAccountFromIntent (data )
371+ .getResult ();
372+ })
373+ .flatMapSingle (account -> {
374+ String token = account .getIdToken ();
375+ IdpAuthCredential credential = GoogleAuthProvider .getCredential (token );
376+ return RxJavaBridge .toV2Single (firebaseAuth .signInWithCredential (credential ));
377+ })
378+ .doOnError (e -> {
379+ dialog (e );
380+ googleSignInClient .signOut ();
381+ })
382+ .subscribe (io .reactivex .internal .functions .Functions .emptyConsumer (), RxUtil .ON_ERROR_LOG_V2 )));
383+ }
384+ },
374385 auth -> {
375- boolean isLoggedOut = !auth .isSignedIn ();
376386 setButtonText (signInWithGoogleButton , Provider .GOOGLE , R .string .google );
377- if (isLoggedOut ) {
387+ if (! auth . isSignedInWith ( Provider . GOOGLE ) ) {
378388 googleSignInClient .signOut ();
379389 }
380390 });
0 commit comments