Google/facebook Sign In In Mvvm
I'm using MVVM structure with Data Binding in my project. Things get weird when it comes to GG/FB Sign In, because they need Context googleApiClient = new GoogleApiClient.Builder(c
Solution 1:
It is very simple thanks to @Erik Browne and Kotlin’s Function literals with receiver
Create SingleLiveEvent
Create LiveMessageEvent
class LiveMessageEvent<T> : SingleLiveEvent<(T.() -> Unit)?>() {
fun setEventReceiver(owner: LifecycleOwner, receiver: T) {
observe(owner, Observer { event ->
if ( event != null ) {
receiver.event()
}
})
}
fun sendEvent(event: (T.() -> Unit)?) {
value = event
}
}
Create interface
interface ActivityNavigation {
fun startActivityForResult(intent: Intent?, requestCode: Int)
}
** Now it's time to implement! **
In your LoginViewModel
const val GOOGLE_SIGN_IN : Int = 9001
class LoginViewModel @Inject constructor(
private val loginRepository: LoginRepository,
private val googleSignInClient: GoogleSignInClient
): ViewModel() {
val startActivityForResultEvent = LiveMessageEvent<ActivityNavigation>()
..
//Called on google login button click
fun googleSignUp() {
val signInIntent = googleSignInClient.signInIntent
startActivityForResultEvent.sendEvent { startActivityForResult(signInIntent, GOOGLE_SIGN_IN) }
}
//Called from Activity receving result
fun onResultFromActivity(requestCode: Int, resultCode: Int, data: Intent?) {
when(requestCode) {
GOOGLE_SIGN_IN -> {
val task = GoogleSignIn.getSignedInAccountFromIntent(data)
googleSignInComplete(task)
}
..
}
}
private fun googleSignInComplete(completedTask: Task<GoogleSignInAccount>) {
try {
val account = completedTask.getResult(ApiException::class.java)
account?.apply {
// .. Store user details
emitUiState(
showSuccess = Event(R.string.login_successful)
)
}
}catch (e: ApiException) {
emitUiState(
showError = Event(R.string.login_failed)
)
}
}
In your LoginActivty[Implement ActivityNavigation interface]
//Called from onCreate once the ViewModel is initialized.
private fun subscribeUi() {
//this sets the LifeCycler owner and receiver
viewModel.startActivityForResultEvent.setEventReceiver(this, this)
..
}
public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
viewModel.onResultFromActivity(requestCode,resultCode,data)
super.onActivityResult(requestCode, resultCode, data)
}
Post a Comment for "Google/facebook Sign In In Mvvm"