@a16n/web-sim 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -2
- package/bin/cli.ts +255 -0
- package/bin/web-sim.js +39 -0
- package/overrides/@backpackapp-io/react-native-toast/overrides.ts +15 -0
- package/overrides/@react-native-firebase/app/overrides.ts +34 -0
- package/overrides/@tiktok-for-business/react-native-sdk/NativeTiktokSdk.ts +29 -0
- package/overrides/@tiktok-for-business/react-native-sdk/overrides.ts +9 -0
- package/overrides/expo-application/ExpoApplication.ts +81 -0
- package/overrides/expo-application/overrides.ts +16 -0
- package/overrides/expo-background-task/ExpoBackgroundTaskModule.ts +30 -0
- package/overrides/expo-background-task/overrides.ts +9 -0
- package/overrides/expo-constants/ExponentConstants.ts +66 -0
- package/overrides/expo-constants/overrides.ts +9 -0
- package/overrides/expo-device/ExpoDevice.ts +104 -0
- package/overrides/expo-device/overrides.ts +9 -0
- package/overrides/expo-glass-effect/GlassContainer.tsx +9 -0
- package/overrides/expo-glass-effect/GlassView.tsx +95 -0
- package/overrides/expo-glass-effect/availability.ts +14 -0
- package/overrides/expo-glass-effect/overrides.ts +14 -0
- package/overrides/expo-haptics/Haptics.ts +67 -0
- package/overrides/expo-haptics/overrides.ts +5 -0
- package/overrides/expo-mail-composer/ExpoMailComposer.ts +40 -0
- package/overrides/expo-mail-composer/overrides.ts +9 -0
- package/overrides/expo-mesh-gradient/MeshGradientView.tsx +44 -0
- package/overrides/expo-mesh-gradient/overrides.ts +9 -0
- package/overrides/expo-modules-core/overrides.ts +12 -0
- package/overrides/expo-navigation-bar/ExpoNavigationBar.ts +29 -0
- package/overrides/expo-navigation-bar/NavigationBar.ts +43 -0
- package/overrides/expo-navigation-bar/overrides.ts +12 -0
- package/overrides/expo-router/NativeStackView.tsx +74 -0
- package/overrides/expo-router/NativeTabsView.tsx +263 -0
- package/overrides/expo-router/SafeAreaProvider.ts +6 -0
- package/overrides/expo-router/overrides.ts +23 -0
- package/overrides/expo-router/statusBarRequests.ts +51 -0
- package/overrides/expo-screen-orientation/ExpoScreenOrientation.ts +173 -0
- package/overrides/expo-screen-orientation/overrides.ts +21 -0
- package/overrides/expo-sharing/SharingNativeModule.ts +21 -0
- package/overrides/expo-sharing/overrides.ts +9 -0
- package/overrides/expo-sms/ExpoSMS.ts +24 -0
- package/overrides/expo-sms/overrides.ts +9 -0
- package/overrides/expo-splash-screen/SplashScreen.ts +25 -0
- package/overrides/expo-splash-screen/overrides.ts +5 -0
- package/overrides/expo-status-bar/StatusBar.tsx +44 -0
- package/overrides/expo-status-bar/overrides.ts +5 -0
- package/overrides/expo-store-review/ExpoStoreReview.ts +18 -0
- package/overrides/expo-store-review/overrides.ts +9 -0
- package/overrides/expo-symbols/SymbolView.tsx +192 -0
- package/overrides/expo-symbols/overrides.ts +9 -0
- package/overrides/expo-system-ui/ExpoSystemUI.ts +34 -0
- package/overrides/expo-system-ui/overrides.ts +9 -0
- package/overrides/expo-task-manager/ExpoTaskManager.ts +39 -0
- package/overrides/expo-task-manager/overrides.ts +9 -0
- package/overrides/expo-tracking-transparency/ExpoTrackingTransparency.ts +30 -0
- package/overrides/expo-tracking-transparency/overrides.ts +11 -0
- package/overrides/expo-video/ExpoVideo.ts +16 -0
- package/overrides/expo-video/overrides.ts +9 -0
- package/overrides/lottie-react-native/LottieView.tsx +87 -0
- package/overrides/lottie-react-native/overrides.ts +9 -0
- package/overrides/react-native-pager-view/PagerViewNativeComponent.tsx +254 -0
- package/overrides/react-native-pager-view/overrides.ts +9 -0
- package/overrides/react-native-reanimated/overrides.ts +28 -0
- package/overrides/react-native-safe-area-context/NativeSafeAreaProvider.tsx +40 -0
- package/overrides/react-native-safe-area-context/overrides.ts +12 -0
- package/overrides/react-native-share/NativeRNShare.ts +22 -0
- package/overrides/react-native-share/overrides.ts +12 -0
- package/overrides/react-native-share/platform.ts +14 -0
- package/overrides/react-native-turbo-image/TurboImage.tsx +101 -0
- package/overrides/react-native-turbo-image/overrides.ts +9 -0
- package/overrides/react-native-web/RefreshControl.tsx +57 -0
- package/overrides/react-native-web/overrides.ts +157 -0
- package/overrides/react-native-web/useElementLayout.ts +152 -0
- package/package.json +80 -4
- package/plugins/battery/overrides/expo-battery/ExpoBattery.ts +57 -0
- package/plugins/battery/overrides/expo-battery/overrides.ts +9 -0
- package/plugins/battery/plugin.ts +8 -0
- package/plugins/battery/shell.tsx +56 -0
- package/plugins/battery/state.ts +11 -0
- package/plugins/biometrics/overrides/expo-local-authentication/ExpoLocalAuthentication.ts +90 -0
- package/plugins/biometrics/overrides/expo-local-authentication/overrides.ts +11 -0
- package/plugins/biometrics/plugin.ts +8 -0
- package/plugins/biometrics/shell.tsx +88 -0
- package/plugins/biometrics/state.ts +41 -0
- package/plugins/brightness/app.ts +5 -0
- package/plugins/brightness/overrides/expo-brightness/Brightness.ts +98 -0
- package/plugins/brightness/overrides/expo-brightness/overrides.ts +10 -0
- package/plugins/brightness/plugin.ts +9 -0
- package/plugins/brightness/shell.tsx +55 -0
- package/plugins/brightness/state.ts +19 -0
- package/plugins/calendar/calendar.ts +299 -0
- package/plugins/calendar/overrides/expo-calendar/ExpoCalendar.ts +358 -0
- package/plugins/calendar/overrides/expo-calendar/LegacyExpoCalendar.ts +163 -0
- package/plugins/calendar/overrides/expo-calendar/overrides.ts +14 -0
- package/plugins/calendar/plugin.ts +8 -0
- package/plugins/calendar/shell.tsx +123 -0
- package/plugins/camera/overrides/expo-camera/ExpoCamera.tsx +269 -0
- package/plugins/camera/overrides/expo-camera/ExpoCameraManager.ts +56 -0
- package/plugins/camera/overrides/expo-camera/overrides.ts +12 -0
- package/plugins/camera/plugin.ts +9 -0
- package/plugins/camera/shell.tsx +107 -0
- package/plugins/camera/state.ts +61 -0
- package/plugins/clipboard/overrides/expo-clipboard/ExpoClipboard.ts +138 -0
- package/plugins/clipboard/overrides/expo-clipboard/overrides.ts +9 -0
- package/plugins/clipboard/plugin.ts +8 -0
- package/plugins/clipboard/shell.tsx +122 -0
- package/plugins/clipboard/state.ts +93 -0
- package/plugins/clock/app.ts +25 -0
- package/plugins/clock/plugin.ts +8 -0
- package/plugins/clock/shell.tsx +225 -0
- package/plugins/clock/state.ts +12 -0
- package/plugins/contacts/contacts.ts +125 -0
- package/plugins/contacts/overrides/expo-contacts/ExpoContacts.ts +153 -0
- package/plugins/contacts/overrides/expo-contacts/ExpoContactsNext.ts +232 -0
- package/plugins/contacts/overrides/expo-contacts/overrides.ts +12 -0
- package/plugins/contacts/plugin.ts +8 -0
- package/plugins/contacts/shell.tsx +109 -0
- package/plugins/home-indicator/app.ts +5 -0
- package/plugins/home-indicator/plugin.ts +9 -0
- package/plugins/home-indicator/requests.ts +48 -0
- package/plugins/home-indicator/shell.tsx +68 -0
- package/plugins/home-indicator/state.ts +31 -0
- package/plugins/keep-awake/app.ts +5 -0
- package/plugins/keep-awake/overrides/expo-keep-awake/ExpoKeepAwake.ts +27 -0
- package/plugins/keep-awake/overrides/expo-keep-awake/overrides.ts +9 -0
- package/plugins/keep-awake/plugin.ts +9 -0
- package/plugins/keep-awake/shell.tsx +80 -0
- package/plugins/keep-awake/state.ts +12 -0
- package/plugins/keyboard/Keyboard.tsx +354 -0
- package/plugins/keyboard/app.ts +90 -0
- package/plugins/keyboard/controller.ts +384 -0
- package/plugins/keyboard/fields.ts +244 -0
- package/plugins/keyboard/icons.tsx +283 -0
- package/plugins/keyboard/keyboard.css +362 -0
- package/plugins/keyboard/layouts/android.ts +175 -0
- package/plugins/keyboard/layouts/index.ts +10 -0
- package/plugins/keyboard/layouts/ios.ts +219 -0
- package/plugins/keyboard/layouts/types.ts +80 -0
- package/plugins/keyboard/overrides/react-native-keyboard-controller/KeyboardController.tsx +470 -0
- package/plugins/keyboard/overrides/react-native-keyboard-controller/overrides.ts +9 -0
- package/plugins/keyboard/plugin.ts +13 -0
- package/plugins/keyboard/shell.tsx +133 -0
- package/plugins/keyboard/state.ts +92 -0
- package/plugins/keyboard/suggestions.ts +63 -0
- package/plugins/keyboard/taps.ts +77 -0
- package/plugins/keyboard/typing.ts +112 -0
- package/plugins/locale/app.ts +232 -0
- package/plugins/locale/locale.ts +143 -0
- package/plugins/locale/overrides/expo-localization/ExpoLocalization.ts +41 -0
- package/plugins/locale/overrides/expo-localization/overrides.ts +9 -0
- package/plugins/locale/plugin.ts +9 -0
- package/plugins/locale/shell.tsx +147 -0
- package/plugins/locale/state.ts +36 -0
- package/plugins/location/geo.ts +56 -0
- package/plugins/location/overrides/expo-location/ExpoLocation.ts +235 -0
- package/plugins/location/overrides/expo-location/overrides.ts +11 -0
- package/plugins/location/places.ts +38 -0
- package/plugins/location/plugin.ts +8 -0
- package/plugins/location/shell.tsx +188 -0
- package/plugins/location/state.ts +51 -0
- package/plugins/motion/overrides/expo-sensors/DeviceSensor.ts +169 -0
- package/plugins/motion/overrides/expo-sensors/ExponentPedometer.ts +54 -0
- package/plugins/motion/overrides/expo-sensors/overrides.ts +14 -0
- package/plugins/motion/plugin.ts +8 -0
- package/plugins/motion/sensors.ts +116 -0
- package/plugins/motion/shell.tsx +136 -0
- package/plugins/motion/state.ts +46 -0
- package/plugins/network/app.ts +400 -0
- package/plugins/network/overrides/expo-network/ExpoNetwork.ts +51 -0
- package/plugins/network/overrides/expo-network/overrides.ts +9 -0
- package/plugins/network/plugin.ts +8 -0
- package/plugins/network/shell.tsx +67 -0
- package/plugins/network/state.ts +29 -0
- package/plugins/notifications/center.ts +255 -0
- package/plugins/notifications/content.ts +36 -0
- package/plugins/notifications/overrides/expo-notifications/NotificationsModule.ts +175 -0
- package/plugins/notifications/overrides/expo-notifications/channels.ts +108 -0
- package/plugins/notifications/overrides/expo-notifications/getDevicePushTokenAsync.ts +19 -0
- package/plugins/notifications/overrides/expo-notifications/getExpoPushTokenAsync.ts +16 -0
- package/plugins/notifications/overrides/expo-notifications/overrides.ts +36 -0
- package/plugins/notifications/plugin.ts +8 -0
- package/plugins/notifications/shell.tsx +192 -0
- package/plugins/notifications/state.ts +56 -0
- package/plugins/notifications/tokens.ts +47 -0
- package/plugins/notifications/trigger.ts +107 -0
- package/plugins/permissions/plugin.ts +6 -0
- package/plugins/permissions/shell.tsx +50 -0
- package/plugins/permissions/state.ts +38 -0
- package/plugins/photos/appLibrary.ts +114 -0
- package/plugins/photos/library.ts +322 -0
- package/plugins/photos/overrides/expo-image-picker/ExponentImagePicker.ts +153 -0
- package/plugins/photos/overrides/expo-image-picker/overrides.ts +13 -0
- package/plugins/photos/overrides/expo-media-library/ExpoMediaLibrary.ts +235 -0
- package/plugins/photos/overrides/expo-media-library/NativeMediaLibraryModule.ts +318 -0
- package/plugins/photos/overrides/expo-media-library/overrides.ts +19 -0
- package/plugins/photos/plugin.ts +8 -0
- package/plugins/photos/samples.ts +367 -0
- package/plugins/photos/shell.tsx +164 -0
- package/plugins/photos/state.ts +88 -0
- package/plugins/storage/files.ts +351 -0
- package/plugins/storage/overrides/expo-file-system/ExpoFileSystem.ts +770 -0
- package/plugins/storage/overrides/expo-file-system/ExponentFileSystem.ts +196 -0
- package/plugins/storage/overrides/expo-file-system/overrides.ts +13 -0
- package/plugins/storage/overrides/expo-secure-store/ExpoSecureStore.ts +75 -0
- package/plugins/storage/overrides/expo-secure-store/overrides.ts +9 -0
- package/plugins/storage/plugin.ts +8 -0
- package/plugins/storage/reset.ts +136 -0
- package/plugins/storage/secure.ts +71 -0
- package/plugins/storage/shell.tsx +365 -0
- package/plugins/storage/state.ts +13 -0
- package/src/client/app/appDocument.ts +129 -0
- package/src/client/app/appFocus.ts +97 -0
- package/src/client/app/console.ts +118 -0
- package/src/client/app/fonts.ts +76 -0
- package/src/client/app/main.tsx +99 -0
- package/src/client/app/routeContext.ts +45 -0
- package/src/client/app/touch.ts +640 -0
- package/src/client/entry.ts +20 -0
- package/src/client/modules.d.ts +8 -0
- package/src/client/react-native/app.ts +57 -0
- package/src/client/react-native/deep.ts +13 -0
- package/src/client/react-native/keyboard.tsx +192 -0
- package/src/client/react-native/library.ts +256 -0
- package/src/client/shell/Cookies.tsx +95 -0
- package/src/client/shell/LogDrawer.tsx +415 -0
- package/src/client/shell/PhotoPicker.tsx +174 -0
- package/src/client/shell/PluginOverlays.tsx +42 -0
- package/src/client/shell/PluginPanels.tsx +82 -0
- package/src/client/shell/ResetApp.tsx +87 -0
- package/src/client/shell/Resizer.tsx +99 -0
- package/src/client/shell/Shell.tsx +531 -0
- package/src/client/shell/SystemBars.tsx +376 -0
- package/src/client/shell/SystemCamera.tsx +181 -0
- package/src/client/shell/SystemOverlay.tsx +422 -0
- package/src/client/shell/SystemSheets.tsx +462 -0
- package/src/client/shell/backdrop.ts +229 -0
- package/src/client/shell/control.ts +91 -0
- package/src/client/shell/events.ts +226 -0
- package/src/client/shell/geometry.ts +68 -0
- package/src/client/shell/host.ts +492 -0
- package/src/client/shell/icons.tsx +34 -0
- package/src/client/shell/logFeed.ts +102 -0
- package/src/client/shell/main.tsx +22 -0
- package/src/client/shell/permissionCopy.ts +169 -0
- package/src/client/shell/plugins.ts +266 -0
- package/src/client/shell/popout.ts +185 -0
- package/src/client/shell/popover.ts +27 -0
- package/src/client/shell/shell.css +878 -0
- package/src/client/shell/statusTime.ts +31 -0
- package/src/client/shell/tooltips.ts +104 -0
- package/src/client/virtual.d.ts +23 -0
- package/src/devices/catalog.ts +223 -0
- package/src/devices/types.ts +83 -0
- package/src/node/appInfo.ts +188 -0
- package/src/node/appSource.ts +140 -0
- package/src/node/config.ts +180 -0
- package/src/node/control.ts +144 -0
- package/src/node/doctor.ts +199 -0
- package/src/node/events/cli.ts +206 -0
- package/src/node/events/shared.ts +238 -0
- package/src/node/events/store.ts +266 -0
- package/src/node/fonts.ts +377 -0
- package/src/node/index.ts +5 -0
- package/src/node/overrides/define.ts +56 -0
- package/src/node/overrides/packages.ts +136 -0
- package/src/node/overrides/platformPatch.ts +56 -0
- package/src/node/overrides/plugin.ts +169 -0
- package/src/node/overrides/registry.ts +258 -0
- package/src/node/paths.ts +18 -0
- package/src/node/platform.ts +171 -0
- package/src/node/plugins.ts +98 -0
- package/src/node/proxy.ts +421 -0
- package/src/node/server.ts +254 -0
- package/src/node/worklets.ts +86 -0
- package/src/plugin/index.ts +33 -0
- package/src/plugin/types.ts +127 -0
- package/src/plugin/ui.tsx +562 -0
- package/src/runtime/clock.ts +112 -0
- package/src/runtime/draw.ts +196 -0
- package/src/runtime/index.ts +519 -0
- package/src/runtime/media.ts +379 -0
- package/src/runtime/records.ts +76 -0
- package/src/runtime/timezone.ts +81 -0
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { ScanSmileyIcon } from '@phosphor-icons/react'
|
|
2
|
+
import { Button, defineShellPlugin, Field, Log, Row, Segmented, Toggle, type PluginContext } from 'web-sim/shell'
|
|
3
|
+
import { type BiometricsState, deviceSensor, INITIAL_BIOMETRICS, SENSOR_LABELS, sensorOf } from './state.ts'
|
|
4
|
+
|
|
5
|
+
/** The prompt's buttons: 0 a match, 1 a failed match, 2 the user's cancel. */
|
|
6
|
+
const ANSWERS = { match: 0, fail: 1, cancel: 2 } as const
|
|
7
|
+
|
|
8
|
+
function answer(context: PluginContext<BiometricsState>, button: number) {
|
|
9
|
+
const answered = context.answerDialog('biometric', { button })
|
|
10
|
+
if (answered === 0) {
|
|
11
|
+
context.set({ prompt: null })
|
|
12
|
+
throw new Error('No biometric prompt is waiting')
|
|
13
|
+
}
|
|
14
|
+
return { answered }
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export default defineShellPlugin<BiometricsState>({
|
|
18
|
+
id: 'biometrics',
|
|
19
|
+
title: 'Biometrics',
|
|
20
|
+
icon: ScanSmileyIcon,
|
|
21
|
+
initialState: INITIAL_BIOMETRICS,
|
|
22
|
+
actions: {
|
|
23
|
+
match: (context) => answer(context, ANSWERS.match),
|
|
24
|
+
fail: (context) => answer(context, ANSWERS.fail),
|
|
25
|
+
cancel: (context) => answer(context, ANSWERS.cancel),
|
|
26
|
+
},
|
|
27
|
+
setup: (context) => {
|
|
28
|
+
// A scan set to match or fail answers the prompt once it has shown for a moment.
|
|
29
|
+
const offPrompt = context.on('prompt', () => {
|
|
30
|
+
const { next } = context.state()
|
|
31
|
+
if (next !== 'ask') {
|
|
32
|
+
setTimeout(() => context.answerDialog('biometric', { button: ANSWERS[next] }), 700)
|
|
33
|
+
}
|
|
34
|
+
})
|
|
35
|
+
// `cancelAuthenticate`: the app takes the prompt down.
|
|
36
|
+
const offCancel = context.on('cancel', () => context.answerDialog('biometric', { button: -1 }))
|
|
37
|
+
return () => {
|
|
38
|
+
offPrompt()
|
|
39
|
+
offCancel()
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
Panel: ({ state, set, run, sim }) => {
|
|
43
|
+
const { platform } = sim.device
|
|
44
|
+
const labels: Record<string, string> = SENSOR_LABELS[platform]
|
|
45
|
+
const waiting = state.prompt !== null
|
|
46
|
+
return (
|
|
47
|
+
<>
|
|
48
|
+
<Toggle label="Enrolled" checked={state.enrolled} onChange={(enrolled) => set({ enrolled })} />
|
|
49
|
+
<Field label="Sensor">
|
|
50
|
+
<Segmented
|
|
51
|
+
label="Sensor"
|
|
52
|
+
value={sensorOf(state, sim.device)}
|
|
53
|
+
options={Object.entries(labels).map(([value, label]) => ({ value: value as keyof typeof labels & string, label }))}
|
|
54
|
+
onChange={(sensor) => set({ sensor: sensor === deviceSensor(sim.device) ? null : (sensor as BiometricsState['sensor']) })}
|
|
55
|
+
/>
|
|
56
|
+
</Field>
|
|
57
|
+
<Field label="Next scan">
|
|
58
|
+
<Segmented
|
|
59
|
+
label="Next scan"
|
|
60
|
+
value={state.next}
|
|
61
|
+
options={[
|
|
62
|
+
{ value: 'ask', label: 'Ask' },
|
|
63
|
+
{ value: 'match', label: 'Match' },
|
|
64
|
+
{ value: 'fail', label: 'Fail' },
|
|
65
|
+
]}
|
|
66
|
+
onChange={(next) => set({ next })}
|
|
67
|
+
/>
|
|
68
|
+
</Field>
|
|
69
|
+
<Field label="Prompt" value={waiting ? state.prompt || 'Waiting' : 'None'}>
|
|
70
|
+
<Row>
|
|
71
|
+
<Button disabled={!waiting} onClick={() => run('match')}>
|
|
72
|
+
Match
|
|
73
|
+
</Button>
|
|
74
|
+
<Button disabled={!waiting} onClick={() => run('fail')}>
|
|
75
|
+
Fail
|
|
76
|
+
</Button>
|
|
77
|
+
<Button disabled={!waiting} onClick={() => run('cancel')}>
|
|
78
|
+
Cancel
|
|
79
|
+
</Button>
|
|
80
|
+
</Row>
|
|
81
|
+
</Field>
|
|
82
|
+
<Field label="Requests">
|
|
83
|
+
<Log entries={state.log} />
|
|
84
|
+
</Field>
|
|
85
|
+
</>
|
|
86
|
+
)
|
|
87
|
+
},
|
|
88
|
+
})
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { BiometricSensor, Device, LogEntry } from '../../src/runtime/index.ts'
|
|
2
|
+
|
|
3
|
+
/** The phone's biometrics, set in the panel and reported by `expo-local-authentication`. */
|
|
4
|
+
export type BiometricsState = {
|
|
5
|
+
enrolled: boolean
|
|
6
|
+
/** The sensor, or null for what the device has. */
|
|
7
|
+
sensor: BiometricSensor | null
|
|
8
|
+
/** What the next scan does: wait for an answer in the prompt or the panel, or match or fail by itself. */
|
|
9
|
+
next: 'ask' | 'match' | 'fail'
|
|
10
|
+
/** The reason the prompt waiting for an answer gives, or null when none is waiting. */
|
|
11
|
+
prompt: string | null
|
|
12
|
+
/** The authentications the app asked for, with their reasons and results. */
|
|
13
|
+
log: LogEntry[]
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export const INITIAL_BIOMETRICS: BiometricsState = { enrolled: true, sensor: null, next: 'ask', prompt: null, log: [] }
|
|
17
|
+
|
|
18
|
+
/** Face ID on an iPhone without a home button; Touch ID with one; a fingerprint sensor on Android. */
|
|
19
|
+
export function deviceSensor(device: Device): BiometricSensor {
|
|
20
|
+
if (device.platform === 'android') {
|
|
21
|
+
return 'fingerprint'
|
|
22
|
+
}
|
|
23
|
+
return device.navigation === 'home-button' ? 'touch' : 'face'
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** The sensor in use: the panel's, as the platform has it, or the device's own. */
|
|
27
|
+
export function sensorOf(state: BiometricsState, device: Device): BiometricSensor {
|
|
28
|
+
const sensor = state.sensor ?? deviceSensor(device)
|
|
29
|
+
if (device.platform === 'android' && sensor === 'touch') {
|
|
30
|
+
return 'fingerprint'
|
|
31
|
+
}
|
|
32
|
+
if (device.platform === 'ios' && sensor === 'fingerprint') {
|
|
33
|
+
return 'touch'
|
|
34
|
+
}
|
|
35
|
+
return sensor
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export const SENSOR_LABELS = {
|
|
39
|
+
ios: { face: 'Face ID', touch: 'Touch ID' },
|
|
40
|
+
android: { face: 'Face', fingerprint: 'Fingerprint' },
|
|
41
|
+
} as const
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { plugin } from 'web-sim/runtime'
|
|
2
|
+
import { type BrightnessState, INITIAL_BRIGHTNESS } from './state.ts'
|
|
3
|
+
|
|
4
|
+
// A new run of the app holds no window brightness: Android's goes with the app's process.
|
|
5
|
+
plugin<BrightnessState>('brightness', INITIAL_BRIGHTNESS).set({ app: null })
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { createPermissionHook, PermissionStatus, type PermissionResponse } from 'expo'
|
|
2
|
+
import { platform, plugin } from 'web-sim/runtime'
|
|
3
|
+
import { type BrightnessState, INITIAL_BRIGHTNESS, screenLevel } from '../../state.ts'
|
|
4
|
+
|
|
5
|
+
export const BrightnessMode = { UNKNOWN: 0, AUTOMATIC: 1, MANUAL: 2 } as const
|
|
6
|
+
export type BrightnessMode = (typeof BrightnessMode)[keyof typeof BrightnessMode]
|
|
7
|
+
|
|
8
|
+
export type BrightnessEvent = { brightness: number }
|
|
9
|
+
|
|
10
|
+
export { PermissionStatus } from 'expo'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The brightness panel's levels. On iOS the app sets the screen's own brightness, which is the system's. On Android
|
|
14
|
+
* it sets its window's, which lasts until the app restarts or restores it, over the system's level.
|
|
15
|
+
*/
|
|
16
|
+
const brightness = () => plugin<BrightnessState>('brightness', INITIAL_BRIGHTNESS)
|
|
17
|
+
|
|
18
|
+
const MODES = { automatic: BrightnessMode.AUTOMATIC, manual: BrightnessMode.MANUAL } as const
|
|
19
|
+
|
|
20
|
+
function clamp(value: number, name: string): number {
|
|
21
|
+
const clamped = Math.max(0, Math.min(value, 1))
|
|
22
|
+
if (Number.isNaN(clamped)) {
|
|
23
|
+
throw new TypeError(`${name} cannot be called with ${value}`)
|
|
24
|
+
}
|
|
25
|
+
return clamped
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const granted: PermissionResponse = { status: PermissionStatus.GRANTED, granted: true, canAskAgain: true, expires: 'never' }
|
|
29
|
+
|
|
30
|
+
export async function isAvailableAsync(): Promise<boolean> {
|
|
31
|
+
return true
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export async function getBrightnessAsync(): Promise<number> {
|
|
35
|
+
return screenLevel(brightness().get(), platform())
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export async function setBrightnessAsync(brightnessValue: number): Promise<void> {
|
|
39
|
+
const level = clamp(brightnessValue, 'setBrightnessAsync')
|
|
40
|
+
brightness().set(platform() === 'android' ? { app: level } : { app: level, system: level })
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export async function getSystemBrightnessAsync(): Promise<number> {
|
|
44
|
+
return brightness().get().system
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export async function setSystemBrightnessAsync(brightnessValue: number): Promise<void> {
|
|
48
|
+
const level = clamp(brightnessValue, 'setSystemBrightnessAsync')
|
|
49
|
+
if (platform() !== 'android') {
|
|
50
|
+
return setBrightnessAsync(level)
|
|
51
|
+
}
|
|
52
|
+
brightness().set({ system: level, mode: 'manual' })
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export async function restoreSystemBrightnessAsync(): Promise<void> {
|
|
56
|
+
if (platform() === 'android') {
|
|
57
|
+
brightness().set({ app: null })
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export async function isUsingSystemBrightnessAsync(): Promise<boolean> {
|
|
62
|
+
return platform() === 'android' && brightness().get().app === null
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export async function getSystemBrightnessModeAsync(): Promise<BrightnessMode> {
|
|
66
|
+
return platform() === 'android' ? MODES[brightness().get().mode] : BrightnessMode.UNKNOWN
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export async function setSystemBrightnessModeAsync(brightnessMode: BrightnessMode): Promise<void> {
|
|
70
|
+
if (platform() === 'android' && brightnessMode !== BrightnessMode.UNKNOWN) {
|
|
71
|
+
brightness().set({ mode: brightnessMode === BrightnessMode.AUTOMATIC ? 'automatic' : 'manual' })
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** iOS needs no permission; Android's `WRITE_SETTINGS` is taken as granted. */
|
|
76
|
+
export async function getPermissionsAsync(): Promise<PermissionResponse> {
|
|
77
|
+
return granted
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export async function requestPermissionsAsync(): Promise<PermissionResponse> {
|
|
81
|
+
return granted
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export const usePermissions = createPermissionHook({
|
|
85
|
+
getMethod: getPermissionsAsync,
|
|
86
|
+
requestMethod: requestPermissionsAsync,
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
/** Hears the screen's brightness change, from the app or the panel. */
|
|
90
|
+
export function addBrightnessListener(listener: (event: BrightnessEvent) => void): { remove: () => void } {
|
|
91
|
+
const remove = brightness().watch((next, previous) => {
|
|
92
|
+
const [level, last] = [screenLevel(next, platform()), screenLevel(previous, platform())]
|
|
93
|
+
if (level !== last) {
|
|
94
|
+
listener({ brightness: level })
|
|
95
|
+
}
|
|
96
|
+
})
|
|
97
|
+
return { remove }
|
|
98
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { defineOverrides } from '../../../../src/node/overrides/define.ts'
|
|
2
|
+
|
|
3
|
+
export default defineOverrides('expo-brightness', [
|
|
4
|
+
{
|
|
5
|
+
versions: '>=58 <59',
|
|
6
|
+
note: "reports and sets the brightness panel's levels, which dim the screen; Android's window and system levels",
|
|
7
|
+
// The JS layer picks the Android or iOS behaviour from `Platform.OS`, which is 'web' inside dependencies.
|
|
8
|
+
files: { 'build/Brightness.js': './Brightness.ts' },
|
|
9
|
+
},
|
|
10
|
+
])
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { SunDimIcon } from '@phosphor-icons/react'
|
|
2
|
+
import { Button, defineShellPlugin, Field, Row, Segmented, Slider } from 'web-sim/shell'
|
|
3
|
+
import { type BrightnessState, INITIAL_BRIGHTNESS, screenLevel } from './state.ts'
|
|
4
|
+
|
|
5
|
+
const percent = (level: number) => `${Math.round(level * 100)}%`
|
|
6
|
+
|
|
7
|
+
export default defineShellPlugin<BrightnessState>({
|
|
8
|
+
id: 'brightness',
|
|
9
|
+
title: 'Brightness',
|
|
10
|
+
icon: SunDimIcon,
|
|
11
|
+
initialState: INITIAL_BRIGHTNESS,
|
|
12
|
+
// The drawn screen at half brightness is the shell's own; lower darkens it, higher lifts it a little.
|
|
13
|
+
screen: (state, sim) => {
|
|
14
|
+
const level = screenLevel(state, sim.device.platform)
|
|
15
|
+
return level === 0.5 ? {} : { filter: `brightness(${level < 0.5 ? 0.3 + 1.4 * level : 1 + (level - 0.5) * 0.3})` }
|
|
16
|
+
},
|
|
17
|
+
actions: {
|
|
18
|
+
/** Android: drops the app's window brightness, as `restoreSystemBrightnessAsync` does. */
|
|
19
|
+
restore: (context) => {
|
|
20
|
+
context.set({ app: null })
|
|
21
|
+
return context.state()
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
Panel: ({ state, set, run, sim }) => {
|
|
25
|
+
const android = sim.device.platform === 'android'
|
|
26
|
+
return (
|
|
27
|
+
<>
|
|
28
|
+
<Field label="System" value={percent(state.system)}>
|
|
29
|
+
<Slider label="System brightness" value={state.system} onChange={(system) => set({ system })} />
|
|
30
|
+
</Field>
|
|
31
|
+
{android && (
|
|
32
|
+
<Segmented
|
|
33
|
+
label="Mode"
|
|
34
|
+
value={state.mode}
|
|
35
|
+
options={[
|
|
36
|
+
{ value: 'automatic', label: 'Automatic' },
|
|
37
|
+
{ value: 'manual', label: 'Manual' },
|
|
38
|
+
]}
|
|
39
|
+
onChange={(mode) => set({ mode })}
|
|
40
|
+
/>
|
|
41
|
+
)}
|
|
42
|
+
<Field label={android ? 'App window' : 'Set by app'} value={state.app === null ? 'None' : percent(state.app)}>
|
|
43
|
+
{android && state.app !== null && (
|
|
44
|
+
<Row>
|
|
45
|
+
<Button title="Restore the system brightness" onClick={() => run('restore')}>
|
|
46
|
+
Restore
|
|
47
|
+
</Button>
|
|
48
|
+
</Row>
|
|
49
|
+
)}
|
|
50
|
+
</Field>
|
|
51
|
+
<Field label="Screen" value={percent(screenLevel(state, sim.device.platform))} />
|
|
52
|
+
</>
|
|
53
|
+
)
|
|
54
|
+
},
|
|
55
|
+
})
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/** The screen's brightness: the system's level, set in the panel, and the level the app sets with `expo-brightness`. */
|
|
2
|
+
export type BrightnessState = {
|
|
3
|
+
/** The system's level, 0 to 1. */
|
|
4
|
+
system: number
|
|
5
|
+
/**
|
|
6
|
+
* The level the app set this run, 0 to 1, or null. On Android it is the app's window brightness, which overrides the
|
|
7
|
+
* system's level until the app restarts or restores it. On iOS the app sets the system's level, so this is a record.
|
|
8
|
+
*/
|
|
9
|
+
app: number | null
|
|
10
|
+
/** Android's automatic or manual brightness. */
|
|
11
|
+
mode: 'automatic' | 'manual'
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const INITIAL_BRIGHTNESS: BrightnessState = { system: 0.5, app: null, mode: 'automatic' }
|
|
15
|
+
|
|
16
|
+
/** The screen's level: the app's window brightness on Android while it holds one, else the system's. */
|
|
17
|
+
export function screenLevel(state: BrightnessState, platform: 'ios' | 'android'): number {
|
|
18
|
+
return platform === 'android' && state.app !== null ? state.app : state.system
|
|
19
|
+
}
|
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
import {
|
|
2
|
+
fromWallTime,
|
|
3
|
+
type LogEntry,
|
|
4
|
+
noRecordChanges,
|
|
5
|
+
phoneNow,
|
|
6
|
+
phoneTimeZone,
|
|
7
|
+
type Platform,
|
|
8
|
+
type RecordChanges,
|
|
9
|
+
simRecords,
|
|
10
|
+
type SimRecords,
|
|
11
|
+
toWallTime,
|
|
12
|
+
} from 'web-sim/runtime'
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The phone's calendars: a few made-up ones with events placed around today, plus the changes the app and the panel
|
|
16
|
+
* make, kept per platform in the plugin's state (iOS and Android give their records different ids).
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
export type CalendarRecord = {
|
|
20
|
+
id: string
|
|
21
|
+
title: string
|
|
22
|
+
color: string
|
|
23
|
+
entityType: 'event' | 'reminder'
|
|
24
|
+
source: { id: string; name: string; type: string; isLocalAccount: boolean }
|
|
25
|
+
sourceId: string
|
|
26
|
+
type?: string
|
|
27
|
+
allowsModifications: boolean
|
|
28
|
+
allowedAvailabilities: string[]
|
|
29
|
+
isPrimary?: boolean
|
|
30
|
+
name?: string | null
|
|
31
|
+
ownerAccount?: string
|
|
32
|
+
timeZone?: string
|
|
33
|
+
allowedReminders?: string[]
|
|
34
|
+
allowedAttendeeTypes?: string[]
|
|
35
|
+
isVisible?: boolean
|
|
36
|
+
isSynced?: boolean
|
|
37
|
+
accessLevel?: string
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export type EventRecord = {
|
|
41
|
+
id: string
|
|
42
|
+
calendarId: string
|
|
43
|
+
title: string
|
|
44
|
+
location: string | null
|
|
45
|
+
notes: string
|
|
46
|
+
startDate: string
|
|
47
|
+
endDate: string
|
|
48
|
+
allDay: boolean
|
|
49
|
+
timeZone: string
|
|
50
|
+
url?: string
|
|
51
|
+
alarms: { relativeOffset?: number; absoluteDate?: string }[]
|
|
52
|
+
recurrenceRule: unknown
|
|
53
|
+
availability: string
|
|
54
|
+
status: string
|
|
55
|
+
creationDate: string
|
|
56
|
+
lastModifiedDate: string
|
|
57
|
+
attendees: { name: string; email: string; role: string; status: string; type: string; isCurrentUser?: boolean }[]
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export type ReminderRecord = {
|
|
61
|
+
id: string
|
|
62
|
+
calendarId: string
|
|
63
|
+
title: string
|
|
64
|
+
location?: string
|
|
65
|
+
notes?: string
|
|
66
|
+
startDate?: string
|
|
67
|
+
dueDate?: string
|
|
68
|
+
allDay?: boolean
|
|
69
|
+
completed: boolean
|
|
70
|
+
completionDate?: string
|
|
71
|
+
creationDate: string
|
|
72
|
+
lastModifiedDate: string
|
|
73
|
+
timeZone?: string
|
|
74
|
+
alarms?: unknown[]
|
|
75
|
+
recurrenceRule?: unknown
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export const TIME_ZONE = 'America/Los_Angeles'
|
|
79
|
+
|
|
80
|
+
export function ids(os: Platform) {
|
|
81
|
+
const ios = os === 'ios'
|
|
82
|
+
return {
|
|
83
|
+
home: ios ? 'CA1E0001-0000-4000-8000-00000000HOME' : '1',
|
|
84
|
+
work: ios ? 'CA1E0002-0000-4000-8000-00000000WORK' : '3',
|
|
85
|
+
family: ios ? 'CA1E0003-0000-4000-8000-0000000FAMLY' : '4',
|
|
86
|
+
holidays: '2',
|
|
87
|
+
reminders: 'CA1E0004-0000-4000-8000-00000REMNDRS',
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function builtInCalendars(os: Platform): CalendarRecord[] {
|
|
92
|
+
const id = ids(os)
|
|
93
|
+
if (os === 'ios') {
|
|
94
|
+
const source = { id: '5A1B0000-0000-4000-8000-0000000C10D0', name: 'iCloud', type: 'caldav', isLocalAccount: false }
|
|
95
|
+
const calendar = (calendarId: string, title: string, color: string, entityType: 'event' | 'reminder' = 'event'): CalendarRecord => ({
|
|
96
|
+
id: calendarId,
|
|
97
|
+
title,
|
|
98
|
+
color,
|
|
99
|
+
entityType,
|
|
100
|
+
source,
|
|
101
|
+
sourceId: source.id,
|
|
102
|
+
type: 'caldav',
|
|
103
|
+
allowsModifications: true,
|
|
104
|
+
allowedAvailabilities: entityType === 'event' ? ['busy', 'free'] : [],
|
|
105
|
+
})
|
|
106
|
+
return [calendar(id.home, 'Home', '#34AADC'), calendar(id.work, 'Work', '#CC73E1'), calendar(id.family, 'Family', '#63DA38'), calendar(id.reminders, 'Reminders', '#FF9500', 'reminder')]
|
|
107
|
+
}
|
|
108
|
+
const account = 'you@example.com'
|
|
109
|
+
const calendar = (calendarId: string, title: string, color: string, extra: Partial<CalendarRecord> = {}): CalendarRecord => ({
|
|
110
|
+
id: calendarId,
|
|
111
|
+
title,
|
|
112
|
+
name: title,
|
|
113
|
+
color,
|
|
114
|
+
entityType: 'event',
|
|
115
|
+
source: { id: account, name: account, type: 'com.google', isLocalAccount: false },
|
|
116
|
+
sourceId: account,
|
|
117
|
+
ownerAccount: account,
|
|
118
|
+
timeZone: TIME_ZONE,
|
|
119
|
+
allowsModifications: true,
|
|
120
|
+
allowedAvailabilities: ['busy', 'free', 'tentative'],
|
|
121
|
+
allowedReminders: ['alert', 'email', 'default'],
|
|
122
|
+
allowedAttendeeTypes: ['none', 'required', 'optional', 'resource'],
|
|
123
|
+
isVisible: true,
|
|
124
|
+
isSynced: true,
|
|
125
|
+
isPrimary: false,
|
|
126
|
+
accessLevel: 'owner',
|
|
127
|
+
...extra,
|
|
128
|
+
})
|
|
129
|
+
return [
|
|
130
|
+
calendar(id.home, account, '#039BE5', { isPrimary: true, name: account }),
|
|
131
|
+
calendar(id.holidays, 'Holidays in United States', '#0B8043', { allowsModifications: false, accessLevel: 'read', ownerAccount: 'en.usa#holiday@group.v.calendar.google.com' }),
|
|
132
|
+
calendar(id.work, 'Work', '#8E24AA'),
|
|
133
|
+
calendar(id.family, 'Family', '#F4511E'),
|
|
134
|
+
]
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Today on the phone: its clock's date in its time zone, as a wall-clock time read with the UTC methods. */
|
|
138
|
+
function today(): Date {
|
|
139
|
+
return new Date(toWallTime(phoneTimeZone(), phoneNow()))
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** A time on a day relative to the phone's today: `at(1, 12, 30)` is tomorrow at half past twelve. */
|
|
143
|
+
function at(days: number, hours = 0, minutes = 0): string {
|
|
144
|
+
const wall = today()
|
|
145
|
+
wall.setUTCHours(hours, minutes, 0, 0)
|
|
146
|
+
wall.setUTCDate(wall.getUTCDate() + days)
|
|
147
|
+
return new Date(fromWallTime(phoneTimeZone(), wall.getTime())).toISOString()
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function builtInEvents(os: Platform): EventRecord[] {
|
|
151
|
+
const id = ids(os)
|
|
152
|
+
const ios = os === 'ios'
|
|
153
|
+
const created = at(-14, 10)
|
|
154
|
+
const event = (n: number, calendarId: string, title: string, start: string, end: string, extra: Partial<EventRecord> = {}): EventRecord => ({
|
|
155
|
+
id: ios ? `E7E00${String(n).padStart(3, '0')}-0000-4000-8000-0000000EVENT` : String(100 + n),
|
|
156
|
+
calendarId,
|
|
157
|
+
title,
|
|
158
|
+
location: null,
|
|
159
|
+
notes: '',
|
|
160
|
+
startDate: start,
|
|
161
|
+
endDate: end,
|
|
162
|
+
allDay: false,
|
|
163
|
+
timeZone: TIME_ZONE,
|
|
164
|
+
alarms: [{ relativeOffset: -15 }],
|
|
165
|
+
recurrenceRule: null,
|
|
166
|
+
availability: 'busy',
|
|
167
|
+
status: ios ? 'confirmed' : 'confirmed',
|
|
168
|
+
creationDate: created,
|
|
169
|
+
lastModifiedDate: created,
|
|
170
|
+
attendees: [],
|
|
171
|
+
...extra,
|
|
172
|
+
})
|
|
173
|
+
const weekday = today().getUTCDay()
|
|
174
|
+
const toSaturday = (6 - weekday + 7) % 7
|
|
175
|
+
const standups = [-2, -1, 0, 1, 2, 3, 4]
|
|
176
|
+
.filter((offset) => {
|
|
177
|
+
const day = (weekday + offset + 7) % 7
|
|
178
|
+
return day !== 0 && day !== 6
|
|
179
|
+
})
|
|
180
|
+
.map((offset, index) => event(index + 1, id.work, 'Team standup', at(offset, 9, 30), at(offset, 9, 45), { alarms: [] }))
|
|
181
|
+
return [
|
|
182
|
+
...standups,
|
|
183
|
+
event(20, id.home, 'Lunch with Ava', at(1, 12, 30), at(1, 13, 30), {
|
|
184
|
+
location: 'Tartine Manufactory, 595 Alabama St, San Francisco',
|
|
185
|
+
attendees: [
|
|
186
|
+
{ name: 'Ava Chen', email: 'ava.chen@example.com', role: 'required', status: 'accepted', type: 'person' },
|
|
187
|
+
{ name: 'You', email: 'you@example.com', role: 'required', status: 'accepted', type: 'person', isCurrentUser: true },
|
|
188
|
+
],
|
|
189
|
+
}),
|
|
190
|
+
event(21, id.home, 'Dentist', at(3, 15), at(3, 16), { location: 'Cupertino Dental' }),
|
|
191
|
+
event(22, id.family, 'Farmers market', at(toSaturday, 9), at(toSaturday, 11), { location: 'Mountain View Farmers Market' }),
|
|
192
|
+
event(23, id.family, 'Dinner party', at(toSaturday, 19), at(toSaturday, 22), { notes: 'Bring the lemon tart.' }),
|
|
193
|
+
event(24, id.home, 'Meal prep', at(toSaturday + 1), at(toSaturday + 2), { allDay: true, availability: 'free', alarms: [] }),
|
|
194
|
+
event(25, id.work, 'Menu planning', at(-1, 14), at(-1, 15)),
|
|
195
|
+
]
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function builtInReminders(os: Platform): ReminderRecord[] {
|
|
199
|
+
if (os !== 'ios') {
|
|
200
|
+
return []
|
|
201
|
+
}
|
|
202
|
+
const calendarId = ids(os).reminders
|
|
203
|
+
const created = at(-3, 18)
|
|
204
|
+
const reminder = (n: number, title: string, extra: Partial<ReminderRecord>): ReminderRecord => ({
|
|
205
|
+
id: `9E31D000-0000-4000-8000-00000000000${n}`,
|
|
206
|
+
calendarId,
|
|
207
|
+
title,
|
|
208
|
+
completed: false,
|
|
209
|
+
creationDate: created,
|
|
210
|
+
lastModifiedDate: created,
|
|
211
|
+
timeZone: TIME_ZONE,
|
|
212
|
+
...extra,
|
|
213
|
+
})
|
|
214
|
+
return [
|
|
215
|
+
reminder(1, 'Buy oat milk', { dueDate: at(0, 18) }),
|
|
216
|
+
reminder(2, 'Pick up basil', { dueDate: at(1, 17), notes: 'The big bunch' }),
|
|
217
|
+
reminder(3, 'Book a table for Friday', { completed: true, completionDate: at(-1, 20) }),
|
|
218
|
+
]
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
type Changes = { calendars: RecordChanges<CalendarRecord>; events: RecordChanges<EventRecord>; reminders: RecordChanges<ReminderRecord> }
|
|
222
|
+
|
|
223
|
+
const noChanges = (): Changes => ({ calendars: noRecordChanges(), events: noRecordChanges(), reminders: noRecordChanges() })
|
|
224
|
+
|
|
225
|
+
export type CalendarState = {
|
|
226
|
+
ios: Changes
|
|
227
|
+
android: Changes
|
|
228
|
+
/** What the app added, changed and deleted. */
|
|
229
|
+
log: LogEntry[]
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
export const INITIAL_CALENDAR: CalendarState = { ios: noChanges(), android: noChanges(), log: [] }
|
|
233
|
+
|
|
234
|
+
export type CalendarHandle = { get(): CalendarState; set(patch: Partial<CalendarState>): void }
|
|
235
|
+
|
|
236
|
+
export type CalendarStore = {
|
|
237
|
+
calendars: SimRecords<CalendarRecord>
|
|
238
|
+
events: SimRecords<EventRecord>
|
|
239
|
+
reminders: SimRecords<ReminderRecord>
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const NOUNS = { calendars: 'calendar', events: 'event', reminders: 'reminder' } as const
|
|
243
|
+
|
|
244
|
+
/** The calendars, events and reminders for a platform. `onWrite` hears each change, with a line for a log. */
|
|
245
|
+
export function calendarStore(handle: CalendarHandle, os: Platform, onWrite?: (text: string) => void): CalendarStore {
|
|
246
|
+
const records = <K extends keyof Changes, T extends { id: string; title: string }>(kind: K, builtIn: (os: Platform) => T[]): SimRecords<T> => {
|
|
247
|
+
const plain = simRecords<T>(() => builtIn(os), {
|
|
248
|
+
read: () => handle.get()[os][kind] as unknown as RecordChanges<T>,
|
|
249
|
+
write: (changes) => handle.set({ [os]: { ...handle.get()[os], [kind]: changes } }),
|
|
250
|
+
})
|
|
251
|
+
if (onWrite === undefined) {
|
|
252
|
+
return plain
|
|
253
|
+
}
|
|
254
|
+
return {
|
|
255
|
+
...plain,
|
|
256
|
+
put(record) {
|
|
257
|
+
const existed = plain.get(record.id) !== null
|
|
258
|
+
plain.put(record)
|
|
259
|
+
onWrite(`${existed ? 'Changed' : 'Added'} ${NOUNS[kind]} ${record.title || 'with no title'}`)
|
|
260
|
+
return record
|
|
261
|
+
},
|
|
262
|
+
remove(id) {
|
|
263
|
+
const record = plain.get(id)
|
|
264
|
+
plain.remove(id)
|
|
265
|
+
onWrite(`Deleted ${NOUNS[kind]} ${record?.title || 'with no title'}`)
|
|
266
|
+
},
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
return {
|
|
270
|
+
calendars: records('calendars', builtInCalendars),
|
|
271
|
+
events: records('events', builtInEvents),
|
|
272
|
+
reminders: records('reminders', builtInReminders),
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/** A new event's fields, with the module's defaults. */
|
|
277
|
+
export function newEvent(id: string, calendarId: string, details: Partial<EventRecord>): EventRecord {
|
|
278
|
+
const now = new Date(phoneNow()).toISOString()
|
|
279
|
+
const start = details.startDate ?? now
|
|
280
|
+
return {
|
|
281
|
+
title: '',
|
|
282
|
+
location: null,
|
|
283
|
+
notes: '',
|
|
284
|
+
allDay: false,
|
|
285
|
+
timeZone: TIME_ZONE,
|
|
286
|
+
alarms: [],
|
|
287
|
+
recurrenceRule: null,
|
|
288
|
+
availability: 'busy',
|
|
289
|
+
status: 'confirmed',
|
|
290
|
+
attendees: [],
|
|
291
|
+
...details,
|
|
292
|
+
startDate: start,
|
|
293
|
+
endDate: details.endDate ?? new Date(new Date(start).getTime() + 3_600_000).toISOString(),
|
|
294
|
+
id,
|
|
295
|
+
calendarId,
|
|
296
|
+
creationDate: now,
|
|
297
|
+
lastModifiedDate: now,
|
|
298
|
+
}
|
|
299
|
+
}
|