我在使用 Java 中的 MVVM 实现 Google 登录时遇到问题。在这里,您将以正常方式看到来自 Google 的示例代码:
问题:
在你的活动中:
@Override
public void onCreate(Bundle savedInstanceState) {
/* Here is the Issue:
* Google Object is defined in View - Activity
* I would like to have Google Object defined in my ViewModel
*/
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestEmail().build();
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
}
// when Google Button CLicked
@Override
public void onClick(View v) { signIn(); }
private void signIn() {
/* Here is the Issue:
* I have to get this process done in View Model
* so view will not reference any Google Object
*/
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Below will be processed in ViewModel
GoogleSignInClient.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(task);
}
}
问题: *见评论
所以我想出了下面的想法:
在活动中:
// when Google Button CLicked
@Override
public void onClick(View v) { viewModel.loginGoogle(); }
private void subscribeUi() {
// register startActivityForResult Event to ViewModel and set this activity as receiver...
// viewModel.startActivityForResultEvent.setEventReceiver(this Activity)
// How to do this?
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// send the result to View Model
viewModel.onResultFromActivity(requestCode,resultCode,data);
// escallate to super
super.onActivityResult(requestCode, resultCode, data)
}
大话西游666
临摹微笑
狐的传说
慕盖茨4494581
相关分类