-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(facebook): move Firebase calls to ViewModel
- Loading branch information
1 parent
e8386de
commit dbcfb82
Showing
2 changed files
with
122 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
auth/app/src/main/java/com/google/firebase/quickstart/auth/kotlin/FacebookLoginViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package com.google.firebase.quickstart.auth.kotlin | ||
|
||
import android.util.Log | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.facebook.AccessToken | ||
import com.facebook.login.LoginManager | ||
import com.google.firebase.auth.FacebookAuthProvider | ||
import com.google.firebase.auth.FirebaseAuth | ||
import com.google.firebase.auth.FirebaseUser | ||
import com.google.firebase.auth.ktx.auth | ||
import com.google.firebase.ktx.Firebase | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.update | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.tasks.await | ||
|
||
class FacebookLoginViewModel( | ||
private val firebaseAuth: FirebaseAuth = Firebase.auth | ||
) : ViewModel() { | ||
private val _uiState = MutableStateFlow(UiState()) | ||
val uiState: StateFlow<UiState> = _uiState | ||
|
||
data class UiState( | ||
var status: String = "", | ||
var detail: String? = null, | ||
var isSignInVisible: Boolean = true, | ||
var isProgressBarVisible: Boolean = false | ||
) | ||
|
||
init { | ||
// Check if user is signed in (non-null) and update UI accordingly. | ||
val firebaseUser = firebaseAuth.currentUser | ||
updateUiState(firebaseUser) | ||
} | ||
|
||
fun handleFacebookAccessToken(token: AccessToken) { | ||
Log.d(TAG, "handleFacebookAccessToken:$token") | ||
toggleProgressbar(isVisible = true) | ||
|
||
val credential = FacebookAuthProvider.getCredential(token.token) | ||
viewModelScope.launch { | ||
try { | ||
val authResult = firebaseAuth.signInWithCredential(credential).await() | ||
// Sign in success, update UI with the signed-in user's information | ||
Log.d(TAG, "signInWithCredential:success") | ||
updateUiState(authResult.user) | ||
} catch (e: Exception) { | ||
// If sign in fails, display a message to the user. | ||
Log.w(TAG, "signInWithCredential:failure", e) | ||
// TODO(thatfiredev): Show snackbar "Authentication failed." | ||
updateUiState(null) | ||
} finally { | ||
toggleProgressbar(isVisible = false) | ||
} | ||
} | ||
} | ||
|
||
fun showInitialState() { | ||
updateUiState(null) | ||
} | ||
|
||
fun signOut() { | ||
firebaseAuth.signOut() | ||
LoginManager.getInstance().logOut() | ||
updateUiState(null) | ||
} | ||
|
||
fun updateUiState(user: FirebaseUser?) { | ||
if (user != null) { | ||
_uiState.update { currentUiState -> | ||
currentUiState.copy( | ||
status = "Facebook User: ${user.displayName}", | ||
detail = "Firebase UID: ${user.uid}", | ||
isSignInVisible = false, | ||
isProgressBarVisible = false | ||
) | ||
} | ||
} else { | ||
_uiState.update { currentUiState -> | ||
currentUiState.copy( | ||
status = "Signed out", | ||
detail = null, | ||
isSignInVisible = true, | ||
isProgressBarVisible = false | ||
) | ||
} | ||
} | ||
} | ||
|
||
private fun toggleProgressbar(isVisible: Boolean) { | ||
_uiState.update { it.copy(isProgressBarVisible = isVisible) } | ||
} | ||
|
||
companion object { | ||
const val TAG = "FacebookLoginViewModel" | ||
} | ||
} |