@@ -8,17 +8,15 @@ import com.example.util.simpletimetracker.domain.notifications.interactor.Notifi
88import com.example.util.simpletimetracker.domain.category.interactor.RecordTypeCategoryInteractor
99import com.example.util.simpletimetracker.domain.recordType.interactor.RecordTypeGoalInteractor
1010import com.example.util.simpletimetracker.domain.record.interactor.RunningRecordInteractor
11- import com.example.util.simpletimetracker.domain.statistics.model.RangeLength
1211import com.example.util.simpletimetracker.domain.category.model.RecordTypeCategory
1312import com.example.util.simpletimetracker.domain.notifications.interactor.ActivityStartedStoppedBroadcastInteractor
1413import com.example.util.simpletimetracker.domain.recordType.model.RecordTypeGoal
1514import com.example.util.simpletimetracker.domain.recordType.model.RecordTypeGoal.Range
1615import com.example.util.simpletimetracker.domain.recordType.model.RecordTypeGoal.Type
1716import com.example.util.simpletimetracker.domain.record.model.RunningRecord
17+ import com.example.util.simpletimetracker.domain.recordType.extension.toRangeLength
1818import com.example.util.simpletimetracker.feature_notification.goalTime.manager.NotificationGoalTimeManager
1919import javax.inject.Inject
20- import kotlin.collections.component1
21- import kotlin.collections.component2
2220
2321class NotificationGoalCountInteractorImpl @Inject constructor(
2422 private val recordTypeGoalInteractor : RecordTypeGoalInteractor ,
@@ -46,7 +44,7 @@ class NotificationGoalCountInteractorImpl @Inject constructor(
4644 // No count goals - exit.
4745 if (goals.isEmpty()) return
4846
49- listOf ( Range . Daily , Range . Weekly , Range . Monthly ).forEach { goalRange ->
47+ getAllGoalRanges( ).forEach { goalRange ->
5048 checkType(
5149 goalRange = goalRange,
5250 goals = goals,
@@ -83,7 +81,7 @@ class NotificationGoalCountInteractorImpl @Inject constructor(
8381 // For each goal check current results.
8482 val runningRecords = runningRecordInteractor.getAll()
8583
86- listOf ( Range . Daily , Range . Weekly , Range . Monthly ).forEach { goalRange ->
84+ getAllGoalRanges( ).forEach { goalRange ->
8785 checkCategory(
8886 goalRange = goalRange,
8987 goals = affectedCategoryGoals,
@@ -117,7 +115,7 @@ class NotificationGoalCountInteractorImpl @Inject constructor(
117115 // For each goal check current results.
118116 val runningRecords = runningRecordInteractor.getAll()
119117
120- listOf ( Range . Daily , Range . Weekly , Range . Monthly ).forEach { goalRange ->
118+ getAllGoalRanges( ).forEach { goalRange ->
121119 checkTag(
122120 goalRange = goalRange,
123121 goals = affectedTagGoals,
@@ -135,12 +133,11 @@ class NotificationGoalCountInteractorImpl @Inject constructor(
135133 val goal = filterGoalsFromRange<RecordTypeGoal .IdData .Type >(goalRange, goals).firstOrNull()
136134 if (goal == null ) return
137135
138- val current = when (goalRange) {
139- is Range .Session -> return
140- is Range .Daily -> getCurrentRecordsDurationInteractor.getDailyCurrent(runningRecord)
141- is Range .Weekly -> getCurrentRecordsDurationInteractor.getWeeklyCurrent(runningRecord)
142- is Range .Monthly -> getCurrentRecordsDurationInteractor.getMonthlyCurrent(runningRecord)
143- }.count
136+ val current = getCurrentRecordsDurationInteractor.getRangeCurrent(
137+ typeId = runningRecord.id,
138+ runningRecord = runningRecord,
139+ rangeLength = goalRange.toRangeLength() ? : return ,
140+ ).count
144141
145142 if (current == goal.value) {
146143 show(
@@ -159,37 +156,15 @@ class NotificationGoalCountInteractorImpl @Inject constructor(
159156 val rangeGoals = filterGoalsFromRange<RecordTypeGoal .IdData .Category >(goalRange, goals)
160157 if (rangeGoals.isEmpty()) return
161158
162- val allTypeIdsFromTheseCategories = categoriesWithThisType.values
163- .flatten().distinct()
164- val allCurrents = getCurrentRecordsDurationInteractor.getAllCurrents(
165- typeIds = allTypeIdsFromTheseCategories,
159+ val allCurrents = getCurrentRecordsDurationInteractor.getAllCategoryCurrents(
160+ recordTypeCategories = categoriesWithThisType,
166161 runningRecords = runningRecords,
167- rangeLength = when (goalRange) {
168- is Range .Session -> return
169- is Range .Daily -> RangeLength .Day
170- is Range .Weekly -> RangeLength .Week
171- is Range .Monthly -> RangeLength .Month
172- },
162+ rangeLength = goalRange.toRangeLength() ? : return ,
173163 )
174164
175- val thisRangeCurrents = categoriesWithThisType.mapNotNull { (categoryId, typeIds) ->
176- val currents = allCurrents
177- .filter { it.key in typeIds }
178- .values
179- .toList()
180- val current = GetCurrentRecordsDurationInteractor .Result (
181- range = allCurrents.values.firstOrNull()?.range ? : return @mapNotNull null ,
182- duration = currents.sumOf { it.duration },
183- count = currents.sumOf { it.count },
184- durationDiffersFromCurrent = currents.any { it.durationDiffersFromCurrent },
185- )
186- categoryId to current
187- }.toMap()
188-
189165 rangeGoals.forEach { goal ->
190- val categoryId = (goal.idData as ? RecordTypeGoal .IdData .Category )?.value
191- ? : return @forEach
192- val current = thisRangeCurrents[categoryId]?.count.orZero()
166+ val categoryId = goal.idData.value
167+ val current = allCurrents[categoryId]?.count.orZero()
193168 if (current == goal.value) {
194169 show(
195170 idData = RecordTypeGoal .IdData .Category (categoryId),
@@ -211,17 +186,11 @@ class NotificationGoalCountInteractorImpl @Inject constructor(
211186 val allCurrents = getCurrentRecordsDurationInteractor.getAllTagCurrents(
212187 tagIds = allTags,
213188 runningRecords = runningRecords,
214- rangeLength = when (goalRange) {
215- is Range .Session -> return
216- is Range .Daily -> RangeLength .Day
217- is Range .Weekly -> RangeLength .Week
218- is Range .Monthly -> RangeLength .Month
219- },
189+ rangeLength = goalRange.toRangeLength() ? : return ,
220190 )
221191
222192 rangeGoals.forEach { goal ->
223- val tagId = (goal.idData as ? RecordTypeGoal .IdData .Tag )?.value
224- ? : return @forEach
193+ val tagId = goal.idData.value
225194 val current = allCurrents[tagId]?.count.orZero()
226195 if (current == goal.value) {
227196 show(
@@ -267,4 +236,8 @@ class NotificationGoalCountInteractorImpl @Inject constructor(
267236 )
268237 }
269238 }
239+
240+ private fun getAllGoalRanges (): List <Range > {
241+ return listOf (Range .Daily , Range .Weekly , Range .Monthly )
242+ }
270243}
0 commit comments