Use repository instead extensions

Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
alperozturk 2024-08-28 15:09:55 +02:00
parent 9913519f72
commit ed7a1bfc15
No known key found for this signature in database
GPG key ID: 4E577DC593B59BDF
5 changed files with 112 additions and 93 deletions

View file

@ -12,12 +12,12 @@ import androidx.work.CoroutineWorker
import androidx.work.WorkerParameters
import com.nextcloud.client.account.User
import com.nextcloud.client.database.entity.OfflineOperationEntity
import com.nextcloud.client.jobs.offlineOperations.repository.OfflineOperationsRepository
import com.nextcloud.client.network.ClientFactoryImpl
import com.nextcloud.client.network.ConnectivityService
import com.nextcloud.model.OfflineOperationType
import com.nextcloud.model.WorkerState
import com.nextcloud.model.WorkerStateLiveData
import com.nextcloud.utils.extensions.updateNextOperations
import com.owncloud.android.datamodel.FileDataStorageManager
import com.owncloud.android.lib.common.operations.RemoteOperation
import com.owncloud.android.lib.common.operations.RemoteOperationResult
@ -44,6 +44,7 @@ class OfflineOperationsWorker(
private val fileDataStorageManager = FileDataStorageManager(user, context.contentResolver)
private val clientFactory = ClientFactoryImpl(context)
private val notificationManager = OfflineOperationsNotificationManager(context, viewThemeUtils)
private var repository = OfflineOperationsRepository(fileDataStorageManager)
@Suppress("TooGenericExceptionCaught", "Deprecation")
override suspend fun doWork(): Result = coroutineScope {
@ -114,11 +115,8 @@ class OfflineOperationsWorker(
Log_OC.d(TAG, "$logMessage path: ${operation.path}, type: ${operation.type}")
if (result.isSuccess) {
fileDataStorageManager.offlineOperationDao.run {
updateNextOperations(fileDataStorageManager)
delete(operation)
}
repository.updateNextOperations()
fileDataStorageManager.offlineOperationDao.delete(operation)
notificationManager.update(operations.size, currentOperationIndex, operation.filename ?: "")
} else {
val excludedErrorCodes = listOf(RemoteOperationResult.ResultCode.FOLDER_ALREADY_EXISTS)

View file

@ -0,0 +1,85 @@
/*
* Nextcloud - Android Client
*
* SPDX-FileCopyrightText: 2024 Alper Ozturk <alper.ozturk@nextcloud.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
package com.nextcloud.client.jobs.offlineOperations.repository
import com.nextcloud.client.database.entity.OfflineOperationEntity
import com.owncloud.android.datamodel.FileDataStorageManager
import com.owncloud.android.datamodel.OCFile
class OfflineOperationsRepository(
private val fileDataStorageManager: FileDataStorageManager
) : OfflineOperationsRepositoryType {
private val dao = fileDataStorageManager.offlineOperationDao
private val pathSeparator = '/'
override fun getAllSubdirectories(fileId: Long): List<OfflineOperationEntity> {
val result = mutableListOf<OfflineOperationEntity>()
val queue = ArrayDeque<Long>()
queue.add(fileId)
val processedIds = mutableSetOf<Long>()
while (queue.isNotEmpty()) {
val currentFileId = queue.removeFirst()
if (currentFileId in processedIds || currentFileId == 1L) continue
processedIds.add(currentFileId)
val subDirectories = dao.getSubDirectoriesByParentOCFileId(currentFileId)
result.addAll(subDirectories)
subDirectories.forEach {
val ocFile = fileDataStorageManager.getFileByDecryptedRemotePath(it.path)
ocFile?.fileId?.let { newFileId ->
if (newFileId != 1L && newFileId !in processedIds) {
queue.add(newFileId)
}
}
}
}
return result
}
override fun deleteOperation(file: OCFile) {
getAllSubdirectories(file.fileId).forEach {
dao.delete(it)
}
file.decryptedRemotePath?.let {
val entity = dao.getByPath(it)
entity?.let {
dao.delete(entity)
}
}
fileDataStorageManager.removeFile(file, true, true)
}
override fun updateNextOperations() {
dao.getAll()
.mapNotNull { nextOperation ->
nextOperation.parentOCFileId?.let { parentId ->
fileDataStorageManager.getFileById(parentId)?.let { ocFile ->
ocFile.decryptedRemotePath?.let { updatedPath ->
val newParentPath = ocFile.parentRemotePath
val newPath = updatedPath + nextOperation.filename + pathSeparator
if (newParentPath != nextOperation.parentPath || newPath != nextOperation.path) {
nextOperation.apply {
parentPath = newParentPath
path = newPath
}
} else null
}
}
}
}
.forEach { dao.update(it) }
}
}

View file

@ -0,0 +1,17 @@
/*
* Nextcloud - Android Client
*
* SPDX-FileCopyrightText: 2024 Alper Ozturk <alper.ozturk@nextcloud.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
package com.nextcloud.client.jobs.offlineOperations.repository
import com.nextcloud.client.database.entity.OfflineOperationEntity
import com.owncloud.android.datamodel.OCFile
interface OfflineOperationsRepositoryType {
fun getAllSubdirectories(fileId: Long): List<OfflineOperationEntity>
fun deleteOperation(file: OCFile)
fun updateNextOperations()
}

View file

@ -1,85 +0,0 @@
/*
* Nextcloud - Android Client
*
* SPDX-FileCopyrightText: 2024 Alper Ozturk <alper.ozturk@nextcloud.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
package com.nextcloud.utils.extensions
import com.nextcloud.client.database.dao.OfflineOperationDao
import com.nextcloud.client.database.entity.OfflineOperationEntity
import com.owncloud.android.datamodel.FileDataStorageManager
import com.owncloud.android.datamodel.OCFile
private const val DELIMITER = '/'
fun OfflineOperationDao.getAllSubdirectories(
fileId: Long,
fileDataStorageManager: FileDataStorageManager
): List<OfflineOperationEntity> {
val result = mutableListOf<OfflineOperationEntity>()
val queue = ArrayDeque<Long>()
queue.add(fileId)
val processedIds = mutableSetOf<Long>()
while (queue.isNotEmpty()) {
val currentFileId = queue.removeFirst()
if (currentFileId in processedIds || currentFileId == 1L) continue
processedIds.add(currentFileId)
val subDirectories = getSubDirectoriesByParentOCFileId(currentFileId)
result.addAll(subDirectories)
subDirectories.forEach {
val ocFile = fileDataStorageManager.getFileByDecryptedRemotePath(it.path)
ocFile?.fileId?.let { newFileId ->
if (newFileId != 1L && newFileId !in processedIds) {
queue.add(newFileId)
}
}
}
}
return result
}
fun OfflineOperationDao.deleteOperation(file: OCFile, fileDataStorageManager: FileDataStorageManager) {
getAllSubdirectories(file.fileId, fileDataStorageManager).forEach {
delete(it)
}
file.decryptedRemotePath?.let {
val entity = getByPath(it)
entity?.let {
delete(entity)
}
}
fileDataStorageManager.removeFile(file, true, true)
}
fun OfflineOperationDao.updateNextOperations(
fileDataStorageManager: FileDataStorageManager
) {
getAll()
.mapNotNull { nextOperation ->
nextOperation.parentOCFileId?.let { parentId ->
fileDataStorageManager.getFileById(parentId)?.let { ocFile ->
ocFile.decryptedRemotePath?.let { updatedPath ->
val newParentPath = ocFile.parentRemotePath
val newPath = updatedPath + nextOperation.filename + DELIMITER
if (newParentPath != nextOperation.parentPath || newPath != nextOperation.path) {
nextOperation.apply {
parentPath = newParentPath
path = newPath
}
} else null
}
}
}
}
.forEach { update(it) }
}

View file

@ -38,10 +38,11 @@ import com.nextcloud.client.database.dao.FileDao;
import com.nextcloud.client.database.dao.OfflineOperationDao;
import com.nextcloud.client.database.entity.FileEntity;
import com.nextcloud.client.database.entity.OfflineOperationEntity;
import com.nextcloud.client.jobs.offlineOperations.repository.OfflineOperationsRepository;
import com.nextcloud.client.jobs.offlineOperations.repository.OfflineOperationsRepositoryType;
import com.nextcloud.model.OfflineOperationType;
import com.nextcloud.utils.date.DateFormatPattern;
import com.nextcloud.utils.extensions.DateExtensionsKt;
import com.nextcloud.utils.extensions.OfflineOperationExtensionsKt;
import com.owncloud.android.MainApp;
import com.owncloud.android.db.ProviderMeta.ProviderTableMeta;
import com.owncloud.android.lib.common.network.WebdavEntry;
@ -105,17 +106,20 @@ public class FileDataStorageManager {
public final OfflineOperationDao offlineOperationDao = NextcloudDatabase.getInstance(MainApp.getAppContext()).offlineOperationDao();
private final FileDao fileDao = NextcloudDatabase.getInstance(MainApp.getAppContext()).fileDao();
private final Gson gson = new Gson();
private final OfflineOperationsRepositoryType offlineOperationsRepository;
public FileDataStorageManager(User user, ContentResolver contentResolver) {
this.contentProviderClient = null;
this.contentResolver = contentResolver;
this.user = user;
offlineOperationsRepository = new OfflineOperationsRepository(this);
}
public FileDataStorageManager(User user, ContentProviderClient contentProviderClient) {
this.contentProviderClient = contentProviderClient;
this.contentResolver = null;
this.user = user;
offlineOperationsRepository = new OfflineOperationsRepository(this);
}
/**
@ -158,7 +162,7 @@ public class FileDataStorageManager {
}
public void deleteOfflineOperation(OCFile file) {
OfflineOperationExtensionsKt.deleteOperation(offlineOperationDao, file, this);
offlineOperationsRepository.deleteOperation(file);
}
public void renameCreateFolderOfflineOperation(OCFile file, String newFolderName) {