get rid of BaseFragment by using RxJava instead of Retrofit Calls (#2055)

* get rid of BaseFragment by using RxJava instead of Retrofit Calls

* fix tests
This commit is contained in:
Konrad Pozniak 2021-01-31 19:34:33 +01:00 committed by GitHub
commit 886ff2f06b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 170 additions and 309 deletions

View file

@ -102,13 +102,11 @@ import at.connyduck.sparkbutton.helpers.Utils;
import io.reactivex.Observable;
import io.reactivex.Single;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import kotlin.Unit;
import kotlin.collections.CollectionsKt;
import kotlin.jvm.functions.Function1;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import static com.keylesspalace.tusky.util.StringUtils.isLessThan;
import static com.uber.autodispose.AutoDispose.autoDisposable;
@ -125,8 +123,9 @@ public class NotificationsFragment extends SFragment implements
private static final int LOAD_AT_ONCE = 30;
private int maxPlaceholderId = 0;
private final Set<Notification.Type> notificationFilter = new HashSet<>();
private Set<Notification.Type> notificationFilter = new HashSet<>();
private final CompositeDisposable disposables = new CompositeDisposable();
private enum FetchEnd {
TOP,
@ -685,32 +684,21 @@ public class NotificationsFragment extends SFragment implements
updateAdapter();
//Execute clear notifications request
Call<ResponseBody> call = mastodonApi.clearNotifications();
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(@NonNull Call<ResponseBody> call, @NonNull Response<ResponseBody> response) {
if (isAdded()) {
if (!response.isSuccessful()) {
//Reload notifications on failure
fullyRefreshWithProgressBar(true);
}
}
}
@Override
public void onFailure(@NonNull Call<ResponseBody> call, @NonNull Throwable t) {
//Reload notifications on failure
fullyRefreshWithProgressBar(true);
}
});
callList.add(call);
mastodonApi.clearNotifications()
.observeOn(AndroidSchedulers.mainThread())
.as(autoDisposable(from(this, Lifecycle.Event.ON_DESTROY)))
.subscribe(
response -> {
// nothing to do
},
throwable -> {
//Reload notifications on failure
fullyRefreshWithProgressBar(true);
});
}
private void resetNotificationsLoad() {
for (Call callItem : callList) {
callItem.cancel();
}
callList.clear();
disposables.clear();
bottomLoading = false;
topLoading = false;
@ -840,8 +828,8 @@ public class NotificationsFragment extends SFragment implements
@Override
public void onRespondToFollowRequest(boolean accept, String id, int position) {
Single<Relationship> request = accept ?
mastodonApi.authorizeFollowRequestObservable(id) :
mastodonApi.rejectFollowRequestObservable(id);
mastodonApi.authorizeFollowRequest(id) :
mastodonApi.rejectFollowRequest(id);
request.observeOn(AndroidSchedulers.mainThread())
.as(autoDisposable(from(this, Lifecycle.Event.ON_DESTROY)))
.subscribe(
@ -959,27 +947,20 @@ public class NotificationsFragment extends SFragment implements
bottomLoading = true;
}
Call<List<Notification>> call = mastodonApi.notifications(fromId, uptoId, LOAD_AT_ONCE, showNotificationsFilter ? notificationFilter : null);
call.enqueue(new Callback<List<Notification>>() {
@Override
public void onResponse(@NonNull Call<List<Notification>> call,
@NonNull Response<List<Notification>> response) {
if (response.isSuccessful()) {
String linkHeader = response.headers().get("Link");
onFetchNotificationsSuccess(response.body(), linkHeader, fetchEnd, pos);
} else {
onFetchNotificationsFailure(new Exception(response.message()), fetchEnd, pos);
}
}
@Override
public void onFailure(@NonNull Call<List<Notification>> call, @NonNull Throwable t) {
if (!call.isCanceled())
onFetchNotificationsFailure((Exception) t, fetchEnd, pos);
}
});
callList.add(call);
Disposable notificationCall = mastodonApi.notifications(fromId, uptoId, LOAD_AT_ONCE, showNotificationsFilter ? notificationFilter : null)
.observeOn(AndroidSchedulers.mainThread())
.as(autoDisposable(from(this, Lifecycle.Event.ON_DESTROY)))
.subscribe(
response -> {
if (response.isSuccessful()) {
String linkHeader = response.headers().get("Link");
onFetchNotificationsSuccess(response.body(), linkHeader, fetchEnd, pos);
} else {
onFetchNotificationsFailure(new Exception(response.message()), fetchEnd, pos);
}
},
throwable -> onFetchNotificationsFailure(throwable, fetchEnd, pos));
disposables.add(notificationCall);
}
private void onFetchNotificationsSuccess(List<Notification> notifications, String linkHeader,
@ -1038,7 +1019,7 @@ public class NotificationsFragment extends SFragment implements
progressBar.setVisibility(View.GONE);
}
private void onFetchNotificationsFailure(Exception exception, FetchEnd fetchEnd, int position) {
private void onFetchNotificationsFailure(Throwable throwable, FetchEnd fetchEnd, int position) {
swipeRefreshLayout.setRefreshing(false);
if (fetchEnd == FetchEnd.MIDDLE && !notifications.get(position).isRight()) {
Placeholder placeholder = notifications.get(position).asLeft();
@ -1050,7 +1031,7 @@ public class NotificationsFragment extends SFragment implements
this.statusView.setVisibility(View.VISIBLE);
swipeRefreshLayout.setEnabled(false);
this.showingError = true;
if (exception instanceof IOException) {
if (throwable instanceof IOException) {
this.statusView.setup(R.drawable.elephant_offline, R.string.error_network, __ -> {
this.progressBar.setVisibility(View.VISIBLE);
this.onRefresh();
@ -1065,7 +1046,7 @@ public class NotificationsFragment extends SFragment implements
}
updateFilterVisibility();
}
Log.e(TAG, "Fetch failure: " + exception.getMessage());
Log.e(TAG, "Fetch failure: " + throwable.getMessage());
if (fetchEnd == FetchEnd.TOP) {
topLoading = false;