-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Yang auth #476
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Yang auth #476
Conversation
64c250c
to
dfde7f7
Compare
android/canonical/app/src/main/java/com/google/samples/quickstart/canonical/MainActivity.kt
Outdated
Show resolved
Hide resolved
android/canonical/app/src/main/java/com/google/samples/quickstart/canonical/MainActivity.kt
Outdated
Show resolved
Hide resolved
android/canonical/app/src/main/java/com/google/samples/quickstart/canonical/MeFragment.kt
Outdated
Show resolved
Hide resolved
android/canonical/app/src/main/java/com/google/samples/quickstart/canonical/MainActivity.kt
Outdated
Show resolved
Hide resolved
android/canonical/app/src/main/java/com/google/samples/quickstart/canonical/MeFragment.kt
Outdated
Show resolved
Hide resolved
android/canonical/app/src/main/java/com/google/samples/quickstart/canonical/RunFragment.kt
Outdated
Show resolved
Hide resolved
android/canonical/app/src/main/java/com/google/samples/quickstart/canonical/MeFragment.kt
Outdated
Show resolved
Hide resolved
2. Reduce unnecessary variables in MainActivity 3. Using ViewModel to save the user 4. Fix bug in firebaseAuthWithGoogle addOnCompleteListener 5. Remove the activity in backstack.
android/canonical/app/src/main/java/com/google/samples/quickstart/canonical/LoginFragment.kt
Outdated
Show resolved
Hide resolved
android/canonical/app/src/main/java/com/google/samples/quickstart/canonical/LoginFragment.kt
Show resolved
Hide resolved
android/canonical/app/src/main/java/com/google/samples/quickstart/canonical/LoginFragment.kt
Outdated
Show resolved
Hide resolved
android/canonical/app/src/main/java/com/google/samples/quickstart/canonical/MainActivity.kt
Outdated
Show resolved
Hide resolved
android/canonical/app/src/main/java/com/google/samples/quickstart/canonical/LoginFragment.kt
Outdated
Show resolved
Hide resolved
android/canonical/app/src/main/java/com/google/samples/quickstart/canonical/LoginFragment.kt
Outdated
Show resolved
Hide resolved
android/canonical/app/src/main/java/com/google/samples/quickstart/canonical/LoginFragment.kt
Outdated
Show resolved
Hide resolved
android/canonical/app/src/main/java/com/google/samples/quickstart/canonical/LoginFragment.kt
Outdated
Show resolved
Hide resolved
android/canonical/app/src/main/java/com/google/samples/quickstart/canonical/LoginFragment.kt
Outdated
Show resolved
Hide resolved
android/canonical/app/src/main/java/com/google/samples/quickstart/canonical/LoginFragment.kt
Show resolved
Hide resolved
android/canonical/app/src/main/java/com/google/samples/quickstart/canonical/SignInViewModel.kt
Outdated
Show resolved
Hide resolved
android/canonical/app/src/main/java/com/google/samples/quickstart/canonical/MeFragment.kt
Outdated
Show resolved
Hide resolved
android/canonical/app/src/main/java/com/google/samples/quickstart/canonical/MeFragment.kt
Outdated
Show resolved
Hide resolved
android/canonical/app/src/main/java/com/google/samples/quickstart/canonical/SignInViewModel.kt
Outdated
Show resolved
Hide resolved
android/canonical/app/src/main/java/com/google/samples/quickstart/canonical/SignInViewModel.kt
Outdated
Show resolved
Hide resolved
2. Add AuthStateListener and oberver to lisen the change of current login status 3. Solve the coordination between async operations
signInVM.signOut() | ||
} | ||
// update UI | ||
view.findViewById<TextView>(R.id.textView)?.text = signInVM.getFirebaseAuthCurUser()?.email |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can either bind the viewmodel's currentUser email to fragment_me.xml or update this in the observer when currentUser is not null (maybe move the observer down here).
data class CurFirebaseUser( | ||
var firebaseUser: MutableLiveData<FirebaseUser?> = MutableLiveData(FirebaseAuth.getInstance().currentUser), | ||
var isLogin: MutableLiveData<Boolean> = MutableLiveData(firebaseUser.value?.let { true } ?: run{ false }) | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we want to wrap the Firebaseuser to antoher class. Seems we can just have something like this
val currentUser : MutableLiveData<FirebaseUser?> by lazy {
MutableLiveData<FirebaseUser?> (FirebaseAuth.getInstance().currentUser)
}
And we can just determine the isLogin status by checking the value of currentUser. Keeping another isLogin might be error prone, cuz you will need to update 2 values together if something has changed. What if you forget to update one of them, it can cause the 2 fields out of sync.
fun getFirebaseAuthLogStatusLiveData(): MutableLiveData<Boolean> { | ||
return curFirebaseUser.value!!.isLogin | ||
} | ||
|
||
fun getFirebaseAuthCurUser(): FirebaseUser? { | ||
return curFirebaseUser.value!!.firebaseUser.value | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These seems unnecessary. In the fragments, we can just observe the currentUser, something like this
signInVM.currentUser.observe(this, Observer {
if (it != null) {
// do something with user logged in
} else {
// do something with user not logged in
}
})
private fun googleSignInInit() { | ||
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) | ||
.requestIdToken(context.getString(R.string.default_web_client_id)) | ||
.requestEmail() | ||
.build() | ||
|
||
googleSignInClient = GoogleSignIn.getClient(activity, gso) | ||
Log.d(SIGN_IN_VM_TAG, "googleSignInClientInit") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
private fun googleSignInInit() { | |
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) | |
.requestIdToken(context.getString(R.string.default_web_client_id)) | |
.requestEmail() | |
.build() | |
googleSignInClient = GoogleSignIn.getClient(activity, gso) | |
Log.d(SIGN_IN_VM_TAG, "googleSignInClientInit") | |
} | |
private fun getGoogleSignInClient() : GoogleSignInClient { | |
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) | |
.requestIdToken(context.getString(R.string.default_web_client_id)) | |
.requestEmail() | |
.build() | |
Log.d(SIGN_IN_VM_TAG, "googleSignInClientInit") | |
return GoogleSignIn.getClient(activity, gso) | |
} |
Seems this can just return a GoogleSignInClient. And the googleSignInClient field on top is not necessary.
This PR contains 2 updates
The last commit: