Skip to content

Commit

Permalink
feat: Save EmailToken in AppSettings
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippeWeidmann committed Dec 11, 2024
1 parent f9fe646 commit c410277
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
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.common.interfaces.appSettings

interface EmailToken {
val email: String
val token: String
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.infomaniak.multiplatform_swisstransfer.database

import com.infomaniak.multiplatform_swisstransfer.database.models.appSettings.AppSettingsDB
import com.infomaniak.multiplatform_swisstransfer.database.models.appSettings.EmailTokenDB
import com.infomaniak.multiplatform_swisstransfer.database.models.transfers.ContainerDB
import com.infomaniak.multiplatform_swisstransfer.database.models.transfers.FileDB
import com.infomaniak.multiplatform_swisstransfer.database.models.transfers.TransferDB
Expand Down Expand Up @@ -58,7 +59,7 @@ class RealmProvider(private val loadDataInMemory: Boolean = false) {
}

private val realmAppSettingsConfiguration = RealmConfiguration
.Builder(schema = setOf(AppSettingsDB::class))
.Builder(schema = setOf(AppSettingsDB::class, EmailTokenDB::class))
.name("AppSettings.realm")
.deleteRealmDataIfNeeded()
.loadDataInMemoryIfNeeded()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ 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.interfaces.appSettings.EmailToken
import com.infomaniak.multiplatform_swisstransfer.common.interfaces.transfers.File
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.models.appSettings.EmailTokenDB
import com.infomaniak.multiplatform_swisstransfer.database.models.transfers.FileDB
import com.infomaniak.multiplatform_swisstransfer.database.models.transfers.TransferDB
import com.infomaniak.multiplatform_swisstransfer.database.utils.RealmUtils.runThrowingRealm
import io.realm.kotlin.UpdatePolicy
import io.realm.kotlin.ext.query
Expand Down Expand Up @@ -56,6 +61,12 @@ class AppSettingsController(private val realmProvider: RealmProvider) {
fun getAppSettings(): AppSettings? = runThrowingRealm {
return appSettingsQuery.find()
}

@Throws(RealmException::class)
fun getTokenForEmail(email: String): EmailToken? = runThrowingRealm {
val query = "${EmailTokenDB::email} == '$email'"
return realm.query<EmailTokenDB>(query).first().find()
}
//endregion

//region Update data
Expand Down Expand Up @@ -116,5 +127,13 @@ class AppSettingsController(private val realmProvider: RealmProvider) {
suspend fun removeData() = runThrowingRealm {
realm.write { deleteAll() }
}

@Throws(RealmException::class, CancellationException::class)
suspend fun setEmailAndToken(email: String, token: String) = runThrowingRealm {
realm.write {
val emailTokenDB = EmailTokenDB(email, token)
this.copyToRealm(emailTokenDB, UpdatePolicy.ALL)
}
}
//endregion
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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.appSettings

import com.infomaniak.multiplatform_swisstransfer.common.interfaces.appSettings.EmailToken
import io.realm.kotlin.types.RealmObject
import io.realm.kotlin.types.annotations.PrimaryKey

class EmailTokenDB(
@PrimaryKey
override var email: String = "",
override var token: String = ""
) : RealmObject, EmailToken

0 comments on commit c410277

Please sign in to comment.