-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix Key [CurrencyCode] was already used
java.lang.IllegalArgumentException: Key TRX was already used. If you are using LazyColumn/Row please make sure you provide a unique key for each item.
- Loading branch information
Showing
4 changed files
with
50 additions
and
27 deletions.
There are no files selected for viewing
58 changes: 35 additions & 23 deletions
58
app/src/main/java/space/taran/arkrate/data/assets/AssetsRepo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,60 @@ | ||
package space.taran.arkrate.data.assets | ||
|
||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.MutableSharedFlow | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.SharedFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.withContext | ||
import space.taran.arkrate.data.CurrencyAmount | ||
import space.taran.arkrate.data.db.AssetsDao | ||
import space.taran.arkrate.data.db.AssetsLocalDataSource | ||
import space.taran.arkrate.utils.replace | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class AssetsRepo @Inject constructor( | ||
private val local: AssetsLocalDataSource | ||
) { | ||
private val codeToAmount = mutableListOf<CurrencyAmount>() | ||
private val codeToAmountFlow = MutableSharedFlow<List<CurrencyAmount>>() | ||
private var currencyAmountList = listOf<CurrencyAmount>() | ||
private val currencyAmountFlow = | ||
MutableStateFlow<List<CurrencyAmount>>(emptyList()) | ||
private val scope = CoroutineScope(Dispatchers.IO) | ||
|
||
suspend fun init() = withContext(Dispatchers.IO) { | ||
codeToAmount.addAll(local.getAll()) | ||
codeToAmountFlow.emit(codeToAmount) | ||
init { | ||
scope.launch { | ||
currencyAmountList = local.getAll() | ||
currencyAmountFlow.emit(currencyAmountList) | ||
} | ||
} | ||
|
||
fun allCurrencyAmount() = codeToAmount | ||
fun allCurrencyAmount(): List<CurrencyAmount> = currencyAmountList | ||
|
||
fun allCurrencyAmountFlow(): SharedFlow<List<CurrencyAmount>?> = codeToAmountFlow | ||
fun allCurrencyAmountFlow(): StateFlow<List<CurrencyAmount>> = currencyAmountFlow | ||
|
||
suspend fun setCurrencyAmount(code: String, amount: Double) { | ||
codeToAmount.find { | ||
it.code == code | ||
}?.let { | ||
it.amount = amount | ||
} ?: let { | ||
codeToAmount.add(CurrencyAmount(code, amount)) | ||
suspend fun setCurrencyAmount(code: String, amount: Double) = | ||
withContext(Dispatchers.IO) { | ||
currencyAmountList.find { | ||
it.code == code | ||
}?.let { currencyAmount -> | ||
currencyAmountList = currencyAmountList.replace( | ||
currencyAmount, | ||
currencyAmount.copy(amount = amount) | ||
) | ||
} ?: let { | ||
currencyAmountList = | ||
currencyAmountList + CurrencyAmount(code, amount) | ||
} | ||
currencyAmountFlow.emit(currencyAmountList.toList()) | ||
local.insert(CurrencyAmount(code, amount)) | ||
} | ||
codeToAmountFlow.emit(codeToAmount) | ||
local.insert(CurrencyAmount(code, amount)) | ||
} | ||
|
||
suspend fun removeCurrency(code: String) { | ||
codeToAmount.find { it.code == code }?.let { | ||
codeToAmount.remove(it) | ||
suspend fun removeCurrency(code: String) = withContext(Dispatchers.IO) { | ||
currencyAmountList.find { it.code == code }?.let { | ||
currencyAmountList = currencyAmountList - it | ||
} | ||
codeToAmountFlow.emit(codeToAmount) | ||
currencyAmountFlow.emit(currencyAmountList) | ||
local.delete(code) | ||
} | ||
} |
5 changes: 4 additions & 1 deletion
5
app/src/main/java/space/taran/arkrate/presentation/assets/AssetsScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
app/src/main/java/space/taran/arkrate/utils/CollectionsUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package space.taran.arkrate.utils | ||
|
||
fun <T> List<T>.replace(targetItem: T, newItem: T) = map { | ||
if (it == targetItem) { | ||
newItem | ||
} else { | ||
it | ||
} | ||
} |