package net.squanchy.signin;

import com.google.android.gms.auth.api.signin.GoogleSignInAccount;

import net.squanchy.service.firebase.FirebaseAuthService;

import io.reactivex.Completable;
import io.reactivex.schedulers.Schedulers;

public class SignInService {

    private final FirebaseAuthService authService;

    SignInService(FirebaseAuthService authService) {
        this.authService = authService;
    }

    public Completable signInAnonymouslyIfNecessary() {
        return authService.currentUser()
                .flatMapCompletable(user -> {
                    if (user.isPresent()) {
                        return Completable.complete();
                    }

                    return authService.signInAnonymously();
                });
    }

    Completable signInWithGoogle(GoogleSignInAccount account) {
        return authService.signInWithGoogle(account)
                .subscribeOn(Schedulers.io());
    }
}

@@ -89,16 +89,62 @@
 Account;
+%0Aimport com.google.firebase.auth.FirebaseUser;
 %0A%0Aimport
@@ -199,36 +199,111 @@
 ce;%0A
-%0Aimport io.reactivex.Complet
+import net.squanchy.support.lang.Optional;%0A%0Aimport io.reactivex.Completable;%0Aimport io.reactivex.Observ
 able
@@ -878,24 +878,181 @@
  %7D);%0A    %7D%0A%0A
+    public Observable%3COptional%3CFirebaseUser%3E%3E currentUser() %7B%0A        return authService.currentUser()%0A                .subscribeOn(Schedulers.io());%0A    %7D%0A%0A
     Completa

Add SignInService#currentUser()