| | |
| | | package com.duqing.missions.ui.login.view; |
| | | |
| | | import android.os.CountDownTimer; |
| | | import android.util.Patterns; |
| | | |
| | | import androidx.lifecycle.LiveData; |
| | |
| | | import androidx.lifecycle.ViewModel; |
| | | |
| | | import com.duqing.missions.R; |
| | | import com.duqing.missions.retrofit.AndroidScheduler; |
| | | import com.duqing.missions.ui.login.data.LoginRepository; |
| | | import com.duqing.missions.ui.login.data.model.LoggedInUser; |
| | | |
| | |
| | | import io.reactivex.disposables.Disposable; |
| | | import io.reactivex.functions.Consumer; |
| | | import io.reactivex.observers.DisposableObserver; |
| | | import io.reactivex.schedulers.Schedulers; |
| | | |
| | | public class LoginViewModel extends ViewModel { |
| | | |
| | | private MutableLiveData<LoginFormState> loginFormState = new MutableLiveData<>(); |
| | | private MutableLiveData<LoginResult> loginResult = new MutableLiveData<>(); |
| | | private LoginRepository loginRepository; |
| | | private MutableLiveData<Long> countDown = new MutableLiveData<>(); |
| | | private CountDownTimer timer; |
| | | |
| | | LoginViewModel(LoginRepository loginRepository) { |
| | | this.loginRepository = loginRepository; |
| | |
| | | public void login(String username, String password) { |
| | | // can be launched in a separate asynchronous job |
| | | Observable<LoggedInUser> result = loginRepository.login(username, password); |
| | | result.doOnSubscribe(new Consumer<Disposable>() { |
| | | @Override |
| | | public void accept(Disposable disposable) throws Exception { |
| | | result.subscribeOn(Schedulers.io())//指定网络请求在io后台线程中进行 |
| | | .observeOn(AndroidScheduler.mainThread())//指定observer回调在UI主线程中进行 |
| | | .doOnSubscribe(new Consumer<Disposable>() { |
| | | @Override |
| | | public void accept(Disposable disposable) throws Exception { |
| | | |
| | | } |
| | | }).subscribe(new DisposableObserver<LoggedInUser>(){ |
| | | } |
| | | }).subscribe(new DisposableObserver<LoggedInUser>(){ |
| | | |
| | | @Override |
| | | public void onNext(LoggedInUser value) { |
| | | loginResult.setValue(new LoginResult((new LoggedInUserView(value.getDisplayName())))); |
| | | } |
| | | @Override |
| | | public void onNext(LoggedInUser value) { |
| | | loginResult.setValue(new LoginResult((new LoggedInUserView(value.getDisplayName())))); |
| | | } |
| | | |
| | | @Override |
| | | public void onError(Throwable e) { |
| | | loginResult.setValue(new LoginResult(R.string.login_failed)); |
| | | } |
| | | @Override |
| | | public void onError(Throwable e) { |
| | | loginResult.setValue(new LoginResult(R.string.login_failed)); |
| | | } |
| | | |
| | | @Override |
| | | public void onComplete() { |
| | | @Override |
| | | public void onComplete() { |
| | | |
| | | } |
| | | }); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | public void loginDataChanged(String username, String password) { |
| | |
| | | private boolean isPasswordValid(String password) { |
| | | return password != null && password.trim().length() > 5; |
| | | } |
| | | } |
| | | |
| | | public MutableLiveData<Long> getCountDown() { |
| | | return countDown; |
| | | } |
| | | |
| | | /** |
| | | * 启动倒计时 |
| | | * @param consumedTime |
| | | */ |
| | | public void startCountDown(long consumedTime){ |
| | | //倒计时 |
| | | timer = new CountDownTimer(60 * 1000-consumedTime, 1000) { |
| | | |
| | | @Override |
| | | public void onTick(long l) { |
| | | countDown.postValue(l / 1000); |
| | | } |
| | | |
| | | @Override |
| | | public void onFinish() { |
| | | countDown.setValue(0l); |
| | | } |
| | | }; |
| | | timer.start(); |
| | | } |
| | | |
| | | |
| | | } |