From 818585d5b641055598d89cc610429e16f46c432e Mon Sep 17 00:00:00 2001 From: Vincent TE Date: Mon, 18 Nov 2024 13:30:31 +0100 Subject: [PATCH] chore: Code review --- .../TransferWithNoFilesException.kt | 23 +++++++++ .../common/interfaces/transfers/File.kt | 49 ------------------- .../controllers/TransferController.kt | 12 +++-- .../database/models/transfers/FileDB.kt | 4 +- .../database/utils/FileUtils.kt | 47 ++++++++++++++++++ .../database/FileUtilsTest.kt | 3 +- 6 files changed, 81 insertions(+), 57 deletions(-) create mode 100644 STCommon/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/common/exceptions/TransferWithNoFilesException.kt diff --git a/STCommon/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/common/exceptions/TransferWithNoFilesException.kt b/STCommon/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/common/exceptions/TransferWithNoFilesException.kt new file mode 100644 index 00000000..c8203f9d --- /dev/null +++ b/STCommon/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/common/exceptions/TransferWithNoFilesException.kt @@ -0,0 +1,23 @@ +/* + * Infomaniak SwissTransfer - Multiplatform + * Copyright (C) 2024 Infomaniak Network SA + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.infomaniak.multiplatform_swisstransfer.common.exceptions + +/** + * Thrown when a Transfer has no files, which should not happen. + */ +class TransferWithNoFilesException : Exception() diff --git a/STCommon/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/common/interfaces/transfers/File.kt b/STCommon/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/common/interfaces/transfers/File.kt index 55df6ec6..1288d084 100644 --- a/STCommon/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/common/interfaces/transfers/File.kt +++ b/STCommon/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/common/interfaces/transfers/File.kt @@ -33,53 +33,4 @@ interface File { val path: String? val thumbnailPath: String? val children: List - - /** - * Recursively searches for a child with the given name. - * - * @param fileName The name of the child to search for. - * @return The [File] object representing the found child, or null if not found. - */ - fun findChildByName(fileName: String): File? { - if (this.fileName == fileName) return this - - children.forEach { child -> - child.findChildByName(fileName)?.let { - return it - } - } - - return null - } - - /** - * Recursively searches for a child with the given UUID. - * - * @param uuid The UUID of the child to search for. - * @return The [File] object representing the found child, or null if not found. - */ - fun findChildByUuid(uuid: String): File? { - if (this.uuid == uuid) return this - - children.forEach { child -> - child.findChildByUuid(uuid)?.let { - return it - } - } - - return null - } - - companion object { - - fun List.findFirstChildByUuid(uuid: String): File? { - forEach { file -> - val foundChild = file.findChildByUuid(uuid) - if (foundChild != null) { - return foundChild - } - } - return null - } - } } diff --git a/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/controllers/TransferController.kt b/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/controllers/TransferController.kt index 5bcc8d4e..43a2f0c1 100644 --- a/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/controllers/TransferController.kt +++ b/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/controllers/TransferController.kt @@ -18,7 +18,7 @@ package com.infomaniak.multiplatform_swisstransfer.database.controllers import com.infomaniak.multiplatform_swisstransfer.common.exceptions.RealmException -import com.infomaniak.multiplatform_swisstransfer.common.interfaces.transfers.File.Companion.findFirstChildByUuid +import com.infomaniak.multiplatform_swisstransfer.common.exceptions.TransferWithNoFilesException import com.infomaniak.multiplatform_swisstransfer.common.interfaces.transfers.Transfer import com.infomaniak.multiplatform_swisstransfer.common.interfaces.ui.FileUi import com.infomaniak.multiplatform_swisstransfer.common.interfaces.upload.UploadSession @@ -27,6 +27,7 @@ import com.infomaniak.multiplatform_swisstransfer.common.models.TransferStatus import com.infomaniak.multiplatform_swisstransfer.database.RealmProvider import com.infomaniak.multiplatform_swisstransfer.database.models.transfers.TransferDB import com.infomaniak.multiplatform_swisstransfer.database.utils.FileUtils +import com.infomaniak.multiplatform_swisstransfer.database.utils.FileUtils.findFirstChildByUuid import com.infomaniak.multiplatform_swisstransfer.database.utils.RealmUtils.runThrowingRealm import io.realm.kotlin.Realm import io.realm.kotlin.UpdatePolicy @@ -38,7 +39,6 @@ import io.realm.kotlin.query.Sort import io.realm.kotlin.query.TRUE_PREDICATE import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.emptyFlow import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.mapLatest import kotlin.coroutines.cancellation.CancellationException @@ -90,12 +90,14 @@ class TransferController(private val realmProvider: RealmProvider) { //endregion //region Upsert data - @Throws(RealmException::class, CancellationException::class) + @Throws(RealmException::class, CancellationException::class, TransferWithNoFilesException::class) suspend fun upsert(transfer: Transfer, transferDirection: TransferDirection) = runThrowingRealm { realm.write { val transferDB = TransferDB(transfer, transferDirection) - transferDB.container?.files?.let { transferDB.container?.files = FileUtils.getFileDBTree(it).toRealmList() } - this.copyToRealm(transferDB, UpdatePolicy.ALL) + transferDB.container?.files?.let { transferFiles -> + transferDB.container?.files = FileUtils.getFileDBTree(transferFiles).toRealmList() + this.copyToRealm(transferDB, UpdatePolicy.ALL) + } ?: throw TransferWithNoFilesException() } } diff --git a/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/models/transfers/FileDB.kt b/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/models/transfers/FileDB.kt index f1502ba1..4bafc2cc 100644 --- a/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/models/transfers/FileDB.kt +++ b/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/models/transfers/FileDB.kt @@ -23,11 +23,11 @@ import com.infomaniak.multiplatform_swisstransfer.common.interfaces.upload.Uploa import io.realm.kotlin.ext.realmListOf import io.realm.kotlin.types.RealmList import io.realm.kotlin.types.RealmObject +import io.realm.kotlin.types.RealmUUID import io.realm.kotlin.types.annotations.Ignore import io.realm.kotlin.types.annotations.PrimaryKey import kotlinx.datetime.Clock import kotlin.uuid.ExperimentalUuidApi -import kotlin.uuid.Uuid @OptIn(ExperimentalUuidApi::class) class FileDB() : File, RealmObject { @@ -85,7 +85,7 @@ class FileDB() : File, RealmObject { // Init for folder constructor(folderName: String) : this() { - uuid = Uuid.random().toString() + uuid = RealmUUID.random().toString() fileName = folderName isFolder = true mimeType = null diff --git a/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/utils/FileUtils.kt b/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/utils/FileUtils.kt index 2b7af137..324ff687 100644 --- a/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/utils/FileUtils.kt +++ b/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/utils/FileUtils.kt @@ -68,4 +68,51 @@ object FileUtils { } return Pair(modifiedTree, currentParent) } + + + /** + * Recursively searches for a child with the given name. + * + * @param fileName The name of the child to search for. + * @return The [File] object representing the found child, or null if not found. + */ + fun FileDB.findChildByName(fileName: String): FileDB? { + if (this.fileName == fileName) return this + + children.forEach { child -> + child.findChildByName(fileName)?.let { + return it + } + } + + return null + } + + /** + * Recursively searches for a child with the given UUID. + * + * @param uuid The UUID of the child to search for. + * @return The [File] object representing the found child, or null if not found. + */ + private fun File.findChildByUuid(uuid: String): File? { + if (this.uuid == uuid) return this + + children.forEach { child -> + child.findChildByUuid(uuid)?.let { + return it + } + } + + return null + } + + fun List.findFirstChildByUuid(uuid: String): File? { + forEach { file -> + val foundChild = file.findChildByUuid(uuid) + if (foundChild != null) { + return foundChild + } + } + return null + } } diff --git a/STDatabase/src/commonTest/kotlin/com/infomaniak/multiplatform_swisstransfer/database/FileUtilsTest.kt b/STDatabase/src/commonTest/kotlin/com/infomaniak/multiplatform_swisstransfer/database/FileUtilsTest.kt index ca5f2768..54e9c2ab 100644 --- a/STDatabase/src/commonTest/kotlin/com/infomaniak/multiplatform_swisstransfer/database/FileUtilsTest.kt +++ b/STDatabase/src/commonTest/kotlin/com/infomaniak/multiplatform_swisstransfer/database/FileUtilsTest.kt @@ -20,6 +20,7 @@ package com.infomaniak.multiplatform_swisstransfer.database import com.infomaniak.multiplatform_swisstransfer.common.interfaces.transfers.File import com.infomaniak.multiplatform_swisstransfer.database.models.transfers.FileDB import com.infomaniak.multiplatform_swisstransfer.database.utils.FileUtils +import com.infomaniak.multiplatform_swisstransfer.database.utils.FileUtils.findChildByName import kotlin.test.AfterTest import kotlin.test.Test import kotlin.test.assertTrue @@ -151,7 +152,7 @@ class FileUtilsTest { private fun List.getFileDB(name: String) = find { it.fileName == name } - private fun findFirstChildByNameInList(tree: List, fileName: String): File? { + private fun findFirstChildByNameInList(tree: List, fileName: String): FileDB? { tree.forEach { file -> val foundChild = file.findChildByName(fileName) if (foundChild != null) {