Skip to content

Commit c9cad81

Browse files
committed
refactor goal scheduling logic
1 parent 3947e27 commit c9cad81

File tree

12 files changed

+62
-82
lines changed

12 files changed

+62
-82
lines changed

core/src/main/java/com/example/util/simpletimetracker/core/interactor/GetCurrentRecordsDurationInteractor.kt

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import com.example.util.simpletimetracker.domain.mapper.RangeMapper
55
import com.example.util.simpletimetracker.domain.model.Range
66
import com.example.util.simpletimetracker.domain.model.RangeLength
77
import com.example.util.simpletimetracker.domain.model.Record
8-
import com.example.util.simpletimetracker.domain.model.RecordType
98
import com.example.util.simpletimetracker.domain.model.RunningRecord
109
import java.lang.Long.max
1110
import javax.inject.Inject
@@ -36,32 +35,32 @@ class GetCurrentRecordsDurationInteractor @Inject constructor(
3635
}
3736

3837
suspend fun getAllCurrents(
39-
typesMap: Map<Long, RecordType>,
38+
typeIds: List<Long>,
4039
runningRecords: List<RunningRecord>,
4140
rangeLength: RangeLength,
4241
): Map<Long, Result> {
4342
val range = getRange(rangeLength)
4443
val rangeRecords = recordInteractor.getFromRangeByType(
45-
typeIds = typesMap.keys.toList(),
46-
range = range
44+
typeIds = typeIds,
45+
range = range,
4746
)
4847

49-
return typesMap.map { (typeId, _) ->
50-
typeId to getRangeCurrent(
48+
return typeIds.associateWith { typeId ->
49+
getRangeCurrent(
5150
typeId = typeId,
5251
runningRecord = runningRecords.firstOrNull { it.id == typeId },
5352
range = range,
5453
rangeRecords = rangeRecords,
5554
)
56-
}.toMap()
55+
}
5756
}
5857

5958
suspend fun getAllDailyCurrents(
60-
typesMap: Map<Long, RecordType>,
59+
typeIds: List<Long>,
6160
runningRecords: List<RunningRecord>,
6261
): Map<Long, Result> {
6362
return getAllCurrents(
64-
typesMap = typesMap,
63+
typeIds = typeIds,
6564
runningRecords = runningRecords,
6665
rangeLength = RangeLength.Day,
6766
)

domain/src/main/java/com/example/util/simpletimetracker/domain/interactor/CategoryInteractor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ class CategoryInteractor @Inject constructor(
2727
}
2828

2929
suspend fun remove(id: Long) {
30-
categoryRepo.remove(id)
3130
recordTypeCategoryRepo.removeAll(id)
3231
recordTypeGoalRepo.removeByCategory(id)
32+
categoryRepo.remove(id)
3333
}
3434
}
Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
package com.example.util.simpletimetracker.domain.interactor
22

3-
import com.example.util.simpletimetracker.domain.model.RecordTypeGoal
4-
53
interface NotificationGoalCountInteractor {
64

75
suspend fun checkAndShow(typeId: Long)
8-
9-
fun cancel(idData: RecordTypeGoal.IdData)
106
}

domain/src/main/java/com/example/util/simpletimetracker/domain/interactor/NotificationGoalTimeInteractor.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import com.example.util.simpletimetracker.domain.model.RecordTypeGoal
44

55
interface NotificationGoalTimeInteractor {
66

7-
suspend fun checkAndReschedule()
8-
9-
suspend fun checkAndReschedule(typeIds: List<Long>)
7+
suspend fun checkAndReschedule(typeIds: List<Long> = emptyList())
108

119
fun cancel(idData: RecordTypeGoal.IdData)
1210

domain/src/main/java/com/example/util/simpletimetracker/domain/interactor/RemoveRunningRecordMediator.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.example.util.simpletimetracker.domain.interactor
22

3-
import com.example.util.simpletimetracker.domain.model.RecordTypeGoal
43
import com.example.util.simpletimetracker.domain.model.RunningRecord
54
import java.util.concurrent.TimeUnit
65
import javax.inject.Inject
@@ -12,7 +11,6 @@ class RemoveRunningRecordMediator @Inject constructor(
1211
private val notificationInactivityInteractor: NotificationInactivityInteractor,
1312
private val notificationActivityInteractor: NotificationActivityInteractor,
1413
private val notificationGoalTimeInteractor: NotificationGoalTimeInteractor,
15-
private val notificationGoalCountInteractor: NotificationGoalCountInteractor,
1614
private val widgetInteractor: WidgetInteractor,
1715
private val prefsInteractor: PrefsInteractor,
1816
private val activityStartedStoppedBroadcastInteractor: ActivityStartedStoppedBroadcastInteractor,
@@ -45,9 +43,9 @@ class RemoveRunningRecordMediator @Inject constructor(
4543
notificationTypeInteractor.checkAndHide(typeId)
4644
notificationInactivityInteractor.checkAndSchedule()
4745
// Cancel if no activity tracked.
48-
if (runningRecordInteractor.getAll().isEmpty()) notificationActivityInteractor.cancel()
49-
notificationGoalTimeInteractor.cancel(RecordTypeGoal.IdData.Type(typeId))
50-
notificationGoalCountInteractor.cancel(RecordTypeGoal.IdData.Type(typeId))
46+
val runningRecordIds = runningRecordInteractor.getAll().map { it.id }
47+
if (runningRecordIds.isEmpty()) notificationActivityInteractor.cancel()
48+
notificationGoalTimeInteractor.checkAndReschedule(runningRecordIds + typeId)
5149
widgetInteractor.updateWidgets()
5250
}
5351
}

features/feature_archive/src/main/java/com/example/util/simpletimetracker/feature_archive/viewModel/ArchiveViewModel.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import androidx.lifecycle.ViewModel
66
import androidx.lifecycle.viewModelScope
77
import com.example.util.simpletimetracker.core.extension.set
88
import com.example.util.simpletimetracker.core.repo.ResourceRepo
9+
import com.example.util.simpletimetracker.domain.interactor.NotificationGoalTimeInteractor
910
import com.example.util.simpletimetracker.domain.interactor.NotificationTypeInteractor
1011
import com.example.util.simpletimetracker.domain.interactor.RecordTagInteractor
1112
import com.example.util.simpletimetracker.domain.interactor.RecordTypeInteractor
@@ -31,6 +32,7 @@ class ArchiveViewModel @Inject constructor(
3132
private val notificationTypeInteractor: NotificationTypeInteractor,
3233
private val recordTypeInteractor: RecordTypeInteractor,
3334
private val recordTagInteractor: RecordTagInteractor,
35+
private val notificationGoalTimeInteractor: NotificationGoalTimeInteractor,
3436
) : ViewModel() {
3537

3638
val viewData: LiveData<ArchiveViewData> by lazy {
@@ -102,6 +104,7 @@ class ArchiveViewModel @Inject constructor(
102104
val message = when (params) {
103105
is ArchiveDialogParams.Activity -> {
104106
recordTypeInteractor.remove(params.id)
107+
notificationGoalTimeInteractor.checkAndReschedule(listOf(params.id))
105108
resourceRepo.getString(R.string.archive_activity_deleted)
106109
}
107110
is ArchiveDialogParams.RecordTag -> {

features/feature_notification/src/main/java/com/example/util/simpletimetracker/feature_notification/goalTime/interactor/NotificationGoalCountInteractorImpl.kt

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ import com.example.util.simpletimetracker.domain.extension.value
66
import com.example.util.simpletimetracker.domain.interactor.NotificationGoalCountInteractor
77
import com.example.util.simpletimetracker.domain.interactor.RecordTypeCategoryInteractor
88
import com.example.util.simpletimetracker.domain.interactor.RecordTypeGoalInteractor
9-
import com.example.util.simpletimetracker.domain.interactor.RecordTypeInteractor
109
import com.example.util.simpletimetracker.domain.interactor.RunningRecordInteractor
1110
import com.example.util.simpletimetracker.domain.model.RangeLength
12-
import com.example.util.simpletimetracker.domain.model.RecordType
1311
import com.example.util.simpletimetracker.domain.model.RecordTypeCategory
1412
import com.example.util.simpletimetracker.domain.model.RecordTypeGoal
1513
import com.example.util.simpletimetracker.domain.model.RecordTypeGoal.Range
@@ -19,7 +17,6 @@ import com.example.util.simpletimetracker.feature_notification.goalTime.manager.
1917
import javax.inject.Inject
2018

2119
class NotificationGoalCountInteractorImpl @Inject constructor(
22-
private val recordTypeInteractor: RecordTypeInteractor,
2320
private val recordTypeGoalInteractor: RecordTypeGoalInteractor,
2421
private val runningRecordInteractor: RunningRecordInteractor,
2522
private val getCurrentRecordsDurationInteractor: GetCurrentRecordsDurationInteractor,
@@ -33,19 +30,9 @@ class NotificationGoalCountInteractorImpl @Inject constructor(
3330
checkAndShowCategory(typeId)
3431
}
3532

36-
override fun cancel(idData: RecordTypeGoal.IdData) {
37-
listOf(
38-
Range.Session,
39-
Range.Daily,
40-
Range.Weekly,
41-
Range.Monthly,
42-
).forEach {
43-
manager.hide(idData, it)
44-
}
45-
}
46-
4733
private suspend fun checkAndShowType(typeId: Long) {
48-
val runningRecord = runningRecordInteractor.get(typeId) ?: return
34+
val runningRecord = runningRecordInteractor.get(typeId)
35+
?: return
4936
val goals = recordTypeGoalInteractor.getByType(typeId)
5037
.filter { it.type is Type.Count }
5138

@@ -100,13 +87,11 @@ class NotificationGoalCountInteractorImpl @Inject constructor(
10087

10188
// For each goal check current results.
10289
val runningRecords = runningRecordInteractor.getAll()
103-
val typesMap = recordTypeInteractor.getAll().associateBy(RecordType::id)
10490

10591
// Daily
10692
checkCategory(
10793
goalRange = Range.Daily,
10894
goals = affectedCategoryGoals,
109-
typesMap = typesMap,
11095
runningRecords = runningRecords,
11196
categoriesWithThisType = categoriesWithThisType,
11297
)
@@ -115,7 +100,6 @@ class NotificationGoalCountInteractorImpl @Inject constructor(
115100
checkCategory(
116101
goalRange = Range.Weekly,
117102
goals = affectedCategoryGoals,
118-
typesMap = typesMap,
119103
runningRecords = runningRecords,
120104
categoriesWithThisType = categoriesWithThisType,
121105
)
@@ -124,7 +108,6 @@ class NotificationGoalCountInteractorImpl @Inject constructor(
124108
checkCategory(
125109
goalRange = Range.Monthly,
126110
goals = affectedCategoryGoals,
127-
typesMap = typesMap,
128111
runningRecords = runningRecords,
129112
categoriesWithThisType = categoriesWithThisType,
130113
)
@@ -143,7 +126,7 @@ class NotificationGoalCountInteractorImpl @Inject constructor(
143126
}
144127

145128
if (goal.value > 1) {
146-
val current = when (goalRange) {
129+
val current = when (goalRange) {
147130
is Range.Session -> return
148131
is Range.Daily -> getCurrentRecordsDurationInteractor.getDailyCurrent(runningRecord)
149132
is Range.Weekly -> getCurrentRecordsDurationInteractor.getWeeklyCurrent(runningRecord)
@@ -162,7 +145,6 @@ class NotificationGoalCountInteractorImpl @Inject constructor(
162145
private suspend fun checkCategory(
163146
goalRange: Range,
164147
goals: List<RecordTypeGoal>,
165-
typesMap: Map<Long, RecordType>,
166148
runningRecords: List<RunningRecord>,
167149
categoriesWithThisType: Map<Long, List<Long>>,
168150
) {
@@ -174,9 +156,10 @@ class NotificationGoalCountInteractorImpl @Inject constructor(
174156
}
175157
if (rangeGoals.isEmpty()) return
176158

177-
val allTypeIdsFromTheseCategories = categoriesWithThisType.values.flatten().toSet()
159+
val allTypeIdsFromTheseCategories = categoriesWithThisType.values
160+
.flatten().toSet().toList()
178161
val allCurrents = getCurrentRecordsDurationInteractor.getAllCurrents(
179-
typesMap = typesMap.filterKeys { it in allTypeIdsFromTheseCategories },
162+
typeIds = allTypeIdsFromTheseCategories,
180163
runningRecords = runningRecords,
181164
rangeLength = when (goalRange) {
182165
is Range.Session -> return
@@ -227,6 +210,6 @@ class NotificationGoalCountInteractorImpl @Inject constructor(
227210
idData = idData,
228211
range = goalRange,
229212
type = NotificationGoalParamsInteractor.Type.Count,
230-
).let(manager::show)
213+
)?.let(manager::show)
231214
}
232215
}

features/feature_notification/src/main/java/com/example/util/simpletimetracker/feature_notification/goalTime/interactor/NotificationGoalParamsInteractor.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,12 @@ class NotificationGoalParamsInteractor @Inject constructor(
4040
idData: RecordTypeGoal.IdData,
4141
range: RecordTypeGoal.Range,
4242
type: Type,
43-
): NotificationGoalTimeParams {
43+
): NotificationGoalTimeParams? {
4444
val typeId = (idData as? RecordTypeGoal.IdData.Type)?.value
4545
val recordType = recordTypeInteractor.get(typeId.orZero())
4646
val categoryId = (idData as? RecordTypeGoal.IdData.Category)?.value
4747
val category = categoryInteractor.get(categoryId.orZero())
48+
if (recordType == null && category == null) return null
4849
val goals = when (idData) {
4950
is RecordTypeGoal.IdData.Type -> {
5051
recordTypeGoalInteractor.getByType(typeId.orZero())

0 commit comments

Comments
 (0)