Skip to content

Commit

Permalink
refactor getCategoryCurrencyTextColor() to an extension function for …
Browse files Browse the repository at this point in the history
…AnalyticType class; move getLastDayOfMonth(), getFirstDayOfMonth() to viewModel
  • Loading branch information
Elen-B committed Jun 25, 2024
1 parent 2c897d5 commit b5afc92
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 35 deletions.
17 changes: 0 additions & 17 deletions app/src/main/java/app/cashadvisor/analytics/presentation/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,6 @@ package app.cashadvisor.analytics.presentation

import java.math.BigDecimal
import java.text.NumberFormat
import java.util.Calendar
import java.util.Date

fun Date.getLastDayOfMonth() : Date {
val current = Calendar.getInstance()
val lastDay = Calendar.getInstance()
lastDay.set(current.get(Calendar.YEAR), current.get(Calendar.MONTH),
current.getActualMaximum(Calendar.DATE))
return lastDay.time
}

fun Date.getFirstDayOfMonth() : Date {
val current = Calendar.getInstance()
val firstDay = Calendar.getInstance()
firstDay.set(current.get(Calendar.YEAR), current.get(Calendar.MONTH), 1)
return firstDay.time
}

fun BigDecimal.formatAmount() : String {
val formatter = NumberFormat.getNumberInstance()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package app.cashadvisor.analytics.presentation.model

import androidx.annotation.ColorRes
import app.cashadvisor.uikit.R

enum class AnalyticType(val value: Int) {
INCOME(0),
EXPENSE(1),
Expand All @@ -14,4 +17,13 @@ enum class AnalyticType(val value: Int) {
}
}
}
}

@ColorRes
fun AnalyticType.getCategoryCurrencyTextColor(): Int {
return when (this) {
AnalyticType.INCOME -> R.color.m1
AnalyticType.EXPENSE -> R.color.m2
else -> R.color.m3
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package app.cashadvisor.analytics.presentation.model

import app.cashadvisor.analytics.presentation.getFirstDayOfMonth
import app.cashadvisor.analytics.presentation.getLastDayOfMonth
import java.util.Calendar
import java.util.Date

data class FilterParams(
Expand All @@ -14,8 +13,8 @@ data class FilterParams(
companion object {
fun getDefault(): FilterParams {
return FilterParams(
beginDate = Date().getFirstDayOfMonth(),
endDate = Date().getLastDayOfMonth(),
beginDate = Calendar.getInstance().time,
endDate = Calendar.getInstance().time,
analyticType = AnalyticType.INCOME,
planned = false)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package app.cashadvisor.analytics.presentation.ui

import androidx.lifecycle.viewModelScope
import app.cashadvisor.analytics.presentation.MockData
import app.cashadvisor.analytics.presentation.getFirstDayOfMonth
import app.cashadvisor.analytics.presentation.getLastDayOfMonth
import app.cashadvisor.analytics.presentation.model.Account
import app.cashadvisor.analytics.presentation.model.AnalyticType
import app.cashadvisor.analytics.presentation.model.CategorySummary
Expand All @@ -17,6 +15,7 @@ import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import java.math.BigDecimal
import java.util.Calendar
import java.util.Date
import javax.inject.Inject

Expand All @@ -33,7 +32,7 @@ class AnalyticsViewModel @Inject constructor() : BaseViewModel() {
get() = _categorySummaryList

init {
setPeriod(Date().getFirstDayOfMonth(), Date().getLastDayOfMonth())
setPeriod(getFirstDayOfMonth(), getLastDayOfMonth())
observeFilterParams()
observeCategorySummaryList()
}
Expand Down Expand Up @@ -163,4 +162,19 @@ class AnalyticsViewModel @Inject constructor() : BaseViewModel() {

return progress
}

private fun getLastDayOfMonth() : Date {
val current = Calendar.getInstance()
val lastDay = Calendar.getInstance()
lastDay.set(current.get(Calendar.YEAR), current.get(Calendar.MONTH),
current.getActualMaximum(Calendar.DATE))
return lastDay.time
}

private fun getFirstDayOfMonth() : Date {
val current = Calendar.getInstance()
val firstDay = Calendar.getInstance()
firstDay.set(current.get(Calendar.YEAR), current.get(Calendar.MONTH), 1)
return firstDay.time
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ 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.databinding.ItemAnalyticsFactBinding
import app.cashadvisor.databinding.ItemAnalyticsFactRowBinding
Expand Down Expand Up @@ -116,8 +117,14 @@ class AnalyticInfoAdapter : ListAdapter<CategorySummary, RecyclerView.ViewHolder
tvCategoryName.text = categorySummary.name
tvCategoryAmount.text = categorySummary.amount.formatAmount()
tvCategoryCurrency.text = getCategoryCurrencyText(categorySummary.analyticType)
tvCategoryCurrency.setTextColor(getColor(itemView.context, getCategoryCurrencyTextColor(categorySummary.analyticType)))
val imageDrawableRes = CategoriesIcon.getCategoriesImageResIdFromId(categorySummary.id.toInt())
tvCategoryCurrency.setTextColor(
getColor(
itemView.context,
categorySummary.analyticType.getCategoryCurrencyTextColor()
)
)
val imageDrawableRes =
CategoriesIcon.getCategoriesImageResIdFromId(categorySummary.id.toInt())
if (imageDrawableRes != null) {
Glide.with(itemView)
.load(ContextCompat.getDrawable(
Expand Down Expand Up @@ -151,15 +158,6 @@ class AnalyticInfoAdapter : ListAdapter<CategorySummary, RecyclerView.ViewHolder
else -> getString(itemView.context,R.string.mp_currency_symbol)
}
}

@ColorRes
private fun getCategoryCurrencyTextColor(type: AnalyticType): Int {
return when (type) {
AnalyticType.INCOME -> R.color.m1
AnalyticType.EXPENSE -> R.color.m2
else -> R.color.m3
}
}
}

companion object {
Expand Down

0 comments on commit b5afc92

Please sign in to comment.