fix isDownloading logic

Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
alperozturk 2024-12-12 11:38:52 +01:00 committed by Alper Öztürk
parent 1e8dc0f762
commit d4d7f60f2c
5 changed files with 27 additions and 12 deletions

View file

@ -172,5 +172,5 @@ interface BackgroundJobManager {
fun startPeriodicallyOfflineOperation()
fun scheduleInternal2WaySync(intervalMinutes: Long)
fun cancelAllFilesDownloadJobs()
fun syncFolder(filePaths: List<String>)
fun syncFolder(filePaths: List<String>, topParentPath: String)
}

View file

@ -715,9 +715,10 @@ internal class BackgroundJobManagerImpl(
workManager.enqueueUniquePeriodicWork(JOB_INTERNAL_TWO_WAY_SYNC, ExistingPeriodicWorkPolicy.UPDATE, request)
}
override fun syncFolder(filePaths: List<String>) {
override fun syncFolder(filePaths: List<String>, 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)

View file

@ -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<String>) {
backgroundJobManager.syncFolder(filePaths)
fun syncFolder(filePaths: List<String>, topParentPath: String) {
backgroundJobManager.syncFolder(filePaths, topParentPath)
}
}

View file

@ -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<String>()
@ -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 {

View file

@ -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<String>();
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());
}
}