Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AppSettingsControler and AppSettings Realm object #18

Merged
merged 8 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions STCommon/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
alias(libs.plugins.kotlinMultiplatform)
alias(libs.plugins.androidLibrary)
id("infomaniak.kotlinMultiplatform")
id("infomaniak.publishPlugin")
}

kotlin {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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.interfaces.appSettings

import com.infomaniak.multiplatform_swisstransfer.common.models.DownloadLimit
import com.infomaniak.multiplatform_swisstransfer.common.models.EmailLanguage
import com.infomaniak.multiplatform_swisstransfer.common.models.Theme
import com.infomaniak.multiplatform_swisstransfer.common.models.ValidityPeriod

interface AppSettings {
var theme: Theme
var validityPeriod: ValidityPeriod
var downloadLimit: DownloadLimit
var emailLanguage: EmailLanguage
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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.models

enum class DownloadLimit(val value: String) {
TWOHUNDREDFIFTY("250"),
ONEHUNDRED("100"),
TWENTY("20"),
ONE("1");
sirambd marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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.models

enum class EmailLanguage(val value: String) {
ENGLISH("english"),
FRENCH("french"),
GERMAN("german"),
ITALIAN("italian"),
SPANISH("spanish");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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.models

enum class Theme(val value: String) {
SYSTEM("system"),
LIGHT("light"),
DARK("dark");

fun indexOf() = entries.indexOf(this)
tevincent marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.infomaniak.multiplatform_swisstransfer.database.models.setting
package com.infomaniak.multiplatform_swisstransfer.common.models

import io.realm.kotlin.types.RealmObject

class AppSettings : RealmObject {
//TODO: implement here
enum class ValidityPeriod(val value: String) {
THIRTY("30"),
FIFTEEN("15"),
SEVEN("7"),
ONE("1");
}
15 changes: 15 additions & 0 deletions STCore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ centralized access point to orchestrate transfer operations.
| Type | Name | Description |
|----------|--------------------|-------------------------------------------------------------------------------|
| Property | transferManager | A manager used to orchestrate transfer operations. |
| Property | appSettingsManager | A manager used to orchestrate AppSettings operations. |
tevincent marked this conversation as resolved.
Show resolved Hide resolved
| Method | loadDefaultAccount | Loads the default user account and initializes Realm transfers for this user. |

### Details of Properties and Methods
Expand All @@ -54,6 +55,20 @@ centralized access point to orchestrate transfer operations.
// Use the transferManager to orchestrate transfers
```

#### Property: `appSettingsManager`
tevincent marked this conversation as resolved.
Show resolved Hide resolved

- **Type**: `AppSettingsManager`
- **Description**:
- `appSettingsManager` is a lazily initialized property that provides a manager to orchestrate all AppSettings operations. It
uses `realmProvider` to configure and manage AppSettings efficiently.

- **Usage Example**:
```kotlin
val core = SwissTransferInjection()
val appSettingsManager = core.appSettingsManager
// Use the appSettingsManager to orchestrate AppSettings related operations
```

#### Method: `loadDefaultAccount`

- **Signature**: `fun loadDefaultAccount()`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
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.managers.AppSettingsManager
import com.infomaniak.multiplatform_swisstransfer.managers.TransferManager
import com.infomaniak.multiplatform_swisstransfer.network.ApiClientProvider
import com.infomaniak.multiplatform_swisstransfer.network.repositories.TransferRepository
Expand All @@ -34,6 +36,7 @@ import com.infomaniak.multiplatform_swisstransfer.utils.Constants
* This class serves as the main access point.
*
* @property transferManager A manager used to orchestrate transfer operations.
* @property appSettingsManager A manager used to orchestrate AppSettings operations.
tevincent marked this conversation as resolved.
Show resolved Hide resolved
*/
class SwissTransferInjection {
private val realmProvider by lazy { RealmProvider() }
Expand All @@ -42,14 +45,20 @@ class SwissTransferInjection {
private val transferRepository by lazy { TransferRepository(apiClientProvider) }
private val uploadRepository by lazy { UploadRepository(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)
fun loadDefaultAccount() {
suspend fun loadDefaultAccount() {
appSettingsController.initAppSettings()
realmProvider.loadRealmTransfers(Constants.DEFAULT_USER_ID)
}

/** 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) }
tevincent marked this conversation as resolved.
Show resolved Hide resolved
}
tevincent marked this conversation as resolved.
Show resolved Hide resolved
tevincent marked this conversation as resolved.
Show resolved Hide resolved
tevincent marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* 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.common.interfaces.appSettings.AppSettings
import com.infomaniak.multiplatform_swisstransfer.common.models.DownloadLimit
import com.infomaniak.multiplatform_swisstransfer.common.models.EmailLanguage
import com.infomaniak.multiplatform_swisstransfer.common.models.Theme
import com.infomaniak.multiplatform_swisstransfer.common.models.ValidityPeriod
import com.infomaniak.multiplatform_swisstransfer.database.cache.setting.AppSettingsController
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.IO
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.withContext

/**
* Provides a convenient interface for managing application settings, abstracting the underlying
* [AppSettingsController]. It handles asynchronous operations and ensures settings updates are
* performed on a background thread.
**/
class AppSettingsManager internal constructor(
private val appSettingsController: AppSettingsController,
) {

/**
* A [Flow] that emits the current [AppSettings] object whenever it changes. This flow operates
* on the [Dispatchers.IO] context to avoid blocking the main thread.
*/
val appSettings: Flow<AppSettings?>
get() = appSettingsController.getAppSettingsFlow().flowOn(Dispatchers.IO)

/**
* Asynchronously sets the application theme.
*
* @param theme The new theme to apply.
*/
suspend fun setTheme(theme: Theme) = withContext(Dispatchers.IO) {
appSettingsController.setTheme(theme)
}

/**
* Asynchronously sets the validity period for certain application features.
*
* @param validityPeriod The new validity period.
*/
suspend fun setValidityPeriod(validityPeriod: ValidityPeriod) = withContext(Dispatchers.IO) {
appSettingsController.setValidityPeriod(validityPeriod)
}

/**
* Asynchronously sets the download limit for files.
*
* @param downloadLimit The new download limit.
*/
suspend fun setDownloadLimit(downloadLimit: DownloadLimit) = withContext(Dispatchers.IO) {
appSettingsController.setDownloadLimit(downloadLimit)
}

/**
* Asynchronously sets the language for email communications.
*
* @param emailLanguage The new email language.
*/
suspend fun setEmailLanguage(emailLanguage: EmailLanguage) = withContext(Dispatchers.IO) {
appSettingsController.setEmailLanguage(emailLanguage)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ package com.infomaniak.multiplatform_swisstransfer.database
import com.infomaniak.multiplatform_swisstransfer.database.models.ContainerDB
import com.infomaniak.multiplatform_swisstransfer.database.models.FileDB
import com.infomaniak.multiplatform_swisstransfer.database.models.TransferDB
import com.infomaniak.multiplatform_swisstransfer.database.models.setting.AppSettings
import com.infomaniak.multiplatform_swisstransfer.database.models.setting.AppSettingsDB
import com.infomaniak.multiplatform_swisstransfer.database.models.upload.UploadTasks
import io.realm.kotlin.Realm
import io.realm.kotlin.RealmConfiguration
Expand Down Expand Up @@ -62,7 +62,7 @@ class RealmProvider {
}

private val realmAppSettingsConfiguration = RealmConfiguration
.Builder(schema = setOf(AppSettings::class))
.Builder(schema = setOf(AppSettingsDB::class))
.name("AppSettings")
.build()

Expand Down
Loading
Loading