-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #117 from Infomaniak/feat-save-email-token
feat: Save EmailToken in AppSettings
- Loading branch information
Showing
7 changed files
with
183 additions
and
5 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
...in/com/infomaniak/multiplatform_swisstransfer/common/interfaces/appSettings/EmailToken.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...mmonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/managers/EmailTokensManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* 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.exceptions.RealmException | ||
import com.infomaniak.multiplatform_swisstransfer.database.controllers.EmailTokensController | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.IO | ||
import kotlinx.coroutines.withContext | ||
import kotlin.coroutines.cancellation.CancellationException | ||
|
||
class EmailTokensManager(private val emailTokensController: EmailTokensController) { | ||
|
||
/** | ||
* Get a token for a given email. | ||
* | ||
* @param email The email possibly associated with a token. | ||
* | ||
* @return A token which may be associated with the email. | ||
* | ||
* @throws RealmException If an error occurs during database access. | ||
* @throws CancellationException If the operation is cancelled. | ||
*/ | ||
@Throws(RealmException::class, CancellationException::class) | ||
suspend fun getTokenForEmail(email: String): String? = withContext(Dispatchers.IO) { | ||
return@withContext emailTokensController.getEmailTokenForEmail(email)?.token | ||
} | ||
|
||
/** | ||
* Asynchronously sets the email and it's corresponding token. | ||
* | ||
* @param email The validated email. | ||
* @param token The valid token associated to the email. | ||
* | ||
* @throws RealmException If an error occurs during database access. | ||
* @throws CancellationException If the operation is cancelled. | ||
*/ | ||
@Throws(RealmException::class, CancellationException::class) | ||
suspend fun setEmailToken(email: String, token: String): Unit = withContext(Dispatchers.IO) { | ||
emailTokensController.setEmailToken(email, token) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
.../com/infomaniak/multiplatform_swisstransfer/database/controllers/EmailTokensController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* 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.controllers | ||
|
||
import com.infomaniak.multiplatform_swisstransfer.common.exceptions.RealmException | ||
import com.infomaniak.multiplatform_swisstransfer.common.interfaces.appSettings.EmailToken | ||
import com.infomaniak.multiplatform_swisstransfer.database.RealmProvider | ||
import com.infomaniak.multiplatform_swisstransfer.database.models.appSettings.EmailTokenDB | ||
import com.infomaniak.multiplatform_swisstransfer.database.utils.RealmUtils.runThrowingRealm | ||
import io.realm.kotlin.UpdatePolicy | ||
import io.realm.kotlin.ext.query | ||
import kotlin.coroutines.cancellation.CancellationException | ||
|
||
class EmailTokensController(private val realmProvider: RealmProvider) { | ||
private val realm by lazy { realmProvider.realmAppSettings } | ||
|
||
//region Get data | ||
@Throws(RealmException::class) | ||
fun getEmailTokenForEmail(email: String): EmailToken? = runThrowingRealm { | ||
val query = "${EmailTokenDB::email.name} == '$email'" | ||
return realm.query<EmailTokenDB>(query).first().find() | ||
} | ||
//endregion | ||
|
||
//region Update data | ||
@Throws(RealmException::class, CancellationException::class) | ||
suspend fun setEmailToken(email: String, token: String) = runThrowingRealm { | ||
realm.write { | ||
val emailTokenDB = EmailTokenDB(email, token) | ||
copyToRealm(emailTokenDB, UpdatePolicy.ALL) | ||
} | ||
} | ||
//endregion | ||
} |
33 changes: 33 additions & 0 deletions
33
...in/com/infomaniak/multiplatform_swisstransfer/database/models/appSettings/EmailTokenDB.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* 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() : RealmObject, EmailToken { | ||
@PrimaryKey | ||
override var email: String = "" | ||
override var token: String = "" | ||
|
||
constructor(email: String, token: String) : this() { | ||
this.email = email | ||
this.token = token | ||
} | ||
} |