|
|
@ -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();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|