From fd3d95097fe1456c510bb0da703109a0a9695eed Mon Sep 17 00:00:00 2001 From: Tangem Date: Tue, 5 Aug 2025 15:45:35 +0400 Subject: [PATCH] Updated on 2026-08-14 --- data/account/.gitignore | 1 + data/account/build.gradle.kts | 37 ++++++++++++++ .../data/account/di/AccountDataModule.kt | 25 ++++++++++ .../DefaultAccountsCRUDRepository.kt | 47 ++++++++++++++++++ .../repository/AccountsCRUDRepository.kt | 49 +++++++++++++++++++ settings.gradle.kts | 1 + 6 files changed, 160 insertions(+) create mode 100644 data/account/.gitignore create mode 100644 data/account/build.gradle.kts create mode 100644 data/account/src/main/kotlin/com/tangem/data/account/di/AccountDataModule.kt create mode 100644 data/account/src/main/kotlin/com/tangem/data/account/repository/DefaultAccountsCRUDRepository.kt create mode 100644 domain/account/src/main/java/com/tangem/domain/account/repository/AccountsCRUDRepository.kt diff --git a/data/account/.gitignore b/data/account/.gitignore new file mode 100644 index 0000000000..42afabfd2a --- /dev/null +++ b/data/account/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/data/account/build.gradle.kts b/data/account/build.gradle.kts new file mode 100644 index 0000000000..464ba8486e --- /dev/null +++ b/data/account/build.gradle.kts @@ -0,0 +1,37 @@ +plugins { + alias(deps.plugins.android.library) + alias(deps.plugins.kotlin.android) + alias(deps.plugins.kotlin.kapt) + id("configuration") +} + +android { + namespace = "com.tangem.data.account" +} + +dependencies { + + // region Project - Core + api(projects.core.utils) + // endregion + + // region Project - Domain + api(projects.domain.account) + api(projects.domain.models) + // endregion + + // Project - Data + implementation(projects.core.datasource) + // endregion + + // region DI + implementation(deps.hilt.core) + kapt(deps.hilt.kapt) + // endregion + + // region Other Dependencies + implementation(deps.arrow.core) + implementation(deps.kotlin.coroutines) + implementation(deps.timber) + // endregion +} \ No newline at end of file diff --git a/data/account/src/main/kotlin/com/tangem/data/account/di/AccountDataModule.kt b/data/account/src/main/kotlin/com/tangem/data/account/di/AccountDataModule.kt new file mode 100644 index 0000000000..e05c402f68 --- /dev/null +++ b/data/account/src/main/kotlin/com/tangem/data/account/di/AccountDataModule.kt @@ -0,0 +1,25 @@ +package com.tangem.data.account.di + +import com.tangem.data.account.repository.DefaultAccountsCRUDRepository +import com.tangem.datasource.local.datastore.RuntimeSharedStore +import com.tangem.datasource.local.userwallet.UserWalletsStore +import com.tangem.domain.account.repository.AccountsCRUDRepository +import dagger.Module +import dagger.Provides +import dagger.hilt.InstallIn +import dagger.hilt.components.SingletonComponent +import javax.inject.Singleton + +@Module +@InstallIn(SingletonComponent::class) +internal object AccountDataModule { + + @Provides + @Singleton + fun provideAccountsCRUDRepository(userWalletsStore: UserWalletsStore): AccountsCRUDRepository { + return DefaultAccountsCRUDRepository( + runtimeStore = RuntimeSharedStore(), + userWalletsStore = userWalletsStore, + ) + } +} \ No newline at end of file diff --git a/data/account/src/main/kotlin/com/tangem/data/account/repository/DefaultAccountsCRUDRepository.kt b/data/account/src/main/kotlin/com/tangem/data/account/repository/DefaultAccountsCRUDRepository.kt new file mode 100644 index 0000000000..76d86bd0fe --- /dev/null +++ b/data/account/src/main/kotlin/com/tangem/data/account/repository/DefaultAccountsCRUDRepository.kt @@ -0,0 +1,47 @@ +package com.tangem.data.account.repository + +import arrow.core.Option +import arrow.core.Option.Companion.catch +import arrow.core.none +import com.tangem.datasource.local.datastore.RuntimeSharedStore +import com.tangem.datasource.local.userwallet.UserWalletsStore +import com.tangem.domain.account.models.AccountList +import com.tangem.domain.account.repository.AccountsCRUDRepository +import com.tangem.domain.models.account.Account +import com.tangem.domain.models.account.AccountId +import com.tangem.domain.models.wallet.UserWallet +import com.tangem.domain.models.wallet.UserWalletId +import com.tangem.utils.extensions.addOrReplace + +/** +[REDACTED_AUTHOR] + */ +// TODO: [REDACTED_JIRA] +internal class DefaultAccountsCRUDRepository( + private val runtimeStore: RuntimeSharedStore>, + private val userWalletsStore: UserWalletsStore, +) : AccountsCRUDRepository { + + override suspend fun getAccounts(userWalletId: UserWalletId): Option = catch { + runtimeStore.getSyncOrNull() + ?.firstOrNull { it.userWallet.walletId == userWalletId } + ?: return none() + } + + override suspend fun getAccount(accountId: AccountId): Option = catch { + runtimeStore.getSyncOrNull().orEmpty() + .flatMap { it.accounts } + .firstOrNull { it.accountId == accountId } as? Account.CryptoPortfolio + ?: return none() + } + + override suspend fun saveAccounts(accountList: AccountList) { + runtimeStore.update(emptyList()) { + it.addOrReplace(accountList) { it.userWallet.walletId == accountList.userWallet.walletId } + } + } + + override fun getUserWallet(userWalletId: UserWalletId): UserWallet { + return userWalletsStore.getSyncStrict(userWalletId) + } +} \ No newline at end of file diff --git a/domain/account/src/main/java/com/tangem/domain/account/repository/AccountsCRUDRepository.kt b/domain/account/src/main/java/com/tangem/domain/account/repository/AccountsCRUDRepository.kt new file mode 100644 index 0000000000..70b8543734 --- /dev/null +++ b/domain/account/src/main/java/com/tangem/domain/account/repository/AccountsCRUDRepository.kt @@ -0,0 +1,49 @@ +package com.tangem.domain.account.repository + +import arrow.core.Option +import com.tangem.domain.account.models.AccountList +import com.tangem.domain.models.account.Account +import com.tangem.domain.models.account.AccountId +import com.tangem.domain.models.wallet.UserWallet +import com.tangem.domain.models.wallet.UserWalletId + +/** + * Repository interface for performing CRUD operations on accounts + * +[REDACTED_AUTHOR] + */ +interface AccountsCRUDRepository { + + /** + * Retrieves a list of accounts associated with a specific user wallet + * + * @param userWalletId the unique identifier of the user wallet + * @return an [Option] containing the [AccountList] if found, or `Option.None` if not + */ + suspend fun getAccounts(userWalletId: UserWalletId): Option + + /** + * Retrieves a specific account by its unique identifier + * + * @param accountId the unique identifier of the account + * @return an [Option] containing the [Account.CryptoPortfolio] if found, or `Option.None` if not + */ + suspend fun getAccount(accountId: AccountId): Option + + /** + * Saves a list of accounts to the repository + * + * @param accountList the list of accounts to be saved. + */ + @Throws + suspend fun saveAccounts(accountList: AccountList) + + /** + * Retrieves a user wallet by its unique identifier + * + * @param userWalletId the unique identifier of the user wallet + * @return the [UserWallet] associated with the given identifier + */ + @Throws + fun getUserWallet(userWalletId: UserWalletId): UserWallet +} \ No newline at end of file diff --git a/settings.gradle.kts b/settings.gradle.kts index de1aaf92ce..bab69533de 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -334,6 +334,7 @@ include(":domain:wallet-manager:models") // endregion Domain modules // region Data modules +include(":data:account") include(":data:app-currency") include(":data:app-theme") include(":data:balance-hiding")