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

CustomSnoozeSheet.android.tsx

297 lines11.7 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 variant="card"
148 options={modes}
149 selected={mode}
150 onSelect={(value) => {
151 setMode(value);
152 setError(null);
153 }}
154 />
155 </RNHostView>
156 {mode === "date" ? (
157 <RNHostView matchContents={{ vertical: true }} modifiers={[fillMaxWidth()]}>
158 <SegmentedControl
159 variant="card"
160 options={[
161 {
162 value: "date",
163 label: date.toLocaleDateString([], { month: "short", day: "numeric" }),
164 },
165 {
166 value: "time",
167 label: date.toLocaleTimeString([], {
168 hour: "numeric",
169 minute: "2-digit",
170 hourCycle: is24Hour ? "h23" : "h12",
171 }),
172 },
173 ]}
174 selected={picker}
175 onSelect={setPicker}
176 />
177 </RNHostView>
178 ) : null}
179 </Column>
180 {mode === "date" ? (
181 <Column horizontalAlignment="center" modifiers={[fillMaxWidth()]}>
182 <SnoozeDateTimePicker
183 key={picker}
184 date={date}
185 picker={picker}
186 is24Hour={is24Hour}
187 colors={pickerColors}
188 onChange={(selected) => {
189 setDate((current) =>
190 picker === "date"
191 ? applySnoozePickerDate(current, selected)
192 : applySnoozePickerTime(current, selected),
193 );
194 setError(null);
195 }}
196 />
197 </Column>
198 ) : (
199 <Column
200 verticalArrangement={{ spacedBy: 16 }}
201 modifiers={[fillMaxWidth(), padding(24, 8, 24, 16)]}
202 >
203 <Row
204 horizontalArrangement="spaceEvenly"
205 verticalAlignment="center"
206 modifiers={[fillMaxWidth()]}
207 >
208 <FilledTonalIconButton
209 enabled={amount > 1}
210 onClick={() => setAmount((current) => Math.max(1, current - 1))}
211 colors={{
212 containerColor: colors["--color-secondary"],
213 contentColor: colors["--color-secondary-foreground"],
214 }}
215 modifiers={[size(48, 48), testID("snooze-decrease-duration")]}
216 >
217 <Text style={titleTypography}>−</Text>
218 </FilledTonalIconButton>
219 <Text style={titleTypography} modifiers={[testID("snooze-duration")]}>
220 {String(amount)}
221 </Text>
222 <FilledTonalIconButton
223 enabled={amount < 99}
224 onClick={() => setAmount((current) => Math.min(99, current + 1))}
225 colors={{
226 containerColor: colors["--color-secondary"],
227 contentColor: colors["--color-secondary-foreground"],
228 }}
229 modifiers={[size(48, 48), testID("snooze-increase-duration")]}
230 >
231 <Text style={titleTypography}>+</Text>
232 </FilledTonalIconButton>
233 </Row>
234 <RNHostView matchContents={{ vertical: true }} modifiers={[fillMaxWidth()]}>
235 <SegmentedControl
236 variant="card"
237 options={units}
238 selected={unit}
239 onSelect={setUnit}
240 />
241 </RNHostView>
242 </Column>
243 )}
244 <Column
245 verticalArrangement={{ spacedBy: 16 }}
246 modifiers={[fillMaxWidth(), padding(24, 8, 24, 24)]}
247 >
248 {error ? (
249 <Text style={bodyTypography} color={colors["--color-danger-foreground"]}>
250 {error}
251 </Text>
252 ) : null}
253 <Row horizontalArrangement="end" modifiers={[fillMaxWidth()]}>
254 <TextButton
255 onClick={props.onClose}
256 colors={{ contentColor: colors["--color-foreground"] }}
257 >
258 <Text style={bodyTypography}>Cancel</Text>
259 </TextButton>
260 <TextButton
261 onClick={submit}
262 colors={{ contentColor: colors["--color-foreground"] }}
263 >
264 <Text style={bodyTypography}>Snooze</Text>
265 </TextButton>
266 </Row>
267 </Column>
268 </Column>
269 </Surface>
270 </BasicAlertDialog>
271 </Host>
272 </OverlayPortal>
273 );
275​
276function SnoozeDateTimePicker(props: {
277 readonly date: Date;
278 readonly picker: "date" | "time";
279 readonly is24Hour: boolean;
280 readonly colors: React.ComponentProps<typeof DateTimePicker>["elementColors"];
281 readonly onChange: (date: Date) => void;
282}) {
283 // Changing initialDate resets Compose's picker state, including its active clock dial.
284 const [initialDate] = useState(() =>
285 props.picker === "date" ? snoozeDateToPickerDate(props.date) : props.date.toISOString(),
286 );
287 return (
288 <DateTimePicker
289 initialDate={initialDate}
290 displayedComponents={props.picker === "date" ? "date" : "hourAndMinute"}
291 is24Hour={props.is24Hour}
292 showVariantToggle={false}
293 elementColors={props.colors}
294 onDateSelected={props.onChange}
295 />
296 );