add isDownloading

Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
alperozturk 2024-12-02 11:21:10 +01:00 committed by Alper Öztürk
parent a776d76b07
commit 658fa8e52d
5 changed files with 43 additions and 29 deletions

View file

@ -146,8 +146,6 @@ interface BackgroundJobManager {
fun cancelFilesDownloadJob(user: User, fileId: Long) fun cancelFilesDownloadJob(user: User, fileId: Long)
fun isStartFileDownloadJobScheduled(user: User, fileId: Long): Boolean
@Suppress("LongParameterList") @Suppress("LongParameterList")
fun startFileDownloadJob( fun startFileDownloadJob(
user: User, user: User,

View file

@ -597,10 +597,6 @@ internal class BackgroundJobManagerImpl(
return JOB_FOLDER_DOWNLOAD + user.accountName + fileId return JOB_FOLDER_DOWNLOAD + user.accountName + fileId
} }
override fun isStartFileDownloadJobScheduled(user: User, fileId: Long): Boolean {
return workManager.isWorkScheduled(startFileDownloadJobTag(user, fileId))
}
override fun startFileDownloadJob( override fun startFileDownloadJob(
user: User, user: User,
file: OCFile, file: OCFile,

View file

@ -9,6 +9,7 @@ package com.nextcloud.client.jobs.download
import com.nextcloud.client.account.User import com.nextcloud.client.account.User
import com.nextcloud.client.jobs.BackgroundJobManager import com.nextcloud.client.jobs.BackgroundJobManager
import com.nextcloud.client.jobs.sync.SyncWorker
import com.owncloud.android.MainApp import com.owncloud.android.MainApp
import com.owncloud.android.datamodel.FileDataStorageManager import com.owncloud.android.datamodel.FileDataStorageManager
import com.owncloud.android.datamodel.OCFile import com.owncloud.android.datamodel.OCFile
@ -46,12 +47,8 @@ class FileDownloadHelper {
return false return false
} }
val fileStorageManager = FileDataStorageManager(user, MainApp.getAppContext().contentResolver) return if (file.isFolder) {
val topParentId = fileStorageManager.getTopParentId(file) SyncWorker.isDownloading(file.fileId)
val isJobScheduled = backgroundJobManager.isStartFileDownloadJobScheduled(user, file.fileId)
return isJobScheduled || if (file.isFolder) {
backgroundJobManager.isStartFileDownloadJobScheduled(user, topParentId)
} else { } else {
FileDownloadWorker.isDownloading(user.accountName, file.fileId) FileDownloadWorker.isDownloading(user.accountName, file.fileId)
} }

View file

@ -12,6 +12,7 @@ import androidx.work.CoroutineWorker
import androidx.work.WorkerParameters import androidx.work.WorkerParameters
import com.nextcloud.client.account.User import com.nextcloud.client.account.User
import com.owncloud.android.datamodel.FileDataStorageManager import com.owncloud.android.datamodel.FileDataStorageManager
import com.owncloud.android.datamodel.OCFile
import com.owncloud.android.lib.common.OwnCloudClientManagerFactory import com.owncloud.android.lib.common.OwnCloudClientManagerFactory
import com.owncloud.android.lib.common.utils.Log_OC import com.owncloud.android.lib.common.utils.Log_OC
import com.owncloud.android.operations.DownloadFileOperation import com.owncloud.android.operations.DownloadFileOperation
@ -26,8 +27,17 @@ class SyncWorker(
) : CoroutineWorker(context, params) { ) : CoroutineWorker(context, params) {
companion object { companion object {
const val FILE_PATHS = "FILE_PATHS"
private const val TAG = "SyncWorker" private const val TAG = "SyncWorker"
const val FILE_PATHS = "FILE_PATHS"
// FIXME it's not synchronous
fun isDownloading(fileId: Long): Boolean {
return downloadFileIds.contains(fileId) || currentDownloadFolderId == fileId
}
private var currentDownloadFolderId: Long? = null
private var downloadFileIds = ArrayList<Long>()
} }
private val notificationManager = SyncWorkerNotificationManager(context) private val notificationManager = SyncWorkerNotificationManager(context)
@ -59,15 +69,15 @@ class SyncWorker(
} }
fileDataStorageManager.getFileByDecryptedRemotePath(path)?.let { file -> fileDataStorageManager.getFileByDecryptedRemotePath(path)?.let { file ->
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
notificationManager.showProgressNotification(file.fileName, index, filePaths.size) notificationManager.showProgressNotification(file.fileName, index, filePaths.size)
} }
delay(1000) delay(1000)
// TODO dont download downloaded files??
val operation = DownloadFileOperation(user, file, context).execute(client) val operation = DownloadFileOperation(user, file, context).execute(client)
setCurrentDownloadFileIds(fileDataStorageManager, file)
Log_OC.d(TAG, "Syncing file: " + file.decryptedRemotePath) Log_OC.d(TAG, "Syncing file: " + file.decryptedRemotePath)
if (!operation.isSuccess) { if (!operation.isSuccess) {
result = false result = false
@ -78,25 +88,26 @@ class SyncWorker(
// TODO add isDownloading // TODO add isDownloading
// TODO add cancel only one file download // TODO add cancel only one file download
if (result) { downloadFileIds.clear()
Log_OC.d(TAG, "SyncWorker completed")
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
notificationManager.showSuccessNotification() notificationManager.showCompletionMessage(result)
} }
if (result) {
Log_OC.d(TAG, "SyncWorker completed")
Result.success() Result.success()
} else { } else {
Log_OC.d(TAG, "SyncWorker failed") Log_OC.d(TAG, "SyncWorker failed")
withContext(Dispatchers.Main) {
notificationManager.showErrorNotification()
delay(1000)
notificationManager.dismiss()
}
Result.failure() Result.failure()
} }
} }
} }
private fun setCurrentDownloadFileIds(fileDataStorageManager: FileDataStorageManager, file: OCFile) {
if (currentDownloadFolderId == null) {
currentDownloadFolderId = fileDataStorageManager.getTopParentId(file)
}
downloadFileIds.add(file.fileId)
}
} }

View file

@ -15,6 +15,7 @@ import android.content.Intent
import android.os.Build import android.os.Build
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import com.owncloud.android.ui.notifications.NotificationUtils import com.owncloud.android.ui.notifications.NotificationUtils
import kotlinx.coroutines.delay
class SyncWorkerNotificationManager(private val context: Context) { class SyncWorkerNotificationManager(private val context: Context) {
@ -75,12 +76,23 @@ class SyncWorkerNotificationManager(private val context: Context) {
notificationManager.notify(notificationId, notification) notificationManager.notify(notificationId, notification)
} }
fun showSuccessNotification() { suspend fun showCompletionMessage(success: Boolean) {
if (success) {
showSuccessNotification()
} else {
showErrorNotification()
}
delay(1000)
dismiss()
}
private fun showSuccessNotification() {
val notification = getNotification("Download Complete", "File downloaded successfully") val notification = getNotification("Download Complete", "File downloaded successfully")
notificationManager.notify(notificationId, notification) notificationManager.notify(notificationId, notification)
} }
fun showErrorNotification() { private fun showErrorNotification() {
val notification = getNotification("Download Failed", "Error downloading file") val notification = getNotification("Download Failed", "Error downloading file")
notificationManager.notify(notificationId, notification) notificationManager.notify(notificationId, notification)
} }