Moved Auto Retry to MainActivity

This commit is contained in:
Kanahia 2024-07-03 11:08:01 +05:30
parent 9cbf9885a7
commit 5c71ca672e
5 changed files with 78 additions and 73 deletions

View file

@ -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
*

View file

@ -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)));
}
}

View file

@ -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

View file

@ -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
}

View file

@ -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<Contribution?> ->
for (contribution in failedUploads) {
if (contribution != null) {
pendingUploadsFragment!!.retryUpload(contribution)
}
}
}
}
}