java 具有MVVM模式的Android ViewModel类示例。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 具有MVVM模式的Android ViewModel类示例。相关的知识,希望对你有一定的参考价值。
package com.dev.ui.auth;
import android.app.Activity;
import android.content.Intent;
import android.databinding.ObservableField;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.GraphRequest;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;
import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.auth.api.signin.GoogleSignInResult;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.mevics.dev.R;
import com.mevics.dev.data.models.User;
import com.mevics.dev.data.source.AuthDataSource;
import com.mevics.dev.data.source.AuthRepository;
import com.mevics.dev.data.source.SettingsRepository;
import com.mevics.dev.data.source.UserRepository;
import com.mevics.dev.ui.dialog.AuthDialog;
import com.mevics.dev.ui.scan.ScanActivity;
import com.mevics.dev.util.MessageEvent;
import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import java.util.Arrays;
import butterknife.BindView;
import butterknife.ButterKnife;
/**
* Exposes the data to be used in the auth screen.
*/
public class MyViewModel implements GoogleApiClient.OnConnectionFailedListener {
public final ObservableField<String> name =
new ObservableField<>();
public final ObservableField<String> email =
new ObservableField<>();
public final ObservableField<String> pwd =
new ObservableField<>();
private Activity context;
private AuthRepository authRepository;
private UserRepository userRepository;
private GoogleApiClient googleApiClient;
private GoogleSignInOptions gso;
private static final int GOOGLE_AUTH = 101;
private static final int FACEBOOK_AUTH = 102;
private static final int FORGOT_PWD = 103;
private CallbackManager callbackManager;
private FragmentManager fragmentManager;
private LoginButton loginButton;
public AuthViewModel(Activity context, CallbackManager callbackManager,
FragmentManager fragmentManager, LoginButton loginButton){
this.context = context;
this.callbackManager = callbackManager;
this.fragmentManager = fragmentManager;
this.loginButton = loginButton;
UserRepository.getInstance().deleteUser();
SettingsRepository.getInstance().deleteSettings(null);
authRepository = AuthRepository.getInstance();
userRepository = UserRepository.getInstance();
gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
googleApiClient = new GoogleApiClient.Builder(context)
.enableAutoManage((FragmentActivity) context, this)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
}
public void onClickedAuth(){
if(name.get() != null) {
authRepository.signUp(email.get(), pwd.get(), name.get(),
new AuthDataSource.AuthCallback() {
@Override
public void onAuthLoaded(User user) {
Log.wtf("EVERT", user.getPhotoUrl());
setLocalData();
Intent intent = new Intent(context,
ScanActivity.class);
context.startActivity(intent);
context.finish();
}
@Override
public void onDataNotAvailable(Throwable err) {
Log.wtf("EVERT", err.getMessage());
}
});
} else {
authRepository.logIn(email.get(), pwd.get(), new AuthDataSource.AuthCallback() {
@Override
public void onAuthLoaded(User user) {
Log.wtf("EVERT", user.getPhotoUrl());
setLocalData();
Intent intent = new Intent(context,
ScanActivity.class);
context.startActivity(intent);
context.finish();
}
@Override
public void onDataNotAvailable(Throwable err) {
Log.wtf("EVERT", err.getMessage());
}
});
}
}
public void onClickedForgotPwd(){
AuthDialog.newInstance(FORGOT_PWD, null).show(fragmentManager);
}
public void onClickedAuthGoogle(){
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(googleApiClient);
context.startActivityForResult(signInIntent, GOOGLE_AUTH);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
callbackManager.onActivityResult(requestCode, resultCode, data);
if (requestCode == GOOGLE_AUTH) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
int statusCode = result.getStatus().getStatusCode();
Log.wtf("EVERT", String.valueOf(statusCode));
if (result.isSuccess()) {
GoogleSignInAccount acct = result.getSignInAccount();
authRepository.googleAuth(acct.getEmail(), acct.getId(),
context.getResources().getString(R.string.secret),
new AuthDataSource.AuthCallback() {
@Override
public void onAuthLoaded(User user) {
Log.wtf("EVERT", user.getPhotoUrl());
setLocalData();
Intent intent = new Intent(context,
ScanActivity.class);
context.startActivity(intent);
context.finish();
}
@Override
public void onDataNotAvailable(Throwable err) {
Log.wtf("EVERT", err.getMessage());
}
});
}
}
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
private void setLocalData(){
userRepository.setCurrentPostureState(false);
userRepository.setUserState(false);
}
}
以上是关于java 具有MVVM模式的Android ViewModel类示例。的主要内容,如果未能解决你的问题,请参考以下文章
Android MVVM startActivity 的最佳实践
WPF 在 MVVM 模式下实现窗口后台代码与ViewModel交互