Improved current implementation

This commit is contained in:
Kanahia 2024-07-21 21:18:51 +05:30
parent b8723ad5af
commit 5b57c25110
8 changed files with 44 additions and 40 deletions

View file

@ -47,17 +47,17 @@ public abstract class ContributionDao {
@Delete @Delete
public abstract void deleteSynchronous(Contribution contribution); public abstract void deleteSynchronous(Contribution contribution);
@Delete @Query("DELETE FROM contribution WHERE state IN (:states)")
public abstract void deleteContributionsSynchronous(List<Contribution> contributions); public abstract void deleteContributionsWithStatesSynchronous(List<Integer> states) throws SQLiteException;
public Completable delete(final Contribution contribution) { public Completable delete(final Contribution contribution) {
return Completable return Completable
.fromAction(() -> deleteSynchronous(contribution)); .fromAction(() -> deleteSynchronous(contribution));
} }
public Completable deleteContributions(final List<Contribution> contributions) { public Completable deleteContributionsWithStates(List<Integer> states) {
return Completable return Completable
.fromAction(() -> deleteContributionsSynchronous(contributions)); .fromAction(() -> deleteContributionsWithStatesSynchronous(states));
} }
@Query("SELECT * from contribution WHERE media_filename=:fileName") @Query("SELECT * from contribution WHERE media_filename=:fileName")

View file

@ -64,8 +64,8 @@ class ContributionsLocalDataSource {
return contributionDao.delete(contribution); return contributionDao.delete(contribution);
} }
public Completable deleteContributions(final List<Contribution> contributions) { public Completable deleteContributionsWithStates(List<Integer> states) {
return contributionDao.deleteContributions(contributions); return contributionDao.deleteContributionsWithStates(states);
} }
public Factory<Integer, Contribution> getContributions() { public Factory<Integer, Contribution> getContributions() {

View file

@ -36,8 +36,8 @@ public class ContributionsRepository {
return localDataSource.deleteContribution(contribution); return localDataSource.deleteContribution(contribution);
} }
public Completable deleteContributionsFromDB(List<Contribution> contributions) { public Completable deleteContributionsFromDBWithStates(List<Integer> states) {
return localDataSource.deleteContributions(contributions); return localDataSource.deleteContributionsWithStates(states);
} }
/** /**

View file

@ -197,7 +197,7 @@ class FailedUploadsFragment : CommonsDaggerSupportFragment(), PendingUploadsCont
ViewUtil.showShortToast(context, R.string.cancelling_upload) ViewUtil.showShortToast(context, R.string.cancelling_upload)
uploadProgressActivity.hidePendingIcons() uploadProgressActivity.hidePendingIcons()
pendingUploadsPresenter.deleteUploads( pendingUploadsPresenter.deleteUploads(
contributionsList, listOf(Contribution.STATE_FAILED),
this.requireContext().applicationContext this.requireContext().applicationContext
) )
}, },

View file

@ -86,7 +86,6 @@ class PendingUploadsAdapter(private val callback: Callback) :
} }
fun bindState(state: Int) { fun bindState(state: Int) {
Timber.tag("PRINT").e("State is: "+state)
if (state == Contribution.STATE_QUEUED || state == Contribution.STATE_PAUSED) { if (state == Contribution.STATE_QUEUED || state == Contribution.STATE_PAUSED) {
errorTextView.text = "Queued" errorTextView.text = "Queued"
errorTextView.visibility = View.VISIBLE errorTextView.visibility = View.VISIBLE
@ -98,7 +97,6 @@ class PendingUploadsAdapter(private val callback: Callback) :
} }
fun bindProgress(transferred: Long, total: Long, state: Int) { fun bindProgress(transferred: Long, total: Long, state: Int) {
Timber.tag("PRINT").e("State is2: "+state)
if (transferred == 0L) { if (transferred == 0L) {
errorTextView.text = "Queued" errorTextView.text = "Queued"
errorTextView.visibility = View.VISIBLE errorTextView.visibility = View.VISIBLE

View file

@ -220,7 +220,7 @@ class PendingUploadsFragment : CommonsDaggerSupportFragment(), PendingUploadsCon
ViewUtil.showShortToast(context, R.string.cancelling_upload) ViewUtil.showShortToast(context, R.string.cancelling_upload)
uploadProgressActivity.hidePendingIcons() uploadProgressActivity.hidePendingIcons()
pendingUploadsPresenter.deleteUploads( pendingUploadsPresenter.deleteUploads(
contributionsList, listOf(Contribution.STATE_QUEUED, Contribution.STATE_IN_PROGRESS, Contribution.STATE_PAUSED),
this.requireContext().applicationContext this.requireContext().applicationContext
) )
}, },

View file

@ -136,9 +136,9 @@ public class PendingUploadsPresenter implements UserActionListener {
)); ));
} }
public void deleteUploads(List<Contribution> contributionList, Context context) { public void deleteUploads(List<Integer> states, Context context) {
compositeDisposable.add(repository compositeDisposable.add(repository
.deleteContributionsFromDB(contributionList) .deleteContributionsFromDBWithStates(states)
.subscribeOn(ioThreadScheduler) .subscribeOn(ioThreadScheduler)
.subscribe(() -> .subscribe(() ->
WorkRequestHelper.Companion.makeOneTimeWorkRequest( WorkRequestHelper.Companion.makeOneTimeWorkRequest(

View file

@ -177,6 +177,8 @@ class UploadWorker(var appContext: Context, workerParams: WorkerParameters) :
CommonsApplication.NOTIFICATION_CHANNEL_ID_ALL CommonsApplication.NOTIFICATION_CHANNEL_ID_ALL
)!! )!!
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
//TODO: Implement Worker Flags
/* /*
queuedContributions receives the results from a one-shot query. queuedContributions receives the results from a one-shot query.
This means that once the list has been fetched from the database, This means that once the list has been fetched from the database,
@ -187,46 +189,50 @@ class UploadWorker(var appContext: Context, workerParams: WorkerParameters) :
https://github.com/commons-app/apps-android-commons/issues/5136 https://github.com/commons-app/apps-android-commons/issues/5136
https://github.com/commons-app/apps-android-commons/issues/5346 https://github.com/commons-app/apps-android-commons/issues/5346
*/ */
val queuedContributions = contributionDao.getContribution(statesToProcess) while (contributionDao.getContribution(statesToProcess)
.blockingGet() .blockingGet().size > 0
//Showing initial notification for the number of uploads being processed ) {
val queuedContributions = contributionDao.getContribution(statesToProcess)
.blockingGet()
//Showing initial notification for the number of uploads being processed
processingUploads.setContentTitle(appContext.getString(R.string.starting_uploads)) processingUploads.setContentTitle(appContext.getString(R.string.starting_uploads))
processingUploads.setContentText( processingUploads.setContentText(
appContext.resources.getQuantityString( appContext.resources.getQuantityString(
R.plurals.starting_multiple_uploads, R.plurals.starting_multiple_uploads,
queuedContributions.size, queuedContributions.size,
queuedContributions.size queuedContributions.size
) )
) )
notificationManager?.notify( notificationManager?.notify(
PROCESSING_UPLOADS_NOTIFICATION_TAG, PROCESSING_UPLOADS_NOTIFICATION_TAG,
PROCESSING_UPLOADS_NOTIFICATION_ID, PROCESSING_UPLOADS_NOTIFICATION_ID,
processingUploads.build() processingUploads.build()
) )
val sortedQueuedContributionsList: List<Contribution> = queuedContributions.sortedBy { it.dateUploadStartedInMillis() } val sortedQueuedContributionsList: List<Contribution> =
queuedContributions.sortedBy { it.dateUploadStartedInMillis() }
/** /**
* To avoid race condition when multiple of these workers are working, assign this state * To avoid race condition when multiple of these workers are working, assign this state
so that the next one does not process these contribution again so that the next one does not process these contribution again
*/ */
// sortedQueuedContributionsList.forEach { // sortedQueuedContributionsList.forEach {
// it.state = Contribution.STATE_IN_PROGRESS // it.state = Contribution.STATE_IN_PROGRESS
// contributionDao.saveSynchronous(it) // contributionDao.saveSynchronous(it)
// } // }
var contribution = sortedQueuedContributionsList.first() var contribution = sortedQueuedContributionsList.first()
if (contributionDao.getContribution(contribution.pageId) != null) { if (contributionDao.getContribution(contribution.pageId) != null) {
contribution.transferred = 0 contribution.transferred = 0
contribution.state = Contribution.STATE_IN_PROGRESS contribution.state = Contribution.STATE_IN_PROGRESS
contributionDao.saveSynchronous(contribution) contributionDao.saveSynchronous(contribution)
setProgressAsync(Data.Builder().putInt("progress", countUpload).build()) setProgressAsync(Data.Builder().putInt("progress", countUpload).build())
countUpload++ countUpload++
uploadContribution(contribution = contribution) uploadContribution(contribution = contribution)
}
} }
//Dismiss the global notification //Dismiss the global notification
notificationManager?.cancel( notificationManager?.cancel(
PROCESSING_UPLOADS_NOTIFICATION_TAG, PROCESSING_UPLOADS_NOTIFICATION_TAG,