From 8c0d38ca581bd773e823011a6f7bf1caf3d4d830 Mon Sep 17 00:00:00 2001 From: Kevin Boulongne Date: Fri, 6 Sep 2024 10:53:52 +0200 Subject: [PATCH] Add AccountManager --- .../SwissTransferInjection.kt | 21 ++++---- .../managers/AccountManager.kt | 54 +++++++++++++++++++ .../cache/setting/AppSettingsController.kt | 5 ++ .../cache/setting/TransfersController.kt | 42 +++++++++++++++ .../cache/setting/UploadController.kt | 42 +++++++++++++++ .../database/models/upload/Upload.kt | 14 ++++- .../models/upload/UploadContainerDB.kt | 42 +++++++++++++++ 7 files changed, 207 insertions(+), 13 deletions(-) create mode 100644 STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/managers/AccountManager.kt create mode 100644 STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/cache/setting/TransfersController.kt create mode 100644 STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/cache/setting/UploadController.kt create mode 100644 STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/models/upload/UploadContainerDB.kt diff --git a/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/SwissTransferInjection.kt b/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/SwissTransferInjection.kt index 08867df6..83c52c77 100644 --- a/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/SwissTransferInjection.kt +++ b/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/SwissTransferInjection.kt @@ -19,12 +19,14 @@ package com.infomaniak.multiplatform_swisstransfer import com.infomaniak.multiplatform_swisstransfer.database.RealmProvider import com.infomaniak.multiplatform_swisstransfer.database.cache.setting.AppSettingsController +import com.infomaniak.multiplatform_swisstransfer.database.cache.setting.TransfersController +import com.infomaniak.multiplatform_swisstransfer.database.cache.setting.UploadController +import com.infomaniak.multiplatform_swisstransfer.managers.AccountManager import com.infomaniak.multiplatform_swisstransfer.managers.AppSettingsManager import com.infomaniak.multiplatform_swisstransfer.managers.TransferManager import com.infomaniak.multiplatform_swisstransfer.network.ApiClientProvider import com.infomaniak.multiplatform_swisstransfer.network.repositories.TransferRepository import com.infomaniak.multiplatform_swisstransfer.network.repositories.UploadRepository -import com.infomaniak.multiplatform_swisstransfer.utils.Constants /** * SwissTransferInjection is a class responsible for initializing all the classes needed @@ -38,26 +40,23 @@ import com.infomaniak.multiplatform_swisstransfer.utils.Constants * @property transferManager A manager used to orchestrate transfer operations. */ class SwissTransferInjection { + private val realmProvider by lazy { RealmProvider() } private val apiClientProvider by lazy { ApiClientProvider() } - private val transferRepository by lazy { TransferRepository(apiClientProvider) } private val uploadRepository by lazy { UploadRepository(apiClientProvider) } + private val transferRepository by lazy { TransferRepository(apiClientProvider) } private val appSettingsController by lazy { AppSettingsController(realmProvider) } - - /** - * Loads the default user account and initializes Realm transfers for the default user ID defined in the constants. - */ - @Throws(IllegalArgumentException::class, IllegalStateException::class) - suspend fun loadDefaultAccount() { - appSettingsController.initAppSettings() - realmProvider.loadRealmTransfers(Constants.DEFAULT_USER_ID) - } + private val uploadController by lazy { UploadController(realmProvider) } + private val transfersController by lazy { TransfersController(realmProvider) } /** A manager used to orchestrate transfer operations. */ val transferManager by lazy { TransferManager(realmProvider, apiClientProvider) } /** A manager used to orchestrate AppSettings operations. */ val appSettingsManager by lazy { AppSettingsManager(appSettingsController) } + + /** A manager used to orchestrate Accounts operations. */ + val accountManager by lazy { AccountManager(appSettingsController, uploadController, transfersController, realmProvider) } } diff --git a/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/managers/AccountManager.kt b/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/managers/AccountManager.kt new file mode 100644 index 00000000..15767d76 --- /dev/null +++ b/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/managers/AccountManager.kt @@ -0,0 +1,54 @@ +/* + * 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.managers + +import com.infomaniak.multiplatform_swisstransfer.database.RealmProvider +import com.infomaniak.multiplatform_swisstransfer.database.cache.setting.AppSettingsController +import com.infomaniak.multiplatform_swisstransfer.database.cache.setting.TransfersController +import com.infomaniak.multiplatform_swisstransfer.database.cache.setting.UploadController +import com.infomaniak.multiplatform_swisstransfer.utils.Constants + +class AccountManager internal constructor( + private val appSettingsController: AppSettingsController, + private val uploadController: UploadController, + private val transfersController: TransfersController, + private val realmProvider: RealmProvider, +) { + + /** + * Loads the default User account and initializes Realm Transfers for the default UserId defined in Constants. + */ + @Throws(IllegalArgumentException::class, IllegalStateException::class) + suspend fun loadUser(userId: Int) { + appSettingsController.initAppSettings() + realmProvider.loadRealmTransfers(Constants.DEFAULT_USER_ID) + } + + /** + * Delete specified User data + */ + @Throws(IllegalArgumentException::class, IllegalStateException::class) + suspend fun removeUser(userId: Int) { + + // TODO: If we are removing the last User, we also need to delete AppSettings data. + // if (users.count() == 1) appSettingsController.removeData() + + uploadController.removeData() + transfersController.removeData() + } +} diff --git a/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/cache/setting/AppSettingsController.kt b/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/cache/setting/AppSettingsController.kt index def7ee5d..822f5775 100644 --- a/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/cache/setting/AppSettingsController.kt +++ b/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/cache/setting/AppSettingsController.kt @@ -90,5 +90,10 @@ class AppSettingsController(private val realmProvider: RealmProvider) { mutableAppSettings.emailLanguage = emailLanguage } } + + @Throws(IllegalArgumentException::class, CancellationException::class) + suspend fun removeData() { + realm.write { deleteAll() } + } //endregion } diff --git a/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/cache/setting/TransfersController.kt b/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/cache/setting/TransfersController.kt new file mode 100644 index 00000000..0a6a7b74 --- /dev/null +++ b/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/cache/setting/TransfersController.kt @@ -0,0 +1,42 @@ +/* + * 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.database.cache.setting + +import com.infomaniak.multiplatform_swisstransfer.database.RealmProvider +import com.infomaniak.multiplatform_swisstransfer.database.models.transfers.TransferDB +import io.realm.kotlin.ext.query +import io.realm.kotlin.query.RealmResults +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlin.coroutines.cancellation.CancellationException + +@OptIn(ExperimentalCoroutinesApi::class) +class TransfersController(private val realmProvider: RealmProvider) { + + private val realm by lazy { realmProvider.realmTransfers } + + //region Get data + fun getTransfers(): RealmResults? = realm?.query()?.find() + //endregion + + //region Update data + @Throws(IllegalArgumentException::class, CancellationException::class) + suspend fun removeData() { + realm?.write { deleteAll() } + } + //endregion +} diff --git a/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/cache/setting/UploadController.kt b/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/cache/setting/UploadController.kt new file mode 100644 index 00000000..c818d49c --- /dev/null +++ b/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/cache/setting/UploadController.kt @@ -0,0 +1,42 @@ +/* + * 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.database.cache.setting + +import com.infomaniak.multiplatform_swisstransfer.database.RealmProvider +import com.infomaniak.multiplatform_swisstransfer.database.models.upload.Upload +import io.realm.kotlin.ext.query +import io.realm.kotlin.query.RealmResults +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlin.coroutines.cancellation.CancellationException + +@OptIn(ExperimentalCoroutinesApi::class) +class UploadController(private val realmProvider: RealmProvider) { + + private val realm by lazy { realmProvider.realmUploads } + + //region Get data + fun getUploads(): RealmResults = realm.query().find() + //endregion + + //region Update data + @Throws(IllegalArgumentException::class, CancellationException::class) + suspend fun removeData() { + realm.write { deleteAll() } + } + //endregion +} diff --git a/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/models/upload/Upload.kt b/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/models/upload/Upload.kt index cecef33b..4d2ff5ee 100644 --- a/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/models/upload/Upload.kt +++ b/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/models/upload/Upload.kt @@ -17,12 +17,22 @@ */ package com.infomaniak.multiplatform_swisstransfer.database.models.upload +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.PrimaryKey /** * Class representing files to be uploaded */ class Upload : RealmObject { - var userId: Long = 0L - //TODO: implement + + @PrimaryKey + var uuid: RealmUUID = RealmUUID.random() + + var container: UploadContainerDB? = null + var uploadHost: String = "" + var filesUuid: RealmList = realmListOf() + var completedFilesUuid: RealmList = realmListOf() } diff --git a/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/models/upload/UploadContainerDB.kt b/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/models/upload/UploadContainerDB.kt new file mode 100644 index 00000000..917930ba --- /dev/null +++ b/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/models/upload/UploadContainerDB.kt @@ -0,0 +1,42 @@ +/* + * 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.database.models.upload + +import com.infomaniak.multiplatform_swisstransfer.common.interfaces.upload.UploadContainer +import io.realm.kotlin.types.EmbeddedRealmObject + +class UploadContainerDB : UploadContainer, EmbeddedRealmObject { + + override var uuid: String = "" + + override var duration: String = "" + override var downloadLimit: Long = 0 + override var lang: String = "" + override var source: String = "" + + override var wsUser: String? = null + + override var authorIP: String = "" + override var swiftVersion: String = "" + + // var createdDate: String // TODO: Why a complex date instead of a simple date ? May be Custom serial this + override var expiredDateTimestamp: Long = 0 + override var needPassword: Boolean = false + override var message: String = "" + override var numberOfFiles: Long = 0 +}