@@ -5,6 +5,7 @@ import com.example.util.simpletimetracker.domain.record.mapper.RangeMapper
55import com.example.util.simpletimetracker.domain.record.model.Range
66import com.example.util.simpletimetracker.domain.statistics.model.RangeLength
77import com.example.util.simpletimetracker.domain.record.model.Record
8+ import com.example.util.simpletimetracker.domain.record.model.RecordBase
89import com.example.util.simpletimetracker.domain.record.model.RunningRecord
910import java.lang.Long.max
1011import javax.inject.Inject
@@ -22,18 +23,6 @@ class GetCurrentRecordsDurationInteractor @Inject constructor(
2223 return getRangeCurrent(typeId, runningRecord, RangeLength .Day )
2324 }
2425
25- suspend fun getDailyCurrent (runningRecord : RunningRecord ): Result {
26- return getRangeCurrent(runningRecord.id, runningRecord, RangeLength .Day )
27- }
28-
29- suspend fun getWeeklyCurrent (runningRecord : RunningRecord ): Result {
30- return getRangeCurrent(runningRecord.id, runningRecord, RangeLength .Week )
31- }
32-
33- suspend fun getMonthlyCurrent (runningRecord : RunningRecord ): Result {
34- return getRangeCurrent(runningRecord.id, runningRecord, RangeLength .Month )
35- }
36-
3726 suspend fun getAllCurrents (
3827 typeIds : List <Long >,
3928 runningRecords : List <RunningRecord >,
@@ -48,8 +37,30 @@ class GetCurrentRecordsDurationInteractor @Inject constructor(
4837
4938 return typeIds.associateWith { typeId ->
5039 getRangeCurrent(
51- filterRecords = { records -> records.filter { it.typeId == typeId } },
52- runningRecords = runningRecords.filter { it.id == typeId },
40+ filter = { record -> typeId in record.typeIds },
41+ allRunningRecords = runningRecords,
42+ range = range,
43+ rangeRecords = rangeRecords,
44+ )
45+ }
46+ }
47+
48+ suspend fun getAllCategoryCurrents (
49+ recordTypeCategories : Map <Long , List <Long >>,
50+ runningRecords : List <RunningRecord >,
51+ rangeLength : RangeLength ,
52+ ): Map <Long , Result > {
53+ val range = getRange(rangeLength)
54+ val rangeRecords = getRangeRecords(
55+ rangeLength = rangeLength,
56+ range = range,
57+ typeIds = recordTypeCategories.values.flatten().distinct(),
58+ )
59+
60+ return recordTypeCategories.mapValues { (_, typeIds) ->
61+ getRangeCurrent(
62+ filter = { record -> record.typeIds.any { it in typeIds } },
63+ allRunningRecords = runningRecords,
5364 range = range,
5465 rangeRecords = rangeRecords,
5566 )
@@ -67,12 +78,8 @@ class GetCurrentRecordsDurationInteractor @Inject constructor(
6778
6879 return tagIds.associateWith { tagId ->
6980 getRangeCurrent(
70- filterRecords = { records ->
71- records.filter { record -> record.tags.any { it.tagId == tagId } }
72- },
73- runningRecords = runningRecords.filter { record ->
74- record.tags.any { it.tagId in tagIds }
75- },
81+ filter = { record -> record.tags.any { it.tagId == tagId } },
82+ allRunningRecords = runningRecords,
7683 range = range,
7784 rangeRecords = rangeRecords,
7885 )
@@ -90,22 +97,10 @@ class GetCurrentRecordsDurationInteractor @Inject constructor(
9097 )
9198 }
9299
93- private suspend fun getRangeCurrent (
100+ suspend fun getRangeCurrent (
94101 typeId : Long ,
95102 runningRecord : RunningRecord ? ,
96103 rangeLength : RangeLength ,
97- ): Result {
98- return getRangeCurrent(
99- typeId = typeId,
100- runningRecords = listOfNotNull(runningRecord),
101- rangeLength = rangeLength,
102- )
103- }
104-
105- private suspend fun getRangeCurrent (
106- typeId : Long ,
107- runningRecords : List <RunningRecord >,
108- rangeLength : RangeLength ,
109104 ): Result {
110105 val range = getRange(rangeLength)
111106 val rangeRecords = getRangeRecords(
@@ -115,19 +110,20 @@ class GetCurrentRecordsDurationInteractor @Inject constructor(
115110 )
116111
117112 return getRangeCurrent(
118- filterRecords = { records -> records.filter { it. typeId == typeId } },
119- runningRecords = runningRecords ,
113+ filter = { record -> typeId in record.typeIds },
114+ allRunningRecords = listOfNotNull(runningRecord) ,
120115 range = range,
121116 rangeRecords = rangeRecords,
122117 )
123118 }
124119
125120 private fun getRangeCurrent (
126- filterRecords : (List < Record > ) -> List < Record > ,
127- runningRecords : List <RunningRecord >,
121+ filter : (RecordBase ) -> Boolean ,
122+ allRunningRecords : List <RunningRecord >,
128123 range : Range ,
129124 rangeRecords : List <Record >,
130125 ): Result {
126+ val runningRecords = allRunningRecords.filter(filter)
131127 val current = System .currentTimeMillis()
132128 val currentRunning = runningRecords.sumOf { runningRecord ->
133129 current - runningRecord.timeStarted
@@ -137,7 +133,7 @@ class GetCurrentRecordsDurationInteractor @Inject constructor(
137133 }
138134 val currentRunningCount = runningRecords.size
139135
140- val records = filterRecords( rangeRecords)
136+ val records = rangeRecords.filter(filter )
141137 .map { rangeMapper.clampToRange(it, range) }
142138 val duration = records
143139 .let (rangeMapper::mapToDuration)
0 commit comments