Skip to content

Commit

Permalink
Refactor change Int to Long and fix month english in month screen
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosgub committed Jul 24, 2024
1 parent 17e3a5a commit 1c5bfdc
Show file tree
Hide file tree
Showing 27 changed files with 85 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,32 @@ import kotlin.math.floor
import kotlin.math.pow
import kotlin.math.roundToInt

fun Double.toMoneyFormat(): String = "$${this.toPrecision(2)}"
fun Long.toMoneyFormat(): String {
return when {
this < 10 -> {
"$0.0${this}"
}

this < 100 -> {
"$0.${this}"
}

else -> {
val numberString = this.toString()
"$${numberString.substring(0, numberString.length - 2)}.${numberString.substring(numberString.length - 2, numberString.length)}"
}
}
}

fun String.toAmount(): Long {
val cleanString: String =
this.replace("""[$,.A-Za-z]""".toRegex(), "").trim().trimStart('0')
return if (cleanString.isBlank()) {
0L
} else {
cleanString.toLongOrNull() ?: Long.MAX_VALUE
}
}

fun Float.toMoneyFormat(): String = "$${this.toPrecision(2)}"

Expand Down Expand Up @@ -78,8 +103,8 @@ fun LocalDateTime.toMillis(): Long = this.toInstant(TimeZone.UTC).toEpochMillise
fun Long.toStringDateFormat(): String {
val localDate = this.toLocalDate()
return "${localDate.dayOfMonth.toNumberOfTwoDigits()}/" +
"${localDate.monthNumber.toNumberOfTwoDigits()}/" +
"${localDate.year}"
"${localDate.monthNumber.toNumberOfTwoDigits()}/" +
"${localDate.year}"
}

fun Month.toLocaleString(): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ interface DatabaseFinanceDataSource {
suspend fun getIncome(id: Long): ResponseResult<Income>

suspend fun createExpense(
amount: Int,
amount: Long,
category: String,
note: String,
dateInMillis: Long,
): ResponseResult<Unit>

suspend fun createIncome(
amount: Int,
amount: Long,
note: String,
dateInMillis: Long,
): ResponseResult<Unit>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class DatabaseFinanceDataSourceImpl(
}

override suspend fun createExpense(
amount: Int,
amount: Long,
category: String,
note: String,
dateInMillis: Long,
Expand All @@ -83,7 +83,7 @@ class DatabaseFinanceDataSourceImpl(
sharedDatabase().createExpense(
Expense(
id = 1,
amount = amount.toLong(),
amount = amount,
category = category,
note = note,
dateInMillis = dateInMillis,
Expand All @@ -99,7 +99,7 @@ class DatabaseFinanceDataSourceImpl(
}

override suspend fun createIncome(
amount: Int,
amount: Long,
note: String,
dateInMillis: Long,
): ResponseResult<Unit> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class FinanceRepositoryImpl(
.toImmutableList()
val monthExpense =
MonthExpense(
incomeTotal = incomeTotal / 100.0,
incomeTotal = incomeTotal,
percentage = if (incomeTotal != 0L) expenseTotal * 100 / incomeTotal else 100,
)
val date =
Expand Down Expand Up @@ -167,7 +167,7 @@ class FinanceRepositoryImpl(
}

override suspend fun createExpense(
amount: Int,
amount: Long,
category: String,
note: String,
dateInMillis: Long,
Expand All @@ -184,7 +184,7 @@ class FinanceRepositoryImpl(
}

override suspend fun createIncome(
amount: Int,
amount: Long,
note: String,
dateInMillis: Long,
): GenericState<Unit> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ data class FinanceScreenExpenses(
)

data class MonthExpense(
val incomeTotal: Double = 0.0,
val incomeTotal: Long = 0L,
val percentage: Long = 0L,
)
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ interface FinanceRepository {
suspend fun getIncome(id: Long): GenericState<FinanceModel>

suspend fun createExpense(
amount: Int,
amount: Long,
category: String,
note: String,
dateInMillis: Long,
): GenericState<Unit>

suspend fun createIncome(
amount: Int,
amount: Long,
note: String,
dateInMillis: Long,
): GenericState<Unit>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class CreateExpenseUseCase(
)

data class Params(
val amount: Int,
val amount: Long,
val category: String,
val note: String,
val dateInMillis: Long,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class CreateIncomeUseCase(
)

data class Params(
val amount: Int,
val amount: Long,
val note: String,
val dateInMillis: Long,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ private fun CategoryMonthExpenseItem(
verticalArrangement = Arrangement.Top,
) {
Text(
text = (expense.amount / 100.0).toMoneyFormat(),
text = expense.amount.toMoneyFormat(),
style = MaterialTheme.typography.bodySmall,
fontWeight = FontWeight.Medium,
textAlign = TextAlign.End,
Expand Down Expand Up @@ -206,7 +206,7 @@ private fun CategoryMonthDetailHeader(state: CategoryMonthDetailScreenState) {
}
Column {
Text(
text = (state.monthDetail.monthAmount / 100.0).toMoneyFormat(),
text = state.monthDetail.monthAmount.toMoneyFormat(),
style = MaterialTheme.typography.titleMedium,
fontWeight = FontWeight.Bold,
color = Gray900,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ private fun FinanceCategoryItem(
horizontalAlignment = Alignment.End,
) {
Text(
text = (expense.amount / 100.0).toMoneyFormat(),
text = expense.amount.toMoneyFormat(),
style = MaterialTheme.typography.bodySmall,
fontWeight = FontWeight.Medium,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ import com.carlosgub.myfinances.theme.spacing_4
import kotlinx.collections.immutable.ImmutableMap
import kotlinx.coroutines.launch
import kotlinx.datetime.LocalDateTime
import kotlinx.datetime.Month

@Composable
fun HomeHeaderContent(
Expand Down Expand Up @@ -173,7 +172,7 @@ private fun HomeHeaderFirstPage(
color = Color.White,
)
Text(
text = (monthAmount / 100.0).toMoneyFormat(),
text = monthAmount.toMoneyFormat(),
style = MaterialTheme.typography.headlineMedium,
color = Color.White,
modifier = Modifier.padding(top = spacing_4),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import androidx.compose.ui.unit.dp
import com.carlosgub.myfinances.components.datazero.DataZero
import com.carlosgub.myfinances.components.divider.HorizontalDivider
import com.carlosgub.myfinances.components.loading.Loading
import com.carlosgub.myfinances.core.utils.toLocaleString
import com.carlosgub.myfinances.core.utils.toMonthKey
import com.carlosgub.myfinances.presentation.viewmodel.months.MonthsScreenState
import com.carlosgub.myfinances.theme.ColorPrimary
Expand Down Expand Up @@ -182,15 +183,15 @@ fun MonthItem(
.background(color = ColorPrimary),
)
Text(
text = localDateTime.month.name.take(1),
text = localDateTime.month.toLocaleString().take(1),
color = White,
textAlign = TextAlign.Center,
fontWeight = FontWeight.Bold,
)
}

Text(
localDateTime.month.name,
localDateTime.month.toLocaleString(),
color = ColorPrimary,
modifier = Modifier.fillMaxSize()
.padding(spacing_2),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import com.carlosgub.myfinances.domain.model.CategoryEnum

data class CreateExpenseScreenState(
val category: CategoryEnum = CategoryEnum.FOOD,
val amountField: String = 0.0.toMoneyFormat(),
val amount: Double = 0.0,
val amountField: String = 0L.toMoneyFormat(),
val amount: Long = 0L,
val showDateError: Boolean = false,
val showNoteError: Boolean = false,
val showError: Boolean = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.carlosgub.myfinances.presentation.viewmodel.createexpense

import androidx.annotation.VisibleForTesting
import com.carlosgub.myfinances.core.state.GenericState
import com.carlosgub.myfinances.core.utils.toAmount
import com.carlosgub.myfinances.core.utils.toMoneyFormat
import com.carlosgub.myfinances.core.utils.toStringDateFormat
import com.carlosgub.myfinances.domain.usecase.CreateExpenseUseCase
Expand Down Expand Up @@ -42,7 +43,7 @@ class CreateExpenseViewModel(
val result =
createExpenseUseCase(
CreateExpenseUseCase.Params(
amount = (state.amount * 100).toInt(),
amount = state.amount,
note = state.note,
dateInMillis = state.dateInMillis,
category = state.category.name,
Expand Down Expand Up @@ -76,16 +77,7 @@ class CreateExpenseViewModel(
override fun setAmount(textFieldValue: String): Job =
intent {
if (textFieldValue != state.amountField) {
val cleanString: String =
textFieldValue.replace("""[$,.A-Za-z]""".toRegex(), "").trim().trimStart('0')
val parsed =
if (cleanString.isBlank()) {
0.0
} else {
cleanString.toDouble()
}
val amount = parsed / 100

val amount = textFieldValue.toAmount()
reduce {
state.copy(
amountField = amount.toMoneyFormat(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import com.carlosgub.myfinances.domain.model.CategoryEnum

data class CreateIncomeScreenState(
val category: CategoryEnum = CategoryEnum.FOOD,
val amountField: String = 0.0.toMoneyFormat(),
val amount: Double = 0.0,
val amountField: String = 0L.toMoneyFormat(),
val amount: Long = 0L,
val showDateError: Boolean = false,
val showNoteError: Boolean = false,
val showError: Boolean = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.carlosgub.myfinances.presentation.viewmodel.createincome

import androidx.annotation.VisibleForTesting
import com.carlosgub.myfinances.core.state.GenericState
import com.carlosgub.myfinances.core.utils.toAmount
import com.carlosgub.myfinances.core.utils.toMoneyFormat
import com.carlosgub.myfinances.core.utils.toStringDateFormat
import com.carlosgub.myfinances.domain.usecase.CreateIncomeUseCase
Expand Down Expand Up @@ -42,7 +43,7 @@ class CreateIncomeViewModel(
val result =
createIncomeUseCase(
CreateIncomeUseCase.Params(
amount = (state.amount * 100).toInt(),
amount = state.amount,
note = state.note,
dateInMillis = state.dateInMillis,
),
Expand Down Expand Up @@ -70,16 +71,7 @@ class CreateIncomeViewModel(
override fun setAmount(textFieldValue: String): Job =
intent {
if (textFieldValue != state.amountField) {
val cleanString: String =
textFieldValue.replace("""[$,.A-Za-z]""".toRegex(), "").trim().trimStart('0')
val parsed =
if (cleanString.isBlank()) {
0.0
} else {
cleanString.toDouble()
}
val amount = parsed / 100

val amount = textFieldValue.toAmount()
reduce {
state.copy(
amountField = amount.toMoneyFormat(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import com.carlosgub.myfinances.domain.model.CategoryEnum

data class EditExpenseScreenState(
val category: CategoryEnum = CategoryEnum.FOOD,
val amountField: String = 0.0.toMoneyFormat(),
val amount: Double = 0.0,
val amountField: String = 0L.toMoneyFormat(),
val amount: Long = 0L,
val showDateError: Boolean = false,
val showNoteError: Boolean = false,
val showError: Boolean = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.carlosgub.myfinances.presentation.viewmodel.editexpense

import androidx.annotation.VisibleForTesting
import com.carlosgub.myfinances.core.state.GenericState
import com.carlosgub.myfinances.core.utils.toAmount
import com.carlosgub.myfinances.core.utils.toMillis
import com.carlosgub.myfinances.core.utils.toMoneyFormat
import com.carlosgub.myfinances.core.utils.toStringDateFormat
Expand Down Expand Up @@ -41,16 +42,7 @@ class EditExpenseViewModel(
override fun setAmount(textFieldValue: String): Job =
intent {
if (textFieldValue != state.amountField) {
val cleanString: String =
textFieldValue.replace("""[$,.A-Za-z]""".toRegex(), "").trim().trimStart('0')
val parsed =
if (cleanString.isBlank()) {
0.0
} else {
cleanString.toDouble()
}
val amount = parsed / 100

val amount = textFieldValue.toAmount()
reduce {
state.copy(
amountField = amount.toMoneyFormat(),
Expand Down Expand Up @@ -84,7 +76,7 @@ class EditExpenseViewModel(
val result =
editExpenseUseCase(
EditExpenseUseCase.Params(
amount = (state.amount * 100).toLong(),
amount = state.amount,
note = state.note,
dateInMillis = state.dateInMillis,
id = id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import com.carlosgub.myfinances.domain.model.CategoryEnum

data class EditIncomeScreenState(
val category: CategoryEnum = CategoryEnum.WORK,
val amountField: String = 0.0.toMoneyFormat(),
val amount: Double = 0.0,
val amountField: String = 0L.toMoneyFormat(),
val amount: Long = 0L,
val showDateError: Boolean = false,
val showNoteError: Boolean = false,
val showError: Boolean = false,
Expand Down
Loading

0 comments on commit 1c5bfdc

Please sign in to comment.