import {useState, useEffect, useRef} from "react" import {View, Pressable, Modal, ScrollView, Platform, ViewStyle, TextStyle} from "react-native" import {Text} from "@/components/ignite" import {useAppTheme} from "@/contexts/ThemeContext" import {ThemedStyle} from "@/theme" type TimeSettingProps = { label: string value: number // Total seconds onValueChange: (value: number) => void containerStyle?: ViewStyle showSeconds?: boolean isFirst?: boolean isLast?: boolean } const TimeSetting: React.FC = ({ label, value, onValueChange, containerStyle, showSeconds = true, isFirst, isLast, }) => { const {theme, themed} = useAppTheme() const groupedStyle: ViewStyle | undefined = isFirst !== undefined || isLast !== undefined ? { borderTopLeftRadius: isFirst ? theme.spacing.s4 : theme.spacing.s1, borderTopRightRadius: isFirst ? theme.spacing.s4 : theme.spacing.s1, borderBottomLeftRadius: isLast ? theme.spacing.s4 : theme.spacing.s1, borderBottomRightRadius: isLast ? theme.spacing.s4 : theme.spacing.s1, marginBottom: isLast ? 0 : theme.spacing.s2, } : undefined const [modalVisible, setModalVisible] = useState(false) const [localHours, setLocalHours] = useState(0) const [localMinutes, setLocalMinutes] = useState(0) const [localSeconds, setLocalSeconds] = useState(0) // Refs for scroll positions const hoursScrollRef = useRef(null) const minutesScrollRef = useRef(null) const secondsScrollRef = useRef(null) // Helper to ensure a safe number const safeNumber = (n: any) => (typeof n === "number" && !isNaN(n) ? n : 0) // Convert total seconds to hours, minutes, seconds useEffect(() => { const totalSeconds = safeNumber(value) const hours = Math.floor(totalSeconds / 3600) const minutes = Math.floor((totalSeconds % 3600) / 60) const seconds = totalSeconds % 60 setLocalHours(hours) setLocalMinutes(minutes) setLocalSeconds(seconds) }, [value]) const REPEATS = 5 // Must be odd for perfect centering const ITEM_HEIGHT = 44 const _VISIBLE_ITEMS = 3 const CENTER_SLOT = Math.floor(REPEATS / 2) // Function to scroll to center the selected value const scrollToCenter = (scrollRef: React.RefObject, value: number, max: number) => { if (scrollRef.current) { // Center slot index for the selected value const centerIndex = CENTER_SLOT * max + value const targetOffset = centerIndex * ITEM_HEIGHT scrollRef.current.scrollTo({y: targetOffset, animated: false}) } } // Function to check if an item is in the center position const isItemInCenter = (itemIndex: number, value: number, max: number) => { // Center slot index for the selected value const centerIndex = CENTER_SLOT * max + value return itemIndex === centerIndex } // Scroll to center when modal opens or values change useEffect(() => { if (modalVisible) { setTimeout(() => { scrollToCenter(hoursScrollRef, localHours, 100) scrollToCenter(minutesScrollRef, localMinutes, 60) scrollToCenter(secondsScrollRef, localSeconds, 60) }, 100) } }, [modalVisible, localHours, localMinutes, localSeconds]) const formatTime = (hours: number, minutes: number, seconds: number) => { hours = safeNumber(hours) minutes = safeNumber(minutes) seconds = safeNumber(seconds) if (showSeconds) { return `${hours.toString().padStart(2, "0")}:${minutes.toString().padStart(2, "0")}:${seconds.toString().padStart(2, "0")}` } else { return `${hours.toString().padStart(2, "0")}:${minutes.toString().padStart(2, "0")}` } } const handleConfirm = () => { const totalSeconds = localHours * 3600 + localMinutes * 60 + localSeconds onValueChange(totalSeconds) setModalVisible(false) } const handleCancel = () => { // Reset to original values const totalSeconds = safeNumber(value) const hours = Math.floor(totalSeconds / 3600) const minutes = Math.floor((totalSeconds % 3600) / 60) const seconds = totalSeconds % 60 setLocalHours(hours) setLocalMinutes(minutes) setLocalSeconds(seconds) setModalVisible(false) } const _generateTimeArray = (max: number) => { return Array.from({length: max}, (_, i) => i) } const generateInfiniteArray = (max: number, repeats: number = REPEATS) => { const baseArray = Array.from({length: max}, (_, i) => i) return Array.from({length: repeats}, () => baseArray).flat() } const hoursArray = generateInfiniteArray(100) // 0-99, repeated 5 times const minutesArray = generateInfiniteArray(60) // 0-59, repeated 5 times const secondsArray = generateInfiniteArray(60) // 0-59, repeated 5 times // When calculating currentHours/currentMinutes/currentSeconds: const currentTotalSeconds = safeNumber(value) const currentHours = Math.floor(currentTotalSeconds / 3600) const currentMinutes = Math.floor((currentTotalSeconds % 3600) / 60) const currentSeconds = currentTotalSeconds % 60 // Add a helper to trigger scrollToCenter on layout const handleScrollViewLayout = (scrollRef: React.RefObject, value: number, max: number) => () => { scrollToCenter(scrollRef, value, max) } return ( setModalVisible(true)} android_ripple={{color: "rgba(0, 0, 0, 0.1)"}}> {formatTime(currentHours, currentMinutes, currentSeconds)} › {/* Hours Picker */} Hours { const offsetY = event.nativeEvent.contentOffset.y const index = Math.round(offsetY / ITEM_HEIGHT) const actualHour = index % 100 setLocalHours(actualHour) }}> {hoursArray.map((hour, index) => ( setLocalHours(hour)}> {hour.toString().padStart(2, "0")} ))} {/* Minutes Picker */} Minutes { const offsetY = event.nativeEvent.contentOffset.y const index = Math.round(offsetY / ITEM_HEIGHT) const actualMinute = index % 60 setLocalMinutes(actualMinute) }}> {minutesArray.map((minute, index) => ( setLocalMinutes(minute)}> {minute.toString().padStart(2, "0")} ))} {/* Seconds Picker (if enabled) */} {showSeconds && ( Seconds { const offsetY = event.nativeEvent.contentOffset.y const index = Math.round(offsetY / ITEM_HEIGHT) const actualSecond = index % 60 setLocalSeconds(actualSecond) }}> {secondsArray.map((second, index) => ( setLocalSeconds(second)}> {second.toString().padStart(2, "0")} ))} )} Cancel Confirm ) } const $container: ThemedStyle = ({colors, spacing}) => ({ backgroundColor: colors.primary_foreground, borderRadius: spacing.s4, paddingVertical: spacing.s4, paddingHorizontal: spacing.s4, width: "100%", }) const $label: ThemedStyle = ({colors, spacing}) => ({ fontSize: 14, color: colors.text, marginBottom: spacing.s2, }) const $timeButton: ThemedStyle = ({colors, spacing}) => ({ flexDirection: "row", justifyContent: "space-between", alignItems: "center", backgroundColor: colors.primary_foreground, borderWidth: 1, borderColor: colors.border, borderRadius: 6, paddingHorizontal: spacing.s3, paddingVertical: spacing.s2, minHeight: Platform.OS === "ios" ? 44 : 48, }) const $timeText: ThemedStyle = ({colors}) => ({ fontSize: 16, color: colors.text, fontFamily: "monospace", }) const $chevronText: ThemedStyle = ({colors}) => ({ fontSize: 18, color: colors.textDim, fontWeight: "bold", }) const $modalOverlay: ThemedStyle = () => ({ flex: 1, backgroundColor: "rgba(0, 0, 0, 0.5)", justifyContent: "center", alignItems: "center", }) const $modalContent: ThemedStyle = ({colors, spacing}) => ({ backgroundColor: colors.primary_foreground, borderRadius: 12, padding: spacing.s6, width: "90%", maxWidth: 400, }) const $modalHeader: ThemedStyle = ({spacing}) => ({ alignItems: "center", marginBottom: spacing.s6, }) const $modalTitle: ThemedStyle = ({colors}) => ({ fontSize: 18, color: colors.text, }) const $pickerContainer: ThemedStyle = ({spacing}) => ({ flexDirection: "row", justifyContent: "space-around", marginBottom: spacing.s6, }) const $pickerColumn: ThemedStyle = () => ({ alignItems: "center", flex: 1, }) const $pickerLabel: ThemedStyle = ({colors, spacing}) => ({ fontSize: 14, color: colors.textDim, marginBottom: spacing.s2, fontWeight: "500", }) const $pickerScroll: ThemedStyle = () => ({ height: 132, // 3 items visible (44 * 3) }) const $pickerItem: ThemedStyle = ({spacing}) => ({ height: 44, justifyContent: "center", alignItems: "center", paddingHorizontal: spacing.s3, }) const $pickerItemSelected: ThemedStyle = ({colors}) => ({ backgroundColor: colors.palette?.primary300 || "#007AFF", borderRadius: 6, }) const $pickerItemText: ThemedStyle = ({colors}) => ({ fontSize: 16, color: colors.text, fontFamily: "monospace", }) const $pickerItemTextSelected: ThemedStyle = () => ({ color: "#FFFFFF", fontWeight: "600", }) const $modalFooter: ThemedStyle = ({spacing}) => ({ flexDirection: "row", justifyContent: "space-between", gap: spacing.s4, }) const $cancelButton: ThemedStyle = ({colors, spacing}) => ({ flex: 1, backgroundColor: colors.primary_foreground, borderWidth: 1, borderColor: colors.border, borderRadius: 6, paddingVertical: spacing.s3, alignItems: "center", }) const $confirmButton: ThemedStyle = ({colors, spacing}) => ({ flex: 1, backgroundColor: colors.palette?.primary300 || "#007AFF", borderRadius: 6, paddingVertical: spacing.s3, alignItems: "center", }) const $cancelButtonText: ThemedStyle = ({colors}) => ({ fontSize: 16, color: colors.text, fontWeight: "500", }) const $confirmButtonText: ThemedStyle = () => ({ fontSize: 16, color: "#FFFFFF", fontWeight: "600", }) export default TimeSetting