mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-28 13:23:58 +01:00
Add pull down to refresh in Contributions screen (#6041)
* pull down to refresh Signed-off-by: parneet-guraya <gurayaparneet@gmail.com> * add kdoc Signed-off-by: parneet-guraya <gurayaparneet@gmail.com> * only enabled for self user Signed-off-by: parneet-guraya <gurayaparneet@gmail.com> * fix test Signed-off-by: parneet-guraya <gurayaparneet@gmail.com> --------- Signed-off-by: parneet-guraya <gurayaparneet@gmail.com>
This commit is contained in:
parent
a4b74794cb
commit
4c9637c821
6 changed files with 160 additions and 108 deletions
|
|
@ -2,7 +2,6 @@ package fr.free.nrw.commons.contributions
|
|||
|
||||
import androidx.paging.PagedList.BoundaryCallback
|
||||
import fr.free.nrw.commons.auth.SessionManager
|
||||
import fr.free.nrw.commons.di.CommonsApplicationModule
|
||||
import fr.free.nrw.commons.di.CommonsApplicationModule.Companion.IO_THREAD
|
||||
import fr.free.nrw.commons.media.MediaClient
|
||||
import io.reactivex.Scheduler
|
||||
|
|
@ -31,10 +30,7 @@ class ContributionBoundaryCallback
|
|||
* network
|
||||
*/
|
||||
override fun onZeroItemsLoaded() {
|
||||
if (sessionManager.userName != null) {
|
||||
mediaClient.resetUserNameContinuation(sessionManager.userName!!)
|
||||
}
|
||||
fetchContributions()
|
||||
refreshList()
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -52,9 +48,25 @@ class ContributionBoundaryCallback
|
|||
}
|
||||
|
||||
/**
|
||||
* Fetches contributions using the MediaWiki API
|
||||
* Fetch list from network and save it to local DB.
|
||||
*
|
||||
* @param onRefreshFinish callback to invoke when operations finishes
|
||||
* with either error or success.
|
||||
*/
|
||||
private fun fetchContributions() {
|
||||
fun refreshList(onRefreshFinish: () -> Unit = {}){
|
||||
if (sessionManager.userName != null) {
|
||||
mediaClient.resetUserNameContinuation(sessionManager.userName!!)
|
||||
}
|
||||
fetchContributions(onRefreshFinish)
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches contributions using the MediaWiki API
|
||||
*
|
||||
* @param onRefreshFinish callback to invoke when operations finishes
|
||||
* with either error or success.
|
||||
*/
|
||||
private fun fetchContributions(onRefreshFinish: () -> Unit = {}) {
|
||||
if (sessionManager.userName != null) {
|
||||
userName
|
||||
?.let { userName ->
|
||||
|
|
@ -65,12 +77,15 @@ class ContributionBoundaryCallback
|
|||
Contribution(media = media, state = Contribution.STATE_COMPLETED)
|
||||
}
|
||||
}.subscribeOn(ioThreadScheduler)
|
||||
.subscribe(::saveContributionsToDB) { error: Throwable ->
|
||||
.subscribe({ list ->
|
||||
saveContributionsToDB(list, onRefreshFinish)
|
||||
},{ error ->
|
||||
onRefreshFinish()
|
||||
Timber.e(
|
||||
"Failed to fetch contributions: %s",
|
||||
error.message,
|
||||
)
|
||||
}
|
||||
})
|
||||
}?.let {
|
||||
compositeDisposable.add(
|
||||
it,
|
||||
|
|
@ -83,13 +98,16 @@ class ContributionBoundaryCallback
|
|||
|
||||
/**
|
||||
* Saves the contributions the the local DB
|
||||
*
|
||||
* @param onRefreshFinish callback to invoke when successfully saved to DB.
|
||||
*/
|
||||
private fun saveContributionsToDB(contributions: List<Contribution>) {
|
||||
private fun saveContributionsToDB(contributions: List<Contribution>, onRefreshFinish: () -> Unit) {
|
||||
compositeDisposable.add(
|
||||
repository
|
||||
.save(contributions)
|
||||
.subscribeOn(ioThreadScheduler)
|
||||
.subscribe { longs: List<Long?>? ->
|
||||
onRefreshFinish()
|
||||
repository["last_fetch_timestamp"] = System.currentTimeMillis()
|
||||
},
|
||||
)
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package fr.free.nrw.commons.contributions;
|
||||
|
||||
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
|
||||
import fr.free.nrw.commons.BasePresenter;
|
||||
|
||||
/**
|
||||
|
|
@ -17,5 +18,8 @@ public class ContributionsListContract {
|
|||
}
|
||||
|
||||
public interface UserActionListener extends BasePresenter<View> {
|
||||
|
||||
void refreshList(SwipeRefreshLayout swipeRefreshLayout);
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -191,6 +191,15 @@ public class ContributionsListFragment extends CommonsDaggerSupportFragment impl
|
|||
|
||||
initAdapter();
|
||||
|
||||
// pull down to refresh only enabled for self user.
|
||||
if(Objects.equals(sessionManager.getUserName(), userName)){
|
||||
binding.swipeRefreshLayout.setOnRefreshListener(() -> {
|
||||
contributionsListPresenter.refreshList(binding.swipeRefreshLayout);
|
||||
});
|
||||
} else {
|
||||
binding.swipeRefreshLayout.setEnabled(false);
|
||||
}
|
||||
|
||||
return binding.getRoot();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -8,14 +8,15 @@ import androidx.paging.DataSource;
|
|||
import androidx.paging.DataSource.Factory;
|
||||
import androidx.paging.LivePagedListBuilder;
|
||||
import androidx.paging.PagedList;
|
||||
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
|
||||
import fr.free.nrw.commons.contributions.ContributionsListContract.UserActionListener;
|
||||
import fr.free.nrw.commons.di.CommonsApplicationModule;
|
||||
import io.reactivex.Scheduler;
|
||||
import io.reactivex.disposables.CompositeDisposable;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Named;
|
||||
import kotlin.Unit;
|
||||
import kotlin.jvm.functions.Function0;
|
||||
|
||||
/**
|
||||
* The presenter class for Contributions
|
||||
|
|
@ -95,4 +96,17 @@ public class ContributionsListPresenter implements UserActionListener {
|
|||
contributionBoundaryCallback.dispose();
|
||||
}
|
||||
|
||||
/**
|
||||
* It is used to refresh list.
|
||||
*
|
||||
* @param swipeRefreshLayout used to stop refresh animation when
|
||||
* refresh finishes.
|
||||
*/
|
||||
@Override
|
||||
public void refreshList(final SwipeRefreshLayout swipeRefreshLayout) {
|
||||
contributionBoundaryCallback.refreshList(() -> {
|
||||
swipeRefreshLayout.setRefreshing(false);
|
||||
return Unit.INSTANCE;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue