import { Card, Text } from "react-native-paper" import CircularProgress from "react-native-circular-progress-indicator" import { Agenda } from "react-native-calendars" import { useState } from "react" import { getNowDate, getISODate } from "@/utils/dates" import type { HabitsTracker } from "@/domain/entities/HabitsTracker" export interface StatsProps { habitsTracker: HabitsTracker } export const Stats: React.FC = (props) => { const { habitsTracker } = props const today = getNowDate() const todayISO = getISODate(today) const [selectedDate, setSelectedDate] = useState(today) const selectedDateISO = getISODate(selectedDate) const habitsHistory = habitsTracker.getAllHabitsHistory() let goalDays = 0 let totalGoalDays = 0 const dailyHabits = habitsHistory.filter((el) => { return ( el.habit.goal.frequency === "daily" && el.habit.startDate <= selectedDate ) }) let displayDaily = true if (dailyHabits.length === 0) { displayDaily = false } else { for (const el of dailyHabits) { totalGoalDays++ if ( el.getProgressesByDate(selectedDate)[0]?.goalProgress.isCompleted() ?? false ) { goalDays++ } } } let goalWeek = 0 let totalGoalWeek = 0 const weeklyHabits = habitsHistory.filter((el) => { return ( el.habit.goal.frequency === "weekly" && el.habit.startDate <= selectedDate ) }) let displayWeekly = true if (weeklyHabits.length === 0) { displayWeekly = false } else { for (const el of weeklyHabits) { totalGoalWeek++ if ( el.getProgressesByDate(selectedDate)[0]?.goalProgress.isCompleted() ?? false ) { goalWeek++ } } } let goalMonth = 0 let totalGoalMonth = 0 const monthlyHabits = habitsHistory.filter((el) => { return ( el.habit.goal.frequency === "monthly" && el.habit.startDate <= selectedDate ) }) let displayMonthly = true if (monthlyHabits.length === 0) { displayMonthly = false } else { for (const el of monthlyHabits) { totalGoalMonth++ if ( el.getProgressesByDate(selectedDate)[0]?.goalProgress.isCompleted() ?? false ) { goalMonth++ } } } return ( { setSelectedDate(new Date(date.dateString)) }} markedDates={{ [todayISO]: { marked: true, today: true }, }} maxDate={todayISO} selected={selectedDateISO} renderList={() => { return ( <> {displayDaily ? ( {goalDays} but réussi dans la journée sur {totalGoalDays} ) : null} {displayWeekly ? ( {goalWeek} but réussi dans la semaine sur {totalGoalWeek} ) : null} {displayMonthly ? ( {goalMonth} but réussi dans le mois sur {totalGoalMonth} ) : null} ) }} /> ) }