Skip to content

Commit

Permalink
refactor: 최근 금액 데이터를 Repository에서 가공하도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
yangsooplus committed Jan 25, 2024
1 parent 92c49a6 commit cd8fea7
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 37 deletions.
2 changes: 2 additions & 0 deletions core/model/src/main/java/com/susu/core/model/MyStatistics.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ data class MyStatistics(
val mostRelationship: StatisticsElement = StatisticsElement(),
val mostSpentMonth: Int = 0,
val recentSpent: List<StatisticsElement> = emptyList(),
val recentTotalSpent: Int = 0,
val recentMaximumSpent: Int = 0,
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.susu.data.data.repository

import com.susu.core.model.MyStatistics
import com.susu.core.model.StatisticsElement
import com.susu.data.remote.api.StatisticsService
import com.susu.data.remote.model.response.toModel
import com.susu.domain.repository.StatisticsRepository
Expand All @@ -8,5 +10,20 @@ import javax.inject.Inject
class StatisticsRepositoryImpl @Inject constructor(
private val statisticsService: StatisticsService,
) : StatisticsRepository {
override suspend fun getMyStatistics() = statisticsService.getMyStatistics().getOrThrow().toModel()
override suspend fun getMyStatistics(): MyStatistics {
val originalStatistic = statisticsService.getMyStatistics().getOrThrow().toModel()
val sortedRecentSpent = originalStatistic.recentSpent.sortedBy { it.title.replace(".", "").toInt() } // api 수정 후 replace 제거
.map { StatisticsElement(title = it.title.removeRange(0 until 5).toInt().toString(), value = it.value) } // api 수정 후 range 수정

return MyStatistics(
highestAmountReceived = originalStatistic.highestAmountReceived,
highestAmountSent = originalStatistic.highestAmountSent,
mostCategory = originalStatistic.mostCategory,
mostRelationship = originalStatistic.mostRelationship,
mostSpentMonth = originalStatistic.mostSpentMonth % 100,
recentSpent = sortedRecentSpent,
recentTotalSpent = originalStatistic.recentTotalSpent,
recentMaximumSpent = originalStatistic.recentMaximumSpent,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ fun MyStatisticsResponse.toModel() = MyStatistics(
mostRelationship = mostRelationship.toModel(),
mostSpentMonth = mostSpentMonth,
recentSpent = recentSpent.map { it.toModel() },
recentMaximumSpent = recentSpent.maxOfOrNull { it.value } ?: 0,
recentTotalSpent = recentSpent.sumOf { it.value }
)

fun StatisticsElement.toModel() = com.susu.core.model.StatisticsElement(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,9 @@ fun RecentSpentGraph(
modifier: Modifier = Modifier,
isActive: Boolean = true,
spentData: List<StatisticsElement> = emptyList(),
totalAmount: Int = 0,
maximumAmount: Int = 0,
) {
val totalAmount by remember { mutableStateOf(spentData.sumOf { it.value }) }
val maximumAmount by remember { mutableStateOf(spentData.maxOfOrNull { it.value }) }

Column(
modifier = modifier
.fillMaxWidth()
Expand Down Expand Up @@ -105,42 +104,38 @@ fun RecentSpentGraph(
horizontalArrangement = Arrangement.Center,
verticalAlignment = Alignment.CenterVertically,
) {
if (maximumAmount != null) {
spentData.forEachIndexed { i, data ->
Column(
horizontalAlignment = Alignment.CenterHorizontally,
) {
StickGraph(
ratio = data.value.toFloat() / maximumAmount!!,
color = if (isActive) {
if (i == spentData.lastIndex) {
Orange60
} else {
Orange30
}
spentData.forEachIndexed { i, data ->
Column(
horizontalAlignment = Alignment.CenterHorizontally,
) {
StickGraph(
ratio = data.value.toFloat() / maximumAmount,
color = if (isActive) {
if (i == spentData.lastIndex) {
Orange60
} else {
if (i == spentData.lastIndex) {
Gray60
} else {
Gray30
}
},
)
Spacer(modifier = Modifier.height(SusuTheme.spacing.spacing_xxxxs))
Text(
text = stringResource(id = R.string.word_month_format, data.title),
style = SusuTheme.typography.title_xxxs,
color = if (i == spentData.lastIndex) {
Gray90
Orange30
}
} else {
if (i == spentData.lastIndex) {
Gray60
} else {
Gray40
},
)
}
Spacer(modifier = Modifier.width(SusuTheme.spacing.spacing_s))
Gray30
}
},
)
Spacer(modifier = Modifier.height(SusuTheme.spacing.spacing_xxxxs))
Text(
text = stringResource(id = R.string.word_month_format, data.title),
style = SusuTheme.typography.title_xxxs,
color = if (i == spentData.lastIndex) {
Gray90
} else {
Gray40
},
)
}
} else {
Spacer(modifier = Modifier.height(100.dp)) // 임시
Spacer(modifier = Modifier.width(SusuTheme.spacing.spacing_s))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ fun MyStatisticsContent(
RecentSpentGraph(
isActive = !isBlind,
spentData = uiState.statistics.recentSpent,
maximumAmount = uiState.statistics.recentMaximumSpent,
totalAmount = uiState.statistics.recentTotalSpent,
)
Row(
modifier = Modifier
Expand Down

0 comments on commit cd8fea7

Please sign in to comment.