Skip to content

Commit

Permalink
fix Key [CurrencyCode] was already used
Browse files Browse the repository at this point in the history
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
mdrlzy committed Nov 7, 2022
1 parent cf4b98b commit c6f852f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 27 deletions.
58 changes: 35 additions & 23 deletions app/src/main/java/space/taran/arkrate/data/assets/AssetsRepo.kt
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)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package space.taran.arkrate.presentation.assets

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.text.KeyboardOptions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package space.taran.arkrate.presentation.assets

import android.util.Log
import androidx.compose.runtime.mutableStateListOf
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
Expand All @@ -17,11 +18,9 @@ class AssetsViewModel(

init {
viewModelScope.launch {
assetsRepo.init()
currencyAmountList.addAll(assetsRepo.allCurrencyAmount())
assetsRepo.allCurrencyAmountFlow().collect {
currencyAmountList.clear()
currencyAmountList.addAll(it!!)
currencyAmountList.addAll(it)
}
}
}
Expand Down
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
}
}

0 comments on commit c6f852f

Please sign in to comment.