Google/Facebook Sign In in MVVM

2019-04-24 01:58发布

问题:

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(context)
            .enableAutoManage(this, this)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(googleApiClient);
startActivityForResult(signInIntent, GOOGLE_AUTH);

GoogleApiClient needs Context so I can't pass it to ViewModel, which receives DataBinding events.

class LoginViewModel(
    dataManager: DataManager,
    schedulerProvider: SchedulerProvider
) : BaseViewModel<LoginNavigator>(dataManager, schedulerProvider) {

    fun loginGoogle(){
        setIsLoading(true)
        //No idea what to write in here
    }
}

Is there any way to use Gg/FB Sign In with MVVM structure ? Or I just have to do the original way (do everything in Activity) ?

回答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

//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)
}

# Following this approach ensures LoginViewModel acts a link between the view(LoginActivity) and model(GoogleSignInClient) and view is only responsible for displaying the UI events.