Adopt to async execution

Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
alperozturk 2024-07-09 12:02:46 +02:00 committed by Alper Öztürk
parent bc563681b4
commit 47cd4b2fd5
3 changed files with 64 additions and 8 deletions

View file

@ -16,6 +16,7 @@ import com.nextcloud.client.device.BatteryStatus
import com.nextcloud.client.device.PowerManagementService
import com.nextcloud.client.jobs.BackgroundJobManager
import com.nextcloud.client.jobs.upload.FileUploadWorker.Companion.currentUploadFileOperation
import com.nextcloud.client.network.ClientFactory
import com.nextcloud.client.network.Connectivity
import com.nextcloud.client.network.ConnectivityService
import com.owncloud.android.MainApp
@ -31,9 +32,12 @@ import com.owncloud.android.lib.common.operations.RemoteOperationResult
import com.owncloud.android.lib.common.utils.Log_OC
import com.owncloud.android.lib.resources.files.ReadFileRemoteOperation
import com.owncloud.android.lib.resources.files.model.RemoteFile
import com.owncloud.android.operations.RemoveFileOperation
import com.owncloud.android.operations.UploadFileOperation
import com.owncloud.android.ui.helpers.FileOperationsHelper
import com.owncloud.android.utils.FileUtil
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.io.File
import java.util.concurrent.Semaphore
import javax.inject.Inject
@ -319,7 +323,7 @@ class FileUploadHelper {
// For file conflicts check old file remote path
upload.remotePath == currentUploadFileOperation.remotePath ||
upload.remotePath == currentUploadFileOperation.oldFile!!
.remotePath
.remotePath
} else {
upload.remotePath == currentUploadFileOperation.remotePath
}
@ -354,12 +358,41 @@ class FileUploadHelper {
backgroundJobManager.startFilesUploadJob(user)
}
fun removeAnyOtherFileHaveSameName(newFile: OCFile, fileOperationsHelper: FileOperationsHelper) {
fun removeAnyOtherFileHaveSameName(
newFile: OCFile,
clientFactory: ClientFactory,
user: User,
onCompleted: () -> Unit
) {
val parentFile: OCFile? = fileStorageManager.getFileById(newFile.parentId)
val folderContent: List<OCFile> = fileStorageManager.getFolderContent(parentFile, false)
folderContent.firstOrNull { it.fileName == newFile.fileName }?.let { replacedFile ->
fileOperationsHelper.removeFiles(listOf(replacedFile), false, true)
val replacedFile: OCFile? = folderContent.find { it.fileName == newFile.fileName }
replacedFile?.let {
val job = CoroutineScope(Dispatchers.IO)
job.launch {
val client = clientFactory.create(user)
val removeFileOperation = RemoveFileOperation(
it,
false,
user,
true,
MainApp.getAppContext(),
fileStorageManager
)
val result = removeFileOperation.execute(client)
if (result.isSuccess) {
Log_OC.d(TAG, "Replaced file successfully removed")
launch(Dispatchers.Main) {
onCompleted()
}
}
}
}
}

View file

@ -881,6 +881,10 @@ public class UploadFileOperation extends SyncOperation {
result = unlockFolderResult;
}
if (unlockFolderResult != null && unlockFolderResult.isSuccess()) {
Log_OC.d(TAG, "Folder successfully unlocked: " + e2eFiles.getParentFile().getFileName());
}
e2eFiles.deleteEncryptedTempFile();
return result;

View file

@ -13,12 +13,15 @@ import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.lifecycle.Observer
import com.nextcloud.client.account.User
import com.nextcloud.client.jobs.download.FileDownloadHelper
import com.nextcloud.client.jobs.upload.FileUploadHelper
import com.nextcloud.client.jobs.upload.FileUploadWorker
import com.nextcloud.client.jobs.upload.UploadNotificationManager
import com.nextcloud.model.HTTPStatusCodes
import com.nextcloud.model.WorkerState
import com.nextcloud.model.WorkerStateLiveData
import com.nextcloud.utils.extensions.getParcelableArgument
import com.nextcloud.utils.extensions.logFileSize
import com.owncloud.android.R
@ -83,6 +86,24 @@ class ConflictsResolveActivity : FileActivity(), OnConflictDecisionMadeListener
}
}
private val workerObserver = Observer<WorkerState> { state ->
if (state is WorkerState.Upload) {
Log_OC.d(TAG, "Upload observation started")
newFile?.let {
val user = user.orElseThrow { RuntimeException() }
FileUploadHelper.instance().removeAnyOtherFileHaveSameName(file, clientFactory, user, onCompleted = {
removeWorkerObserver()
})
}
}
}
private fun removeWorkerObserver() {
Log_OC.d(TAG, "Upload observation stopped")
WorkerStateLiveData.instance().removeObserver(workerObserver)
}
private fun setupOnConflictDecisionMadeListener(upload: OCUpload?) {
listener = OnConflictDecisionMadeListener { decision: Decision? ->
val file = newFile // local file got changed, so either upload it or replace it again by server
@ -101,9 +122,7 @@ class ConflictsResolveActivity : FileActivity(), OnConflictDecisionMadeListener
NameCollisionPolicy.OVERWRITE
)
file?.let {
FileUploadHelper.instance().removeAnyOtherFileHaveSameName(file, fileOperationsHelper)
}
WorkerStateLiveData.instance().observe(this, workerObserver)
}
Decision.KEEP_BOTH -> {