Updated on 2026-08-14
This commit is contained in:
parent
2002834e57
commit
8bfc3d7f8c
16 changed files with 182 additions and 131 deletions
|
|
@ -8,6 +8,7 @@ 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.WcPendingApprovalSessionDTO
|
||||
import com.tangem.domain.walletconnect.model.WcSessionDTO
|
||||
import com.tangem.utils.coroutines.CoroutineDispatcherProvider
|
||||
import dagger.Module
|
||||
|
|
@ -30,6 +31,7 @@ object WalletConnectModule {
|
|||
@ApplicationContext context: Context,
|
||||
dispatchers: CoroutineDispatcherProvider,
|
||||
): WalletConnectStore {
|
||||
val scope = CoroutineScope(context = dispatchers.io + SupervisorJob())
|
||||
return DefaultWalletConnectStore(
|
||||
persistenceStore = DataStoreFactory.create(
|
||||
serializer = MoshiDataStoreSerializer(
|
||||
|
|
@ -38,7 +40,16 @@ object WalletConnectModule {
|
|||
defaultValue = emptySet(),
|
||||
),
|
||||
produceFile = { context.dataStoreFile(fileName = "wallet_connect_sessions") },
|
||||
scope = CoroutineScope(context = dispatchers.io + SupervisorJob()),
|
||||
scope = scope,
|
||||
),
|
||||
pendingApprovalSessionsStore = DataStoreFactory.create(
|
||||
serializer = MoshiDataStoreSerializer(
|
||||
moshi = moshi,
|
||||
types = setTypes<WcPendingApprovalSessionDTO>(),
|
||||
defaultValue = emptySet(),
|
||||
),
|
||||
produceFile = { context.dataStoreFile(fileName = "wallet_connect_pending_approval_sessions") },
|
||||
scope = scope,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,18 +1,39 @@
|
|||
package com.tangem.datasource.local.walletconnect
|
||||
|
||||
import androidx.datastore.core.DataStore
|
||||
import com.tangem.domain.walletconnect.model.WcPendingApprovalSessionDTO
|
||||
import com.tangem.domain.walletconnect.model.WcSessionDTO
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||
import kotlinx.coroutines.flow.transform
|
||||
import org.joda.time.DateTime
|
||||
|
||||
internal typealias WcSessionCollection = Set<WcSessionDTO>
|
||||
|
||||
internal class DefaultWalletConnectStore(
|
||||
private val persistenceStore: DataStore<WcSessionCollection>,
|
||||
private val pendingApprovalSessionsStore: DataStore<Set<WcPendingApprovalSessionDTO>>,
|
||||
) : WalletConnectStore {
|
||||
|
||||
override val sessions: Flow<WcSessionCollection>
|
||||
get() = persistenceStore.data
|
||||
|
||||
override val pendingApproval: Flow<Set<WcPendingApprovalSessionDTO>>
|
||||
get() = pendingApprovalSessionsStore.data
|
||||
.transform { pendingApprovalSet ->
|
||||
val now = DateTime.now()
|
||||
val expired = pendingApprovalSet
|
||||
.filterTo(mutableSetOf()) { it.expiredTime < now.millis }
|
||||
val shouldSomeClear = expired.isNotEmpty()
|
||||
val actualData = if (shouldSomeClear) {
|
||||
removePendingApproval(expired)
|
||||
} else {
|
||||
pendingApprovalSet
|
||||
}
|
||||
emit(actualData)
|
||||
}
|
||||
.distinctUntilChanged()
|
||||
|
||||
override suspend fun saveSessions(sessions: WcSessionCollection) {
|
||||
persistenceStore.updateData { data -> data.plus(sessions) }
|
||||
}
|
||||
|
|
@ -20,4 +41,16 @@ internal class DefaultWalletConnectStore(
|
|||
override suspend fun removeSessions(sessions: WcSessionCollection) {
|
||||
persistenceStore.updateData { data -> data.minus(sessions) }
|
||||
}
|
||||
|
||||
override suspend fun savePendingApproval(
|
||||
sessions: Set<WcPendingApprovalSessionDTO>,
|
||||
): Set<WcPendingApprovalSessionDTO> {
|
||||
return pendingApprovalSessionsStore.updateData { data -> data.plus(sessions) }
|
||||
}
|
||||
|
||||
override suspend fun removePendingApproval(
|
||||
sessions: Set<WcPendingApprovalSessionDTO>,
|
||||
): Set<WcPendingApprovalSessionDTO> {
|
||||
return pendingApprovalSessionsStore.updateData { data -> data.minus(sessions) }
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
package com.tangem.datasource.local.walletconnect
|
||||
|
||||
import com.tangem.domain.walletconnect.model.WcPendingApprovalSessionDTO
|
||||
import com.tangem.domain.walletconnect.model.WcSessionDTO
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.first
|
||||
|
|
@ -7,6 +8,8 @@ import kotlinx.coroutines.flow.first
|
|||
interface WalletConnectStore {
|
||||
|
||||
val sessions: Flow<WcSessionCollection>
|
||||
val pendingApproval: Flow<Set<WcPendingApprovalSessionDTO>>
|
||||
|
||||
suspend fun findSessionByTopic(topic: String) = sessions.first().find { it.topic == topic }
|
||||
|
||||
suspend fun saveSessions(sessions: WcSessionCollection)
|
||||
|
|
@ -14,4 +17,7 @@ interface WalletConnectStore {
|
|||
|
||||
suspend fun removeSessions(sessions: WcSessionCollection)
|
||||
suspend fun removeSession(session: WcSessionDTO) = removeSessions(setOf(session))
|
||||
|
||||
suspend fun savePendingApproval(sessions: Set<WcPendingApprovalSessionDTO>): Set<WcPendingApprovalSessionDTO>
|
||||
suspend fun removePendingApproval(sessions: Set<WcPendingApprovalSessionDTO>): Set<WcPendingApprovalSessionDTO>
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue