diff --git a/app/src/main/java/space/taran/arkrate/data/assets/AssetsRepo.kt b/app/src/main/java/space/taran/arkrate/data/assets/AssetsRepo.kt index 9aab2f762..80a00d9ae 100644 --- a/app/src/main/java/space/taran/arkrate/data/assets/AssetsRepo.kt +++ b/app/src/main/java/space/taran/arkrate/data/assets/AssetsRepo.kt @@ -1,12 +1,15 @@ 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 @@ -14,35 +17,44 @@ import javax.inject.Singleton class AssetsRepo @Inject constructor( private val local: AssetsLocalDataSource ) { - private val codeToAmount = mutableListOf() - private val codeToAmountFlow = MutableSharedFlow>() + private var currencyAmountList = listOf() + private val currencyAmountFlow = + MutableStateFlow>(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 = currencyAmountList - fun allCurrencyAmountFlow(): SharedFlow?> = codeToAmountFlow + fun allCurrencyAmountFlow(): StateFlow> = 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) } } \ No newline at end of file diff --git a/app/src/main/java/space/taran/arkrate/presentation/assets/AssetsScreen.kt b/app/src/main/java/space/taran/arkrate/presentation/assets/AssetsScreen.kt index 499aae380..e78c8f84a 100644 --- a/app/src/main/java/space/taran/arkrate/presentation/assets/AssetsScreen.kt +++ b/app/src/main/java/space/taran/arkrate/presentation/assets/AssetsScreen.kt @@ -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 diff --git a/app/src/main/java/space/taran/arkrate/presentation/assets/AssetsViewModel.kt b/app/src/main/java/space/taran/arkrate/presentation/assets/AssetsViewModel.kt index e38b4a7f3..5e06cb60f 100644 --- a/app/src/main/java/space/taran/arkrate/presentation/assets/AssetsViewModel.kt +++ b/app/src/main/java/space/taran/arkrate/presentation/assets/AssetsViewModel.kt @@ -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 @@ -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) } } } diff --git a/app/src/main/java/space/taran/arkrate/utils/CollectionsUtils.kt b/app/src/main/java/space/taran/arkrate/utils/CollectionsUtils.kt new file mode 100644 index 000000000..14f333c0b --- /dev/null +++ b/app/src/main/java/space/taran/arkrate/utils/CollectionsUtils.kt @@ -0,0 +1,9 @@ +package space.taran.arkrate.utils + +fun List.replace(targetItem: T, newItem: T) = map { + if (it == targetItem) { + newItem + } else { + it + } +} \ No newline at end of file