From ae00a9d19f115ca22f7b781a677c71e88dbc09df Mon Sep 17 00:00:00 2001 From: Kanahia Date: Fri, 26 Jul 2024 18:04:46 +0530 Subject: [PATCH] Made changes to visibility implementation --- .../contributions/ContributionController.java | 56 +++++++++++++++++++ .../contributions/ContributionsFragment.java | 26 ++++++++- .../ContributionsListFragment.java | 19 ------- .../ContributionsListPresenter.java | 47 ---------------- 4 files changed, 79 insertions(+), 69 deletions(-) diff --git a/app/src/main/java/fr/free/nrw/commons/contributions/ContributionController.java b/app/src/main/java/fr/free/nrw/commons/contributions/ContributionController.java index 7c48bb2b0..67b15d2d2 100644 --- a/app/src/main/java/fr/free/nrw/commons/contributions/ContributionController.java +++ b/app/src/main/java/fr/free/nrw/commons/contributions/ContributionController.java @@ -9,6 +9,10 @@ import android.content.Intent; import android.widget.Toast; import androidx.activity.result.ActivityResultLauncher; import androidx.annotation.NonNull; +import androidx.lifecycle.LiveData; +import androidx.paging.DataSource.Factory; +import androidx.paging.LivePagedListBuilder; +import androidx.paging.PagedList; import fr.free.nrw.commons.R; import fr.free.nrw.commons.filepicker.DefaultCallback; import fr.free.nrw.commons.filepicker.FilePicker; @@ -25,6 +29,8 @@ import fr.free.nrw.commons.utils.DialogUtil; import fr.free.nrw.commons.utils.PermissionUtils; import fr.free.nrw.commons.utils.ViewUtil; import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; import java.util.List; import javax.inject.Inject; import javax.inject.Named; @@ -39,10 +45,16 @@ public class ContributionController { private boolean isInAppCameraUpload; public LocationPermissionCallback locationPermissionCallback; private LocationPermissionsHelper locationPermissionsHelper; + LiveData> failedAndPendingContributionList; + LiveData> pendingContributionList; + LiveData> failedContributionList; @Inject LocationServiceManager locationManager; + @Inject + ContributionsRepository repository; + @Inject public ContributionController(@Named("default_preferences") JsonKvStore defaultKvStore) { this.defaultKvStore = defaultKvStore; @@ -307,4 +319,48 @@ public class ContributionController { isInAppCameraUpload = false; // reset the flag for next use return shareIntent; } + + void getPendingContributions() { + final PagedList.Config pagedListConfig = + (new PagedList.Config.Builder()) + .setPrefetchDistance(50) + .setPageSize(10).build(); + Factory factory; + factory = repository.fetchContributionsWithStates( + Arrays.asList(Contribution.STATE_IN_PROGRESS, Contribution.STATE_QUEUED, + Contribution.STATE_PAUSED)); + + LivePagedListBuilder livePagedListBuilder = new LivePagedListBuilder(factory, + pagedListConfig); + pendingContributionList = livePagedListBuilder.build(); + } + + void getFailedContributions() { + final PagedList.Config pagedListConfig = + (new PagedList.Config.Builder()) + .setPrefetchDistance(50) + .setPageSize(10).build(); + Factory factory; + factory = repository.fetchContributionsWithStates( + Collections.singletonList(Contribution.STATE_FAILED)); + + LivePagedListBuilder livePagedListBuilder = new LivePagedListBuilder(factory, + pagedListConfig); + failedContributionList = livePagedListBuilder.build(); + } + + void getFailedAndPendingContributions() { + final PagedList.Config pagedListConfig = + (new PagedList.Config.Builder()) + .setPrefetchDistance(50) + .setPageSize(10).build(); + Factory factory; + factory = repository.fetchContributionsWithStates( + Arrays.asList(Contribution.STATE_IN_PROGRESS, Contribution.STATE_QUEUED, + Contribution.STATE_PAUSED, Contribution.STATE_FAILED)); + + LivePagedListBuilder livePagedListBuilder = new LivePagedListBuilder(factory, + pagedListConfig); + failedAndPendingContributionList = livePagedListBuilder.build(); + } } diff --git a/app/src/main/java/fr/free/nrw/commons/contributions/ContributionsFragment.java b/app/src/main/java/fr/free/nrw/commons/contributions/ContributionsFragment.java index 594b322ec..bc76b9dd8 100644 --- a/app/src/main/java/fr/free/nrw/commons/contributions/ContributionsFragment.java +++ b/app/src/main/java/fr/free/nrw/commons/contributions/ContributionsFragment.java @@ -108,6 +108,8 @@ public class ContributionsFragment LocationServiceManager locationManager; @Inject NotificationController notificationController; + @Inject + ContributionController contributionController; private CompositeDisposable compositeDisposable = new CompositeDisposable(); @@ -298,6 +300,25 @@ public class ContributionsFragment throwable -> Timber.e(throwable, "Error occurred while loading notifications"))); } + public void setUploadIconVisibility() { + contributionController.getFailedAndPendingContributions(); + contributionController.failedAndPendingContributionList.observe(getViewLifecycleOwner(), list -> { + updateUploadIcon(list.size()); + }); + } + + public void setUploadIconCount() { + contributionController.getPendingContributions(); + contributionController.pendingContributionList.observe(getViewLifecycleOwner(), + list -> { + updatePendingIcon(list.size()); + }); + contributionController.getFailedContributions(); + contributionController.failedContributionList.observe(getViewLifecycleOwner(), list -> { + updateErrorIcon(list.size()); + }); + } + public void scrollToTop() { if (contributionsListFragment != null) { contributionsListFragment.scrollToTop(); @@ -499,6 +520,8 @@ public class ContributionsFragment if(!isUserProfile) { setNotificationCount(); fetchCampaigns(); + setUploadIconVisibility(); + setUploadIconCount(); } } mSensorManager.registerListener(this, mLight, SensorManager.SENSOR_DELAY_UI); @@ -688,7 +711,6 @@ public class ContributionsFragment } } - @Override public void updatePendingIcon(int pendingCount) { if (pendingUploadsCountTextView != null){ if (pendingCount != 0){ @@ -700,7 +722,6 @@ public class ContributionsFragment } } - @Override public void updateErrorIcon(int errorCount) { if (uploadsErrorTextView != null){ if (errorCount != 0){ @@ -712,7 +733,6 @@ public class ContributionsFragment } } - @Override public void updateUploadIcon(int count) { if (pendingUploadsImageView != null){ if (count != 0){ diff --git a/app/src/main/java/fr/free/nrw/commons/contributions/ContributionsListFragment.java b/app/src/main/java/fr/free/nrw/commons/contributions/ContributionsListFragment.java index ccc0e250c..ed06f8969 100644 --- a/app/src/main/java/fr/free/nrw/commons/contributions/ContributionsListFragment.java +++ b/app/src/main/java/fr/free/nrw/commons/contributions/ContributionsListFragment.java @@ -214,20 +214,6 @@ public class ContributionsListFragment extends CommonsDaggerSupportFragment impl contributionsListPresenter.setup(userName, Objects.equals(sessionManager.getUserName(), userName)); - contributionsListPresenter.getPendingContributions(); - contributionsListPresenter.pendingContributionList.observe(getViewLifecycleOwner(), list -> { - pendingUploadsCount = list.size(); - callback.updatePendingIcon(pendingUploadsCount); - }); - contributionsListPresenter.getFailedContributions(); - contributionsListPresenter.failedContributionList.observe(getViewLifecycleOwner(), list -> { - uploadErrorCount = list.size(); - callback.updateErrorIcon(uploadErrorCount); - }); - contributionsListPresenter.getFailedAndPendingContributions(); - contributionsListPresenter.failedAndPendingContributionList.observe(getViewLifecycleOwner(), list -> { - callback.updateUploadIcon(list.size()); - }); contributionsListPresenter.contributionList.observe(getViewLifecycleOwner(), list -> { contributionsSize = list.size(); adapter.submitList(list); @@ -509,10 +495,5 @@ public class ContributionsListFragment extends CommonsDaggerSupportFragment impl // Notify the viewpager that number of items have changed. void viewPagerNotifyDataSetChanged(); - void updatePendingIcon(int pendingCount); - - void updateErrorIcon(int errorCount); - - void updateUploadIcon(int count); } } diff --git a/app/src/main/java/fr/free/nrw/commons/contributions/ContributionsListPresenter.java b/app/src/main/java/fr/free/nrw/commons/contributions/ContributionsListPresenter.java index 16e7779d0..42495889d 100644 --- a/app/src/main/java/fr/free/nrw/commons/contributions/ContributionsListPresenter.java +++ b/app/src/main/java/fr/free/nrw/commons/contributions/ContributionsListPresenter.java @@ -28,9 +28,6 @@ public class ContributionsListPresenter implements UserActionListener { private final ContributionsRemoteDataSource contributionsRemoteDataSource; LiveData> contributionList; - LiveData> pendingContributionList; - LiveData> failedContributionList; - LiveData> failedAndPendingContributionList; @Inject ContributionsListPresenter( @@ -96,48 +93,4 @@ public class ContributionsListPresenter implements UserActionListener { contributionBoundaryCallback.dispose(); } - void getPendingContributions() { - final PagedList.Config pagedListConfig = - (new PagedList.Config.Builder()) - .setPrefetchDistance(50) - .setPageSize(10).build(); - Factory factory; - factory = repository.fetchContributionsWithStates( - Arrays.asList(Contribution.STATE_IN_PROGRESS, Contribution.STATE_QUEUED, - Contribution.STATE_PAUSED)); - - LivePagedListBuilder livePagedListBuilder = new LivePagedListBuilder(factory, - pagedListConfig); - pendingContributionList = livePagedListBuilder.build(); - } - - void getFailedContributions() { - final PagedList.Config pagedListConfig = - (new PagedList.Config.Builder()) - .setPrefetchDistance(50) - .setPageSize(10).build(); - Factory factory; - factory = repository.fetchContributionsWithStates( - Collections.singletonList(Contribution.STATE_FAILED)); - - LivePagedListBuilder livePagedListBuilder = new LivePagedListBuilder(factory, - pagedListConfig); - failedContributionList = livePagedListBuilder.build(); - } - - void getFailedAndPendingContributions() { - final PagedList.Config pagedListConfig = - (new PagedList.Config.Builder()) - .setPrefetchDistance(50) - .setPageSize(10).build(); - Factory factory; - factory = repository.fetchContributionsWithStates( - Arrays.asList(Contribution.STATE_IN_PROGRESS, Contribution.STATE_QUEUED, - Contribution.STATE_PAUSED, Contribution.STATE_FAILED)); - - LivePagedListBuilder livePagedListBuilder = new LivePagedListBuilder(factory, - pagedListConfig); - failedAndPendingContributionList = livePagedListBuilder.build(); - } - }