Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite logic of habit status computation #4

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## 2023-12-20

### Changed
- Rewrite logic of habit status computation
- Refactor routine to habit
- Use HabitStatus instead of HistoricalStatus and PlanningStatus
- Add additional statuses for clarity
- Rewrite logic of habit status computation so that both historical and future statuses can be computed on the go instead of being cached in the database. The only data that needs to be stored is the number of times completed.
Comment on lines +1 to +8
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CHANGELOG.md update

Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.rendox.routinetracker.core.data.routine

import com.rendox.routinetracker.core.model.Routine
import com.rendox.routinetracker.core.model.Habit
import kotlinx.datetime.LocalTime

interface RoutineRepository {

suspend fun getRoutineById(id: Long): Routine
suspend fun getRoutineById(id: Long): Habit

suspend fun insertRoutine(routine: Routine)
suspend fun insertRoutine(habit: Habit)

suspend fun getAllRoutines(): List<Routine>
suspend fun getAllRoutines(): List<Habit>

suspend fun updateDueDateSpecificCompletionTime(
time: LocalTime, routineId: Long, dueDateNumber: Int
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package com.rendox.routinetracker.core.data.routine

import com.rendox.routinetracker.core.database.routine.RoutineLocalDataSource
import com.rendox.routinetracker.core.model.Routine
import com.rendox.routinetracker.core.model.Habit
import kotlinx.datetime.LocalTime

class RoutineRepositoryImpl(
private val localDataSource: RoutineLocalDataSource,
) : RoutineRepository {

override suspend fun getRoutineById(id: Long): Routine {
override suspend fun getRoutineById(id: Long): Habit {
return localDataSource.getRoutineById(routineId = id)
}

override suspend fun insertRoutine(routine: Routine) {
localDataSource.insertRoutine(routine)
override suspend fun insertRoutine(habit: Habit) {
localDataSource.insertRoutine(habit)
}

override suspend fun getAllRoutines(): List<Routine> {
override suspend fun getAllRoutines(): List<Habit> {
return localDataSource.getAllRoutines()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.rendox.routinetracker.core.database.routine

import com.rendox.routinetracker.core.model.Routine
import com.rendox.routinetracker.core.model.Habit
import kotlinx.datetime.LocalTime

interface RoutineLocalDataSource {

suspend fun getRoutineById(routineId: Long): Routine
suspend fun getRoutineById(routineId: Long): Habit

suspend fun insertRoutine(routine: Routine)
suspend fun insertRoutine(habit: Habit)

suspend fun getAllRoutines(): List<Routine>
suspend fun getAllRoutines(): List<Habit>

suspend fun updateDueDateSpecificCompletionTime(
newTime: LocalTime, routineId: Long, dueDateNumber: Int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import com.rendox.routinetracker.core.database.routine.model.toExternalModel
import com.rendox.routinetracker.core.database.schedule.GetCompletionTime
import com.rendox.routinetracker.core.database.schedule.ScheduleEntity
import com.rendox.routinetracker.core.logic.time.WeekDayMonthRelated
import com.rendox.routinetracker.core.model.Routine
import com.rendox.routinetracker.core.model.Habit
import com.rendox.routinetracker.core.model.Schedule
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.withContext
Expand All @@ -21,7 +21,7 @@ class RoutineLocalDataSourceImpl(
private val dispatcher: CoroutineDispatcher,
) : RoutineLocalDataSource {

override suspend fun getRoutineById(routineId: Long): Routine {
override suspend fun getRoutineById(routineId: Long): Habit {
return withContext(dispatcher) {
db.routineEntityQueries.transactionWithResult {
val schedule = getScheduleEntity(routineId).toExternalModel(
Expand Down Expand Up @@ -53,30 +53,30 @@ class RoutineLocalDataSourceImpl(
)
}

override suspend fun insertRoutine(routine: Routine) {
override suspend fun insertRoutine(habit: Habit) {
return withContext(dispatcher) {
db.routineEntityQueries.transaction {
when (routine) {
is Routine.YesNoRoutine -> {
insertYesNoRoutine(routine)
insertSchedule(routine.schedule)
when (habit) {
is Habit.YesNoHabit -> {
insertYesNoRoutine(habit)
insertSchedule(habit.schedule)
}
}
}
}
}

@Suppress("UnusedReceiverParameter")
private fun TransactionWithoutReturn.insertYesNoRoutine(routine: Routine.YesNoRoutine) {
private fun TransactionWithoutReturn.insertYesNoRoutine(habit: Habit.YesNoHabit) {
db.routineEntityQueries.insertRoutine(
id = routine.id,
id = habit.id,
type = RoutineType.YesNoRoutine,
name = routine.name,
description = routine.description,
sessionDurationMinutes = routine.sessionDurationMinutes,
progress = routine.progress,
defaultCompletionTimeHour = routine.defaultCompletionTime?.hour,
defaultCompletionTimeMinute = routine.defaultCompletionTime?.minute,
name = habit.name,
description = habit.description,
sessionDurationMinutes = habit.sessionDurationMinutes,
progress = habit.progress,
defaultCompletionTimeHour = habit.defaultCompletionTime?.hour,
defaultCompletionTimeMinute = habit.defaultCompletionTime?.minute,
)
}

Expand Down Expand Up @@ -127,10 +127,10 @@ class RoutineLocalDataSourceImpl(
db.scheduleEntityQueries.insertSchedule(
id = null,
type = ScheduleType.EveryDaySchedule,
routineStartDate = schedule.routineStartDate,
routineEndDate = schedule.routineEndDate,
routineStartDate = schedule.startDate,
routineEndDate = schedule.endDate,
backlogEnabled = schedule.backlogEnabled,
cancelDuenessIfDoneAhead = schedule.cancelDuenessIfDoneAhead,
cancelDuenessIfDoneAhead = schedule.completingAheadEnabled,
vacationStartDate = schedule.vacationStartDate,
vacationEndDate = schedule.vacationEndDate,
startDayOfWeekInWeeklySchedule = null,
Expand All @@ -147,10 +147,10 @@ class RoutineLocalDataSourceImpl(
db.scheduleEntityQueries.insertSchedule(
id = null,
type = ScheduleType.WeeklyScheduleByDueDaysOfWeek,
routineStartDate = schedule.routineStartDate,
routineEndDate = schedule.routineEndDate,
routineStartDate = schedule.startDate,
routineEndDate = schedule.endDate,
backlogEnabled = schedule.backlogEnabled,
cancelDuenessIfDoneAhead = schedule.cancelDuenessIfDoneAhead,
cancelDuenessIfDoneAhead = schedule.completingAheadEnabled,
vacationStartDate = schedule.vacationStartDate,
vacationEndDate = schedule.vacationEndDate,
startDayOfWeekInWeeklySchedule = schedule.startDayOfWeek,
Expand All @@ -167,10 +167,10 @@ class RoutineLocalDataSourceImpl(
db.scheduleEntityQueries.insertSchedule(
id = null,
type = ScheduleType.WeeklyScheduleByNumOfDueDays,
routineStartDate = schedule.routineStartDate,
routineEndDate = schedule.routineEndDate,
routineStartDate = schedule.startDate,
routineEndDate = schedule.endDate,
backlogEnabled = schedule.backlogEnabled,
cancelDuenessIfDoneAhead = schedule.cancelDuenessIfDoneAhead,
cancelDuenessIfDoneAhead = schedule.completingAheadEnabled,
vacationStartDate = schedule.vacationStartDate,
vacationEndDate = schedule.vacationEndDate,
startDayOfWeekInWeeklySchedule = schedule.startDayOfWeek,
Expand All @@ -187,10 +187,10 @@ class RoutineLocalDataSourceImpl(
db.scheduleEntityQueries.insertSchedule(
id = null,
type = ScheduleType.MonthlyScheduleByDueDatesIndices,
routineStartDate = schedule.routineStartDate,
routineEndDate = schedule.routineEndDate,
routineStartDate = schedule.startDate,
routineEndDate = schedule.endDate,
backlogEnabled = schedule.backlogEnabled,
cancelDuenessIfDoneAhead = schedule.cancelDuenessIfDoneAhead,
cancelDuenessIfDoneAhead = schedule.completingAheadEnabled,
vacationStartDate = schedule.vacationStartDate,
vacationEndDate = schedule.vacationEndDate,
startDayOfWeekInWeeklySchedule = null,
Expand All @@ -207,10 +207,10 @@ class RoutineLocalDataSourceImpl(
db.scheduleEntityQueries.insertSchedule(
id = null,
type = ScheduleType.MonthlyScheduleByNumOfDueDays,
routineStartDate = schedule.routineStartDate,
routineEndDate = schedule.routineEndDate,
routineStartDate = schedule.startDate,
routineEndDate = schedule.endDate,
backlogEnabled = schedule.backlogEnabled,
cancelDuenessIfDoneAhead = schedule.cancelDuenessIfDoneAhead,
cancelDuenessIfDoneAhead = schedule.completingAheadEnabled,
vacationStartDate = schedule.vacationStartDate,
vacationEndDate = schedule.vacationEndDate,
startDayOfWeekInWeeklySchedule = null,
Expand Down Expand Up @@ -238,10 +238,10 @@ class RoutineLocalDataSourceImpl(
db.scheduleEntityQueries.insertSchedule(
id = null,
type = ScheduleType.PeriodicCustomSchedule,
routineStartDate = schedule.routineStartDate,
routineEndDate = schedule.routineEndDate,
routineStartDate = schedule.startDate,
routineEndDate = schedule.endDate,
backlogEnabled = schedule.backlogEnabled,
cancelDuenessIfDoneAhead = schedule.cancelDuenessIfDoneAhead,
cancelDuenessIfDoneAhead = schedule.completingAheadEnabled,
vacationStartDate = schedule.vacationStartDate,
vacationEndDate = schedule.vacationEndDate,
startDayOfWeekInWeeklySchedule = null,
Expand All @@ -258,10 +258,10 @@ class RoutineLocalDataSourceImpl(
db.scheduleEntityQueries.insertSchedule(
id = null,
type = ScheduleType.CustomDateSchedule,
routineStartDate = schedule.routineStartDate,
routineEndDate = schedule.routineEndDate,
routineStartDate = schedule.startDate,
routineEndDate = schedule.endDate,
backlogEnabled = schedule.backlogEnabled,
cancelDuenessIfDoneAhead = schedule.cancelDuenessIfDoneAhead,
cancelDuenessIfDoneAhead = schedule.completingAheadEnabled,
vacationStartDate = schedule.vacationStartDate,
vacationEndDate = schedule.vacationEndDate,
startDayOfWeekInWeeklySchedule = null,
Expand All @@ -278,10 +278,10 @@ class RoutineLocalDataSourceImpl(
db.scheduleEntityQueries.insertSchedule(
id = null,
type = ScheduleType.AnnualScheduleByDueDates,
routineStartDate = schedule.routineStartDate,
routineEndDate = schedule.routineEndDate,
routineStartDate = schedule.startDate,
routineEndDate = schedule.endDate,
backlogEnabled = schedule.backlogEnabled,
cancelDuenessIfDoneAhead = schedule.cancelDuenessIfDoneAhead,
cancelDuenessIfDoneAhead = schedule.completingAheadEnabled,
vacationStartDate = schedule.vacationStartDate,
vacationEndDate = schedule.vacationEndDate,
startDayOfWeekInWeeklySchedule = null,
Expand All @@ -298,10 +298,10 @@ class RoutineLocalDataSourceImpl(
db.scheduleEntityQueries.insertSchedule(
id = null,
type = ScheduleType.AnnualScheduleByNumOfDueDays,
routineStartDate = schedule.routineStartDate,
routineEndDate = schedule.routineEndDate,
routineStartDate = schedule.startDate,
routineEndDate = schedule.endDate,
backlogEnabled = schedule.backlogEnabled,
cancelDuenessIfDoneAhead = schedule.cancelDuenessIfDoneAhead,
cancelDuenessIfDoneAhead = schedule.completingAheadEnabled,
vacationStartDate = schedule.vacationStartDate,
vacationEndDate = schedule.vacationEndDate,
startDayOfWeekInWeeklySchedule = null,
Expand All @@ -327,7 +327,7 @@ class RoutineLocalDataSourceImpl(
}
}

override suspend fun getAllRoutines(): List<Routine> {
override suspend fun getAllRoutines(): List<Habit> {
return withContext(dispatcher) {
db.routineEntityQueries.getAllRoutines()
.executeAsList()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.rendox.routinetracker.core.database.routine.model

import com.rendox.routinetracker.core.database.routine.RoutineEntity
import com.rendox.routinetracker.core.model.Routine
import com.rendox.routinetracker.core.model.Habit
import com.rendox.routinetracker.core.model.Schedule
import kotlinx.datetime.LocalTime

Expand All @@ -19,13 +19,13 @@ fun RoutineEntity.toExternalModel(schedule: Schedule) = when (this.type) {

internal fun RoutineEntity.toYesNoRoutine(
schedule: Schedule,
): Routine {
): Habit {
val defaultCompletionTime =
if (defaultCompletionTimeHour != null && defaultCompletionTimeMinute != null) {
LocalTime(hour = defaultCompletionTimeHour, minute = defaultCompletionTimeMinute)
} else null

return Routine.YesNoRoutine(
return Habit.YesNoHabit(
id = id,
name = name,
description = description,
Expand Down
Loading