@@ -88,7 +88,7 @@ class AlbumFileUploadWorker(
8888 private val notificationId = Random .nextInt()
8989 private val notificationManager = UploadNotificationManager (context, viewThemeUtils, notificationId)
9090 private val intents = FileUploaderIntents (context)
91- private val fileUploaderDelegate = FileUploaderDelegate ( )
91+ private val fileUploadBroadcastManager = FileUploadBroadcastManager (localBroadcastManager )
9292
9393 override suspend fun doWork (): Result = try {
9494 Log_OC .d(TAG , " AlbumFileUploadWorker started" )
@@ -100,14 +100,15 @@ class AlbumFileUploadWorker(
100100 val result = uploadFiles()
101101 backgroundJobManager.logEndOfWorker(workerName, result)
102102 notificationManager.dismissNotification()
103- if (result == Result .success()) {
104- setIdleWorkerState()
105- }
106103 result
107104 } catch (t: Throwable ) {
108- Log_OC .e(TAG , " Error caught at AlbumFileUploadWorker $t " )
109- cleanup( )
105+ Log_OC .e(TAG , " exception $t " )
106+ currentUploadFileOperation?.cancel( null )
110107 Result .failure()
108+ } finally {
109+ // Ensure all database operations are complete before signaling completion
110+ uploadsStorageManager.notifyObserversNow()
111+ notificationManager.dismissNotification()
111112 }
112113
113114 private suspend fun trySetForeground () {
@@ -155,22 +156,6 @@ class AlbumFileUploadWorker(
155156 .setSilent(true )
156157 .build()
157158
158- private fun cleanup () {
159- Log_OC .e(TAG , " AlbumFileUploadWorker stopped" )
160-
161- setIdleWorkerState()
162- currentUploadFileOperation?.cancel(null )
163- notificationManager.dismissNotification()
164- }
165-
166- private fun setWorkerState (user : User ? ) {
167- WorkerStateObserver .send(WorkerState .FileUploadStarted (user))
168- }
169-
170- private fun setIdleWorkerState () {
171- WorkerStateObserver .send(WorkerState .FileUploadCompleted (currentUploadFileOperation?.file))
172- }
173-
174159 @Suppress(" ReturnCount" , " LongMethod" , " DEPRECATION" )
175160 private suspend fun uploadFiles (): Result = withContext(Dispatchers .IO ) {
176161 val accountName = inputData.getString(ACCOUNT )
@@ -232,7 +217,7 @@ class AlbumFileUploadWorker(
232217 return @withContext Result .failure()
233218 }
234219
235- setWorkerState(user )
220+ fileUploadBroadcastManager.sendAdded(context )
236221 val operation = createUploadFileOperation(upload, user)
237222 currentUploadFileOperation = operation
238223
@@ -270,17 +255,18 @@ class AlbumFileUploadWorker(
270255 operation : UploadFileOperation ,
271256 result : RemoteOperationResult <* >
272257 ) {
258+ val isLastUpload = currentUploadIndex == totalUploadSize
259+
273260 val shouldBroadcast =
274- (totalUploadSize > BATCH_SIZE && currentUploadIndex > 0 ) && currentUploadIndex % BATCH_SIZE == 0
261+ (currentUploadIndex % BATCH_SIZE == 0 && totalUploadSize > BATCH_SIZE ) ||
262+ isLastUpload
275263
276264 if (shouldBroadcast) {
277- // delay broadcast
278- fileUploaderDelegate.sendBroadcastUploadFinished(
265+ fileUploadBroadcastManager.sendFinished(
279266 operation,
280267 result,
281268 operation.oldFile?.storagePath,
282- context,
283- localBroadcastManager
269+ context
284270 )
285271 }
286272 }
@@ -341,13 +327,14 @@ class AlbumFileUploadWorker(
341327 } else {
342328 Log_OC .e(TAG , " Failed to copy file to Album: $albumName due to ${copyResult.logMessage} " )
343329 }
330+ fileUploadBroadcastManager.sendStarted(operation, context)
344331 } catch (e: Exception ) {
345332 Log_OC .e(TAG , " Error uploading" , e)
346333 result = RemoteOperationResult <Any ?>(e)
347334 } finally {
348335 if (! isStopped) {
349336 uploadsStorageManager.updateDatabaseUploadResult(result, operation)
350- // NMC: resolving file conflict will trigger normal file upload and shows two upload process
337+ // resolving file conflict will trigger normal file upload and shows two upload process
351338 // one for normal and one for Album upload
352339 // as customizing conflict can break normal upload
353340 // so we are removing the upload if it's a conflict
0 commit comments