Skip to content

Commit

Permalink
chore: Code review
Browse files Browse the repository at this point in the history
  • Loading branch information
tevincent committed Nov 18, 2024
1 parent 26d3e0a commit a6e58f6
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 57 deletions.
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/
package com.infomaniak.multiplatform_swisstransfer.common.exceptions

/**
* Thrown when a Transfer has no files, which should not happen.
*/
class TransferWithNoFilesException : Exception()
Original file line number Diff line number Diff line change
Expand Up @@ -33,53 +33,4 @@ interface File {
val path: String?
val thumbnailPath: String?
val children: List<File>

/**
* 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<File>.findFirstChildByUuid(uuid: String): File? {
forEach { file ->
val foundChild = file.findChildByUuid(uuid)
if (foundChild != null) {
return foundChild
}
}
return null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<File>.findFirstChildByUuid(uuid: String): File? {
forEach { file ->
val foundChild = file.findChildByUuid(uuid)
if (foundChild != null) {
return foundChild
}
}
return null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -151,7 +152,7 @@ class FileUtilsTest {

private fun List<FileDB>.getFileDB(name: String) = find { it.fileName == name }

private fun findFirstChildByNameInList(tree: List<FileDB>, fileName: String): File? {
private fun findFirstChildByNameInList(tree: List<FileDB>, fileName: String): FileDB? {
tree.forEach { file ->
val foundChild = file.findChildByName(fileName)
if (foundChild != null) {
Expand Down

0 comments on commit a6e58f6

Please sign in to comment.