Skip to content

Commit

Permalink
add singleton MoneyFormatter instead of extension BigDecimal.formatAm…
Browse files Browse the repository at this point in the history
…ount()
  • Loading branch information
Elen-B committed Jun 25, 2024
1 parent b5afc92 commit 1bb1d8b
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,28 @@ import androidx.lifecycle.repeatOnLifecycle
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import app.cashadvisor.R
import app.cashadvisor.analytics.presentation.formatAmount
import app.cashadvisor.analytics.presentation.model.AnalyticType
import app.cashadvisor.analytics.presentation.model.FilterParams
import app.cashadvisor.analytics.presentation.ui.adapter.AnalyticInfoAdapter
import app.cashadvisor.analytics.presentation.ui.state.AnalyticsUiState
import app.cashadvisor.common.ui.BaseFragment
import app.cashadvisor.common.utils.MoneyFormatter
import app.cashadvisor.databinding.FragmentAnalyticsBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import java.math.BigDecimal
import java.util.Date
import javax.inject.Inject


@AndroidEntryPoint
class AnalyticsFragment : BaseFragment<FragmentAnalyticsBinding, AnalyticsViewModel>(FragmentAnalyticsBinding::inflate) {

override val viewModel: AnalyticsViewModel by viewModels()
private val analyticInfoAdapter by lazy { AnalyticInfoAdapter() }
private val analyticInfoAdapter by lazy { AnalyticInfoAdapter(formatter) }
@Inject
lateinit var formatter: MoneyFormatter

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down Expand Up @@ -144,8 +150,8 @@ class AnalyticsFragment : BaseFragment<FragmentAnalyticsBinding, AnalyticsViewMo
getProgressDrawable(defaultDrawable)
)
piAnalyticProgress.progress = percent.toInt()
tvProgressPercent.text = String.format("%s %%", percent.formatAmount())
tvProgressInfoAmount.text = state.remainAmount.formatAmount()
tvProgressPercent.text = String.format("%s %%", formatter.format(percent))
tvProgressInfoAmount.text = formatter.format(state.remainAmount)
grProgress.isVisible = true
}

Expand All @@ -170,7 +176,7 @@ class AnalyticsFragment : BaseFragment<FragmentAnalyticsBinding, AnalyticsViewMo
}

private fun setTotalAnalyticAmount(amount: BigDecimal) {
binding.tvAnalyticAmount.text = amount.formatAmount()
binding.tvAnalyticAmount.text = formatter.format(amount)
}

private fun tuneNavigationForState(params: FilterParams) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ import androidx.core.content.ContextCompat.getColor
import androidx.core.content.ContextCompat.getString
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import app.cashadvisor.analytics.presentation.formatAmount
import app.cashadvisor.analytics.presentation.model.AnalyticType
import app.cashadvisor.analytics.presentation.model.CategorySummary
import app.cashadvisor.analytics.presentation.model.SubcategorySummary
import app.cashadvisor.analytics.presentation.model.getCategoryCurrencyTextColor
import app.cashadvisor.categories.presentation.ui.CategoriesIcon
import app.cashadvisor.common.utils.MoneyFormatter
import app.cashadvisor.databinding.ItemAnalyticsFactBinding
import app.cashadvisor.databinding.ItemAnalyticsFactRowBinding
import app.cashadvisor.databinding.ItemAnalyticsPlanBinding
import app.cashadvisor.uikit.R
import com.bumptech.glide.Glide

class AnalyticInfoAdapter : ListAdapter<CategorySummary, RecyclerView.ViewHolder>(
class AnalyticInfoAdapter(private val moneyFormatter: MoneyFormatter) : ListAdapter<CategorySummary, RecyclerView.ViewHolder>(
CategorySummaryDiffUtilCallback()
) {
override fun onCreateViewHolder(
Expand All @@ -32,17 +32,17 @@ class AnalyticInfoAdapter : ListAdapter<CategorySummary, RecyclerView.ViewHolder
PLAN_VIEW_TYPE -> {
val binding =
ItemAnalyticsPlanBinding.inflate(layout, parent, false)
SimpleCategorySummaryViewHolder(binding)
SimpleCategorySummaryViewHolder(binding, moneyFormatter)
}
FACT_VIEW_TYPE -> {
val binding =
ItemAnalyticsFactBinding.inflate(layout, parent, false)
CategorySummaryViewHolder(binding)
CategorySummaryViewHolder(binding, moneyFormatter)
}
else -> {
val binding =
ItemAnalyticsFactBinding.inflate(layout, parent, false)
CategorySummaryViewHolder(binding)
CategorySummaryViewHolder(binding, moneyFormatter)
}
}
}
Expand All @@ -63,11 +63,12 @@ class AnalyticInfoAdapter : ListAdapter<CategorySummary, RecyclerView.ViewHolder
}

inner class SimpleCategorySummaryViewHolder(
private val binding: ItemAnalyticsPlanBinding
private val binding: ItemAnalyticsPlanBinding,
private val moneyFormatter: MoneyFormatter
) : RecyclerView.ViewHolder(binding.root) {
fun bind(categorySummary: CategorySummary) = with(binding) {
tvCategoryName.text = categorySummary.name
tvCategoryAmount.text = categorySummary.amount.formatAmount()
tvCategoryAmount.text = moneyFormatter.format(categorySummary.amount)
tvCategoryCurrency.text = getCategoryCurrencyText(categorySummary.analyticType)
val imageDrawableRes = CategoriesIcon.getCategoriesImageResIdFromId(categorySummary.id.toInt())
if (imageDrawableRes != null) {
Expand Down Expand Up @@ -111,11 +112,12 @@ class AnalyticInfoAdapter : ListAdapter<CategorySummary, RecyclerView.ViewHolder
}

inner class CategorySummaryViewHolder(
private val binding: ItemAnalyticsFactBinding
private val binding: ItemAnalyticsFactBinding,
private val moneyFormatter: MoneyFormatter
) : RecyclerView.ViewHolder(binding.root) {
fun bind(categorySummary: CategorySummary) = with(binding) {
tvCategoryName.text = categorySummary.name
tvCategoryAmount.text = categorySummary.amount.formatAmount()
tvCategoryAmount.text = moneyFormatter.format(categorySummary.amount)
tvCategoryCurrency.text = getCategoryCurrencyText(categorySummary.analyticType)
tvCategoryCurrency.setTextColor(
getColor(
Expand Down Expand Up @@ -146,7 +148,7 @@ class AnalyticInfoAdapter : ListAdapter<CategorySummary, RecyclerView.ViewHolder
val layout = LayoutInflater.from(parent.context)
val binding = ItemAnalyticsFactRowBinding.inflate(layout, parent, false)
binding.tvCategoryItemName.text = subcategorySummary.name
binding.tvCategoryItemAmount.text = subcategorySummary.amount.formatAmount()
binding.tvCategoryItemAmount.text = moneyFormatter.format(subcategorySummary.amount)
parent.addView(binding.root)
}
}
Expand Down
17 changes: 17 additions & 0 deletions app/src/main/java/app/cashadvisor/common/di/UtilsModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package app.cashadvisor.common.di

import app.cashadvisor.common.utils.MoneyFormatter
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
class UtilsModule {

@Provides
@Singleton
fun providesMoneyFormatter(): MoneyFormatter = MoneyFormatter()
}
19 changes: 19 additions & 0 deletions app/src/main/java/app/cashadvisor/common/utils/MoneyFormatter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package app.cashadvisor.common.utils

import java.math.BigDecimal
import java.text.NumberFormat

class MoneyFormatter {
private val formatter: NumberFormat = NumberFormat.getNumberInstance()

fun format(amount: BigDecimal): String {
formatter.maximumFractionDigits = MAX_FRACTION_DIGITS
formatter.minimumFractionDigits = MIN_FRACTION_DIGITS
return formatter.format(amount)
}

companion object {
private const val MIN_FRACTION_DIGITS = 2
private const val MAX_FRACTION_DIGITS = 2
}
}

0 comments on commit 1bb1d8b

Please sign in to comment.