Skip to content

Commit

Permalink
Separate category month detail into two one for expenses and one for …
Browse files Browse the repository at this point in the history
…incomes
  • Loading branch information
carlosgub committed Sep 16, 2024
1 parent 8b7b52b commit 0852a7f
Show file tree
Hide file tree
Showing 40 changed files with 904 additions and 249 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import com.carlosgub.myfinances.domain.model.FinanceExpenses
import com.carlosgub.myfinances.domain.model.FinanceLocalDate
import com.carlosgub.myfinances.domain.model.FinanceModel
import com.carlosgub.myfinances.domain.model.IncomeModel
import com.carlosgub.myfinances.domain.model.MonthDetailModel
import com.carlosgub.myfinances.domain.model.MonthDetailExpenseModel
import com.carlosgub.myfinances.domain.model.MonthDetailIncomeModel
import com.carlosgub.myfinances.domain.model.MonthExpense
import com.carlosgub.myfinances.domain.repository.FinanceRepository
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -282,7 +283,7 @@ class FinanceRepositoryImpl(
override suspend fun getExpenseMonthDetail(
categoryEnum: com.carlosgub.myfinances.domain.model.CategoryEnum,
monthKey: String,
): Flow<GenericState<MonthDetailModel>> =
): Flow<GenericState<MonthDetailExpenseModel>> =
flow {
databaseFinance.getExpenseMonthDetail(categoryEnum, monthKey).collect { result ->
when (result) {
Expand Down Expand Up @@ -321,9 +322,9 @@ class FinanceRepositoryImpl(
}
emit(
GenericState.Success(
MonthDetailModel(
MonthDetailExpenseModel(
monthAmount = monthAmount,
expenseModel = expenseScreenModelList,
expenseModelList = expenseScreenModelList,
daySpent = daySpent,
),
),
Expand All @@ -333,7 +334,7 @@ class FinanceRepositoryImpl(
}
}

override suspend fun getIncomeMonthDetail(monthKey: String): Flow<GenericState<MonthDetailModel>> =
override suspend fun getIncomeMonthDetail(monthKey: String): Flow<GenericState<MonthDetailIncomeModel>> =
flow {
databaseFinance
.getIncomeMonthDetail(monthKey)
Expand All @@ -342,16 +343,16 @@ class FinanceRepositoryImpl(
is ResponseResult.Error -> GenericState.Error(result.error.message.orEmpty())
is ResponseResult.Success -> {
val monthAmount = result.data.sumOf { it.amount }
val expenseScreenModelList =
val incomeScreenModelList =
result.data
.map { expense ->
.map { income ->
val localDate =
FinanceLocalDate(expense.dateInMillis.toLocalDate())
ExpenseModel(
id = expense.id,
amount = expense.amount,
note = expense.note.replaceFirstChar { it.uppercase() },
category = expense.category,
FinanceLocalDate(income.dateInMillis.toLocalDate())
IncomeModel(
id = income.id,
amount = income.amount,
note = income.note.replaceFirstChar { it.uppercase() },
category = income.category,
localDateTime = localDate.localDateTime,
date = localDate.date,
monthKey = monthKey,
Expand All @@ -371,16 +372,16 @@ class FinanceRepositoryImpl(
.toInt(),
dayOfMonth = day,
)
dateInternal to expenseScreenModelList
dateInternal to incomeScreenModelList
.filter { expense ->
expense.localDateTime == dateInternal
}.sumOf { it.amount }
}
emit(
GenericState.Success(
MonthDetailModel(
MonthDetailIncomeModel(
monthAmount = monthAmount,
expenseModel = expenseScreenModelList,
incomeModelList = incomeScreenModelList,
daySpent = daySpent,
),
),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.carlosgub.myfinances.domain.model

import kotlinx.datetime.LocalDateTime

data class MonthDetailExpenseModel(
val monthAmount: Long = 0L,
val daySpent: Map<LocalDateTime, Long> = mapOf(),
val expenseModelList: List<ExpenseModel> = listOf(),
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package com.carlosgub.myfinances.domain.model

import kotlinx.datetime.LocalDateTime

data class MonthDetailModel(
data class MonthDetailIncomeModel(
val monthAmount: Long = 0L,
val daySpent: Map<LocalDateTime, Long> = mapOf(),
val expenseModel: List<ExpenseModel> = listOf(),
val incomeModelList: List<IncomeModel> = listOf(),
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import com.carlosgub.myfinances.domain.model.CategoryEnum
import com.carlosgub.myfinances.domain.model.ExpenseModel
import com.carlosgub.myfinances.domain.model.FinanceModel
import com.carlosgub.myfinances.domain.model.IncomeModel
import com.carlosgub.myfinances.domain.model.MonthDetailModel
import com.carlosgub.myfinances.domain.model.MonthDetailExpenseModel
import com.carlosgub.myfinances.domain.model.MonthDetailIncomeModel
import kotlinx.coroutines.flow.Flow
import kotlinx.datetime.LocalDateTime

Expand Down Expand Up @@ -57,9 +58,9 @@ interface FinanceRepository {
suspend fun getExpenseMonthDetail(
categoryEnum: CategoryEnum,
monthKey: String,
): Flow<GenericState<MonthDetailModel>>
): Flow<GenericState<MonthDetailExpenseModel>>

suspend fun getIncomeMonthDetail(monthKey: String): Flow<GenericState<MonthDetailModel>>
suspend fun getIncomeMonthDetail(monthKey: String): Flow<GenericState<MonthDetailIncomeModel>>

suspend fun getMonths(): Flow<GenericState<Map<Int, List<LocalDateTime>>>>
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.carlosgub.myfinances.domain.usecase

import com.carlosgub.myfinances.core.state.GenericState
import com.carlosgub.myfinances.domain.model.MonthDetailModel
import com.carlosgub.myfinances.domain.model.MonthDetailExpenseModel
import com.carlosgub.myfinances.domain.repository.FinanceRepository
import kotlinx.coroutines.flow.Flow

class GetExpenseMonthDetailUseCase(
private val financeRepository: FinanceRepository,
) {
suspend operator fun invoke(params: Params): Flow<GenericState<MonthDetailModel>> =
suspend operator fun invoke(params: Params): Flow<GenericState<MonthDetailExpenseModel>> =
financeRepository.getExpenseMonthDetail(
categoryEnum = params.categoryEnum,
monthKey = params.monthKey,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.carlosgub.myfinances.domain.usecase

import com.carlosgub.myfinances.core.state.GenericState
import com.carlosgub.myfinances.domain.model.MonthDetailModel
import com.carlosgub.myfinances.domain.model.MonthDetailIncomeModel
import com.carlosgub.myfinances.domain.repository.FinanceRepository
import kotlinx.coroutines.flow.Flow

class GetIncomeMonthDetailUseCase(
private val financeRepository: FinanceRepository,
) {
suspend operator fun invoke(params: Params): Flow<GenericState<MonthDetailModel>> =
suspend operator fun invoke(params: Params): Flow<GenericState<MonthDetailIncomeModel>> =
financeRepository.getIncomeMonthDetail(
monthKey = params.monthKey,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.graphics.Color
import com.carlosgub.myfinances.core.navigation.LocalNavController
import com.carlosgub.myfinances.core.utils.getCurrentMonthKey
import com.carlosgub.myfinances.presentation.screen.categorymonthdetail.CategoryMonthDetailScreen
import com.carlosgub.myfinances.presentation.screen.categorymonthdetailexpense.CategoryMonthDetailScreenExpense
import com.carlosgub.myfinances.presentation.screen.categorymonthdetailincome.CategoryMonthDetailScreenIncome
import com.carlosgub.myfinances.presentation.screen.createexpense.CreateExpenseScreen
import com.carlosgub.myfinances.presentation.screen.createincome.CreateIncomeScreen
import com.carlosgub.myfinances.presentation.screen.editexpense.EditExpenseScreen
Expand Down Expand Up @@ -62,12 +63,22 @@ fun App() {
scene(route = Navigation.MonthsScreen.route) {
MonthsScreen()
}
scene(route = Navigation.CategoryMonthDetailScreen.route) { backStackEntry ->
scene(route = Navigation.CategoryMonthDetailExpenseScreen.route) { backStackEntry ->
val monthKey =
backStackEntry.path<String>(NavArgs.MONTH_KEY.key)!!
val categoryName =
backStackEntry.path<String>(NavArgs.CATEGORY_NAME.key)!!
CategoryMonthDetailScreen(
CategoryMonthDetailScreenExpense(
monthKey = monthKey,
categoryName = categoryName,
)
}
scene(route = Navigation.CategoryMonthDetailIncomeScreen.route) { backStackEntry ->
val monthKey =
backStackEntry.path<String>(NavArgs.MONTH_KEY.key)!!
val categoryName =
backStackEntry.path<String>(NavArgs.CATEGORY_NAME.key)!!
CategoryMonthDetailScreenIncome(
monthKey = monthKey,
categoryName = categoryName,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,20 @@ sealed class Navigation(

data object MonthsScreen : Navigation("MonthsScreen")

data object CategoryMonthDetailScreen :
Navigation("CategoryMonthDetailScreen/{${NavArgs.MONTH_KEY.key}}/{${NavArgs.CATEGORY_NAME.key}}") {
data object CategoryMonthDetailExpenseScreen :
Navigation("CategoryMonthDetailExpenseScreen/{${NavArgs.MONTH_KEY.key}}/{${NavArgs.CATEGORY_NAME.key}}") {
fun createRoute(
monthKey: String,
categoryName: String,
) = "CategoryMonthDetailScreen/$monthKey/$categoryName"
) = "CategoryMonthDetailExpenseScreen/$monthKey/$categoryName"
}

data object CategoryMonthDetailIncomeScreen :
Navigation("CategoryMonthDetailIncomeScreen/{${NavArgs.MONTH_KEY.key}}/{${NavArgs.CATEGORY_NAME.key}}") {
fun createRoute(
monthKey: String,
categoryName: String,
) = "CategoryMonthDetailIncomeScreen/$monthKey/$categoryName"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import com.carlosgub.myfinances.domain.usecase.GetIncomeUseCase
import com.carlosgub.myfinances.domain.usecase.GetMonthsUseCase
import com.carlosgub.myfinances.navigation.impl.AppNavigationImpl
import com.carlosgub.myfinances.presentation.navigation.AppNavigation
import com.carlosgub.myfinances.presentation.viewmodel.categorymonthdetail.CategoryMonthDetailViewModel
import com.carlosgub.myfinances.presentation.viewmodel.categorymonthdetailexpense.CategoryMonthDetailExpenseViewModel
import com.carlosgub.myfinances.presentation.viewmodel.categorymonthdetailincome.CategoryMonthDetailIncomeViewModel
import com.carlosgub.myfinances.presentation.viewmodel.createexpense.CreateExpenseViewModel
import com.carlosgub.myfinances.presentation.viewmodel.createincome.CreateIncomeViewModel
import com.carlosgub.myfinances.presentation.viewmodel.editexpense.EditExpenseViewModel
Expand Down Expand Up @@ -74,8 +75,13 @@ val homeModule =
}

factory {
CategoryMonthDetailViewModel(
CategoryMonthDetailExpenseViewModel(
getExpenseMonthDetailUseCase = get(),
)
}

factory {
CategoryMonthDetailIncomeViewModel(
getIncomeMonthDetailUseCase = get(),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,26 @@ class AppNavigationImpl : AppNavigation {
)
}

override fun navigateToMonthDetail(
override fun navigateToMonthExpenseDetail(
navigator: Navigator,
monthKey: String,
categoryName: String,
) {
navigator.navigate(
Navigation.CategoryMonthDetailScreen.createRoute(
Navigation.CategoryMonthDetailExpenseScreen.createRoute(
monthKey = monthKey,
categoryName = categoryName,
),
)
}

override fun navigateToMonthIncomeDetail(
navigator: Navigator,
monthKey: String,
categoryName: String,
) {
navigator.navigate(
Navigation.CategoryMonthDetailIncomeScreen.createRoute(
monthKey = monthKey,
categoryName = categoryName,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ package com.carlosgub.myfinances.presentation.mapper
import com.carlosgub.myfinances.core.mapper.Mapper
import com.carlosgub.myfinances.domain.model.ExpenseModel
import com.carlosgub.myfinances.domain.model.FinanceModel
import com.carlosgub.myfinances.domain.model.MonthDetailModel
import com.carlosgub.myfinances.domain.model.IncomeModel
import com.carlosgub.myfinances.domain.model.MonthDetailExpenseModel
import com.carlosgub.myfinances.domain.model.MonthDetailIncomeModel
import com.carlosgub.myfinances.presentation.model.ExpenseScreenModel
import com.carlosgub.myfinances.presentation.model.FinanceScreenModel
import com.carlosgub.myfinances.presentation.model.MonthDetailScreenModel
import com.carlosgub.myfinances.presentation.model.IncomeScreenModel
import com.carlosgub.myfinances.presentation.model.MonthDetailExpenseScreenModel
import com.carlosgub.myfinances.presentation.model.MonthDetailIncomeScreenModel
import kotlinx.collections.immutable.toImmutableList
import kotlinx.collections.immutable.toImmutableMap

Expand All @@ -22,12 +26,21 @@ object FinanceModelToFinanceScreenModel : Mapper<FinanceModel, FinanceScreenMode
)
}

object MonthDetailModelToMonthDetailScreenModel : Mapper<MonthDetailModel, MonthDetailScreenModel> {
override fun map(from: MonthDetailModel) =
MonthDetailScreenModel(
object MonthDetailExpenseModelToMonthDetailExpenseScreenModel : Mapper<MonthDetailExpenseModel, MonthDetailExpenseScreenModel> {
override fun map(from: MonthDetailExpenseModel) =
MonthDetailExpenseScreenModel(
monthAmount = from.monthAmount,
daySpent = from.daySpent.toImmutableMap(),
expenseScreenModel = from.expenseModel.map { ExpenseModelToExpenseScreenModel.map(it) }.toImmutableList(),
expenseScreenModelList = from.expenseModelList.map { ExpenseModelToExpenseScreenModel.map(it) }.toImmutableList(),
)
}

object MonthDetailIncomeModelToMonthDetailIncomeScreenModel : Mapper<MonthDetailIncomeModel, MonthDetailIncomeScreenModel> {
override fun map(from: MonthDetailIncomeModel) =
MonthDetailIncomeScreenModel(
monthAmount = from.monthAmount,
daySpent = from.daySpent.toImmutableMap(),
incomeScreenModelList = from.incomeModelList.map { IncomeModelToIncomeScreenModel.map(it) }.toImmutableList(),
)
}

Expand All @@ -42,3 +55,15 @@ object ExpenseModelToExpenseScreenModel : Mapper<ExpenseModel, ExpenseScreenMode
date = from.date,
)
}

object IncomeModelToIncomeScreenModel : Mapper<IncomeModel, IncomeScreenModel> {
override fun map(from: IncomeModel) =
IncomeScreenModel(
amount = from.amount,
id = from.id,
note = from.note,
category = from.category,
localDateTime = from.localDateTime,
date = from.date,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.carlosgub.myfinances.presentation.model

import kotlinx.datetime.LocalDateTime

data class IncomeScreenModel(
val id: Long,
val amount: Long,
val note: String,
val category: String,
val localDateTime: LocalDateTime,
val date: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.carlosgub.myfinances.presentation.model

import kotlinx.collections.immutable.ImmutableMap
import kotlinx.collections.immutable.persistentMapOf
import kotlinx.datetime.LocalDateTime

data class MonthDetailExpenseScreenModel(
val monthAmount: Long = 0L,
val daySpent: ImmutableMap<LocalDateTime, Long> = persistentMapOf(),
val expenseScreenModelList: List<ExpenseScreenModel> = listOf(),
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import kotlinx.collections.immutable.ImmutableMap
import kotlinx.collections.immutable.persistentMapOf
import kotlinx.datetime.LocalDateTime

data class MonthDetailScreenModel(
data class MonthDetailIncomeScreenModel(
val monthAmount: Long = 0L,
val daySpent: ImmutableMap<LocalDateTime, Long> = persistentMapOf(),
val expenseScreenModel: List<ExpenseScreenModel> = listOf(),
val incomeScreenModelList: List<IncomeScreenModel> = listOf(),
)
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ interface AppNavigation {
id: Long,
)

fun navigateToMonthDetail(
fun navigateToMonthExpenseDetail(
navigator: Navigator,
monthKey: String,
categoryName: String,
)

fun navigateToMonthIncomeDetail(
navigator: Navigator,
monthKey: String,
categoryName: String,
Expand Down
Loading

0 comments on commit 0852a7f

Please sign in to comment.