ConverterCurrencyViewModel.kt 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package com.mrozon.currencyconverter.presentation
  2. import android.app.Application
  3. import androidx.lifecycle.ViewModel
  4. import androidx.lifecycle.viewModelScope
  5. import com.mrozon.currencyconverter.CoroutineContextDispatchers
  6. import com.mrozon.currencyconverter.MyApp
  7. import com.mrozon.currencyconverter.R
  8. import com.mrozon.currencyconverter.domain.ICalculateCurrencyUseCase
  9. import com.mrozon.currencyconverter.domain.model.Currency
  10. import com.mrozon.currencyconverter.format
  11. import com.mrozon.currencyconverter.presentation.model.CurrencyUI
  12. import dagger.hilt.android.lifecycle.HiltViewModel
  13. import kotlinx.coroutines.flow.Flow
  14. import kotlinx.coroutines.flow.MutableStateFlow
  15. import kotlinx.coroutines.flow.SharingStarted
  16. import kotlinx.coroutines.flow.stateIn
  17. import kotlinx.coroutines.flow.combine
  18. import javax.inject.Inject
  19. @HiltViewModel
  20. class ConverterCurrencyViewModel @Inject constructor(
  21. private val useCase: ICalculateCurrencyUseCase,
  22. private val dispatchers: CoroutineContextDispatchers,
  23. application: Application
  24. ): ViewModel() {
  25. private val rubCharCode = application.getString(R.string.rub_char_code)
  26. private val delChar = application.getString(R.string.buttonDel)
  27. private val comma = application.getString(R.string.buttonComma)
  28. private val flowCurrencies = useCase.loadCurrencies().stateIn(
  29. viewModelScope,
  30. started = SharingStarted.Lazily,
  31. initialValue = emptyList())
  32. private val flowSelected = MutableStateFlow(rubCharCode)
  33. private val flowInputValue = MutableStateFlow("1")
  34. val state: Flow<List<CurrencyUI>> = combine(
  35. flowCurrencies,
  36. flowSelected,
  37. flowInputValue
  38. ) { currencies, selected, inputValue ->
  39. calculateCorrectExchange(currencies, selected, inputValue)
  40. }.stateIn(viewModelScope, started = SharingStarted.Lazily, initialValue = emptyList())
  41. private fun calculateCorrectExchange(
  42. currencies: List<Currency>,
  43. selected: String,
  44. inputValue: String
  45. ): List<CurrencyUI> {
  46. val selectedCurrency = currencies.firstOrNull { it.charCode == selected } ?: return emptyList()
  47. val listCurrencyVH = mutableListOf<CurrencyUI>()
  48. currencies.forEach { currency ->
  49. val total = useCase.convertCurrency(selectedCurrency, currency,
  50. inputValue.replace(',','.').toDouble())
  51. val currencyVH = CurrencyUI(
  52. charCode = currency.charCode,
  53. name = currency.name,
  54. total = if (currency.charCode == selected) inputValue else total.format(MyApp.PRECISION),
  55. selected = currency.charCode == selected
  56. )
  57. listCurrencyVH.add(currencyVH)
  58. }
  59. return listCurrencyVH.toList()
  60. }
  61. fun selectCurrency(charCode: String) {
  62. flowSelected.value = charCode
  63. flowInputValue.value = "1"
  64. }
  65. fun input(text: String) {
  66. val inputValue = flowInputValue.value
  67. when (text) {
  68. delChar -> {
  69. flowInputValue.value = if (inputValue.length==1) "0" else inputValue.dropLast(1)
  70. }
  71. comma -> {
  72. if (!inputValue.contains(comma))
  73. flowInputValue.value = inputValue+text
  74. }
  75. else -> {
  76. flowInputValue.value = if (inputValue=="0") text else inputValue+text
  77. }
  78. }
  79. }
  80. }