From 5c71ca672e89e3bc439921a7159e225afecf57ff Mon Sep 17 00:00:00 2001 From: Kanahia Date: Wed, 3 Jul 2024 11:08:01 +0530 Subject: [PATCH] Moved Auto Retry to MainActivity --- .../contributions/ContributionsFragment.java | 48 ++++++++++++++++- .../contributions/ContributionsPresenter.java | 13 +++++ .../commons/contributions/MainActivity.java | 17 +++++++ .../commons/upload/PendingUploadsFragment.kt | 51 +------------------ .../commons/upload/UploadProgressActivity.kt | 22 -------- 5 files changed, 78 insertions(+), 73 deletions(-) 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 0646f8309..219151250 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 @@ -114,7 +114,7 @@ public class ContributionsFragment private static final String CONTRIBUTION_LIST_FRAGMENT_TAG = "ContributionListFragmentTag"; private MediaDetailPagerFragment mediaDetailPagerFragment; static final String MEDIA_DETAIL_PAGER_FRAGMENT_TAG = "MediaDetailFragmentTag"; - + private static final int MAX_RETRIES = 10; public FragmentContributionsBinding binding; @@ -791,6 +791,52 @@ public class ContributionsFragment } + /** + * Restarts the upload process for a contribution + * + * @param contribution + */ + public void restartUpload(Contribution contribution) { + contribution.setState(Contribution.STATE_QUEUED); + contributionsPresenter.saveContribution(contribution); + Timber.d("Restarting for %s", contribution.toString()); + } + + /** + * Retry upload when it is failed + * + * @param contribution contribution to be retried + */ + public void retryUpload(Contribution contribution) { + if (NetworkUtils.isInternetConnectionEstablished(getContext())) { + if (contribution.getState() == STATE_PAUSED + || contribution.getState() == Contribution.STATE_QUEUED_LIMITED_CONNECTION_MODE) { + restartUpload(contribution); + } else if (contribution.getState() == STATE_FAILED) { + int retries = contribution.getRetries(); + // TODO: Improve UX. Additional details: https://github.com/commons-app/apps-android-commons/pull/5257#discussion_r1304662562 + /* Limit the number of retries for a failed upload + to handle cases like invalid filename as such uploads + will never be successful */ + if (retries < MAX_RETRIES) { + contribution.setRetries(retries + 1); + Timber.d("Retried uploading %s %d times", contribution.getMedia().getFilename(), + retries + 1); + restartUpload(contribution); + } else { + // TODO: Show the exact reason for failure + Toast.makeText(getContext(), + R.string.retry_limit_reached, Toast.LENGTH_SHORT).show(); + } + } else { + Timber.d("Skipping re-upload for non-failed %s", contribution.toString()); + } + } else { + ViewUtil.showLongToast(getContext(), R.string.this_function_needs_network_connection); + } + + } + /** * Reload media detail fragment once media is nominated * diff --git a/app/src/main/java/fr/free/nrw/commons/contributions/ContributionsPresenter.java b/app/src/main/java/fr/free/nrw/commons/contributions/ContributionsPresenter.java index c77514756..8d08105f4 100644 --- a/app/src/main/java/fr/free/nrw/commons/contributions/ContributionsPresenter.java +++ b/app/src/main/java/fr/free/nrw/commons/contributions/ContributionsPresenter.java @@ -47,4 +47,17 @@ public class ContributionsPresenter implements UserActionListener { return repository.getContributionWithFileName(title); } + /** + * Update the contribution's state in the databse, upon completion, trigger the workmanager to + * process this contribution + * + * @param contribution + */ + public void saveContribution(Contribution contribution) { + compositeDisposable.add(repository + .save(contribution) + .subscribeOn(ioThreadScheduler) + .subscribe(() -> WorkRequestHelper.Companion.makeOneTimeWorkRequest( + view.getContext().getApplicationContext(), ExistingWorkPolicy.KEEP))); + } } diff --git a/app/src/main/java/fr/free/nrw/commons/contributions/MainActivity.java b/app/src/main/java/fr/free/nrw/commons/contributions/MainActivity.java index a16d79d70..52d97fdb9 100644 --- a/app/src/main/java/fr/free/nrw/commons/contributions/MainActivity.java +++ b/app/src/main/java/fr/free/nrw/commons/contributions/MainActivity.java @@ -384,6 +384,21 @@ public class MainActivity extends BaseActivity //initBackButton(); } + /** + * Retry all failed uploads as soon as the user returns to the app + */ + @SuppressLint("CheckResult") + private void retryAllFailedUploads() { + contributionDao. + getContribution(Collections.singletonList(Contribution.STATE_FAILED)) + .subscribeOn(Schedulers.io()) + .subscribe(failedUploads -> { + for (Contribution contribution: failedUploads) { + contributionsFragment.retryUpload(contribution); + } + }); + } + @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { @@ -425,6 +440,8 @@ public class MainActivity extends BaseActivity defaultKvStore.putBoolean("inAppCameraFirstRun", true); WelcomeActivity.startYourself(this); } + + retryAllFailedUploads(); } @Override diff --git a/app/src/main/java/fr/free/nrw/commons/upload/PendingUploadsFragment.kt b/app/src/main/java/fr/free/nrw/commons/upload/PendingUploadsFragment.kt index 7f4f4e275..4d8674bd7 100644 --- a/app/src/main/java/fr/free/nrw/commons/upload/PendingUploadsFragment.kt +++ b/app/src/main/java/fr/free/nrw/commons/upload/PendingUploadsFragment.kt @@ -38,7 +38,6 @@ class PendingUploadsFragment : CommonsDaggerSupportFragment(), PendingUploadsCon private var param2: String? = null private val ARG_PARAM1 = "param1" private val ARG_PARAM2 = "param2" - private val MAX_RETRIES = 10 @Inject lateinit var pendingUploadsPresenter: PendingUploadsPresenter @@ -154,7 +153,7 @@ class PendingUploadsFragment : CommonsDaggerSupportFragment(), PendingUploadsCon val last = sortedContributionsList.last() for (i in sortedContributionsList.indices) { val current = sortedContributionsList[i] - if (current.transferred == 0L && current.state != Contribution.STATE_QUEUED && (current.dateModifiedInMillis() / 100) > (last.dateModifiedInMillis() / 100)){ + if (current.transferred == 0L && (current.dateModifiedInMillis() / 100) > (last.dateModifiedInMillis() / 100)){ listOfRemoved.add(current) } } @@ -242,54 +241,6 @@ class PendingUploadsFragment : CommonsDaggerSupportFragment(), PendingUploadsCon } } - /** - * Restarts the upload process for a contribution - * - * @param contribution - */ - fun restartUpload(contribution: Contribution) { - contribution.state = Contribution.STATE_QUEUED - pendingUploadsPresenter.saveContribution(contribution, this.requireContext().applicationContext) - Timber.d("Restarting for %s", contribution.toString()) - } - - /** - * Retry upload when it is failed - * - * @param contribution contribution to be retried - */ - fun retryUpload(contribution: Contribution) { - if (NetworkUtils.isInternetConnectionEstablished(context)) { - if (contribution.state == Contribution.STATE_PAUSED) { - restartUpload(contribution) - } else if (contribution.state == Contribution.STATE_FAILED) { - val retries = contribution.retries - // TODO: Improve UX. Additional details: https://github.com/commons-app/apps-android-commons/pull/5257#discussion_r1304662562 - /* Limit the number of retries for a failed upload - to handle cases like invalid filename as such uploads - will never be successful */ - if (retries < MAX_RETRIES) { - contribution.retries = retries + 1 - Timber.d( - "Retried uploading %s %d times", contribution.media.filename, - retries + 1 - ) - restartUpload(contribution) - } else { - // TODO: Show the exact reason for failure - Toast.makeText( - context, - R.string.retry_limit_reached, Toast.LENGTH_SHORT - ).show() - } - } else { - Timber.d("Skipping re-upload for non-failed %s", contribution.toString()) - } - } else { - ViewUtil.showLongToast(context, R.string.this_function_needs_network_connection) - } - } - fun resetProgressBar() { totalUploads = 0 } diff --git a/app/src/main/java/fr/free/nrw/commons/upload/UploadProgressActivity.kt b/app/src/main/java/fr/free/nrw/commons/upload/UploadProgressActivity.kt index 04f2408c2..50342b185 100644 --- a/app/src/main/java/fr/free/nrw/commons/upload/UploadProgressActivity.kt +++ b/app/src/main/java/fr/free/nrw/commons/upload/UploadProgressActivity.kt @@ -169,26 +169,4 @@ class UploadProgressActivity : BaseActivity() { pendingUploadsFragment!!.resetProgressBar() } - override fun onResume() { - super.onResume() - retryAllFailedUploads() - } - - /** - * Retry all failed uploads as soon as the user returns to the app - */ - @SuppressLint("CheckResult") - private fun retryAllFailedUploads() { - pendingUploadsFragment!!.resetProgressBar() - contributionDao.getContribution(listOf(Contribution.STATE_FAILED)) - .subscribeOn(Schedulers.io()) - .subscribe { failedUploads: List -> - for (contribution in failedUploads) { - if (contribution != null) { - pendingUploadsFragment!!.retryUpload(contribution) - } - } - } - } - }