Updated on 2026-08-14

This commit is contained in:
Tangem 2025-04-02 14:24:40 +07:00
parent ac6f73170e
commit bdcd8f2ce8
25 changed files with 224 additions and 30 deletions

View file

@ -0,0 +1,45 @@
package com.tangem.datasource.di
import android.content.Context
import androidx.datastore.core.DataStoreFactory
import androidx.datastore.dataStoreFile
import com.squareup.moshi.Moshi
import com.tangem.datasource.local.walletconnect.DefaultWalletConnectStore
import com.tangem.datasource.local.walletconnect.WalletConnectStore
import com.tangem.datasource.utils.MoshiDataStoreSerializer
import com.tangem.datasource.utils.setTypes
import com.tangem.domain.walletconnect.model.WcSessionDTO
import com.tangem.utils.coroutines.CoroutineDispatcherProvider
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import javax.inject.Singleton
@Module
@InstallIn(SingletonComponent::class)
object WalletConnectModule {
@Provides
@Singleton
fun provideWalletConnectStore(
@NetworkMoshi moshi: Moshi,
@ApplicationContext context: Context,
dispatchers: CoroutineDispatcherProvider,
): WalletConnectStore {
return DefaultWalletConnectStore(
persistenceStore = DataStoreFactory.create(
serializer = MoshiDataStoreSerializer(
moshi = moshi,
types = setTypes<WcSessionDTO>(),
defaultValue = emptySet(),
),
produceFile = { context.dataStoreFile(fileName = "wallet_connect_sessions") },
scope = CoroutineScope(context = dispatchers.io + SupervisorJob()),
),
)
}
}

View file

@ -0,0 +1,23 @@
package com.tangem.datasource.local.walletconnect
import androidx.datastore.core.DataStore
import com.tangem.domain.walletconnect.model.WcSessionDTO
import kotlinx.coroutines.flow.Flow
internal typealias WcSessionCollection = Set<WcSessionDTO>
class DefaultWalletConnectStore(
private val persistenceStore: DataStore<WcSessionCollection>,
) : WalletConnectStore {
override val sessions: Flow<WcSessionCollection>
get() = persistenceStore.data
override suspend fun saveSessions(sessions: WcSessionCollection) {
persistenceStore.updateData { data -> data.plus(sessions) }
}
override suspend fun removeSessions(sessions: WcSessionCollection) {
persistenceStore.updateData { data -> data.minus(sessions) }
}
}

View file

@ -0,0 +1,17 @@
package com.tangem.datasource.local.walletconnect
import com.tangem.domain.walletconnect.model.WcSessionDTO
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first
interface WalletConnectStore {
val sessions: Flow<WcSessionCollection>
suspend fun findSessionByTopic(topic: String) = sessions.first().find { it.topic == topic }
suspend fun saveSessions(sessions: WcSessionCollection)
suspend fun saveSession(session: WcSessionDTO) = saveSessions(setOf(session))
suspend fun removeSessions(sessions: WcSessionCollection)
suspend fun removeSession(session: WcSessionDTO) = removeSessions(setOf(session))
}