ensure that cancelled uploads are really getting cancelled (#5367)

This commit is contained in:
Ritika Pahwa 2023-10-29 19:51:16 +05:30 committed by GitHub
parent 6166fde7cd
commit 39a2fbe3d5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 15 deletions

View file

@ -141,6 +141,11 @@ public class CommonsApplication extends MultiDexApplication {
*/
public static Map<String, Boolean> pauseUploads = new HashMap<>();
/**
* In-memory list of uploads that have been cancelled by the user
*/
public static HashSet<String> cancelledUploads = new HashSet<>();
/**
* Used to declare and initialize various components and dependencies
*/

View file

@ -36,6 +36,7 @@ import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import fr.free.nrw.commons.CommonsApplication;
import fr.free.nrw.commons.Media;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.Utils;
@ -429,6 +430,7 @@ public class ContributionsListFragment extends CommonsDaggerSupportFragment impl
() -> {
ViewUtil.showShortToast(getContext(), R.string.cancelling_upload);
contributionsListPresenter.deleteUpload(contribution);
CommonsApplication.cancelledUploads.add(contribution.getPageId());
}, () -> {
// Do nothing
});

View file

@ -172,6 +172,16 @@ class UploadWorker(var appContext: Context, workerParams: WorkerParameters) :
CommonsApplication.NOTIFICATION_CHANNEL_ID_ALL
)!!
withContext(Dispatchers.IO) {
/*
queuedContributions receives the results from a one-shot query.
This means that once the list has been fetched from the database,
it does not get updated even if some changes (insertions, deletions, etc.)
are made to the contribution table afterwards.
Related issues (fixed):
https://github.com/commons-app/apps-android-commons/issues/5136
https://github.com/commons-app/apps-android-commons/issues/5346
*/
val queuedContributions = contributionDao.getContribution(statesToProcess)
.blockingGet()
//Showing initial notification for the number of uploads being processed
@ -202,24 +212,32 @@ class UploadWorker(var appContext: Context, workerParams: WorkerParameters) :
}
queuedContributions.asFlow().map { contribution ->
/**
* If the limited connection mode is on, lets iterate through the queued
* contributions
* and set the state as STATE_QUEUED_LIMITED_CONNECTION_MODE ,
* otherwise proceed with the upload
*/
if (isLimitedConnectionModeEnabled()) {
if (contribution.state == Contribution.STATE_QUEUED) {
contribution.state = Contribution.STATE_QUEUED_LIMITED_CONNECTION_MODE
// Upload the contribution if it has not been cancelled by the user
if (!CommonsApplication.cancelledUploads.contains(contribution.pageId)) {
/**
* If the limited connection mode is on, lets iterate through the queued
* contributions
* and set the state as STATE_QUEUED_LIMITED_CONNECTION_MODE ,
* otherwise proceed with the upload
*/
if (isLimitedConnectionModeEnabled()) {
if (contribution.state == Contribution.STATE_QUEUED) {
contribution.state = Contribution.STATE_QUEUED_LIMITED_CONNECTION_MODE
contributionDao.saveSynchronous(contribution)
}
} else {
contribution.transferred = 0
contribution.state = Contribution.STATE_IN_PROGRESS
contributionDao.saveSynchronous(contribution)
setProgressAsync(Data.Builder().putInt("progress", countUpload).build())
countUpload++
uploadContribution(contribution = contribution)
}
} else {
contribution.transferred = 0
contribution.state = Contribution.STATE_IN_PROGRESS
contributionDao.saveSynchronous(contribution)
setProgressAsync(Data.Builder().putInt("progress", countUpload).build())
countUpload++
uploadContribution(contribution = contribution)
/* We can remove the cancelled upload from the hashset
as this contribution will not be processed again
*/
removeUploadFromInMemoryHashSet(contribution)
}
}.collect()
@ -240,6 +258,13 @@ class UploadWorker(var appContext: Context, workerParams: WorkerParameters) :
return Result.success()
}
/**
* Removes the processed contribution from the cancelledUploads in-memory hashset
*/
private fun removeUploadFromInMemoryHashSet(contribution: Contribution) {
CommonsApplication.cancelledUploads.remove(contribution.pageId)
}
/**
* Create new notification for foreground service
*/