2024-03-25 13:05:15 +01:00
|
|
|
import type { GoalFrequency } from "./Goal"
|
2024-04-05 00:08:40 +02:00
|
|
|
import type { Habit } from "./Habit"
|
|
|
|
import { HabitHistory } from "./HabitHistory"
|
2024-04-11 23:03:45 +02:00
|
|
|
import type { HabitProgress } from "./HabitProgress"
|
2024-03-16 00:36:44 +01:00
|
|
|
|
2024-03-22 10:23:28 +01:00
|
|
|
export interface HabitsTrackerData {
|
2024-03-25 13:05:15 +01:00
|
|
|
habitsHistory: {
|
|
|
|
[key in GoalFrequency]: HabitHistory[]
|
|
|
|
}
|
2024-03-16 00:36:44 +01:00
|
|
|
}
|
|
|
|
|
2024-03-22 10:23:28 +01:00
|
|
|
export class HabitsTracker implements HabitsTrackerData {
|
2024-03-25 13:05:15 +01:00
|
|
|
public habitsHistory: HabitsTrackerData["habitsHistory"]
|
2024-03-16 00:36:44 +01:00
|
|
|
|
2024-03-22 10:23:28 +01:00
|
|
|
public constructor(options: HabitsTrackerData) {
|
2024-03-16 00:36:44 +01:00
|
|
|
const { habitsHistory } = options
|
|
|
|
this.habitsHistory = habitsHistory
|
|
|
|
}
|
|
|
|
|
|
|
|
public static default(): HabitsTracker {
|
2024-03-25 13:05:15 +01:00
|
|
|
return new HabitsTracker({
|
|
|
|
habitsHistory: {
|
|
|
|
daily: [],
|
|
|
|
weekly: [],
|
|
|
|
monthly: [],
|
|
|
|
},
|
|
|
|
})
|
2024-03-16 00:36:44 +01:00
|
|
|
}
|
2024-04-05 00:08:40 +02:00
|
|
|
|
|
|
|
public addHabit(habit: Habit): void {
|
|
|
|
this.habitsHistory[habit.goal.frequency].push(
|
|
|
|
new HabitHistory({
|
|
|
|
habit,
|
|
|
|
progressHistory: [],
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
}
|
2024-04-05 15:31:41 +02:00
|
|
|
|
2024-04-11 13:07:17 +02:00
|
|
|
public editHabit(habit: Habit): void {
|
|
|
|
const habitHistory = this.getHabitHistoryById(habit.id)
|
|
|
|
if (habitHistory == null) {
|
|
|
|
return
|
2024-04-11 12:32:09 +02:00
|
|
|
}
|
2024-04-11 13:07:17 +02:00
|
|
|
habitHistory.habit = habit
|
2024-04-11 12:32:09 +02:00
|
|
|
}
|
|
|
|
|
2024-04-11 23:03:45 +02:00
|
|
|
public updateHabitProgress(habitProgress: HabitProgress): void {
|
|
|
|
const habitHistory = this.getHabitHistoryById(habitProgress.habitId)
|
|
|
|
if (habitHistory == null) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const habitProgressSaved = habitHistory.progressHistory.find((progress) => {
|
|
|
|
return progress.id === habitProgress.id
|
|
|
|
})
|
|
|
|
if (habitProgressSaved == null) {
|
|
|
|
habitHistory.progressHistory = [
|
|
|
|
...habitHistory.progressHistory,
|
|
|
|
habitProgress,
|
|
|
|
]
|
|
|
|
return
|
|
|
|
}
|
|
|
|
habitProgressSaved.goalProgress = habitProgress.goalProgress
|
|
|
|
habitProgressSaved.date = habitProgress.date
|
|
|
|
}
|
|
|
|
|
2024-04-05 15:31:41 +02:00
|
|
|
public getAllHabitsHistory(): HabitHistory[] {
|
|
|
|
return [
|
|
|
|
...this.habitsHistory.daily,
|
|
|
|
...this.habitsHistory.weekly,
|
|
|
|
...this.habitsHistory.monthly,
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
public getHabitHistoryById(id: Habit["id"]): HabitHistory | undefined {
|
|
|
|
return this.getAllHabitsHistory().find((habitHistory) => {
|
|
|
|
return habitHistory.habit.id === id
|
|
|
|
})
|
|
|
|
}
|
2024-05-20 12:23:17 +02:00
|
|
|
|
|
|
|
public getHabitsHistoriesByDate({
|
|
|
|
selectedDate,
|
|
|
|
frequency,
|
|
|
|
}: {
|
|
|
|
selectedDate: Date
|
|
|
|
frequency: GoalFrequency
|
|
|
|
}): HabitHistory[] {
|
|
|
|
return this.habitsHistory[frequency].filter((habitItem) => {
|
|
|
|
const startDate = new Date(habitItem.habit.startDate)
|
|
|
|
startDate.setHours(0, 0, 0, 0)
|
|
|
|
|
|
|
|
return (
|
|
|
|
startDate <= selectedDate &&
|
|
|
|
(habitItem.habit.endDate == null ||
|
|
|
|
(habitItem.habit.endDate != null &&
|
|
|
|
habitItem.habit.endDate >= selectedDate))
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
2024-05-22 13:44:54 +02:00
|
|
|
|
|
|
|
public getHabitsStatisticsByDateAndFrequency({
|
|
|
|
selectedDate,
|
|
|
|
frequency,
|
|
|
|
}: {
|
|
|
|
selectedDate: Date
|
|
|
|
frequency: GoalFrequency
|
|
|
|
}): {
|
|
|
|
totalGoalsSuccess: number
|
|
|
|
totalGoals: number
|
|
|
|
} {
|
|
|
|
const habitsHistory = this.getHabitsHistoriesByDate({
|
|
|
|
selectedDate,
|
|
|
|
frequency,
|
|
|
|
})
|
|
|
|
let totalGoalsSuccess = 0
|
|
|
|
const totalGoals = habitsHistory.length
|
|
|
|
for (const habitHistory of habitsHistory) {
|
|
|
|
const goalProgress = habitHistory.getGoalProgressByDate(selectedDate)
|
|
|
|
if (goalProgress.isCompleted()) {
|
|
|
|
totalGoalsSuccess++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return { totalGoalsSuccess, totalGoals }
|
|
|
|
}
|
2024-03-16 00:36:44 +01:00
|
|
|
}
|