import { useState } from "react" import { ScrollView, StyleSheet, View } from "react-native" import { Button, Snackbar, Text, TextInput } from "react-native-paper" import { SafeAreaView } from "react-native-safe-area-context" import type { GoalNumeric } from "@/domain/entities/Goal" import { GoalNumericProgress } from "@/domain/entities/Goal" import type { HabitHistory } from "@/domain/entities/HabitHistory" import { useHabitsTracker } from "@/presentation/react/contexts/HabitsTracker" import { LOCALE, capitalize } from "@/utils/strings" export interface HabitProgressProps { habitHistory: HabitHistory selectedDate: Date } export const HabitProgress: React.FC = ({ habitHistory, selectedDate, }) => { const { habitsTrackerPresenter, habitGoalProgressUpdate } = useHabitsTracker() const [isVisibleSnackbar, setIsVisibleSnackbar] = useState(false) const onDismissSnackbar = (): void => { setIsVisibleSnackbar(false) } const goalProgress = habitHistory.getGoalProgressByDate(selectedDate) const values = { progress: 0, min: 0, max: 0, } if (goalProgress.isNumeric()) { values.max = goalProgress.goal.target.value } const [progressValue, setProgressValue] = useState(values.progress) if (!goalProgress.isNumeric()) { return <> } const progressTotal = goalProgress.progress + progressValue const handleSave = async (): Promise => { setIsVisibleSnackbar(true) await habitsTrackerPresenter.habitUpdateProgress({ date: selectedDate, habitHistory, goalProgress: new GoalNumericProgress({ goal: habitHistory.habit.goal as GoalNumeric, progress: progressValue, }), }) setProgressValue(0) } return ( {habitHistory.habit.name} {capitalize(habitHistory.habit.goal.frequency)} Progress {selectedDate.toLocaleDateString(LOCALE, { weekday: "long", year: "numeric", month: "long", day: "numeric", })} {goalProgress.progress.toLocaleString()} /{" "} {goalProgress.goal.target.value.toLocaleString()}{" "} {goalProgress.goal.target.unit} { const hasDigits = /\d+$/.test(text) if (text.length <= 0 || !hasDigits) { setProgressValue(0) return } setProgressValue(Number.parseInt(text, 10)) }} style={[ styles.spacing, { width: "80%", }, ]} mode="outlined" keyboardType="numeric" /> {goalProgress.progress > 0 && progressValue > 0 ? ( {goalProgress.progress.toLocaleString()} +{" "} {progressValue.toLocaleString()} = {progressTotal.toLocaleString()}{" "} {goalProgress.goal.target.unit} ) : ( <> )} ✅ Habit Saved successfully! ) } const styles = StyleSheet.create({ spacing: { marginVertical: 16, }, })