Browse Source

code refactoring

MrOzOn 4 năm trước cách đây
mục cha
commit
6f72d7c1eb

+ 3 - 1
README.md

@@ -26,9 +26,11 @@
 ## Используемый стек
 - MVVM
 - Kotlin Coroutines
+- Navigation Component
 - Dagger2 and Hilt
 - Room
 - Retrofit
 
 ## Дополнительные инструменты
-- статический анализатор кода [Detekt](https://github.com/detekt/detekt) (запуск проверки - команда `./gradlew detekt`)
+- статический анализатор кода [Detekt](https://github.com/detekt/detekt) (запуск проверки - команда `./gradlew detekt`)
+- подключен Robolectric для тестирования на JVM без запуска эмулятора

+ 7 - 0
app/src/main/java/com/mrozon/currencyconverter/Ext.kt

@@ -0,0 +1,7 @@
+package com.mrozon.currencyconverter
+
+import java.text.DecimalFormatSymbols
+
+fun Double.format(digits: Int) = "%.${digits}f".format(this)
+    .trimEnd('0').
+    trimEnd(DecimalFormatSymbols.getInstance().decimalSeparator)

+ 4 - 0
app/src/main/java/com/mrozon/currencyconverter/MyApp.kt

@@ -16,4 +16,8 @@ class MyApp: Application() {
             Timber.plant(Timber.DebugTree())
         }
     }
+
+    companion object {
+        const val PRECISION = 4
+    }
 }

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

@@ -3,17 +3,16 @@ package com.mrozon.currencyconverter.presentation
 import android.graphics.Color
 import android.widget.TextView
 import androidx.databinding.BindingAdapter
+import com.mrozon.currencyconverter.MyApp
+import com.mrozon.currencyconverter.presentation.model.CurrencyUI
 
 @BindingAdapter("show_total")
 fun TextView.showTotal(currencyUI: CurrencyUI) {
-//    text = currencyUI.total.format(4)
     if(currencyUI.selected) {
         setTextColor(Color.MAGENTA)
         text = currencyUI.total
     } else {
         setTextColor(Color.BLACK)
-        text = currencyUI.total.format(4)
+        text = currencyUI.total.format(MyApp.PRECISION)
     }
 }
-
-fun Double.format(digits: Int) = "%.${digits}f".format(this).trimEnd('0').trim('.')

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

@@ -5,7 +5,6 @@ import android.view.LayoutInflater
 import android.view.View
 import android.view.ViewGroup
 import android.widget.Button
-import android.widget.Toast
 import androidx.fragment.app.Fragment
 import androidx.fragment.app.viewModels
 import androidx.lifecycle.lifecycleScope
@@ -13,7 +12,7 @@ import androidx.recyclerview.widget.LinearLayoutManager
 import com.mrozon.currencyconverter.databinding.FragmentConverterCurrencyBinding
 import dagger.hilt.android.AndroidEntryPoint
 import kotlinx.coroutines.flow.collect
-import timber.log.Timber
+import java.text.DecimalFormatSymbols
 
 @AndroidEntryPoint
 class ConverterCurrencyFragment: Fragment() {
@@ -36,9 +35,9 @@ class ConverterCurrencyFragment: Fragment() {
             layoutManager = LinearLayoutManager(requireContext())
             adapter = currencyAdapter
         }
+        binding?.buttonComma?.text = DecimalFormatSymbols.getInstance().decimalSeparator.toString()
         binding?.setClickListener { view ->
             val button = view as Button
-//            Toast.makeText(requireContext(),button.text,Toast.LENGTH_SHORT).show()
             viewModel.input(button.text.toString())
         }
         return binding?.root!!
@@ -49,7 +48,6 @@ class ConverterCurrencyFragment: Fragment() {
 
         lifecycleScope.launchWhenStarted {
             viewModel.state.collect { list ->
-                Timber.d(list.toString())
                 currencyAdapter.submitList(list)
             }
         }

+ 16 - 14
app/src/main/java/com/mrozon/currencyconverter/presentation/ConverterCurrencyViewModel.kt

@@ -4,11 +4,18 @@ import android.app.Application
 import androidx.lifecycle.ViewModel
 import androidx.lifecycle.viewModelScope
 import com.mrozon.currencyconverter.CoroutineContextDispatchers
+import com.mrozon.currencyconverter.MyApp
 import com.mrozon.currencyconverter.R
 import com.mrozon.currencyconverter.domain.ICalculateCurrencyUseCase
 import com.mrozon.currencyconverter.domain.model.Currency
+import com.mrozon.currencyconverter.format
+import com.mrozon.currencyconverter.presentation.model.CurrencyUI
 import dagger.hilt.android.lifecycle.HiltViewModel
-import kotlinx.coroutines.flow.*
+import kotlinx.coroutines.flow.Flow
+import kotlinx.coroutines.flow.MutableStateFlow
+import kotlinx.coroutines.flow.SharingStarted
+import kotlinx.coroutines.flow.stateIn
+import kotlinx.coroutines.flow.combine
 import javax.inject.Inject
 
 @HiltViewModel
@@ -46,12 +53,13 @@ class ConverterCurrencyViewModel @Inject constructor(
 
         val listCurrencyVH = mutableListOf<CurrencyUI>()
         currencies.forEach { currency ->
-            val total = useCase.convertCurrency(selectedCurrency, currency, inputValue.toDouble())
+            val total = useCase.convertCurrency(selectedCurrency, currency,
+                inputValue.replace(',','.').toDouble())
 
             val currencyVH = CurrencyUI(
                 charCode = currency.charCode,
                 name = currency.name,
-                total = if (currency.charCode == selected) inputValue else total.format(4),
+                total = if (currency.charCode == selected) inputValue else total.format(MyApp.PRECISION),
                 selected = currency.charCode == selected
             )
             listCurrencyVH.add(currencyVH)
@@ -65,23 +73,17 @@ class ConverterCurrencyViewModel @Inject constructor(
     }
 
     fun input(text: String) {
+        val inputValue = flowInputValue.value
         when (text) {
             delChar -> {
-                val v: String = flowInputValue.value.format(4)
-                if(v.length==1) {
-                    flowInputValue.value = "0"
-                } else {
-                    flowInputValue.value = v.dropLast(1)
-                }
+                flowInputValue.value = if (inputValue.length==1) "0" else inputValue.dropLast(1)
             }
             comma -> {
-                val v = flowInputValue.value
-                if (!v.contains(comma))
-                    flowInputValue.value = v+text
+                if (!inputValue.contains(comma))
+                    flowInputValue.value = inputValue+text
             }
             else -> {
-                val v = flowInputValue.value
-                flowInputValue.value = if (v=="0") text else v+text
+                flowInputValue.value = if (inputValue=="0") text else inputValue+text
             }
         }
     }

+ 2 - 3
app/src/main/java/com/mrozon/currencyconverter/presentation/CurrencyAdapter.kt → app/src/main/java/com/mrozon/currencyconverter/presentation/adapter/CurrencyAdapter.kt

@@ -6,7 +6,7 @@ import androidx.recyclerview.widget.DiffUtil
 import androidx.recyclerview.widget.ListAdapter
 import androidx.recyclerview.widget.RecyclerView
 import com.mrozon.currencyconverter.databinding.IemCurrencyBinding
-import timber.log.Timber
+import com.mrozon.currencyconverter.presentation.model.CurrencyUI
 
 class CurrencyAdapter(
     private var click: ((CurrencyUI) -> Unit)
@@ -38,7 +38,6 @@ class CurrencyAdapter(
                 executePendingBindings()
             }
             binding.setClickListener {
-                Timber.d("Click!")
                 click(item)
             }
         }
@@ -55,4 +54,4 @@ private class CurrencyDiffCallback : DiffUtil.ItemCallback<CurrencyUI>() {
     override fun areContentsTheSame(oldItem: CurrencyUI, newItem: CurrencyUI): Boolean {
         return oldItem == newItem
     }
-}
+}

+ 2 - 2
app/src/main/java/com/mrozon/currencyconverter/presentation/CurrencyUI.kt → app/src/main/java/com/mrozon/currencyconverter/presentation/model/CurrencyUI.kt

@@ -1,8 +1,8 @@
-package com.mrozon.currencyconverter.presentation
+package com.mrozon.currencyconverter.presentation.model
 
 data class CurrencyUI (
     val charCode: String,
     val name: String,
     val total: String,
     val selected: Boolean,
-)
+)

+ 1 - 1
app/src/main/res/layout/iem_currency.xml

@@ -9,7 +9,7 @@
             type="android.view.View.OnClickListener"/>
         <variable
             name="currencyUI"
-            type="com.mrozon.currencyconverter.presentation.CurrencyUI" />
+            type="com.mrozon.currencyconverter.presentation.model.CurrencyUI" />
     </data>
 
     <androidx.constraintlayout.widget.ConstraintLayout