package ml.docilealligator.infinityforreddit.message; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.lifecycle.MutableLiveData; import androidx.paging.PageKeyedDataSource; import java.util.ArrayList; import java.util.Locale; import ml.docilealligator.infinityforreddit.NetworkState; import retrofit2.Retrofit; class MessageDataSource extends PageKeyedDataSource { private final Retrofit oauthRetrofit; private final Locale locale; private final String accessToken; private final String where; private final int messageType; private final MutableLiveData paginationNetworkStateLiveData; private final MutableLiveData initialLoadStateLiveData; private final MutableLiveData hasPostLiveData; private LoadParams params; private LoadCallback callback; MessageDataSource(Retrofit oauthRetrofit, Locale locale, String accessToken, String where) { this.oauthRetrofit = oauthRetrofit; this.locale = locale; this.accessToken = accessToken; this.where = where; if (where.equals(FetchMessage.WHERE_MESSAGES)) { messageType = FetchMessage.MESSAGE_TYPE_PRIVATE_MESSAGE; } else { messageType = FetchMessage.MESSAGE_TYPE_INBOX; } paginationNetworkStateLiveData = new MutableLiveData<>(); initialLoadStateLiveData = new MutableLiveData<>(); hasPostLiveData = new MutableLiveData<>(); } MutableLiveData getPaginationNetworkStateLiveData() { return paginationNetworkStateLiveData; } MutableLiveData getInitialLoadStateLiveData() { return initialLoadStateLiveData; } MutableLiveData hasPostLiveData() { return hasPostLiveData; } void retryLoadingMore() { loadAfter(params, callback); } @Override public void loadInitial(@NonNull LoadInitialParams params, @NonNull LoadInitialCallback callback) { initialLoadStateLiveData.postValue(NetworkState.LOADING); FetchMessage.fetchInbox(oauthRetrofit, locale, accessToken, where, null, messageType, new FetchMessage.FetchMessagesListener() { @Override public void fetchSuccess(ArrayList messages, @Nullable String after) { if (messages.size() == 0) { hasPostLiveData.postValue(false); } else { hasPostLiveData.postValue(true); } if (after == null || after.equals("") || after.equals("null")) { callback.onResult(messages, null, null); } else { callback.onResult(messages, null, after); } initialLoadStateLiveData.postValue(NetworkState.LOADED); } @Override public void fetchFailed() { initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error fetch messages")); } }); } @Override public void loadBefore(@NonNull LoadParams params, @NonNull LoadCallback callback) { } @Override public void loadAfter(@NonNull LoadParams params, @NonNull LoadCallback callback) { this.params = params; this.callback = callback; paginationNetworkStateLiveData.postValue(NetworkState.LOADING); FetchMessage.fetchInbox(oauthRetrofit, locale, accessToken, where, params.key, messageType, new FetchMessage.FetchMessagesListener() { @Override public void fetchSuccess(ArrayList messages, @Nullable String after) { if (after == null || after.equals("") || after.equals("null")) { callback.onResult(messages, null); } else { callback.onResult(messages, after); } paginationNetworkStateLiveData.postValue(NetworkState.LOADED); } @Override public void fetchFailed() { paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error fetching data")); } }); } }