From d4d7f60f2c55fdbd586a739ea92121f96347f95e Mon Sep 17 00:00:00 2001 From: alperozturk Date: Thu, 12 Dec 2024 11:38:52 +0100 Subject: [PATCH] fix isDownloading logic Signed-off-by: alperozturk --- .../client/jobs/BackgroundJobManager.kt | 2 +- .../client/jobs/BackgroundJobManagerImpl.kt | 3 ++- .../client/jobs/download/FileDownloadHelper.kt | 6 +++--- .../nextcloud/client/jobs/sync/SyncWorker.kt | 17 ++++++++++++----- .../operations/SynchronizeFolderOperation.java | 11 +++++++++-- 5 files changed, 27 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManager.kt b/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManager.kt index 6c967e1441..d65b20ff62 100644 --- a/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManager.kt +++ b/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManager.kt @@ -172,5 +172,5 @@ interface BackgroundJobManager { fun startPeriodicallyOfflineOperation() fun scheduleInternal2WaySync(intervalMinutes: Long) fun cancelAllFilesDownloadJobs() - fun syncFolder(filePaths: List) + fun syncFolder(filePaths: List, topParentPath: String) } diff --git a/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManagerImpl.kt b/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManagerImpl.kt index be7d333b61..d4f79c4847 100644 --- a/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManagerImpl.kt +++ b/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManagerImpl.kt @@ -715,9 +715,10 @@ internal class BackgroundJobManagerImpl( workManager.enqueueUniquePeriodicWork(JOB_INTERNAL_TWO_WAY_SYNC, ExistingPeriodicWorkPolicy.UPDATE, request) } - override fun syncFolder(filePaths: List) { + override fun syncFolder(filePaths: List, topParentPath: String) { val data = Data.Builder() .putStringArray(SyncWorker.FILE_PATHS, filePaths.toTypedArray()) + .putString(SyncWorker.TOP_PARENT_PATH, topParentPath) .build() val request = oneTimeRequestBuilder(SyncWorker::class, JOB_SYNC_FOLDER) diff --git a/app/src/main/java/com/nextcloud/client/jobs/download/FileDownloadHelper.kt b/app/src/main/java/com/nextcloud/client/jobs/download/FileDownloadHelper.kt index 58bc099bc6..75e10efcaa 100644 --- a/app/src/main/java/com/nextcloud/client/jobs/download/FileDownloadHelper.kt +++ b/app/src/main/java/com/nextcloud/client/jobs/download/FileDownloadHelper.kt @@ -48,7 +48,7 @@ class FileDownloadHelper { } return if (file.isFolder) { - SyncWorker.isDownloading(file.fileId) + SyncWorker.isDownloading(file.decryptedRemotePath) } else { FileDownloadWorker.isDownloading(user.accountName, file.fileId) } @@ -139,7 +139,7 @@ class FileDownloadHelper { ) } - fun syncFolder(filePaths: List) { - backgroundJobManager.syncFolder(filePaths) + fun syncFolder(filePaths: List, topParentPath: String) { + backgroundJobManager.syncFolder(filePaths, topParentPath) } } diff --git a/app/src/main/java/com/nextcloud/client/jobs/sync/SyncWorker.kt b/app/src/main/java/com/nextcloud/client/jobs/sync/SyncWorker.kt index 3bf7d9461e..f052c91813 100644 --- a/app/src/main/java/com/nextcloud/client/jobs/sync/SyncWorker.kt +++ b/app/src/main/java/com/nextcloud/client/jobs/sync/SyncWorker.kt @@ -29,6 +29,7 @@ class SyncWorker( private const val TAG = "SyncWorker" const val FILE_PATHS = "FILE_PATHS" + const val TOP_PARENT_PATH = "TOP_PARENT_PATH" private var downloadingFilePaths = ArrayList() @@ -48,13 +49,15 @@ class SyncWorker( return withContext(Dispatchers.IO) { Log_OC.d(TAG, "SyncWorker started") val filePaths = inputData.getStringArray(FILE_PATHS) + val topParentPath = inputData.getString(TOP_PARENT_PATH) - if (filePaths.isNullOrEmpty()) { + if (filePaths.isNullOrEmpty() || topParentPath.isNullOrEmpty()) { return@withContext Result.failure() } - // TODO - // downloadingFilePaths = filePaths + downloadingFilePaths = ArrayList(filePaths.toList()).apply { + add(topParentPath) + } val fileDataStorageManager = FileDataStorageManager(user, context.contentResolver) @@ -77,19 +80,23 @@ class SyncWorker( val operation = DownloadFileOperation(user, file, context).execute(client) Log_OC.d(TAG, "Syncing file: " + file.decryptedRemotePath) - if (!operation.isSuccess) { + + if (operation.isSuccess) { + downloadingFilePaths.remove(path) + } else { result = false } } } - // TODO add isDownloading + // TODO add notify isDownloading for adapter // TODO add cancel only one file download withContext(Dispatchers.Main) { notificationManager.showCompletionMessage(result) } if (result) { + downloadingFilePaths.remove(topParentPath) Log_OC.d(TAG, "SyncWorker completed") Result.success() } else { diff --git a/app/src/main/java/com/owncloud/android/operations/SynchronizeFolderOperation.java b/app/src/main/java/com/owncloud/android/operations/SynchronizeFolderOperation.java index 733293ba49..a2e79d350e 100644 --- a/app/src/main/java/com/owncloud/android/operations/SynchronizeFolderOperation.java +++ b/app/src/main/java/com/owncloud/android/operations/SynchronizeFolderOperation.java @@ -40,7 +40,6 @@ import java.util.List; import java.util.Map; import java.util.Vector; import java.util.concurrent.atomic.AtomicBoolean; -import java.util.function.Consumer; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; @@ -503,7 +502,15 @@ public class SynchronizeFolderOperation extends SyncOperation { } else { final var filePaths = new ArrayList(); mFilesForDirectDownload.forEach(file -> filePaths.add(file.getDecryptedRemotePath())); - fileDownloadHelper.syncFolder(filePaths); + + if (filePaths.isEmpty()) { + return; + } + + final var storageManager = getStorageManager(); + final OCFile firstFile = storageManager.getFileByDecryptedRemotePath(filePaths.get(0)); + final OCFile topParent = storageManager.getTopParent(firstFile); + fileDownloadHelper.syncFolder(filePaths, topParent.getDecryptedRemotePath()); } }