Updated on 2026-08-14
This commit is contained in:
parent
78a69e656b
commit
2596a73c7b
13 changed files with 591 additions and 0 deletions
|
|
@ -0,0 +1,30 @@
|
|||
package com.tangem.datasource.local.db
|
||||
|
||||
import androidx.room.Database
|
||||
import androidx.room.RoomDatabase
|
||||
import androidx.room.TypeConverters
|
||||
import com.tangem.datasource.local.db.dao.CryptoCurrenciesAccountDao
|
||||
import com.tangem.datasource.local.db.dao.CryptoCurrencyDao
|
||||
import com.tangem.datasource.local.db.dao.UserWalletDao
|
||||
import com.tangem.datasource.local.db.entity.CryptoCurrenciesAccountEntity
|
||||
import com.tangem.datasource.local.db.entity.CryptoCurrencyEntity
|
||||
import com.tangem.datasource.local.db.entity.UserWalletEntity
|
||||
import com.tangem.datasource.local.db.utils.Converters
|
||||
|
||||
@Database(
|
||||
entities = [
|
||||
UserWalletEntity::class,
|
||||
CryptoCurrenciesAccountEntity::class,
|
||||
CryptoCurrencyEntity::class,
|
||||
],
|
||||
version = 1,
|
||||
)
|
||||
@TypeConverters(Converters::class)
|
||||
abstract class TangemDatabase : RoomDatabase() {
|
||||
|
||||
abstract fun userWalletDao(): UserWalletDao
|
||||
|
||||
abstract fun cryptoCurrencyDao(): CryptoCurrencyDao
|
||||
|
||||
abstract fun cryptoCurrenciesAccountDao(): CryptoCurrenciesAccountDao
|
||||
}
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
package com.tangem.datasource.local.db.dao
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Insert
|
||||
import androidx.room.Query
|
||||
import androidx.room.Update
|
||||
import com.tangem.datasource.local.db.entity.CryptoCurrenciesAccountEntity
|
||||
import com.tangem.domain.wallets.models.UserWalletId
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
@Dao
|
||||
interface CryptoCurrenciesAccountDao {
|
||||
|
||||
@Insert
|
||||
suspend fun insert(account: CryptoCurrenciesAccountEntity)
|
||||
|
||||
@Update
|
||||
suspend fun update(account: CryptoCurrenciesAccountEntity)
|
||||
|
||||
@Query("SELECT * FROM CryptoCurrenciesAccountEntity WHERE userWalletId = :userWalletId")
|
||||
fun observeByUserWalletId(userWalletId: UserWalletId): Flow<List<CryptoCurrenciesAccountEntity>>
|
||||
|
||||
@Query("SELECT * FROM CryptoCurrenciesAccountEntity WHERE userWalletId = :userWalletId")
|
||||
suspend fun selectByUserWalletId(userWalletId: UserWalletId): List<CryptoCurrenciesAccountEntity>
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
package com.tangem.datasource.local.db.dao
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Insert
|
||||
import androidx.room.Query
|
||||
import com.tangem.datasource.local.db.entity.CryptoCurrencyEntity
|
||||
import com.tangem.domain.wallets.models.UserWalletId
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
@Dao
|
||||
interface CryptoCurrencyDao {
|
||||
|
||||
@Insert
|
||||
suspend fun insert(currencies: List<CryptoCurrencyEntity>)
|
||||
|
||||
@Query(
|
||||
"""
|
||||
SELECT * FROM CryptoCurrencyEntity
|
||||
WHERE userWalletId = :userWalletId AND accountId = :accountId
|
||||
""",
|
||||
)
|
||||
suspend fun selectByAccountId(userWalletId: UserWalletId, accountId: Int): List<CryptoCurrencyEntity>
|
||||
|
||||
@Query(
|
||||
"""
|
||||
SELECT * FROM CryptoCurrencyEntity
|
||||
WHERE userWalletId = :userWalletId AND accountId = :accountId
|
||||
""",
|
||||
)
|
||||
fun observeByAccountId(userWalletId: UserWalletId, accountId: Int): Flow<List<CryptoCurrencyEntity>>
|
||||
|
||||
@Query(
|
||||
"""
|
||||
SELECT COUNT(*) FROM CryptoCurrencyEntity
|
||||
WHERE userWalletId = :userWalletId
|
||||
""",
|
||||
)
|
||||
suspend fun countByUserWalletId(userWalletId: UserWalletId): Int
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
package com.tangem.datasource.local.db.dao
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Insert
|
||||
import androidx.room.Query
|
||||
import androidx.room.Update
|
||||
import com.tangem.datasource.local.db.entity.UserWalletEntity
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
@Dao
|
||||
interface UserWalletDao {
|
||||
|
||||
@Insert
|
||||
suspend fun insert(vararg wallets: UserWalletEntity)
|
||||
|
||||
@Update
|
||||
suspend fun update(wallet: UserWalletEntity)
|
||||
|
||||
@Query("SELECT * FROM UserWalletEntity")
|
||||
suspend fun selectAll(): List<UserWalletEntity>
|
||||
|
||||
@Query("SELECT * FROM UserWalletEntity")
|
||||
fun observeAll(): Flow<List<UserWalletEntity>>
|
||||
|
||||
@Query("SELECT * FROM UserWalletEntity WHERE id = :id")
|
||||
suspend fun selectById(id: String): UserWalletEntity?
|
||||
|
||||
@Query("SELECT * FROM UserWalletEntity WHERE id = :id")
|
||||
fun observeById(id: String): Flow<UserWalletEntity>
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
package com.tangem.datasource.local.db.di
|
||||
|
||||
import android.content.Context
|
||||
import androidx.room.Room
|
||||
import com.tangem.datasource.local.db.TangemDatabase
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Module
|
||||
@InstallIn(SingletonComponent::class)
|
||||
internal object TangemDatabaseModule {
|
||||
|
||||
private const val DATABASE_NAME = "tangem_database.db"
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideDatabase(@ApplicationContext context: Context): TangemDatabase {
|
||||
return Room.databaseBuilder(
|
||||
context,
|
||||
TangemDatabase::class.java,
|
||||
DATABASE_NAME,
|
||||
).build()
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
package com.tangem.datasource.local.db.entity
|
||||
|
||||
import androidx.room.Entity
|
||||
import androidx.room.ForeignKey
|
||||
import androidx.room.Index
|
||||
import androidx.room.PrimaryKey
|
||||
|
||||
@Entity(
|
||||
foreignKeys = [
|
||||
ForeignKey(
|
||||
entity = UserWalletEntity::class,
|
||||
parentColumns = ["id"],
|
||||
childColumns = ["userWalletId"],
|
||||
onDelete = ForeignKey.CASCADE,
|
||||
),
|
||||
],
|
||||
indices = [
|
||||
Index(value = ["id"]),
|
||||
Index(value = ["userWalletId"]),
|
||||
],
|
||||
)
|
||||
data class CryptoCurrenciesAccountEntity(
|
||||
@PrimaryKey
|
||||
val id: Int,
|
||||
val userWalletId: String,
|
||||
val title: String,
|
||||
val currenciesCount: Int,
|
||||
val isArchived: Boolean,
|
||||
val ordinalNumber: Int,
|
||||
)
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
package com.tangem.datasource.local.db.entity
|
||||
|
||||
import androidx.room.Entity
|
||||
import androidx.room.ForeignKey
|
||||
import androidx.room.Index
|
||||
import androidx.room.PrimaryKey
|
||||
|
||||
@Entity(
|
||||
foreignKeys = [
|
||||
ForeignKey(
|
||||
entity = CryptoCurrenciesAccountEntity::class,
|
||||
parentColumns = ["id"],
|
||||
childColumns = ["accountId"],
|
||||
onDelete = ForeignKey.CASCADE,
|
||||
),
|
||||
ForeignKey(
|
||||
entity = UserWalletEntity::class,
|
||||
parentColumns = ["id"],
|
||||
childColumns = ["userWalletId"],
|
||||
onDelete = ForeignKey.CASCADE,
|
||||
),
|
||||
],
|
||||
indices = [
|
||||
Index(value = ["currencyBackendId"]),
|
||||
Index(value = ["networkId"]),
|
||||
Index(value = ["accountId"]),
|
||||
Index(value = ["userWalletId"]),
|
||||
],
|
||||
)
|
||||
data class CryptoCurrencyEntity(
|
||||
@PrimaryKey(autoGenerate = true)
|
||||
val id: Int = 0,
|
||||
val currencyBackendId: String?,
|
||||
val networkId: String,
|
||||
val accountId: Int,
|
||||
val userWalletId: String,
|
||||
val name: String,
|
||||
val symbol: String,
|
||||
val decimals: Int,
|
||||
val contractAddress: String?,
|
||||
val derivationPath: String?,
|
||||
)
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
package com.tangem.datasource.local.db.entity
|
||||
|
||||
import androidx.room.Entity
|
||||
import androidx.room.Index
|
||||
import androidx.room.PrimaryKey
|
||||
|
||||
@Entity(
|
||||
indices = [
|
||||
Index(value = ["id"], unique = true),
|
||||
Index(value = ["ordinalNumber"], unique = true),
|
||||
],
|
||||
)
|
||||
data class UserWalletEntity(
|
||||
@PrimaryKey
|
||||
val id: String,
|
||||
val name: String,
|
||||
val artworkUrl: String,
|
||||
val isMultiCurrency: Boolean,
|
||||
val hasBackupError: Boolean,
|
||||
val cardsInWallet: Set<String>,
|
||||
val ordinalNumber: Int,
|
||||
)
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
package com.tangem.datasource.local.db.utils
|
||||
|
||||
import androidx.room.TypeConverter
|
||||
import com.tangem.domain.wallets.models.UserWalletId
|
||||
|
||||
internal class Converters {
|
||||
|
||||
@TypeConverter
|
||||
fun setFromString(value: String): Set<String> {
|
||||
return value.split(",").toSet()
|
||||
}
|
||||
|
||||
@TypeConverter
|
||||
fun setToString(set: Set<Any>): String {
|
||||
return set.joinToString(",")
|
||||
}
|
||||
|
||||
@TypeConverter
|
||||
fun userWalletIdFromString(value: String): UserWalletId {
|
||||
return UserWalletId(value)
|
||||
}
|
||||
|
||||
@TypeConverter
|
||||
fun userWalletIdToString(userWalletId: UserWalletId): String {
|
||||
return userWalletId.stringValue
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue