فهرست منبع

correct input flow

MrOzOn 4 سال پیش
والد
کامیت
ae1e57dda4

+ 1 - 10
app/src/main/java/com/mrozon/currencyconverter/domain/CalculateCurrencyUseCase.kt

@@ -34,16 +34,7 @@ class CalculateCurrencyUseCase @Inject constructor(
         from: Currency,
         to: Currency,
         value: Double
-    ) =
-//        return if (to.charCode==rub.charCode) {
-//            //  переводим в рубли любую валюту
-//            value * from.value
-//        } else {
-//            //  переводим в другую валюту
-//            (from.value * value) / to.value
-//        }
-
-            from.value * value / to.value
+    ) =  from.value * value / to.value
 
 
 

+ 2 - 2
app/src/main/java/com/mrozon/currencyconverter/presentation/BindingUtils.kt

@@ -9,11 +9,11 @@ fun TextView.showTotal(currencyUI: CurrencyUI) {
 //    text = currencyUI.total.format(4)
     if(currencyUI.selected) {
         setTextColor(Color.MAGENTA)
-        text = currencyUI.total.format(4)
+        text = currencyUI.total
     } else {
         setTextColor(Color.BLACK)
         text = currencyUI.total.format(4)
     }
 }
 
-fun Double.format(digits: Int) = "%.${digits}f".format(this).trimEnd('0')//.trim('.')
+fun Double.format(digits: Int) = "%.${digits}f".format(this).trimEnd('0').trim('.')

+ 13 - 11
app/src/main/java/com/mrozon/currencyconverter/presentation/ConverterCurrencyViewModel.kt

@@ -27,7 +27,7 @@ class ConverterCurrencyViewModel @Inject constructor(
         started = SharingStarted.Lazily,
         initialValue = emptyList())
     private val flowSelected = MutableStateFlow(rubCharCode)
-    private val flowInputValue = MutableStateFlow(1.0)
+    private val flowInputValue = MutableStateFlow("1")
 
     val state: Flow<List<CurrencyUI>> = combine(
         flowCurrencies,
@@ -40,17 +40,18 @@ class ConverterCurrencyViewModel @Inject constructor(
     private fun calculateCorrectExchange(
         currencies: List<Currency>,
         selected: String,
-        inputValue: Double
+        inputValue: String
     ): List<CurrencyUI> {
         val selectedCurrency = currencies.firstOrNull { it.charCode == selected } ?: return emptyList()
 
         val listCurrencyVH = mutableListOf<CurrencyUI>()
         currencies.forEach { currency ->
-            val total = useCase.convertCurrency(selectedCurrency, currency, inputValue)
+            val total = useCase.convertCurrency(selectedCurrency, currency, inputValue.toDouble())
+
             val currencyVH = CurrencyUI(
                 charCode = currency.charCode,
                 name = currency.name,
-                total = total,
+                total = if (currency.charCode == selected) inputValue else total.format(4),
                 selected = currency.charCode == selected
             )
             listCurrencyVH.add(currencyVH)
@@ -60,7 +61,7 @@ class ConverterCurrencyViewModel @Inject constructor(
 
     fun selectCurrency(charCode: String) {
         flowSelected.value = charCode
-        flowInputValue.value = 1.0
+        flowInputValue.value = "1"
     }
 
     fun input(text: String) {
@@ -68,18 +69,19 @@ class ConverterCurrencyViewModel @Inject constructor(
             delChar -> {
                 val v: String = flowInputValue.value.format(4)
                 if(v.length==1) {
-                    flowInputValue.value = 0.0
+                    flowInputValue.value = "0"
                 } else {
-                    flowInputValue.value = v.dropLast(1).toDouble()
+                    flowInputValue.value = v.dropLast(1)
                 }
             }
             comma -> {
-                val v: Double = flowInputValue.value
-                flowInputValue.value = (v.format(4)+text).toDouble()
+                val v = flowInputValue.value
+                if (!v.contains(comma))
+                    flowInputValue.value = v+text
             }
             else -> {
-                val v: Double = flowInputValue.value
-                flowInputValue.value = (v.format(4)+text).toDouble()
+                val v = flowInputValue.value
+                flowInputValue.value = if (v=="0") text else v+text
             }
         }
     }

+ 1 - 1
app/src/main/java/com/mrozon/currencyconverter/presentation/CurrencyUI.kt

@@ -3,6 +3,6 @@ package com.mrozon.currencyconverter.presentation
 data class CurrencyUI (
     val charCode: String,
     val name: String,
-    val total: Double,
+    val total: String,
     val selected: Boolean,
 )