Fix view model error handling

pull/795/head
Alex Baker 6 years ago
parent 13eab349a6
commit 6c11e07938

@ -7,12 +7,14 @@ import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel; import androidx.lifecycle.ViewModel;
import io.reactivex.Completable; import io.reactivex.Completable;
import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.functions.Action; import io.reactivex.functions.Action;
import io.reactivex.schedulers.Schedulers; import io.reactivex.schedulers.Schedulers;
public class ActionViewModel extends ViewModel { public class ActionViewModel extends ViewModel {
private MutableLiveData<Boolean> completed = new MutableLiveData<>(); private final MutableLiveData<Boolean> completed = new MutableLiveData<>();
private MutableLiveData<Throwable> error = new MutableLiveData<>(); private final MutableLiveData<Throwable> error = new MutableLiveData<>();
private final CompositeDisposable disposables = new CompositeDisposable();
private boolean inProgress; private boolean inProgress;
public LiveData<Boolean> getData() { public LiveData<Boolean> getData() {
@ -32,16 +34,21 @@ public class ActionViewModel extends ViewModel {
if (!inProgress) { if (!inProgress) {
inProgress = true; inProgress = true;
Completable.fromAction(action) disposables.add(
.subscribeOn(Schedulers.io()) Completable.fromAction(action)
.observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io())
.doOnComplete(() -> completed.setValue(true)) .observeOn(AndroidSchedulers.mainThread())
.doOnError(error::setValue) .doFinally(
.doFinally(() -> { () -> {
assertMainThread(); assertMainThread();
inProgress = false; inProgress = false;
}) })
.subscribe(); .subscribe(() -> completed.setValue(true), error::setValue));
} }
} }
@Override
protected void onCleared() {
disposables.clear();
}
} }

@ -7,12 +7,14 @@ import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel; import androidx.lifecycle.ViewModel;
import io.reactivex.Single; import io.reactivex.Single;
import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers; import io.reactivex.schedulers.Schedulers;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
public abstract class CompletableViewModel<T> extends ViewModel { public abstract class CompletableViewModel<T> extends ViewModel {
private MutableLiveData<T> data = new MutableLiveData<>(); private final MutableLiveData<T> data = new MutableLiveData<>();
private MutableLiveData<Throwable> error = new MutableLiveData<>(); private final MutableLiveData<Throwable> error = new MutableLiveData<>();
private final CompositeDisposable disposables = new CompositeDisposable();
private boolean inProgress; private boolean inProgress;
public LiveData<T> getData() { public LiveData<T> getData() {
@ -32,16 +34,21 @@ public abstract class CompletableViewModel<T> extends ViewModel {
if (!inProgress) { if (!inProgress) {
inProgress = true; inProgress = true;
Single.fromCallable(callable) disposables.add(
.subscribeOn(Schedulers.io()) Single.fromCallable(callable)
.observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io())
.doOnSuccess(data::setValue) .observeOn(AndroidSchedulers.mainThread())
.doOnError(error::setValue) .doFinally(
.doFinally(() -> { () -> {
assertMainThread(); assertMainThread();
inProgress = false; inProgress = false;
}) })
.subscribe(); .subscribe(data::setValue, error::setValue));
} }
} }
@Override
protected void onCleared() {
disposables.dispose();
}
} }

Loading…
Cancel
Save