Updated on 2026-08-14
This commit is contained in:
parent
0dd4d45b62
commit
479a67f3be
19 changed files with 257 additions and 61 deletions
|
|
@ -0,0 +1,34 @@
|
|||
package com.tangem.datasource.di
|
||||
|
||||
import android.content.Context
|
||||
import com.squareup.moshi.Moshi
|
||||
import com.squareup.moshi.Types
|
||||
import com.tangem.datasource.local.card.DefaultUsedCardsStore
|
||||
import com.tangem.datasource.local.card.UsedCardInfo
|
||||
import com.tangem.datasource.local.card.UsedCardsStore
|
||||
import com.tangem.datasource.local.datastore.SharedPreferencesDataStore
|
||||
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 CardDataModule {
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideUsedCardsStore(@ApplicationContext context: Context, @NetworkMoshi moshi: Moshi): UsedCardsStore {
|
||||
return DefaultUsedCardsStore(
|
||||
store = SharedPreferencesDataStore(
|
||||
preferencesName = "tapPrefs",
|
||||
context = context,
|
||||
adapter = moshi.adapter(
|
||||
Types.newParameterizedType(List::class.java, UsedCardInfo::class.java),
|
||||
),
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
package com.tangem.datasource.local.card
|
||||
|
||||
data class UsedCardInfo(
|
||||
val cardId: String,
|
||||
val isScanned: Boolean = false,
|
||||
val isActivationStarted: Boolean = false,
|
||||
val isActivationFinished: Boolean = false,
|
||||
)
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
package com.tangem.datasource.local.card
|
||||
|
||||
import com.tangem.datasource.local.datastore.SharedPreferencesDataStore
|
||||
import com.tangem.datasource.local.datastore.core.KeylessDataStoreDecorator
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
interface UsedCardsStore {
|
||||
|
||||
fun get(): Flow<List<UsedCardInfo>>
|
||||
|
||||
suspend fun getSyncOrNull(): List<UsedCardInfo>?
|
||||
|
||||
suspend fun store(item: List<UsedCardInfo>)
|
||||
}
|
||||
|
||||
internal class DefaultUsedCardsStore(
|
||||
store: SharedPreferencesDataStore<List<UsedCardInfo>>,
|
||||
) : UsedCardsStore, KeylessDataStoreDecorator<List<UsedCardInfo>>(
|
||||
wrappedDataStore = store,
|
||||
key = "usedCardsInfo_v2",
|
||||
)
|
||||
|
|
@ -1,32 +1,21 @@
|
|||
package com.tangem.datasource.local.datastore.core
|
||||
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
internal abstract class KeylessDataStoreDecorator<Value : Any>(
|
||||
wrappedDataStore: StringKeyDataStore<Value>,
|
||||
) : StringKeyDataStoreDecorator<Unit, Value>(wrappedDataStore) {
|
||||
private val key: String = DEFAULT_STRING_KEY,
|
||||
) : StringKeyDataStoreDecorator<String, Value>(wrappedDataStore) {
|
||||
|
||||
override fun provideStringKey(key: Unit): String {
|
||||
return STRING_KEY
|
||||
}
|
||||
override fun provideStringKey(key: String) = key
|
||||
|
||||
open fun get(): Flow<Value> {
|
||||
return get(Unit)
|
||||
}
|
||||
open fun get() = get(key)
|
||||
|
||||
open suspend fun getSyncOrNull(): Value? {
|
||||
return getSyncOrNull(Unit)
|
||||
}
|
||||
open suspend fun getSyncOrNull() = getSyncOrNull(key)
|
||||
|
||||
open suspend fun store(item: Value) {
|
||||
store(Unit, item)
|
||||
}
|
||||
open suspend fun store(item: Value) = store(key, item)
|
||||
|
||||
open suspend fun isEmpty(): Boolean {
|
||||
return getSyncOrNull() == null
|
||||
}
|
||||
open suspend fun isEmpty() = getSyncOrNull() == null
|
||||
|
||||
private companion object {
|
||||
const val STRING_KEY = "key"
|
||||
const val DEFAULT_STRING_KEY = "key"
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue