| | |
| | | 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 { |
| | | |
| | |
| | | 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) { |