Skip to content

Commit a2639b6

Browse files
committed
album upload worker state handled via localbroadcastmanager
Signed-off-by: Surinder Kumar <surinder.kumar@t-systems.com>
1 parent b20debe commit a2639b6

4 files changed

Lines changed: 17 additions & 44 deletions

File tree

app/src/main/java/com/nextcloud/client/jobs/upload/AlbumFileUploadWorker.kt

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -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

app/src/main/java/com/owncloud/android/ui/adapter/GalleryAdapter.kt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -259,14 +259,6 @@ class GalleryAdapter(
259259
notifyDataSetChanged()
260260
}
261261

262-
@SuppressLint("NotifyDataSetChanged")
263-
fun showAlbumItems(
264-
albumItems: List<OCFile>,
265-
) {
266-
files = albumItems.toGalleryItems()
267-
Handler(Looper.getMainLooper()).post { notifyDataSetChanged() }
268-
}
269-
270262
private fun transformToRows(list: List<OCFile>): List<GalleryRow> {
271263
if (list.isEmpty()) return emptyList()
272264

app/src/main/java/com/owncloud/android/ui/dialog/CreateAlbumDialogFragment.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ class CreateAlbumDialogFragment :
9999
val inflater = requireActivity().layoutInflater
100100
binding = EditBoxDialogBinding.inflate(inflater, null, false)
101101

102-
103102
binding.userInput.setText(albumName ?: "")
104103
viewThemeUtils.material.colorTextInputLayout(binding.userInputContainer)
105104
albumName?.let {

app/src/main/java/com/owncloud/android/ui/fragment/albums/AlbumItemsFragment.kt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ import com.owncloud.android.ui.preview.PreviewImageFragment
9191
import com.owncloud.android.ui.preview.PreviewMediaActivity.Companion.canBePreviewed
9292
import com.owncloud.android.utils.DisplayUtils
9393
import com.owncloud.android.utils.ErrorMessageAdapter
94-
import com.owncloud.android.utils.FileStorageUtils
9594
import com.owncloud.android.utils.theme.ViewThemeUtils
9695
import kotlinx.coroutines.Dispatchers
9796
import kotlinx.coroutines.FlowPreview
@@ -270,8 +269,7 @@ class AlbumItemsFragment :
270269
true
271270
}
272271

273-
else -> false
274-
}
272+
else -> false
275273
}
276274

277275
override fun onPrepareMenu(menu: Menu) {
@@ -333,7 +331,6 @@ class AlbumItemsFragment :
333331
binding.listRoot.setEmptyView(binding.emptyList.emptyListView)
334332
val layoutManager = GridLayoutManager(requireContext(), 1)
335333
binding.listRoot.layoutManager = layoutManager
336-
fetchAndSetData()
337334
}
338335

339336
private fun setupContainingList() {
@@ -382,7 +379,6 @@ class AlbumItemsFragment :
382379

383380
contentValues.add(cv)
384381
}
385-
ocFileList.add(ocFile!!)
386382
}
387383

388384
mContainerActivity?.storageManager?.saveVirtuals(contentValues)
@@ -520,7 +516,6 @@ class AlbumItemsFragment :
520516
override fun onDestroyView() {
521517
lastMediaItemPosition = 0
522518
super.onDestroyView()
523-
lastMediaItemPosition = 0
524519
}
525520

526521
override fun getColumnsCount(): Int = columnSize

0 commit comments

Comments
 (0)