11package com.threegap.bitnagil.presentation.reporthistory
22
33import androidx.lifecycle.ViewModel
4+ import com.threegap.bitnagil.domain.report.usecase.GetReportHistoriesUseCase
45import com.threegap.bitnagil.presentation.reporthistory.model.ReportCategory
56import com.threegap.bitnagil.presentation.reporthistory.model.ReportHistoriesPerDayUiModel
67import com.threegap.bitnagil.presentation.reporthistory.model.ReportHistorySideEffect
@@ -14,73 +15,72 @@ import org.orbitmvi.orbit.viewmodel.container
1415import javax.inject.Inject
1516
1617@HiltViewModel
17- class ReportHistoryViewModel @Inject constructor() : ContainerHost<ReportHistoryState, ReportHistorySideEffect>, ViewModel() {
18+ class ReportHistoryViewModel @Inject constructor(
19+ private val getReportHistoriesUseCase : GetReportHistoriesUseCase ,
20+ ) : ContainerHost<ReportHistoryState, ReportHistorySideEffect>, ViewModel() {
1821 override val container: Container <ReportHistoryState , ReportHistorySideEffect > = container(initialState = ReportHistoryState .Init )
1922
2023 init {
2124 loadReportHistories()
2225 }
2326
2427 private fun loadReportHistories () = intent {
25- reduce {
26- state.copy(
27- reportHistoriesPerDays = List (10 ) {
28- ReportHistoriesPerDayUiModel (
29- date = java.time.LocalDate .now(),
30- reports = listOf (
31- ReportHistoryUiModel (
32- id = " 1" ,
33- title = " 제보 1" ,
34- imageUrl = " -" ,
35- location = " 서울특별시 성북구 안암로 106" ,
36- process = ReportProcess .Reported ,
37- category = ReportCategory .Amenities ,
38- ),
39- ReportHistoryUiModel (
40- id = " 1" ,
41- title = " 제보 1" ,
42- imageUrl = " -" ,
43- location = " 서울특별시 성북구 안암로 106" ,
44- process = ReportProcess .Progress ,
45- category = ReportCategory .Amenities ,
46- ),
47- ),
28+ getReportHistoriesUseCase.invoke().fold(
29+ onSuccess = { reportHistoriesPerDate ->
30+ val reportHistoriesPerDays = reportHistoriesPerDate
31+ .map { reportHistoryPerDateMap ->
32+ ReportHistoriesPerDayUiModel (
33+ date = reportHistoryPerDateMap.key,
34+ reports = reportHistoryPerDateMap.value.map {
35+ ReportHistoryUiModel .fromDomain(it)
36+ },
37+ )
38+ }
39+ .sortedByDescending { reportHistoryPerDate ->
40+ reportHistoryPerDate.date
41+ }
42+
43+ reduce {
44+ state.copy(
45+ reportHistoriesPerDays = reportHistoriesPerDays,
4846 )
49- },
50- )
51- }
47+ }
48+ },
49+ onFailure = {
50+ },
51+ )
5252 }
5353
5454 fun selectReportCategory (reportCategory : ReportCategory ) = intent {
5555 val currentSelectedReportCategory = state.selectedReportCategory
5656
5757 reduce {
5858 state.copy(
59- selectedReportCategory = if (currentSelectedReportCategory == reportCategory) null else reportCategory
59+ selectedReportCategory = if (currentSelectedReportCategory == reportCategory) null else reportCategory,
6060 )
6161 }
6262 }
6363
6464 fun selectReportProcess (reportProcess : ReportProcess ) = intent {
6565 reduce {
6666 state.copy(
67- selectedReportProcess = reportProcess
67+ selectedReportProcess = reportProcess,
6868 )
6969 }
7070 }
7171
7272 fun showReportCategoryBottomSheet () = intent {
7373 reduce {
7474 state.copy(
75- showSelectReportCategoryBottomSheet = true
75+ showSelectReportCategoryBottomSheet = true ,
7676 )
7777 }
7878 }
7979
8080 fun hideReportCategoryBottomSheet () = intent {
8181 reduce {
8282 state.copy(
83- showSelectReportCategoryBottomSheet = false
83+ showSelectReportCategoryBottomSheet = false ,
8484 )
8585 }
8686 }
0 commit comments