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

[i04] core: Config Core module #8

Merged
merged 8 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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

import com.infomaniak.multiplatform_swisstransfer.database.RealmProvider
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
* to manage transfers and downloads. It replaces traditional dependency injections
* and provides centralized access to all functionality via lazily initialized properties and methods.
* lazily initialized properties and methods.
*
* This class serves as the main access point.
*
* @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) }

/**
* Loads the default user account and initializes Realm transfers
* for the default user ID defined in the constants.
*/
fun loadDefaultAccount() {
realmProvider.loadRealmTransfers(Constants.DEFAULT_USER_ID)
}

/** A manager used to orchestrate transfer operations. */
val transferManager by lazy { TransferManager(realmProvider, apiClientProvider) }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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.network.ApiClientProvider

/**
* TransferManager is responsible for orchestrating data transfer operations
* using Realm for local data management and an API client for network communications.
*
* This class integrates with both RealmProvider and ApiClientProvider to ensure
* smooth and efficient data transfers, providing a centralized management point
* for transfer-related activities.
*
* @property realmProvider The provider for managing Realm database operations.
* @property clientProvider The provider for creating and configuring HTTP clients for API communication.
*/
class TransferManager internal constructor(
private val realmProvider: RealmProvider,
private val clientProvider: ApiClientProvider,
) {
//TODO: Implement here
}
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.utils

object Constants {
const val DEFAULT_USER_ID = 0
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,22 @@ import com.infomaniak.multiplatform_swisstransfer.database.models.upload.UploadT
import io.realm.kotlin.Realm
import io.realm.kotlin.RealmConfiguration

internal class RealmProvider {
class RealmProvider {

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

val realmUploadTasks by lazy { Realm.open(realmUploadTasksConfiguration) }

var realmTransfers: Realm? = null
private set

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

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

private val realmAppSettingsConfiguration = RealmConfiguration
.Builder(schema = setOf(AppSettings::class))
Expand All @@ -38,20 +53,10 @@ internal class RealmProvider {
.name("UploadTasks")
.build()

private val realmTransfersConfiguration = RealmConfiguration
private fun realmTransfersConfiguration(userId: Int) = RealmConfiguration
.Builder(schema = setOf(TransferDB::class, ContainerDB::class, FileDB::class))
.name(transferRealmName())
.name(transferRealmName(userId))
.build()

private fun transferRealmName(userId: Int = 0) = "Transfers-$userId"

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

val realmUploadTasks by lazy { Realm.open(realmUploadTasksConfiguration) }

fun realmTransfers(userId: Int): Realm = Realm.open(realmTransfersConfiguration)

fun closeRealm(realm: Realm) {
realm.close()
}
private fun transferRealmName(userId: Int) = "Transfers-$userId"
}
2 changes: 1 addition & 1 deletion Network/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ kotlin {
implementation(libs.ktor.client.content.negociation)
implementation(libs.ktor.client.json)
implementation(libs.ktor.client.encoding)
implementation(libs.kotlinx.serialization.json)
api(libs.kotlinx.serialization.json)
}
commonTest.dependencies {
implementation(libs.kotlin.test)
Expand Down
Loading