@@ -105,6 +105,7 @@ class UploadWorker(var appContext: Context, workerParams: WorkerParameters) :
105
105
106
106
statesToProcess.add(Contribution .STATE_QUEUED )
107
107
statesToProcess.add(Contribution .STATE_QUEUED_LIMITED_CONNECTION_MODE )
108
+ statesToProcess.add(Contribution .STATE_FAILED )
108
109
}
109
110
110
111
@dagger.Module
@@ -164,75 +165,77 @@ class UploadWorker(var appContext: Context, workerParams: WorkerParameters) :
164
165
}
165
166
166
167
override suspend fun doWork (): Result {
167
- try {
168
- var countUpload = 0
169
- notificationManager = NotificationManagerCompat .from(appContext)
170
- val processingUploads = getNotificationBuilder(
171
- CommonsApplication .NOTIFICATION_CHANNEL_ID_ALL
172
- )!!
173
- withContext(Dispatchers .IO ) {
174
- val queuedContributions = contributionDao.getContribution(statesToProcess)
175
- .blockingGet()
176
- // Showing initial notification for the number of uploads being processed
177
-
178
- Timber .e(" Queued Contributions: " + queuedContributions.size)
179
-
180
- processingUploads.setContentTitle(appContext.getString(R .string.starting_uploads))
181
- processingUploads.setContentText(
182
- appContext.resources.getQuantityString(
183
- R .plurals.starting_multiple_uploads,
184
- queuedContributions.size,
185
- queuedContributions.size
186
- )
187
- )
188
- notificationManager?.notify(
189
- PROCESSING_UPLOADS_NOTIFICATION_TAG ,
190
- PROCESSING_UPLOADS_NOTIFICATION_ID ,
191
- processingUploads.build()
168
+ var countUpload = 0
169
+ notificationManager = NotificationManagerCompat .from(appContext)
170
+ val processingUploads = getNotificationBuilder(
171
+ CommonsApplication .NOTIFICATION_CHANNEL_ID_ALL
172
+ )!!
173
+ withContext(Dispatchers .IO ) {
174
+ val queuedContributions = contributionDao.getContribution(statesToProcess)
175
+ .blockingGet()
176
+ // Showing initial notification for the number of uploads being processed
177
+
178
+ Timber .e(" Queued Contributions: " + queuedContributions.size)
179
+
180
+ processingUploads.setContentTitle(appContext.getString(R .string.starting_uploads))
181
+ processingUploads.setContentText(
182
+ appContext.resources.getQuantityString(
183
+ R .plurals.starting_multiple_uploads,
184
+ queuedContributions.size,
185
+ queuedContributions.size
192
186
)
187
+ )
188
+ notificationManager?.notify(
189
+ PROCESSING_UPLOADS_NOTIFICATION_TAG ,
190
+ PROCESSING_UPLOADS_NOTIFICATION_ID ,
191
+ processingUploads.build()
192
+ )
193
193
194
+ /* *
195
+ * To avoid race condition when multiple of these workers are working, assign this state
196
+ so that the next one does not process these contribution again
197
+ */
198
+ queuedContributions.forEach {
199
+ it.state = Contribution .STATE_IN_PROGRESS
200
+ contributionDao.saveSynchronous(it)
201
+ }
202
+
203
+ queuedContributions.asFlow().map { contribution ->
194
204
/* *
195
- * To avoid race condition when multiple of these workers are working, assign this state
196
- so that the next one does not process these contribution again
205
+ * If the limited connection mode is on, lets iterate through the queued
206
+ * contributions
207
+ * and set the state as STATE_QUEUED_LIMITED_CONNECTION_MODE ,
208
+ * otherwise proceed with the upload
197
209
*/
198
- queuedContributions.forEach {
199
- it.state = Contribution .STATE_IN_PROGRESS
200
- contributionDao.saveSynchronous(it)
201
- }
202
-
203
- queuedContributions.asFlow().map { contribution ->
204
- /* *
205
- * If the limited connection mode is on, lets iterate through the queued
206
- * contributions
207
- * and set the state as STATE_QUEUED_LIMITED_CONNECTION_MODE ,
208
- * otherwise proceed with the upload
209
- */
210
- if (isLimitedConnectionModeEnabled()) {
211
- if (contribution.state == Contribution .STATE_QUEUED ) {
212
- contribution.state = Contribution .STATE_QUEUED_LIMITED_CONNECTION_MODE
213
- contributionDao.saveSynchronous(contribution)
214
- }
215
- } else {
216
- contribution.transferred = 0
217
- contribution.state = Contribution .STATE_IN_PROGRESS
210
+ if (isLimitedConnectionModeEnabled()) {
211
+ if (contribution.state == Contribution .STATE_QUEUED ) {
212
+ contribution.state = Contribution .STATE_QUEUED_LIMITED_CONNECTION_MODE
218
213
contributionDao.saveSynchronous(contribution)
219
- setProgressAsync(Data .Builder ().putInt(" progress" , countUpload).build())
220
- countUpload++
221
- uploadContribution(contribution = contribution)
222
214
}
223
- }.collect()
215
+ } else {
216
+ contribution.transferred = 0
217
+ contribution.state = Contribution .STATE_IN_PROGRESS
218
+ contributionDao.saveSynchronous(contribution)
219
+ setProgressAsync(Data .Builder ().putInt(" progress" , countUpload).build())
220
+ countUpload++
221
+ uploadContribution(contribution = contribution)
222
+ }
223
+ }.collect()
224
224
225
- // Dismiss the global notification
226
- notificationManager?.cancel(
227
- PROCESSING_UPLOADS_NOTIFICATION_TAG ,
228
- PROCESSING_UPLOADS_NOTIFICATION_ID
229
- )
230
- }
231
- // TODO make this smart, think of handling retries in the future
232
- return Result .success()
233
- } catch (e: Exception ) {
225
+ // Dismiss the global notification
226
+ notificationManager?.cancel(
227
+ PROCESSING_UPLOADS_NOTIFICATION_TAG ,
228
+ PROCESSING_UPLOADS_NOTIFICATION_ID
229
+ )
230
+ }
231
+
232
+ // Retry if any new contribution is added to the queue
233
+ val updatedContributionsQueue = contributionDao.getContribution(statesToProcess).blockingGet()
234
+ if (updatedContributionsQueue.isNotEmpty()) {
234
235
return Result .retry()
235
236
}
237
+
238
+ return Result .success()
236
239
}
237
240
238
241
override suspend fun getForegroundInfo (): ForegroundInfo {
0 commit comments