Skip to content

Commit

Permalink
Add AccountManager
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinBoulongne committed Sep 6, 2024
1 parent 09a50b4 commit 27fcb30
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) }
}
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/
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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/
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<TransferDB>? = realm?.query<TransferDB>()?.find()
//endregion

//region Update data
@Throws(IllegalArgumentException::class, CancellationException::class)
suspend fun removeData() {
realm?.write { deleteAll() }
}
//endregion
}
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/
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<Upload> = realm.query<Upload>().find()
//endregion

//region Update data
@Throws(IllegalArgumentException::class, CancellationException::class)
suspend fun removeData() {
realm.write { deleteAll() }
}
//endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 = 0
//TODO: implement

@PrimaryKey
var uuid: RealmUUID = RealmUUID.random()

var container: UploadContainerDB? = null
var uploadHost: String = ""
var filesUuid: RealmList<String> = realmListOf()
var completedFilesUuid: RealmList<String> = realmListOf()
}
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/
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
}

0 comments on commit 27fcb30

Please sign in to comment.