Updated on 2026-08-14
This commit is contained in:
parent
9a8ff1830d
commit
a0798cfef2
252 changed files with 247 additions and 5857 deletions
|
|
@ -36,13 +36,4 @@ sealed class AddCustomTokenError(
|
|||
|
||||
object FieldIsEmpty : AddCustomTokenError()
|
||||
object InvalidContractAddress : AddCustomTokenError()
|
||||
object NetworkIsNotSelected : AddCustomTokenError()
|
||||
object InvalidDecimalsCount : AddCustomTokenError()
|
||||
object InvalidDerivationPath : AddCustomTokenError()
|
||||
|
||||
sealed class Warning : AddCustomTokenError() {
|
||||
object PotentialScamToken : Warning()
|
||||
object TokenAlreadyAdded : Warning()
|
||||
object UnsupportedSolanaToken : Warning()
|
||||
}
|
||||
}
|
||||
|
|
@ -3,10 +3,10 @@ package com.tangem.domain.common
|
|||
import com.squareup.moshi.JsonClass
|
||||
import com.tangem.blockchain.common.Blockchain
|
||||
import com.tangem.blockchain.common.Token
|
||||
import com.tangem.blockchain.common.WalletManager
|
||||
import com.tangem.common.extensions.calculateHashCode
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
@Deprecated("Use Network model")
|
||||
data class BlockchainNetwork(
|
||||
val blockchain: Blockchain,
|
||||
val derivationPath: String?,
|
||||
|
|
@ -19,12 +19,6 @@ data class BlockchainNetwork(
|
|||
tokens = emptyList(),
|
||||
)
|
||||
|
||||
fun updateTokens(tokens: List<Token>): BlockchainNetwork {
|
||||
return copy(
|
||||
tokens = (this.tokens + tokens).distinct(),
|
||||
)
|
||||
}
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
if (javaClass != other?.javaClass) return false
|
||||
|
|
@ -32,23 +26,11 @@ data class BlockchainNetwork(
|
|||
other as BlockchainNetwork
|
||||
|
||||
if (blockchain != other.blockchain) return false
|
||||
if (derivationPath != other.derivationPath) return false
|
||||
|
||||
return true
|
||||
return derivationPath == other.derivationPath
|
||||
}
|
||||
|
||||
override fun hashCode(): Int = calculateHashCode(
|
||||
blockchain.hashCode(),
|
||||
derivationPath?.hashCode() ?: 0,
|
||||
)
|
||||
|
||||
companion object {
|
||||
fun fromWalletManager(walletManager: WalletManager): BlockchainNetwork {
|
||||
return BlockchainNetwork(
|
||||
walletManager.wallet.blockchain,
|
||||
walletManager.wallet.publicKey.derivationPath?.rawPath,
|
||||
walletManager.cardTokens.toList(),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,64 +0,0 @@
|
|||
package com.tangem.domain.common
|
||||
|
||||
/**
|
||||
[REDACTED_AUTHOR]
|
||||
*/
|
||||
open class Throttler<T>(
|
||||
private val duration: Long,
|
||||
) : Throttle<T> {
|
||||
|
||||
private val items: MutableMap<T, Long> = mutableMapOf()
|
||||
|
||||
override fun isStillThrottled(item: T): Boolean {
|
||||
val inThrottlingUpTo = items[item] ?: return false
|
||||
val diff = System.currentTimeMillis() - inThrottlingUpTo
|
||||
return diff < 0
|
||||
}
|
||||
|
||||
override fun updateThrottlingTo(item: T): T {
|
||||
val now = System.currentTimeMillis()
|
||||
val throttledUpTo = items[item] ?: 0L
|
||||
if (throttledUpTo == 0L || throttledUpTo < now) {
|
||||
val newTime = now + duration
|
||||
items[item] = newTime
|
||||
}
|
||||
return item
|
||||
}
|
||||
|
||||
open fun clear() {
|
||||
items.clear()
|
||||
}
|
||||
}
|
||||
|
||||
class ThrottlerWithValues<T, V>(
|
||||
duration: Long,
|
||||
) : Throttler<T>(duration), ValuesHolder<T, V> {
|
||||
|
||||
private val valuesHolder: MutableMap<T, V?> = mutableMapOf()
|
||||
|
||||
override fun setValue(item: T, value: V) {
|
||||
valuesHolder[item] = value
|
||||
}
|
||||
|
||||
override fun geValue(item: T): V? = valuesHolder[item]
|
||||
|
||||
override fun remove(item: T) {
|
||||
valuesHolder.remove(item)
|
||||
}
|
||||
|
||||
override fun clear() {
|
||||
valuesHolder.clear()
|
||||
super.clear()
|
||||
}
|
||||
}
|
||||
|
||||
interface Throttle<T> {
|
||||
fun isStillThrottled(item: T): Boolean
|
||||
fun updateThrottlingTo(item: T): T
|
||||
}
|
||||
|
||||
interface ValuesHolder<K, V> {
|
||||
fun setValue(item: K, value: V)
|
||||
fun geValue(item: K): V?
|
||||
fun remove(item: K)
|
||||
}
|
||||
|
|
@ -1,11 +1,7 @@
|
|||
package com.tangem.domain.common.extensions
|
||||
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
/**
|
||||
|
|
@ -13,20 +9,4 @@ import kotlinx.coroutines.withContext
|
|||
*/
|
||||
suspend fun <T> withMainContext(block: suspend CoroutineScope.() -> T): T = withContext(Dispatchers.Main, block)
|
||||
|
||||
suspend fun <T> withIOContext(block: suspend CoroutineScope.() -> T): T = withContext(Dispatchers.IO, block)
|
||||
|
||||
fun <T> debounce(
|
||||
waitMs: Long = 300L,
|
||||
coroutineScope: CoroutineScope,
|
||||
runDestinationOn: CoroutineDispatcher = Dispatchers.Unconfined,
|
||||
destinationFunction: (T) -> Unit,
|
||||
): (T) -> Unit {
|
||||
var debounceJob: Job? = null
|
||||
return { param: T ->
|
||||
debounceJob?.cancel()
|
||||
debounceJob = coroutineScope.launch {
|
||||
delay(waitMs)
|
||||
withContext(runDestinationOn) { destinationFunction(param) }
|
||||
}
|
||||
}
|
||||
}
|
||||
suspend fun <T> withIOContext(block: suspend CoroutineScope.() -> T): T = withContext(Dispatchers.IO, block)
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
package com.tangem.domain.common.extensions
|
||||
|
||||
import com.tangem.common.CompletionResult
|
||||
import com.tangem.common.services.Result
|
||||
|
||||
/**
|
||||
[REDACTED_AUTHOR]
|
||||
*/
|
||||
inline fun <T> Result<T>.successOr(failureClause: (Result.Failure) -> T): T {
|
||||
return when (this) {
|
||||
is Result.Success -> this.data
|
||||
is Result.Failure -> failureClause(this)
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <T> CompletionResult<T>.successOr(failureClause: (CompletionResult.Failure<T>) -> T): T {
|
||||
return when (this) {
|
||||
is CompletionResult.Success -> this.data
|
||||
is CompletionResult.Failure -> failureClause(this)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,53 +0,0 @@
|
|||
package com.tangem.domain.common.util
|
||||
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.collect
|
||||
import kotlinx.coroutines.flow.debounce
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
|
||||
/**
|
||||
[REDACTED_AUTHOR]
|
||||
*/
|
||||
@Suppress("MagicNumber")
|
||||
class ValueDebouncer<T>(
|
||||
private val initialValue: T,
|
||||
private val debounceDuration: Long = 400,
|
||||
private val onValueChanged: (T) -> Unit,
|
||||
private val onEmitValueReceived: (T) -> Unit = {},
|
||||
) {
|
||||
|
||||
var emittedValue: T = initialValue
|
||||
private set
|
||||
var emitsCountBeforeDebounce: Int = 0
|
||||
private set
|
||||
var debounced: T = initialValue
|
||||
private set
|
||||
|
||||
private val debounceScope: CoroutineScope = CoroutineScope(Job() + Dispatchers.Main)
|
||||
private val flow = MutableStateFlow(debounced)
|
||||
|
||||
init {
|
||||
debounceScope.launch {
|
||||
flow.debounce(debounceDuration)
|
||||
.onEach {
|
||||
debounced = it
|
||||
onValueChanged(it)
|
||||
debounceScope.launch {
|
||||
delay(500)
|
||||
emitsCountBeforeDebounce = 0
|
||||
}
|
||||
}
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
fun isDebounced(value: T): Boolean = this.debounced == value
|
||||
|
||||
fun emmit(emmitValue: T) {
|
||||
emitsCountBeforeDebounce++
|
||||
emittedValue = emmitValue
|
||||
onEmitValueReceived.invoke(emmitValue)
|
||||
debounceScope.launch { flow.emit(emmitValue) }
|
||||
}
|
||||
}
|
||||
|
|
@ -1,8 +0,0 @@
|
|||
package com.tangem.domain.redux.state
|
||||
|
||||
/**
|
||||
[REDACTED_AUTHOR]
|
||||
*/
|
||||
interface StringStateConverter<StateHolder> {
|
||||
fun convert(stateHolder: StateHolder): String
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue