Skip to content

Commit

Permalink
Merge pull request #26 from Infomaniak/account-manager
Browse files Browse the repository at this point in the history
Add AccountManager
  • Loading branch information
KevinBoulongne authored Oct 9, 2024
2 parents e7ce1ec + eb99d90 commit 484303d
Show file tree
Hide file tree
Showing 11 changed files with 254 additions and 39 deletions.
30 changes: 15 additions & 15 deletions STCore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ centralized access point to orchestrate transfer operations.

### Table of Public Properties and Methods

| Type | Name | Description |
|----------|--------------------|-------------------------------------------------------------------------------|
| Property | appSettingsManager | A manager used to orchestrate AppSettings operations. |
| Property | transferManager | A manager used to orchestrate transfer operations. |
| Method | loadDefaultAccount | Loads the default user account and initializes Realm transfers for this user. |
| Type | Name | Description |
|----------|--------------------|-------------------------------------------------------|
| Property | appSettingsManager | A manager used to orchestrate AppSettings operations. |
| Property | transferManager | A manager used to orchestrate Transfers operations. |
| Property | accountManager | A manager used to orchestrate Accounts operations. |

### Details of Properties and Methods

Expand All @@ -52,36 +52,36 @@ centralized access point to orchestrate transfer operations.
```kotlin
val core = SwissTransferInjection()
val appSettingsManager = core.appSettingsManager
// Use the appSettingsManager to orchestrate AppSettings related operations
// Use the appSettingsManager to orchestrate AppSettings
```

#### Property: `transferManager`

- **Type**: `TransferManager`
- **Description**:
- `transferManager` is a lazily initialized property that provides a manager to orchestrate all transfer operations. It
uses `realmProvider` and `apiClientProvider` to configure and manage transfers efficiently.
uses `realmProvider` and `apiClientProvider` to configure and manage Transfers efficiently.

- **Usage Example**:
```kotlin
val core = SwissTransferInjection()
val transferManager = core.transferManager
// Use the transferManager to orchestrate transfers
// Use the transferManager to orchestrate Transfers
```

#### Method: `loadDefaultAccount`
#### Property: `accountManager`

- **Signature**: `fun loadDefaultAccount()`
- **Type**: `AccountManager`
- **Description**:
- `loadDefaultAccount` is a method that loads the default user account and initializes Realm transfers for the default user ID
defined in the constants. This method is essential to ensure the application is correctly set up for the default user from
the start.
- `accountManager` is a lazily initialized property that provides a manager to orchestrate all Accounts operations. It uses
`appSettingsController`, `uploadController`, `transfersController` and `realmProvider` to configure and manage Accounts
efficiently.

- **Usage Example**:
```kotlin
val core = SwissTransferInjection()
core.loadDefaultAccount()
// The default user account is now loaded and ready to use
val accountManager = core.accountManager
// Use the accountManager to orchestrate Accounts
```

## Contributing
Expand Down
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 @@ -46,19 +48,15 @@ class SwissTransferInjection {
private val transferRepository by lazy { TransferRepository(apiClientProvider) }

private val appSettingsController by lazy { AppSettingsController(realmProvider) }
private val uploadController by lazy { UploadController(realmProvider) }
private val transfersController by lazy { TransfersController(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)
}

/** A manager used to orchestrate transfer operations. */
/** A manager used to orchestrate Transfers 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,61 @@
/*
* 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

/**
* AccountManager is responsible for orchestrating Accounts operations using Realm for local data management.
*
* @property appSettingsController The controller for managing AppSettings operations.
* @property uploadController The controller for managing Upload operations.
* @property transfersController The controller for managing Transfers operation.
* @property realmProvider The provider for managing Realm database operations.
*/
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.openRealmTransfers(userId)
}

/**
* Delete specified User data
*/
@Throws(IllegalArgumentException::class, IllegalStateException::class)
suspend fun removeUser(userId: Int) {

appSettingsController.removeData()
uploadController.removeData()
transfersController.removeData()

realmProvider.closeAllRealms()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ class TransferManager internal constructor(
private val realmProvider: RealmProvider,
private val clientProvider: ApiClientProvider,
) {
//TODO: Implement here
// TODO: Implement here
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,11 @@ import io.realm.kotlin.RealmConfiguration
class RealmProvider {

val realmAppSettings by lazy { Realm.open(realmAppSettingsConfiguration) }

val realmUploads by lazy { Realm.open(realmUploadConfiguration) }

var realmTransfers: Realm? = null
private set

fun loadRealmTransfers(userId: Int) {
fun openRealmTransfers(userId: Int) {
realmTransfers = Realm.open(realmTransfersConfiguration(userId))
}

Expand All @@ -50,10 +48,6 @@ class RealmProvider {
realmTransfers?.close()
}

fun closeRealm(realm: Realm) {
realm.close()
}

fun closeAllRealms() {
closeRealmAppSettings()
closeRealmUploads()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class AppSettingsController(private val realmProvider: RealmProvider) {

private val appSettingsQuery get() = realm.query<AppSettingsDB>().first()

@Throws(IllegalArgumentException::class, CancellationException::class)
suspend fun initAppSettings() {
if (appSettingsQuery.find() == null) {
realm.write {
Expand All @@ -46,6 +47,7 @@ class AppSettingsController(private val realmProvider: RealmProvider) {
}

//region Get data
@Throws(IllegalArgumentException::class, CancellationException::class)
fun getAppSettingsFlow(): Flow<AppSettingsDB?> {
return appSettingsQuery.asFlow().mapLatest { it.obj }
}
Expand Down Expand Up @@ -90,5 +92,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,41 @@
/*
* 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 kotlin.coroutines.cancellation.CancellationException

class TransfersController(private val realmProvider: RealmProvider) {

private val realm by lazy { realmProvider.realmTransfers }

//region Get data
@Throws(IllegalArgumentException::class, CancellationException::class)
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,48 @@
/*
* 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 kotlin.coroutines.cancellation.CancellationException

class UploadController(private val realmProvider: RealmProvider) {

private val realm by lazy { realmProvider.realmUploads }

//region Queries
private fun getUploadsQuery() = realm.query<Upload>()
//endregion

//region Get data
@Throws(IllegalArgumentException::class, CancellationException::class)
fun getUploads(): RealmResults<Upload> = getUploadsQuery().find()

@Throws(IllegalArgumentException::class, CancellationException::class)
fun getUploadsCount(): Long = getUploadsQuery().count().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,18 @@
*/
package com.infomaniak.multiplatform_swisstransfer.database.models.upload

import io.realm.kotlin.ext.realmListOf
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 files = realmListOf<UploadFile>()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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 language: 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: Int = 0
}
Loading

0 comments on commit 484303d

Please sign in to comment.