Skip to content

Commit de094f3

Browse files
committed
Fix app crush when habit starts and ends in the future
App crashed when requested streaks for a habit that starts and ends in future.
1 parent c71de57 commit de094f3

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

.idea/deploymentTargetDropDown.xml

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/domain/src/main/java/com/rendox/routinetracker/core/domain/streak/GetAllStreaksWithCashingUseCase.kt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,19 @@ class GetAllStreaksWithCashingUseCase(
4848
val computedPeriods = mutableListOf<LocalDateRange>()
4949

5050
val firstDateToLookFor = habit.schedule.startDate
51-
var period = getPeriodRange(habit.schedule, firstDateToLookFor)
51+
var period = getPeriodRange(habit.schedule, firstDateToLookFor)!!
5252

53-
val habitEndDate = habit.schedule.endDate
54-
55-
while (!(habitEndDate != null && period.start > habitEndDate) && !(period.contains(today))) {
53+
while (!period.contains(today)) {
5654
val cashedStreaksContainsPeriod = cashedPeriods.any { cashedPeriod ->
5755
period.isSubsetOf(cashedPeriod)
5856
}
5957
if (cashedStreaksContainsPeriod) {
60-
period = getPeriodRange(habit.schedule, period.endInclusive.plusDays(1))
58+
period = getPeriodRange(habit.schedule, period.endInclusive.plusDays(1)) ?: break
6159
continue
6260
}
6361
computedStreaks.addAll(getStreaksInPeriod(habit, period, today))
6462
computedPeriods.add(period)
65-
period = getPeriodRange(habit.schedule, period.endInclusive.plusDays(1))
63+
period = getPeriodRange(habit.schedule, period.endInclusive.plusDays(1)) ?: break
6664
}
6765
val distinctComputedStreaks = computedStreaks.distinct()
6866
val actuallyNeedToInsertComputedStreaks = computedPeriods.isNotEmpty()
@@ -110,8 +108,8 @@ class GetAllStreaksWithCashingUseCase(
110108
private fun getPeriodRange(
111109
schedule: Schedule,
112110
currentDate: LocalDate,
113-
): LocalDateRange = when (schedule) {
114-
is Schedule.PeriodicSchedule -> schedule.getPeriodRange(currentDate)!!
111+
): LocalDateRange? = when (schedule) {
112+
is Schedule.PeriodicSchedule -> schedule.getPeriodRange(currentDate)
115113
is Schedule.EveryDaySchedule -> currentDate.withDayOfMonth(1)..currentDate.atEndOfMonth
116114
is Schedule.CustomDateSchedule -> throw IllegalArgumentException()
117115
}

0 commit comments

Comments
 (0)