2024-04-11 23:03:45 +02:00
|
|
|
import LottieView from "lottie-react-native"
|
|
|
|
import { useRef, useState } from "react"
|
|
|
|
import { Dimensions, ScrollView, View } from "react-native"
|
2024-05-20 12:23:17 +02:00
|
|
|
import { Divider, List, Text } from "react-native-paper"
|
2024-04-08 23:21:36 +02:00
|
|
|
|
2024-05-20 12:23:17 +02:00
|
|
|
import { GOAL_FREQUENCIES, type GoalFrequency } from "@/domain/entities/Goal"
|
|
|
|
import type { HabitHistory } from "@/domain/entities/HabitHistory"
|
2024-04-08 23:21:36 +02:00
|
|
|
import type { HabitsTracker } from "@/domain/entities/HabitsTracker"
|
|
|
|
import { capitalize } from "@/utils/strings"
|
2024-05-02 23:48:47 +02:00
|
|
|
import confettiJSON from "../../../assets/confetti.json"
|
2024-04-08 23:21:36 +02:00
|
|
|
import { HabitCard } from "./HabitCard"
|
|
|
|
|
|
|
|
export interface HabitsListProps {
|
|
|
|
habitsTracker: HabitsTracker
|
|
|
|
selectedDate: Date
|
|
|
|
}
|
|
|
|
|
|
|
|
export const HabitsList: React.FC<HabitsListProps> = (props) => {
|
2024-05-20 12:23:17 +02:00
|
|
|
const { habitsTracker, selectedDate } = props
|
2024-04-08 23:21:36 +02:00
|
|
|
|
|
|
|
const [accordionExpanded, setAccordionExpanded] = useState<{
|
|
|
|
[key in GoalFrequency]: boolean
|
|
|
|
}>({
|
|
|
|
daily: true,
|
|
|
|
weekly: true,
|
|
|
|
monthly: true,
|
|
|
|
})
|
|
|
|
|
2024-04-11 23:03:45 +02:00
|
|
|
const confettiRef = useRef<LottieView | null>(null)
|
|
|
|
|
2024-05-20 12:23:17 +02:00
|
|
|
const habitsHistoriesByFrequency: Record<GoalFrequency, HabitHistory[]> = {
|
|
|
|
daily: habitsTracker.getHabitsHistoriesByDate({
|
|
|
|
selectedDate,
|
|
|
|
frequency: "daily",
|
|
|
|
}),
|
|
|
|
weekly: habitsTracker.getHabitsHistoriesByDate({
|
|
|
|
selectedDate,
|
|
|
|
frequency: "weekly",
|
|
|
|
}),
|
|
|
|
monthly: habitsTracker.getHabitsHistoriesByDate({
|
|
|
|
selectedDate,
|
|
|
|
frequency: "monthly",
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
const frequenciesFiltered = GOAL_FREQUENCIES.filter((frequency) => {
|
|
|
|
return habitsHistoriesByFrequency[frequency].length > 0
|
|
|
|
})
|
|
|
|
|
2024-04-08 23:21:36 +02:00
|
|
|
return (
|
2024-04-11 23:03:45 +02:00
|
|
|
<>
|
|
|
|
<View
|
|
|
|
pointerEvents="none"
|
|
|
|
style={{
|
|
|
|
width: "100%",
|
|
|
|
height: "100%",
|
|
|
|
position: "absolute",
|
|
|
|
zIndex: 100,
|
|
|
|
justifyContent: "center",
|
|
|
|
alignItems: "center",
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<LottieView
|
|
|
|
ref={confettiRef}
|
|
|
|
source={confettiJSON}
|
|
|
|
autoPlay={false}
|
|
|
|
loop={false}
|
|
|
|
style={[
|
|
|
|
{
|
|
|
|
position: "absolute",
|
|
|
|
top: 0,
|
|
|
|
left: 0,
|
|
|
|
right: 0,
|
|
|
|
bottom: 0,
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
resizeMode="cover"
|
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
|
|
|
|
<ScrollView
|
|
|
|
showsVerticalScrollIndicator={false}
|
|
|
|
style={{
|
|
|
|
paddingHorizontal: 20,
|
|
|
|
width: Dimensions.get("window").width,
|
|
|
|
backgroundColor: "white",
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Divider />
|
|
|
|
|
2024-05-20 12:23:17 +02:00
|
|
|
<Text
|
|
|
|
style={{
|
|
|
|
fontWeight: "bold",
|
|
|
|
fontSize: 22,
|
|
|
|
textAlign: "center",
|
|
|
|
marginTop: 20,
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{selectedDate.toLocaleDateString("en-US", {
|
|
|
|
weekday: "long",
|
|
|
|
year: "numeric",
|
|
|
|
month: "long",
|
|
|
|
day: "numeric",
|
|
|
|
})}
|
|
|
|
</Text>
|
2024-05-20 11:41:04 +02:00
|
|
|
|
2024-05-20 12:23:17 +02:00
|
|
|
{frequenciesFiltered.length > 0 ? (
|
|
|
|
<List.Section>
|
|
|
|
{frequenciesFiltered.map((frequency) => {
|
|
|
|
return (
|
|
|
|
<List.Accordion
|
|
|
|
expanded={accordionExpanded[frequency]}
|
|
|
|
onPress={() => {
|
|
|
|
setAccordionExpanded((old) => {
|
|
|
|
return {
|
|
|
|
...old,
|
|
|
|
[frequency]: !old[frequency],
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}}
|
|
|
|
key={frequency}
|
|
|
|
title={capitalize(frequency)}
|
|
|
|
titleStyle={[
|
|
|
|
{
|
|
|
|
fontSize: 26,
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
>
|
|
|
|
{habitsHistoriesByFrequency[frequency].map((item) => {
|
2024-04-12 15:20:08 +02:00
|
|
|
return (
|
|
|
|
<HabitCard
|
|
|
|
habitHistory={item}
|
|
|
|
selectedDate={selectedDate}
|
|
|
|
key={item.habit.id + selectedDate.toISOString()}
|
|
|
|
confettiRef={confettiRef}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
})}
|
2024-05-20 12:23:17 +02:00
|
|
|
</List.Accordion>
|
|
|
|
)
|
|
|
|
})}
|
|
|
|
</List.Section>
|
|
|
|
) : (
|
|
|
|
<View
|
|
|
|
style={{
|
|
|
|
justifyContent: "center",
|
|
|
|
alignItems: "center",
|
|
|
|
marginVertical: 6,
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Text variant="titleLarge">No habits for this date</Text>
|
|
|
|
</View>
|
|
|
|
)}
|
2024-04-11 23:03:45 +02:00
|
|
|
</ScrollView>
|
|
|
|
</>
|
2024-04-08 23:21:36 +02:00
|
|
|
)
|
|
|
|
}
|