Skip to content

Commit

Permalink
fix: moved behavior condition to UploadFileFromContentUriUseCase and …
Browse files Browse the repository at this point in the history
…some refactor
  • Loading branch information
Aitorbp committed Jul 22, 2024
1 parent bb34839 commit 84ec62b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.WorkManager
import androidx.work.workDataOf
import com.owncloud.android.domain.BaseUseCase
import com.owncloud.android.domain.camerauploads.model.UploadBehavior
import com.owncloud.android.workers.RemoveLocalFileWorker
import com.owncloud.android.workers.UploadFileFromContentUriWorker
import timber.log.Timber
Expand All @@ -47,7 +48,6 @@ class UploadFileFromContentUriUseCase(
UploadFileFromContentUriWorker.KEY_PARAM_UPLOAD_ID to params.uploadIdInStorageManager
)
val inputDataRemoveLocalFileWorker = workDataOf(
UploadFileFromContentUriWorker.KEY_PARAM_BEHAVIOR to params.behavior,
UploadFileFromContentUriWorker.KEY_PARAM_CONTENT_URI to params.contentUri.toString(),
)

Expand All @@ -64,13 +64,15 @@ class UploadFileFromContentUriUseCase(
.addTag(params.uploadIdInStorageManager.toString())
.build()

val removeLocalFileWorker = OneTimeWorkRequestBuilder<RemoveLocalFileWorker>()
.setInputData(inputDataRemoveLocalFileWorker)
.build()

workManager.beginWith(uploadFileFromContentUriWorker)
.then(removeLocalFileWorker) // File is already uploaded, so the original one can be removed if the behaviour is MOVE
.enqueue()
var workContinuation = workManager.beginWith(uploadFileFromContentUriWorker)
val behavior = UploadBehavior.fromString(params.behavior)
if (behavior == UploadBehavior.MOVE) {
val removeLocalFileWorker = OneTimeWorkRequestBuilder<RemoveLocalFileWorker>()
.setInputData(inputDataRemoveLocalFileWorker)
.build()
workContinuation = workContinuation.then(removeLocalFileWorker) // File is already uploaded, so the original one can be removed if the behaviour is MOVE
}
workContinuation.enqueue()

Timber.i("Plain upload of ${params.contentUri.path} has been enqueued.")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ import androidx.core.net.toUri
import androidx.documentfile.provider.DocumentFile
import androidx.work.CoroutineWorker
import androidx.work.WorkerParameters
import com.owncloud.android.domain.camerauploads.model.UploadBehavior
import com.owncloud.android.workers.UploadFileFromContentUriWorker.Companion.KEY_PARAM_BEHAVIOR
import com.owncloud.android.workers.UploadFileFromContentUriWorker.Companion.KEY_PARAM_CONTENT_URI
import org.koin.core.component.KoinComponent
import timber.log.Timber
Expand All @@ -40,15 +38,13 @@ class RemoveLocalFileWorker(
workerParameters
), KoinComponent {

private lateinit var behavior: UploadBehavior
private lateinit var contentUri: Uri

override suspend fun doWork(): Result {
if (!areParametersValid()) return Result.failure()
return try {
if (behavior == UploadBehavior.MOVE) {
removeLocalFile()
}
val documentFile = DocumentFile.fromSingleUri(appContext, contentUri)
documentFile?.delete()
Result.success()
} catch (throwable: Throwable) {
Timber.e(throwable)
Expand All @@ -57,17 +53,10 @@ class RemoveLocalFileWorker(
}

private fun areParametersValid(): Boolean {
val paramBehavior = workerParameters.inputData.getString(KEY_PARAM_BEHAVIOR)
val paramContentUri = workerParameters.inputData.getString(KEY_PARAM_CONTENT_URI)

contentUri = paramContentUri?.toUri() ?: return false
behavior = paramBehavior?.let { UploadBehavior.fromString(it) } ?: return false

return true
}

private fun removeLocalFile() {
val documentFile = DocumentFile.fromSingleUri(appContext, contentUri)
documentFile?.delete()
}
}

0 comments on commit 84ec62b

Please sign in to comment.