add cancel sync functionality

Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
alperozturk 2024-12-19 09:48:42 +01:00
parent 0c5db2c5bf
commit a54bdb7559
No known key found for this signature in database
GPG key ID: 4E577DC593B59BDF
5 changed files with 22 additions and 14 deletions

View file

@ -172,6 +172,6 @@ interface BackgroundJobManager {
fun startPeriodicallyOfflineOperation() fun startPeriodicallyOfflineOperation()
fun scheduleInternal2WaySync(intervalMinutes: Long) fun scheduleInternal2WaySync(intervalMinutes: Long)
fun cancelAllFilesDownloadJobs() fun cancelAllFilesDownloadJobs()
fun syncFolder(files: List<OCFile>) fun syncFolder(files: List<OCFile>, folderId: Long)
fun cancelSyncFolder() fun cancelSyncFolder(folderId: Long)
} }

View file

@ -715,8 +715,7 @@ internal class BackgroundJobManagerImpl(
workManager.enqueueUniquePeriodicWork(JOB_INTERNAL_TWO_WAY_SYNC, ExistingPeriodicWorkPolicy.UPDATE, request) workManager.enqueueUniquePeriodicWork(JOB_INTERNAL_TWO_WAY_SYNC, ExistingPeriodicWorkPolicy.UPDATE, request)
} }
// TODO: Add tag for chosen folder override fun syncFolder(files: List<OCFile>, folderId: Long) {
override fun syncFolder(files: List<OCFile>) {
val filePaths = files.map { it.decryptedRemotePath } val filePaths = files.map { it.decryptedRemotePath }
val data = Data.Builder() val data = Data.Builder()
@ -724,15 +723,14 @@ internal class BackgroundJobManagerImpl(
.build() .build()
val request = oneTimeRequestBuilder(SyncWorker::class, JOB_SYNC_FOLDER) val request = oneTimeRequestBuilder(SyncWorker::class, JOB_SYNC_FOLDER)
.addTag(JOB_SYNC_FOLDER) .addTag(JOB_SYNC_FOLDER + folderId.toString())
.setInputData(data) .setInputData(data)
.build() .build()
workManager.enqueueUniqueWork(JOB_SYNC_FOLDER, ExistingWorkPolicy.REPLACE, request) workManager.enqueueUniqueWork(JOB_SYNC_FOLDER, ExistingWorkPolicy.REPLACE, request)
} }
// TODO: Add cancellation for chosen folder override fun cancelSyncFolder(folderId: Long) {
override fun cancelSyncFolder() { workManager.cancelAllWorkByTag(JOB_SYNC_FOLDER + folderId.toString())
TODO("Not yet implemented")
} }
} }

View file

@ -28,6 +28,9 @@ class FileDownloadHelper {
@Inject @Inject
lateinit var uploadsStorageManager: UploadsStorageManager lateinit var uploadsStorageManager: UploadsStorageManager
@Inject
lateinit var fileStorageManager: FileDataStorageManager
companion object { companion object {
private var instance: FileDownloadHelper? = null private var instance: FileDownloadHelper? = null
@ -139,6 +142,8 @@ class FileDownloadHelper {
} }
fun syncFolder(files: List<OCFile>) { fun syncFolder(files: List<OCFile>) {
backgroundJobManager.syncFolder(files) val firstFile = files.first()
val topParentFileId = fileStorageManager.getTopParentId(firstFile)
backgroundJobManager.syncFolder(files, topParentFileId)
} }
} }

View file

@ -15,6 +15,7 @@ import com.nextcloud.client.account.UserAccountManager
import com.nextcloud.client.device.BatteryStatus import com.nextcloud.client.device.BatteryStatus
import com.nextcloud.client.device.PowerManagementService import com.nextcloud.client.device.PowerManagementService
import com.nextcloud.client.jobs.BackgroundJobManager import com.nextcloud.client.jobs.BackgroundJobManager
import com.nextcloud.client.jobs.BackgroundJobManagerImpl.Companion.JOB_SYNC_FOLDER
import com.nextcloud.client.jobs.upload.FileUploadWorker.Companion.currentUploadFileOperation import com.nextcloud.client.jobs.upload.FileUploadWorker.Companion.currentUploadFileOperation
import com.nextcloud.client.network.Connectivity import com.nextcloud.client.network.Connectivity
import com.nextcloud.client.network.ConnectivityService import com.nextcloud.client.network.ConnectivityService
@ -245,6 +246,8 @@ class FileUploadHelper {
} }
} }
fun cancelSyncFolder(folderId: Long) = backgroundJobManager.cancelSyncFolder(folderId)
fun cancelFileUploads(uploads: List<OCUpload>, accountName: String) { fun cancelFileUploads(uploads: List<OCUpload>, accountName: String) {
for (upload in uploads) { for (upload in uploads) {
upload.uploadStatus = UploadStatus.UPLOAD_CANCELLED upload.uploadStatus = UploadStatus.UPLOAD_CANCELLED

View file

@ -975,18 +975,20 @@ public class FileOperationsHelper {
} }
} }
final var fileUploadHelper = FileUploadHelper.Companion.instance();
if (file.isFolder()) { if (file.isFolder()) {
// TODO: Call cancellation function fileUploadHelper.cancelSyncFolder(file.getFileId());
} }
if (FileDownloadHelper.Companion.instance().isDownloading(currentUser, file)) { final var fileDownloadHelper = FileDownloadHelper.Companion.instance();
if (fileDownloadHelper.isDownloading(currentUser, file)) {
List<OCFile> files = fileActivity.getStorageManager().getAllFilesRecursivelyInsideFolder(file); List<OCFile> files = fileActivity.getStorageManager().getAllFilesRecursivelyInsideFolder(file);
FileDownloadHelper.Companion.instance().cancelPendingOrCurrentDownloads(currentUser, files); fileDownloadHelper.cancelPendingOrCurrentDownloads(currentUser, files);
} }
if (FileUploadHelper.Companion.instance().isUploading(currentUser, file)) { if (fileUploadHelper.isUploading(currentUser, file)) {
try { try {
FileUploadHelper.Companion.instance().cancelFileUpload(file.getRemotePath(), currentUser.getAccountName()); fileUploadHelper.cancelFileUpload(file.getRemotePath(), currentUser.getAccountName());
} catch (NoSuchElementException e) { } catch (NoSuchElementException e) {
Log_OC.e(TAG, "Error cancelling current upload because user does not exist!"); Log_OC.e(TAG, "Error cancelling current upload because user does not exist!");
} }