mirror of
https://github.com/nextcloud/android.git
synced 2024-12-20 07:52:18 +03:00
add isDownloading
Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
parent
a776d76b07
commit
658fa8e52d
5 changed files with 43 additions and 29 deletions
|
@ -146,8 +146,6 @@ interface BackgroundJobManager {
|
|||
|
||||
fun cancelFilesDownloadJob(user: User, fileId: Long)
|
||||
|
||||
fun isStartFileDownloadJobScheduled(user: User, fileId: Long): Boolean
|
||||
|
||||
@Suppress("LongParameterList")
|
||||
fun startFileDownloadJob(
|
||||
user: User,
|
||||
|
|
|
@ -597,10 +597,6 @@ internal class BackgroundJobManagerImpl(
|
|||
return JOB_FOLDER_DOWNLOAD + user.accountName + fileId
|
||||
}
|
||||
|
||||
override fun isStartFileDownloadJobScheduled(user: User, fileId: Long): Boolean {
|
||||
return workManager.isWorkScheduled(startFileDownloadJobTag(user, fileId))
|
||||
}
|
||||
|
||||
override fun startFileDownloadJob(
|
||||
user: User,
|
||||
file: OCFile,
|
||||
|
|
|
@ -9,6 +9,7 @@ package com.nextcloud.client.jobs.download
|
|||
|
||||
import com.nextcloud.client.account.User
|
||||
import com.nextcloud.client.jobs.BackgroundJobManager
|
||||
import com.nextcloud.client.jobs.sync.SyncWorker
|
||||
import com.owncloud.android.MainApp
|
||||
import com.owncloud.android.datamodel.FileDataStorageManager
|
||||
import com.owncloud.android.datamodel.OCFile
|
||||
|
@ -46,12 +47,8 @@ class FileDownloadHelper {
|
|||
return false
|
||||
}
|
||||
|
||||
val fileStorageManager = FileDataStorageManager(user, MainApp.getAppContext().contentResolver)
|
||||
val topParentId = fileStorageManager.getTopParentId(file)
|
||||
|
||||
val isJobScheduled = backgroundJobManager.isStartFileDownloadJobScheduled(user, file.fileId)
|
||||
return isJobScheduled || if (file.isFolder) {
|
||||
backgroundJobManager.isStartFileDownloadJobScheduled(user, topParentId)
|
||||
return if (file.isFolder) {
|
||||
SyncWorker.isDownloading(file.fileId)
|
||||
} else {
|
||||
FileDownloadWorker.isDownloading(user.accountName, file.fileId)
|
||||
}
|
||||
|
|
|
@ -12,6 +12,7 @@ import androidx.work.CoroutineWorker
|
|||
import androidx.work.WorkerParameters
|
||||
import com.nextcloud.client.account.User
|
||||
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.utils.Log_OC
|
||||
import com.owncloud.android.operations.DownloadFileOperation
|
||||
|
@ -26,8 +27,17 @@ class SyncWorker(
|
|||
) : CoroutineWorker(context, params) {
|
||||
|
||||
companion object {
|
||||
const val FILE_PATHS = "FILE_PATHS"
|
||||
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)
|
||||
|
@ -59,15 +69,15 @@ class SyncWorker(
|
|||
}
|
||||
|
||||
fileDataStorageManager.getFileByDecryptedRemotePath(path)?.let { file ->
|
||||
|
||||
withContext(Dispatchers.Main) {
|
||||
notificationManager.showProgressNotification(file.fileName, index, filePaths.size)
|
||||
}
|
||||
|
||||
delay(1000)
|
||||
|
||||
// TODO dont download downloaded files??
|
||||
val operation = DownloadFileOperation(user, file, context).execute(client)
|
||||
setCurrentDownloadFileIds(fileDataStorageManager, file)
|
||||
|
||||
Log_OC.d(TAG, "Syncing file: " + file.decryptedRemotePath)
|
||||
if (!operation.isSuccess) {
|
||||
result = false
|
||||
|
@ -78,25 +88,26 @@ class SyncWorker(
|
|||
// TODO add isDownloading
|
||||
// TODO add cancel only one file download
|
||||
|
||||
downloadFileIds.clear()
|
||||
withContext(Dispatchers.Main) {
|
||||
notificationManager.showCompletionMessage(result)
|
||||
}
|
||||
|
||||
if (result) {
|
||||
Log_OC.d(TAG, "SyncWorker completed")
|
||||
|
||||
withContext(Dispatchers.Main) {
|
||||
notificationManager.showSuccessNotification()
|
||||
}
|
||||
|
||||
Result.success()
|
||||
} else {
|
||||
Log_OC.d(TAG, "SyncWorker failed")
|
||||
|
||||
withContext(Dispatchers.Main) {
|
||||
notificationManager.showErrorNotification()
|
||||
delay(1000)
|
||||
notificationManager.dismiss()
|
||||
}
|
||||
|
||||
Result.failure()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun setCurrentDownloadFileIds(fileDataStorageManager: FileDataStorageManager, file: OCFile) {
|
||||
if (currentDownloadFolderId == null) {
|
||||
currentDownloadFolderId = fileDataStorageManager.getTopParentId(file)
|
||||
}
|
||||
|
||||
downloadFileIds.add(file.fileId)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,6 +15,7 @@ import android.content.Intent
|
|||
import android.os.Build
|
||||
import androidx.core.app.NotificationCompat
|
||||
import com.owncloud.android.ui.notifications.NotificationUtils
|
||||
import kotlinx.coroutines.delay
|
||||
|
||||
class SyncWorkerNotificationManager(private val context: Context) {
|
||||
|
||||
|
@ -75,12 +76,23 @@ class SyncWorkerNotificationManager(private val context: Context) {
|
|||
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")
|
||||
notificationManager.notify(notificationId, notification)
|
||||
}
|
||||
|
||||
fun showErrorNotification() {
|
||||
private fun showErrorNotification() {
|
||||
val notification = getNotification("Download Failed", "Error downloading file")
|
||||
notificationManager.notify(notificationId, notification)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue