Cancel fix

Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
alperozturk 2024-01-10 16:11:49 +01:00 committed by Alper Öztürk
parent c4ba0e0b43
commit 15dedf8116
5 changed files with 53 additions and 9 deletions

View file

@ -60,15 +60,17 @@ class FileDownloadHelper {
return false return false
} }
return backgroundJobManager.isStartFileDownloadJobScheduled(user, file.fileId) || return FileDownloadWorker.isDownloading(user.accountName, file.fileId) ||
backgroundJobManager.isStartFileDownloadJobScheduled(user, file.parentId) FileDownloadWorker.isDownloading(user.accountName, file.parentId)
} }
fun cancelPendingOrCurrentDownloads(user: User?, file: OCFile?) { fun cancelPendingOrCurrentDownloads(user: User?, files: List<OCFile>?) {
if (user == null || file == null) return if (user == null || files == null) return
FileDownloadWorker.cancelOperation(user.accountName, file.fileId) files.forEach {
backgroundJobManager.cancelFilesDownloadJob(user, file.fileId) FileDownloadWorker.cancelOperation(user.accountName, it.fileId)
backgroundJobManager.cancelFilesDownloadJob(user, it.fileId)
}
} }
fun cancelAllDownloadsForAccount(accountName: String?, currentDownload: DownloadFileOperation?) { fun cancelAllDownloadsForAccount(accountName: String?, currentDownload: DownloadFileOperation?) {

View file

@ -75,6 +75,21 @@ class FileDownloadWorker(
} }
} }
/*
Folder 1 -- id = 100
file 1-- parentID 100
folder 2-- parentID 100
file 3-- parentID 100
file 4 -- parentID 100
Folder 4 -- parentID 100
file 6 -- parentID 100
*/
fun isDownloading(accountName: String, fileId: Long): Boolean {
return pendingDownloads.all.any { it.value.payload.isMatching(accountName, fileId) }
}
const val WORKER_ID = "WORKER_ID" const val WORKER_ID = "WORKER_ID"
const val FILES_SEPARATOR = "," const val FILES_SEPARATOR = ","
const val FOLDER_REMOTE_PATH = "FOLDER_REMOTE_PATH" const val FOLDER_REMOTE_PATH = "FOLDER_REMOTE_PATH"

View file

@ -180,6 +180,29 @@ public class FileDataStorageManager {
return fileDao.getFileByEncryptedRemotePath(path, user.getAccountName()) != null; return fileDao.getFileByEncryptedRemotePath(path, user.getAccountName()) != null;
} }
public List<OCFile> getAllFilesRecursivelyInsideFolder(OCFile file) {
ArrayList<OCFile> result = new ArrayList<>();
if (file == null || !file.fileExists()) {
return result;
}
if (!file.isFolder()) {
result.add(file);
return result;
}
List<OCFile> filesInsideFolder = getFolderContent(file.getFileId(), false);
for (OCFile item: filesInsideFolder) {
if (!item.isFolder()) {
result.add(item);
} else {
result.addAll(getAllFilesRecursivelyInsideFolder(item));
}
}
return result;
}
public List<OCFile> getFolderContent(OCFile ocFile, boolean onlyOnDevice) { public List<OCFile> getFolderContent(OCFile ocFile, boolean onlyOnDevice) {
if (ocFile != null && ocFile.isFolder() && ocFile.fileExists()) { if (ocFile != null && ocFile.isFolder() && ocFile.fileExists()) {
@ -189,7 +212,6 @@ public class FileDataStorageManager {
} }
} }
public List<OCFile> getFolderImages(OCFile folder, boolean onlyOnDevice) { public List<OCFile> getFolderImages(OCFile folder, boolean onlyOnDevice) {
List<OCFile> imageList = new ArrayList<>(); List<OCFile> imageList = new ArrayList<>();

View file

@ -99,8 +99,12 @@ public class DownloadFileOperation extends RemoteOperation {
this(user, file, null, null, null, context, DownloadType.DOWNLOAD); this(user, file, null, null, null, context, DownloadType.DOWNLOAD);
} }
public boolean isMatching(String accountName, long fileId) {
return getFile().getFileId() == fileId && getUser().getAccountName().equals(accountName);
}
public void cancelMatchingOperation(String accountName, long fileId) { public void cancelMatchingOperation(String accountName, long fileId) {
if (getFile().getFileId() == fileId && getUser().getAccountName().equals(accountName)) { if (isMatching(accountName, fileId)) {
cancel(); cancel();
} }
} }

View file

@ -997,7 +997,8 @@ public class FileOperationsHelper {
} }
if (FileDownloadHelper.Companion.instance().isDownloading(currentUser, file)) { if (FileDownloadHelper.Companion.instance().isDownloading(currentUser, file)) {
FileDownloadHelper.Companion.instance().cancelPendingOrCurrentDownloads(currentUser, file); List<OCFile> files = fileActivity.getStorageManager().getAllFilesRecursivelyInsideFolder(file);
FileDownloadHelper.Companion.instance().cancelPendingOrCurrentDownloads(currentUser, files);
} }
FileUploaderBinder uploaderBinder = fileActivity.getFileUploaderBinder(); FileUploaderBinder uploaderBinder = fileActivity.getFileUploaderBinder();