Skip to content

Commit

Permalink
refactor(MyKSuiteDataModels): Clean data models and database dao
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianDevel committed Feb 19, 2025
1 parent be19b8e commit 5a59c0d
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 146 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,21 @@
"formatVersion": 1,
"database": {
"version": 1,
"identityHash": "7b5e43bd9cc17b034cf90891e63090d7",
"identityHash": "daf7033b58a05ae65b82dee555601e73",
"entities": [
{
"tableName": "MyKSuiteData",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER NOT NULL, `status` TEXT NOT NULL, `pack_id` INTEGER NOT NULL, `trialExpiryAt` INTEGER, `is_free` INTEGER NOT NULL, `has_auto_renew` INTEGER NOT NULL, `canTrial` INTEGER NOT NULL, `user_id` INTEGER NOT NULL, `k_suite_pack_id` INTEGER NOT NULL, `k_suite_pack_name` TEXT NOT NULL, `k_suite_pack_drive_storage` INTEGER NOT NULL, `k_suite_pack_mail_storage` INTEGER NOT NULL, `k_suite_pack_mail_daily_limit_send` INTEGER NOT NULL, `k_suite_pack_is_max_storage_offer` INTEGER NOT NULL, `drive_id` INTEGER NOT NULL, `drive_name` TEXT NOT NULL, `drive_size` INTEGER NOT NULL, `drive_used_size` INTEGER NOT NULL, `mail_id` INTEGER NOT NULL, `mail_email` TEXT NOT NULL, `mail_daily_limit_sent` INTEGER NOT NULL, `mail_storage_size_limit` INTEGER NOT NULL, `mail_used_size` INTEGER NOT NULL, `mail_mailboxId` INTEGER NOT NULL, PRIMARY KEY(`id`))",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER NOT NULL, `trial_expiry_at` INTEGER, `is_free` INTEGER NOT NULL, `user_id` INTEGER NOT NULL, `drive_id` INTEGER NOT NULL, `drive_name` TEXT NOT NULL, `drive_size` INTEGER NOT NULL, `drive_used_size` INTEGER NOT NULL, `mail_id` INTEGER NOT NULL, `mail_email` TEXT NOT NULL, `mail_daily_limit_sent` INTEGER NOT NULL, `mail_storage_size_limit` INTEGER NOT NULL, `mail_used_size` INTEGER NOT NULL, `mail_mailbox_id` INTEGER NOT NULL, PRIMARY KEY(`id`))",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "status",
"columnName": "status",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "kSuitePackId",
"columnName": "pack_id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "trialExpiryAt",
"columnName": "trialExpiryAt",
"columnName": "trial_expiry_at",
"affinity": "INTEGER",
"notNull": false
},
Expand All @@ -38,60 +26,12 @@
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "hasAutoRenew",
"columnName": "has_auto_renew",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "canTrial",
"columnName": "canTrial",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "userId",
"columnName": "user_id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "kSuitePack.id",
"columnName": "k_suite_pack_id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "kSuitePack.name",
"columnName": "k_suite_pack_name",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "kSuitePack.driveStorage",
"columnName": "k_suite_pack_drive_storage",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "kSuitePack.mailStorage",
"columnName": "k_suite_pack_mail_storage",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "kSuitePack.mailDailyLimitSend",
"columnName": "k_suite_pack_mail_daily_limit_send",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "kSuitePack.isMaxStorageOffer",
"columnName": "k_suite_pack_is_max_storage_offer",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "drive.id",
"columnName": "drive_id",
Expand Down Expand Up @@ -148,7 +88,7 @@
},
{
"fieldPath": "mail.mailboxId",
"columnName": "mail_mailboxId",
"columnName": "mail_mailbox_id",
"affinity": "INTEGER",
"notNull": true
}
Expand All @@ -166,7 +106,7 @@
"views": [],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '7b5e43bd9cc17b034cf90891e63090d7')"
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'daf7033b58a05ae65b82dee555601e73')"
]
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import androidx.room.ColumnInfo
import androidx.room.Embedded
import androidx.room.Entity
import androidx.room.PrimaryKey
import com.infomaniak.core.myksuite.R
import com.infomaniak.core.myksuite.ui.components.MyKSuiteTier
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
Expand All @@ -30,13 +32,6 @@ import java.util.Date
@Entity
data class MyKSuiteData(
@PrimaryKey val id: Int,
val status: String,
@SerialName("pack_id")
@ColumnInfo("pack_id")
val kSuitePackId: Int,
@SerialName("pack")
@Embedded("k_suite_pack_")
val kSuitePack: KSuitePack,
@SerialName("trial_expiry_at")
@ColumnInfo("trial_expiry_at")
val trialExpiryAt: Long? = null,
Expand All @@ -47,22 +42,13 @@ data class MyKSuiteData(
val drive: KSuiteDrive,
@Embedded("mail_")
val mail: KSuiteMail,
@SerialName("has_auto_renew")
@ColumnInfo("has_auto_renew")
val hasAutoRenew: Boolean,
@SerialName("can_trial")
@ColumnInfo("can_trial")
val canTrial: Boolean,
) {

@Transient
@ColumnInfo("user_id")
var userId: Int = 0

val isMyKSuite get() = kSuitePack.type == KSuitePack.KSuitePackType.MY_KSUITE
val isMyKSuitePlus
get() = kSuitePack.type == KSuitePack.KSuitePackType.MY_KSUITE_PLUS ||
kSuitePack.type == KSuitePack.KSuitePackType.MY_KSUITE_PLUS_DRIVE_SOLO

inline val name get() = if (isFree) R.string.myKSuiteName else R.string.myKSuitePlusName
inline val tier get() = if (isFree) MyKSuiteTier.Free else MyKSuiteTier.Plus
inline val trialExpiryDate get() = trialExpiryAt?.let { Date(it * 1_000) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ import androidx.room.Upsert

@Dao
interface MyKSuiteDataDao {

@Query("SELECT * FROM MyKSuiteData")
suspend fun getAll(): List<MyKSuiteData>

@Query("SELECT * FROM MyKSuiteData WHERE id = :id LIMIT 1")
suspend fun findById(id: Int): MyKSuiteData?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ import android.content.Context

abstract class MyKSuiteDataManager {

abstract var myKSuiteId: Int
abstract var myKSuite: MyKSuiteData?
abstract val userId: Int
protected abstract val currentUserId: Int

private var myKSuiteDatabase: MyKSuiteDatabase? = null

Expand All @@ -37,15 +36,17 @@ abstract class MyKSuiteDataManager {

suspend fun upsertKSuiteData(kSuiteData: MyKSuiteData) {
myKSuite = kSuiteData
myKSuiteDatabase?.myKSuiteDataDao()?.upsert(kSuiteData.apply { userId = this@MyKSuiteDataManager.userId })
myKSuiteDatabase?.myKSuiteDataDao()?.upsert(kSuiteData.apply { userId = this@MyKSuiteDataManager.currentUserId })
}

suspend fun deleteKSuiteData(kSuiteData: MyKSuiteData) {
suspend fun deleteKSuiteData(userId: Int) {
(getKSuiteDataByUser(userId) ?: myKSuite)?.let { myKSuiteDatabase?.myKSuiteDataDao()?.delete(it) }
myKSuite = null
myKSuiteDatabase?.myKSuiteDataDao()?.delete(kSuiteData)
}

private suspend fun getKSuiteData(id: Int) = myKSuiteDatabase?.myKSuiteDataDao()?.findById(id)

private suspend fun getKSuiteDataByUser() = myKSuiteDatabase?.myKSuiteDataDao()?.findByUserId(userId)
private suspend fun getKSuiteDataByUser(userId: Int? = null): MyKSuiteData? {
return myKSuiteDatabase?.myKSuiteDataDao()?.findByUserId(userId ?: currentUserId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ data class KSuiteMail(
@ColumnInfo("used_size")
val usedSize: Long,
@SerialName("mailbox_id")
@ColumnInfo("mailbox_id")
val mailboxId: Int,
)
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,5 @@ object ApiRoutes {

private const val BASE_URL = "https://api.infomaniak.com"

fun myKSuiteData() = "$BASE_URL/1/my_ksuite/current?with=drive,mail,pack,can_trial,has_auto_renew"

fun myKSuiteData() = "$BASE_URL/1/my_ksuite/current?with=drive,mail"
}

0 comments on commit 5a59c0d

Please sign in to comment.