プロジェクトでMVVM構造とデータバインディングを使用しています。 GG/FBサインインに関しては、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
にはContext
が必要なので、DataBindingイベントを受け取るViewModelに渡すことはできません。
class LoginViewModel(
dataManager: DataManager,
schedulerProvider: SchedulerProvider
) : BaseViewModel<LoginNavigator>(dataManager, schedulerProvider) {
fun loginGoogle(){
setIsLoading(true)
//No idea what to write in here
}
}
MVVM構造でGg/FBサインインを使用する方法はありますか?または、私は元の方法を実行する必要があります(Activity
ですべてを行う)?
@ Erik Browne とレシーバー付きのKotlinの関数リテラルのおかげで、それは非常に簡単です
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
}
}
インターフェースを作成
interface ActivityNavigation {
fun startActivityForResult(intent: Intent?, requestCode: Int)
}
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)
)
}
}
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)
}