Updated on 2026-08-14

This commit is contained in:
Tangem 2026-03-24 13:11:50 +01:00
parent b74acabcc4
commit 4649abbb60
21 changed files with 627 additions and 0 deletions

1
domain/search/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/build

View file

@ -0,0 +1,20 @@
plugins {
alias(deps.plugins.android.library)
alias(deps.plugins.kotlin.android)
id("configuration")
}
android {
namespace = "com.tangem.domain.search"
}
dependencies {
api(projects.domain.core)
api(projects.domain.models)
implementation(projects.domain.common)
implementation(projects.domain.markets.models)
implementation(projects.domain.wallets)
implementation(projects.domain.appCurrency)
implementation(projects.domain.account)
implementation(projects.domain.account.status)
}

View file

@ -0,0 +1,14 @@
package com.tangem.domain.search.model
import com.tangem.domain.models.currency.CryptoCurrency
/**
* @property timestamp epoch milliseconds
*/
data class RecentSearchToken(
val id: CryptoCurrency.RawID,
val name: String,
val symbol: String,
val imageUrl: String?,
val timestamp: Long,
)

View file

@ -0,0 +1,10 @@
package com.tangem.domain.search.model
import com.tangem.domain.markets.TokenMarket
data class SearchResult(
val textHints: List<SearchTextHint>,
val recentTokens: List<RecentSearchToken>,
val userAssets: List<UserAssetSearchEntry>,
val marketTokens: List<TokenMarket>,
)

View file

@ -0,0 +1,6 @@
package com.tangem.domain.search.model
/**
* @property timestamp epoch milliseconds
*/
data class SearchTextHint(val text: String, val timestamp: Long)

View file

@ -0,0 +1,14 @@
package com.tangem.domain.search.model
import com.tangem.domain.models.account.AccountId
import com.tangem.domain.models.account.AccountName
import com.tangem.domain.models.currency.CryptoCurrencyStatus
import com.tangem.domain.models.wallet.UserWalletId
data class UserAssetSearchEntry(
val userWalletId: UserWalletId,
val userWalletName: String,
val accountId: AccountId,
val accountName: AccountName,
val currencyStatus: CryptoCurrencyStatus,
)

View file

@ -0,0 +1,39 @@
package com.tangem.domain.search.repository
import com.tangem.domain.search.model.RecentSearchToken
import com.tangem.domain.search.model.SearchTextHint
import kotlinx.coroutines.flow.Flow
/**
* Repository responsible for managing local search history storage.
* Handles persistence of user's past search queries and recently viewed market tokens.
* Each history type is limited to 3 entries, sorted by timestamp in descending order.
*/
interface SearchRepository {
/** Observes the list of saved text hints, sorted by timestamp descending. */
fun getTextHints(): Flow<List<SearchTextHint>>
/** Observes the list of recently viewed market tokens, sorted by timestamp descending. */
fun getRecentTokens(): Flow<List<RecentSearchToken>>
/**
* Saves a text hint to the search history.
* If the hint already exists, its timestamp is updated. Oldest entries are evicted when the limit is exceeded.
*
* @param text the search query text to save
*/
suspend fun saveTextHint(text: String)
/**
* Saves a recently viewed market token to the search history.
* If a token with the same ID already exists, it is moved to the top. Oldest entries are evicted when the limit
* is exceeded.
*
* @param token the market token entry to save
*/
suspend fun saveRecentToken(token: RecentSearchToken)
/** Clears all search history, including both text hints and recent tokens. */
suspend fun clearHistory()
}

View file

@ -0,0 +1,17 @@
package com.tangem.domain.search.usecase
import com.tangem.domain.search.repository.SearchRepository
/**
* Clears the entire search history, removing both text hints and recently viewed tokens.
*
* @property searchRepository local search history storage
*/
class ClearSearchHistoryUseCase(
private val searchRepository: SearchRepository,
) {
suspend operator fun invoke() {
searchRepository.clearHistory()
}
}

View file

@ -0,0 +1,126 @@
package com.tangem.domain.search.usecase
import com.tangem.domain.account.models.AccountStatusList
import com.tangem.domain.account.status.supplier.MultiAccountStatusListSupplier
import com.tangem.domain.common.wallets.UserWalletsListRepository
import com.tangem.domain.markets.TokenMarket
import com.tangem.domain.models.account.filterCryptoPortfolio
import com.tangem.domain.models.wallet.UserWallet
import com.tangem.domain.models.wallet.UserWalletId
import com.tangem.domain.models.wallet.isLocked
import com.tangem.domain.search.model.SearchResult
import com.tangem.domain.search.model.UserAssetSearchEntry
import com.tangem.domain.search.repository.SearchRepository
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.flowOf
/**
* Primary search use case that produces [SearchResult] based on the current query.
*
* Behavior depends on the query:
* - **Empty query** returns search history: text hints and recently viewed tokens.
* - **Non-empty query** performs the search across all unlocked user wallets,
* matching currencies by name or symbol, and combines the results with externally provided market tokens.
*
* @property searchRepository local search history storage
* @property multiAccountStatusListSupplier supplier for loaded account status lists across all wallets
* @property userWalletsListRepository repository providing the list of user wallets
*/
class GetSearchResultsUseCase(
private val searchRepository: SearchRepository,
private val multiAccountStatusListSupplier: MultiAccountStatusListSupplier,
private val userWalletsListRepository: UserWalletsListRepository,
) {
/**
* Produces a [Flow] of [SearchResult] for the given [query].
*
* @param query the search query string; blank means "show history"
* @param marketTokens external flow of market token search results (provided by presentation layer)
*/
operator fun invoke(
query: String,
marketTokens: Flow<List<TokenMarket>> = flowOf(emptyList()),
): Flow<SearchResult> {
return if (query.isBlank()) {
observeHistory()
} else {
searchAssets(query, marketTokens)
}
}
private fun observeHistory(): Flow<SearchResult> {
return combine(
searchRepository.getTextHints(),
searchRepository.getRecentTokens(),
) { hints, tokens ->
SearchResult(
textHints = hints,
recentTokens = tokens,
userAssets = emptyList(),
marketTokens = emptyList(),
)
}
}
private fun searchAssets(query: String, marketTokens: Flow<List<TokenMarket>>): Flow<SearchResult> {
return combine(
observeUserAssets(query),
marketTokens,
) { userAssets, markets ->
SearchResult(
textHints = emptyList(),
recentTokens = emptyList(),
userAssets = userAssets,
marketTokens = markets,
)
}
}
private fun observeUserAssets(query: String): Flow<List<UserAssetSearchEntry>> {
val lowerQuery = query.lowercase()
return combine(
multiAccountStatusListSupplier(),
userWalletsListRepository.userWallets,
) { statusLists, wallets ->
val unlockedWallets = wallets
.orEmpty()
.filterNot(UserWallet::isLocked)
.associateBy { it.walletId }
if (unlockedWallets.isEmpty()) return@combine emptyList()
statusLists
.filter { it.userWalletId in unlockedWallets }
.flatMap { statusList -> extractMatchingAssets(statusList, unlockedWallets, lowerQuery) }
}
}
private fun extractMatchingAssets(
statusList: AccountStatusList,
wallets: Map<UserWalletId, UserWallet>,
lowerQuery: String,
): List<UserAssetSearchEntry> {
val wallet = wallets[statusList.userWalletId] ?: return emptyList()
return statusList.accountStatuses
.filterCryptoPortfolio()
.flatMap { accountStatus ->
accountStatus.flattenCurrencies()
.filter { currencyStatus ->
val name = currencyStatus.currency.name.lowercase()
val symbol = currencyStatus.currency.symbol.lowercase()
name.contains(lowerQuery) || symbol.contains(lowerQuery)
}
.map { currencyStatus ->
UserAssetSearchEntry(
userWalletId = statusList.userWalletId,
userWalletName = wallet.name,
accountId = accountStatus.accountId,
accountName = accountStatus.account.accountName,
currencyStatus = currencyStatus,
)
}
}
}
}

View file

@ -0,0 +1,23 @@
package com.tangem.domain.search.usecase
import com.tangem.domain.search.model.RecentSearchToken
import com.tangem.domain.search.repository.SearchRepository
/**
* Saves a market token to the "recently viewed" search history.
* Only market assets should be saved (user's own assets are ignored).
* The history is limited to 3 entries; oldest entries are evicted automatically.
*
* @property searchRepository local search history storage
*/
class SaveRecentSearchTokenUseCase(
private val searchRepository: SearchRepository,
) {
/**
* @param token the market token to persist as a recent search entry
*/
suspend operator fun invoke(token: RecentSearchToken) {
searchRepository.saveRecentToken(token)
}
}

View file

@ -0,0 +1,23 @@
package com.tangem.domain.search.usecase
import com.tangem.domain.search.repository.SearchRepository
/**
* Saves the current search query text to the local search history.
* Blank queries are ignored. The history is limited to 3 entries; oldest entries are evicted automatically.
* Should be called when the user selects any asset from the search results.
*
* @property searchRepository local search history storage
*/
class SaveSearchQueryUseCase(
private val searchRepository: SearchRepository,
) {
/**
* @param query the search text to persist; blank values are silently ignored
*/
suspend operator fun invoke(query: String) {
if (query.isBlank()) return
searchRepository.saveTextHint(query.trim())
}
}