Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

CustomSnoozeSheet.android.tsx

290 lines11.5 KB

1import { SegmentedControl } from "../../components/ui/SegmentedControl";
2import {
3 BasicAlertDialog,
4 Column,
5 DateTimePicker,
6 Host,
7 FilledTonalIconButton,
8 Row,
9 RNHostView,
10 Shape,
11 Surface,
12 Text,
13 TextButton,
14} from "@expo/ui/jetpack-compose";
15import {
16 padding,
17 size,
18 width,
19 fillMaxWidth,
20 testID,
21 verticalScroll,
22} from "@expo/ui/jetpack-compose/modifiers";
23import {
24 localSnoozeDate,
25 localSnoozeTime,
26 resolveCustomSnooze,
27 type CustomSnoozeInput,
28} from "@t3tools/client-runtime/state/thread-settled";
29import { requireNativeModule } from "expo";
30import { useEffect, useState } from "react";
31import { AppState, useWindowDimensions } from "react-native";
32​
33import { OverlayPortal } from "../../components/ui/OverlayPortal";
34import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider";
35import { useScaledTextRole } from "../settings/appearance/useScaledTextRole";
36import type { CustomSnoozeSheet as SharedCustomSnoozeSheet } from "./CustomSnoozeSheet.shared";
37import {
38 applySnoozePickerDate,
39 applySnoozePickerTime,
40 snoozeDateToPickerDate,
41} from "./customSnoozeDate";
42​
43type Props = Parameters<typeof SharedCustomSnoozeSheet>[0];
44​
45const roundedCorner = Shape.RoundedCorner;
46​
47const modes = [
48 { value: "date", label: "Date and time" },
49 { value: "duration", label: "Duration" },
50] as const;
51const units = [
52 { value: "minutes", label: "Minutes" },
53 { value: "hours", label: "Hours" },
54 { value: "days", label: "Days" },
55] as const;
56​
57function systemUses24HourClock() {
58 return requireNativeModule<{ is24HourFormat(): boolean }>("T3NativeControls").is24HourFormat();
60​
61export function CustomSnoozeSheet(props: Props) {
62 const { themeAppearance, themeVariables: colors } = useAppearancePreferences();
63 const [is24Hour, setIs24Hour] = useState(systemUses24HourClock);
64 useEffect(() => {
65 const subscription = AppState.addEventListener("change", (state) => {
66 if (state === "active") setIs24Hour(systemUses24HourClock());
67 });
68 return () => subscription.remove();
69 }, []);
70 const titleTypography = useScaledTextRole("title");
71 const bodyTypography = useScaledTextRole("footnote");
72 const { width: windowWidth } = useWindowDimensions();
73 const [mode, setMode] = useState<CustomSnoozeInput["mode"]>("date");
74 const [date, setDate] = useState(() => new Date(Date.now() + 3_600_000));
75 const [picker, setPicker] = useState<"date" | "time">("date");
76 const [amount, setAmount] = useState(2);
77 const [unit, setUnit] = useState<"minutes" | "hours" | "days">("hours");
78 const [error, setError] = useState<string | null>(null);
79 const submit = () => {
80 const input: CustomSnoozeInput =
81 mode === "date"
82 ? { mode, date: localSnoozeDate(date), time: localSnoozeTime(date) }
83 : { mode, amount: String(amount), unit };
84 const snoozedUntil = resolveCustomSnooze(input, new Date());
85 if (!snoozedUntil) {
86 setError(
87 mode === "date" ? "Choose a date and time in the future." : "Enter a positive duration.",
88 );
89 return;
90 }
91 props.onSnooze(snoozedUntil);
92 props.onClose();
93 };
94 const pickerColors = {
95 containerColor: colors["--color-card-alt"],
96 titleContentColor: colors["--color-foreground-secondary"],
97 headlineContentColor: colors["--color-foreground"],
98 weekdayContentColor: colors["--color-foreground-secondary"],
99 subheadContentColor: colors["--color-foreground"],
100 navigationContentColor: colors["--color-foreground"],
101 yearContentColor: colors["--color-foreground"],
102 dayContentColor: colors["--color-foreground"],
103 currentYearContentColor: colors["--color-primary"],
104 selectedYearContainerColor: colors["--color-primary"],
105 selectedYearContentColor: colors["--color-primary-foreground"],
106 selectedDayContainerColor: colors["--color-primary"],
107 selectedDayContentColor: colors["--color-primary-foreground"],
108 todayContentColor: colors["--color-primary"],
109 todayDateBorderColor: colors["--color-primary"],
110 dividerColor: colors["--color-border"],
111 clockDialColor: colors["--color-secondary"],
112 clockDialSelectedContentColor: colors["--color-primary-foreground"],
113 clockDialUnselectedContentColor: colors["--color-secondary-foreground"],
114 selectorColor: colors["--color-primary"],
115 periodSelectorSelectedContainerColor: colors["--color-secondary"],
116 periodSelectorSelectedContentColor: colors["--color-secondary-foreground"],
117 periodSelectorUnselectedContentColor: colors["--color-foreground"],
118 timeSelectorSelectedContainerColor: colors["--color-secondary"],
119 timeSelectorSelectedContentColor: colors["--color-secondary-foreground"],
120 timeSelectorUnselectedContainerColor: colors["--color-card"],
121 timeSelectorUnselectedContentColor: colors["--color-foreground"],
122 };
123 return (
124 // Recycled thread rows can detach a zero-sized native dialog host.
125 <OverlayPortal>
126 <Host colorScheme={themeAppearance} style={{ height: 0, width: 0 }}>
127 <BasicAlertDialog
128 onDismissRequest={props.onClose}
129 properties={{ usePlatformDefaultWidth: false }}
130 modifiers={[width(Math.min(360, windowWidth - 32))]}
131 >
132 <Surface
133 color={colors["--color-card-alt"]}
134 contentColor={colors["--color-foreground"]}
135 shape={roundedCorner({
136 cornerRadii: { topStart: 28, topEnd: 28, bottomStart: 28, bottomEnd: 28 },
137 })}
138 >
139 <Column modifiers={[fillMaxWidth(), verticalScroll()]}>
140 <Column
141 verticalArrangement={{ spacedBy: 16 }}
142 modifiers={[fillMaxWidth(), padding(24, 24, 24, 16)]}
143 >
144 <Text style={titleTypography}>Custom snooze</Text>
145 <RNHostView matchContents={{ vertical: true }} modifiers={[fillMaxWidth()]}>
146 <SegmentedControl
147 options={modes}
148 selected={mode}
149 onSelect={(value) => {
150 setMode(value);
151 setError(null);
152 }}
153 />
154 </RNHostView>
155 {mode === "date" ? (
156 <RNHostView matchContents={{ vertical: true }} modifiers={[fillMaxWidth()]}>
157 <SegmentedControl
158 options={[
159 {
160 value: "date",
161 label: date.toLocaleDateString([], { month: "short", day: "numeric" }),
162 },
163 {
164 value: "time",
165 label: date.toLocaleTimeString([], {
166 hour: "numeric",
167 minute: "2-digit",
168 hourCycle: is24Hour ? "h23" : "h12",
169 }),
170 },
171 ]}
172 selected={picker}
173 onSelect={setPicker}
174 />
175 </RNHostView>
176 ) : null}
177 </Column>
178 {mode === "date" ? (
179 <Column horizontalAlignment="center" modifiers={[fillMaxWidth()]}>
180 <SnoozeDateTimePicker
181 key={picker}
182 date={date}
183 picker={picker}
184 is24Hour={is24Hour}
185 colors={pickerColors}
186 onChange={(selected) => {
187 setDate((current) =>
188 picker === "date"
189 ? applySnoozePickerDate(current, selected)
190 : applySnoozePickerTime(current, selected),
191 );
192 setError(null);
193 }}
194 />
195 </Column>
196 ) : (
197 <Column
198 verticalArrangement={{ spacedBy: 16 }}
199 modifiers={[fillMaxWidth(), padding(24, 8, 24, 16)]}
200 >
201 <Row
202 horizontalArrangement="spaceEvenly"
203 verticalAlignment="center"
204 modifiers={[fillMaxWidth()]}
205 >
206 <FilledTonalIconButton
207 enabled={amount > 1}
208 onClick={() => setAmount((current) => Math.max(1, current - 1))}
209 colors={{
210 containerColor: colors["--color-secondary"],
211 contentColor: colors["--color-secondary-foreground"],
212 }}
213 modifiers={[size(48, 48), testID("snooze-decrease-duration")]}
214 >
215 <Text style={titleTypography}>−</Text>
216 </FilledTonalIconButton>
217 <Text style={titleTypography} modifiers={[testID("snooze-duration")]}>
218 {String(amount)}
219 </Text>
220 <FilledTonalIconButton
221 enabled={amount < 99}
222 onClick={() => setAmount((current) => Math.min(99, current + 1))}
223 colors={{
224 containerColor: colors["--color-secondary"],
225 contentColor: colors["--color-secondary-foreground"],
226 }}
227 modifiers={[size(48, 48), testID("snooze-increase-duration")]}
228 >
229 <Text style={titleTypography}>+</Text>
230 </FilledTonalIconButton>
231 </Row>
232 <RNHostView matchContents={{ vertical: true }} modifiers={[fillMaxWidth()]}>
233 <SegmentedControl options={units} selected={unit} onSelect={setUnit} />
234 </RNHostView>
235 </Column>
236 )}
237 <Column
238 verticalArrangement={{ spacedBy: 16 }}
239 modifiers={[fillMaxWidth(), padding(24, 8, 24, 24)]}
240 >
241 {error ? (
242 <Text style={bodyTypography} color={colors["--color-danger-foreground"]}>
243 {error}
244 </Text>
245 ) : null}
246 <Row horizontalArrangement="end" modifiers={[fillMaxWidth()]}>
247 <TextButton
248 onClick={props.onClose}
249 colors={{ contentColor: colors["--color-foreground"] }}
250 >
251 <Text style={bodyTypography}>Cancel</Text>
252 </TextButton>
253 <TextButton
254 onClick={submit}
255 colors={{ contentColor: colors["--color-foreground"] }}
256 >
257 <Text style={bodyTypography}>Snooze</Text>
258 </TextButton>
259 </Row>
260 </Column>
261 </Column>
262 </Surface>
263 </BasicAlertDialog>
264 </Host>
265 </OverlayPortal>
266 );
268​
269function SnoozeDateTimePicker(props: {
270 readonly date: Date;
271 readonly picker: "date" | "time";
272 readonly is24Hour: boolean;
273 readonly colors: React.ComponentProps<typeof DateTimePicker>["elementColors"];
274 readonly onChange: (date: Date) => void;
275}) {
276 // Changing initialDate resets Compose's picker state, including its active clock dial.
277 const [initialDate] = useState(() =>
278 props.picker === "date" ? snoozeDateToPickerDate(props.date) : props.date.toISOString(),
279 );
280 return (
281 <DateTimePicker
282 initialDate={initialDate}
283 displayedComponents={props.picker === "date" ? "date" : "hourAndMinute"}
284 is24Hour={props.is24Hour}
285 showVariantToggle={false}
286 elementColors={props.colors}
287 onDateSelected={props.onChange}
288 />
289 );