Updated on 2026-08-14

This commit is contained in:
Tangem 2023-08-03 16:28:14 +03:00
parent cd78832ff1
commit c4fcd17bef
2 changed files with 18 additions and 4 deletions

View file

@ -1,7 +1,7 @@
package com.tangem.tap.domain.configurable.warningMessage
import com.tangem.blockchain.common.Blockchain
import com.tangem.utils.extensions.removeBy
import com.tangem.utils.extensions.removeByReplace
import com.tangem.wallet.R
import java.util.concurrent.CopyOnWriteArrayList
@ -44,12 +44,12 @@ class WarningMessagesManager {
}
fun removeWarnings(origin: WarningMessage.Origin) {
warningsList.removeBy { it.origin == origin }
warningsList.removeByReplace { it.origin == origin }
sortByPriority()
}
fun removeWarnings(messageRes: Int) {
warningsList.removeBy { it.messageResId == messageRes }
warningsList.removeByReplace { it.messageResId == messageRes }
}
fun containsWarning(warning: WarningMessage) = warning in warningsList

View file

@ -53,11 +53,12 @@ inline fun <T> MutableCollection<T>.addOrReplace(item: T, predicate: (T) -> Bool
/**
* Removes an element from the collection based on the provided predicate.
* Uses iterator, avoid using it in COW collections
*
* @param predicate The condition to remove an element.
* @return [Boolean] indicating whether an element was removed.
*/
inline fun <T> MutableCollection<T>.removeBy(predicate: (T) -> Boolean): Boolean {
inline fun <T> MutableCollection<T>.removeByIterate(predicate: (T) -> Boolean): Boolean {
var removed = false
val iterator = this.iterator()
@ -73,6 +74,19 @@ inline fun <T> MutableCollection<T>.removeBy(predicate: (T) -> Boolean): Boolean
return removed
}
/**
* Removes an element from the collection based on the provided predicate.
* Uses removeAll() method and could be used for COW collections
*
* @param predicate The condition to remove an element.
* @return [Boolean] indicating whether an element was removed.
*/
fun <T> MutableList<T>.removeByReplace(predicate: (T) -> Boolean): Boolean {
val toRemove = this.filter(predicate)
this.removeAll(toRemove)
return toRemove.isNotEmpty()
}
/**
* Replaces an element in the collection with the provided item based on the predicate.
*