Skip to content

Commit

Permalink
Merge pull request #89 from Infomaniak/save-transfer-type
Browse files Browse the repository at this point in the history
feat: Save TransferType and AuthorEmail
  • Loading branch information
sirambd authored Nov 13, 2024
2 parents 4360aa0 + 8145cb6 commit 1130c53
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@
*/
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
import com.infomaniak.multiplatform_swisstransfer.common.models.*

interface AppSettings {
val theme: Theme
val validityPeriod: ValidityPeriod
val downloadLimit: DownloadLimit
val emailLanguage: EmailLanguage
val lastTransferType: TransferType
val lastAuthorEmail: String?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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 TransferType {
LINK,
QR_CODE,
PROXIMITY,
MAIL,
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ package com.infomaniak.multiplatform_swisstransfer.managers

import com.infomaniak.multiplatform_swisstransfer.common.exceptions.RealmException
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.common.models.*
import com.infomaniak.multiplatform_swisstransfer.database.controllers.AppSettingsController
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.IO
Expand Down Expand Up @@ -100,4 +97,30 @@ class AppSettingsManager internal constructor(
suspend fun setEmailLanguage(emailLanguage: EmailLanguage): Unit = withContext(Dispatchers.IO) {
appSettingsController.setEmailLanguage(emailLanguage)
}

/**
* Asynchronously sets the last type of transfer selected by the user.
*
* @param transferType The last type of transfer selected.
*
* @throws RealmException If the provided type of transfer is invalid.
* @throws CancellationException If the operation is cancelled.
*/
@Throws(RealmException::class, CancellationException::class)
suspend fun setLastTransferType(transferType: TransferType): Unit = withContext(Dispatchers.IO) {
appSettingsController.setLastTransferType(transferType)
}

/**
* Asynchronously sets the last email of the transfer author entered by the user.
*
* @param authorEmail The last email of the transfer author entered.
*
* @throws RealmException If the provided email is invalid.
* @throws CancellationException If the operation is cancelled.
*/
@Throws(RealmException::class, CancellationException::class)
suspend fun setLastAuthorEmail(authorEmail: String?): Unit = withContext(Dispatchers.IO) {
appSettingsController.setLastAuthorEmail(authorEmail)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ package com.infomaniak.multiplatform_swisstransfer.database.controllers

import com.infomaniak.multiplatform_swisstransfer.common.exceptions.RealmException
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.common.models.*
import com.infomaniak.multiplatform_swisstransfer.database.RealmProvider
import com.infomaniak.multiplatform_swisstransfer.database.models.appSettings.AppSettingsDB
import com.infomaniak.multiplatform_swisstransfer.database.utils.RealmUtils.runThrowingRealm
Expand Down Expand Up @@ -101,6 +98,20 @@ class AppSettingsController(private val realmProvider: RealmProvider) {
}
}

@Throws(RealmException::class, CancellationException::class)
suspend fun setLastTransferType(transferType: TransferType) = runThrowingRealm {
updateAppSettings { mutableAppSettings ->
mutableAppSettings.lastTransferType = transferType
}
}

@Throws(RealmException::class, CancellationException::class)
suspend fun setLastAuthorEmail(authorEmail: String?) = runThrowingRealm {
updateAppSettings { mutableAppSettings ->
mutableAppSettings.lastAuthorEmail = authorEmail
}
}

@Throws(RealmException::class, CancellationException::class)
suspend fun removeData() = runThrowingRealm {
realm.write { deleteAll() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@
package com.infomaniak.multiplatform_swisstransfer.database.models.appSettings

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.common.models.*
import io.realm.kotlin.types.RealmObject

class AppSettingsDB : RealmObject, AppSettings {
Expand Down Expand Up @@ -54,10 +51,20 @@ class AppSettingsDB : RealmObject, AppSettings {
_emailLanguage = value.value
}

private var _lastTransferType: String = DEFAULT_TRANSFER_TYPE.name
override var lastTransferType: TransferType
get() = TransferType.entries.find { it.name == _lastTransferType } ?: DEFAULT_TRANSFER_TYPE
set(value) {
_lastTransferType = value.name
}

override var lastAuthorEmail: String? = null

companion object {
private val DEFAULT_THEME = Theme.SYSTEM
private val DEFAULT_VALIDITY_PERIOD = ValidityPeriod.THIRTY
private val DEFAULT_DOWNLOAD_LIMIT = DownloadLimit.TWO_HUNDRED_FIFTY
private val DEFAULT_EMAIL_LANGUAGE = EmailLanguage.ENGLISH
private val DEFAULT_TRANSFER_TYPE = TransferType.QR_CODE
}
}

0 comments on commit 1130c53

Please sign in to comment.