@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,192 @@
|
|
|
1
|
+
import { BellIcon, LightningIcon, PaperPlaneTiltIcon, TrashIcon, XIcon } from '@phosphor-icons/react'
|
|
2
|
+
import { useState } from 'react'
|
|
3
|
+
import { app } from 'web-sim/runtime'
|
|
4
|
+
import { Button, Copyable, defineShellPlugin, Field, IconButton, List, ListItem, NumberField, Row, Section, Select, TextField } from 'web-sim/shell'
|
|
5
|
+
import { NotificationCenter } from './center.ts'
|
|
6
|
+
import { INITIAL_NOTIFICATIONS, type Json, type NotificationsState, type Push } from './state.ts'
|
|
7
|
+
import { configProjectId, devicePushToken, expoPushToken } from './tokens.ts'
|
|
8
|
+
import { phoneTimeZone } from './trigger.ts'
|
|
9
|
+
|
|
10
|
+
/** The shell's one notification centre, made by `setup`; its timers are not state. */
|
|
11
|
+
let center: NotificationCenter | null = null
|
|
12
|
+
|
|
13
|
+
function requireCenter(): NotificationCenter {
|
|
14
|
+
if (center === null) {
|
|
15
|
+
throw new Error('The notifications plugin has not started')
|
|
16
|
+
}
|
|
17
|
+
return center
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function when(time: number): string {
|
|
21
|
+
return new Date(time).toLocaleString('en-US', {
|
|
22
|
+
timeZone: phoneTimeZone(),
|
|
23
|
+
month: 'short',
|
|
24
|
+
day: 'numeric',
|
|
25
|
+
hour: 'numeric',
|
|
26
|
+
minute: '2-digit',
|
|
27
|
+
second: '2-digit',
|
|
28
|
+
})
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function heading(content: Json, identifier: string): string {
|
|
32
|
+
return [content.title, content.body].find((value) => typeof value === 'string' && value !== '') as string | undefined ?? identifier
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function triggerLabel(trigger: Json | null): string {
|
|
36
|
+
if (trigger === null) {
|
|
37
|
+
return 'now'
|
|
38
|
+
}
|
|
39
|
+
switch (trigger.type) {
|
|
40
|
+
case 'timeInterval':
|
|
41
|
+
return `${trigger.repeats === true ? 'every' : 'after'} ${trigger.seconds}s`
|
|
42
|
+
case 'calendar':
|
|
43
|
+
return trigger.repeats === true ? 'calendar, repeats' : 'calendar'
|
|
44
|
+
default:
|
|
45
|
+
return String(trigger.type)
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
type Draft = { title: string; subtitle: string; body: string; data: string; badge: string; channelId: string }
|
|
50
|
+
|
|
51
|
+
const EMPTY_DRAFT: Draft = { title: 'New message', subtitle: '', body: 'Hello from web-sim', data: '', badge: '', channelId: '' }
|
|
52
|
+
|
|
53
|
+
function parseDraft(draft: Draft): Push | null {
|
|
54
|
+
let data: Json
|
|
55
|
+
try {
|
|
56
|
+
data = draft.data.trim() === '' ? {} : (JSON.parse(draft.data) as Json)
|
|
57
|
+
if (typeof data !== 'object' || data === null || Array.isArray(data)) {
|
|
58
|
+
return null
|
|
59
|
+
}
|
|
60
|
+
} catch {
|
|
61
|
+
return null
|
|
62
|
+
}
|
|
63
|
+
const badge = draft.badge.trim() === '' ? undefined : Number(draft.badge)
|
|
64
|
+
if (badge !== undefined && !Number.isInteger(badge)) {
|
|
65
|
+
return null
|
|
66
|
+
}
|
|
67
|
+
return {
|
|
68
|
+
title: draft.title || undefined,
|
|
69
|
+
subtitle: draft.subtitle || undefined,
|
|
70
|
+
body: draft.body || undefined,
|
|
71
|
+
data,
|
|
72
|
+
badge,
|
|
73
|
+
channelId: draft.channelId || undefined,
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export default defineShellPlugin<NotificationsState>({
|
|
78
|
+
id: 'notifications',
|
|
79
|
+
title: 'Notifications',
|
|
80
|
+
icon: BellIcon,
|
|
81
|
+
initialState: INITIAL_NOTIFICATIONS,
|
|
82
|
+
setup: (context) => {
|
|
83
|
+
center = new NotificationCenter(context)
|
|
84
|
+
const stop = center.start()
|
|
85
|
+
return () => {
|
|
86
|
+
stop()
|
|
87
|
+
center = null
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
appStarted: () => center?.appStarted(),
|
|
91
|
+
actions: {
|
|
92
|
+
/** `{title, subtitle, body, data, badge, channelId, sound}`: a remote notification, arriving as a push does. */
|
|
93
|
+
push: (_context, args) => requireCenter().push((args ?? {}) as Push),
|
|
94
|
+
/** Delivers every scheduled notification now. */
|
|
95
|
+
fireAll: (context) => {
|
|
96
|
+
const { scheduled } = context.state()
|
|
97
|
+
for (const { request } of scheduled) {
|
|
98
|
+
requireCenter().fire(request.identifier)
|
|
99
|
+
}
|
|
100
|
+
return scheduled.length
|
|
101
|
+
},
|
|
102
|
+
fire: (_context, args) => requireCenter().fire((args as { identifier: string }).identifier),
|
|
103
|
+
cancel: (context, args) => {
|
|
104
|
+
const identifier = (args as { identifier?: string } | undefined)?.identifier
|
|
105
|
+
context.set({ scheduled: identifier === undefined ? [] : context.state().scheduled.filter((item) => item.request.identifier !== identifier) })
|
|
106
|
+
},
|
|
107
|
+
dismiss: (context, args) => {
|
|
108
|
+
const identifier = (args as { identifier?: string } | undefined)?.identifier
|
|
109
|
+
context.set({ delivered: identifier === undefined ? [] : context.state().delivered.filter((item) => item.request.identifier !== identifier) })
|
|
110
|
+
},
|
|
111
|
+
clearBadge: (context) => context.set({ badge: 0 }),
|
|
112
|
+
},
|
|
113
|
+
Panel: ({ state, set, run, sim }) => {
|
|
114
|
+
const [draft, setDraft] = useState<Draft>(EMPTY_DRAFT)
|
|
115
|
+
const push = parseDraft(draft)
|
|
116
|
+
const platform = sim.device.platform
|
|
117
|
+
const deviceToken = devicePushToken(app().slug, platform, state.tokenGeneration)
|
|
118
|
+
const channels = Object.values(state.channels)
|
|
119
|
+
return (
|
|
120
|
+
<>
|
|
121
|
+
<Section title="Remote push">
|
|
122
|
+
<TextField label="Title" placeholder="Title" value={draft.title} onChange={(title) => setDraft({ ...draft, title })} />
|
|
123
|
+
{platform === 'ios' && <TextField label="Subtitle" placeholder="Subtitle" value={draft.subtitle} onChange={(subtitle) => setDraft({ ...draft, subtitle })} />}
|
|
124
|
+
<TextField label="Body" placeholder="Body" multiline value={draft.body} onChange={(body) => setDraft({ ...draft, body })} />
|
|
125
|
+
<TextField label="Data (JSON)" placeholder="Data (JSON)" multiline value={draft.data} onChange={(data) => setDraft({ ...draft, data })} />
|
|
126
|
+
<Row>
|
|
127
|
+
<TextField label="Badge" placeholder="Badge" value={draft.badge} onChange={(badge) => setDraft({ ...draft, badge })} />
|
|
128
|
+
{platform === 'android' && (
|
|
129
|
+
<Select
|
|
130
|
+
label="Channel"
|
|
131
|
+
value={draft.channelId}
|
|
132
|
+
options={[{ value: '', label: 'Default channel' }, ...channels.map((channel) => ({ value: String(channel.id), label: String(channel.name ?? channel.id) }))]}
|
|
133
|
+
onChange={(channelId) => setDraft({ ...draft, channelId })}
|
|
134
|
+
/>
|
|
135
|
+
)}
|
|
136
|
+
</Row>
|
|
137
|
+
<Row>
|
|
138
|
+
<Button disabled={push === null} title={push === null ? 'Invalid data or badge' : undefined} onClick={() => push !== null && run('push', push)}>
|
|
139
|
+
Send
|
|
140
|
+
</Button>
|
|
141
|
+
</Row>
|
|
142
|
+
</Section>
|
|
143
|
+
<Section title={`Scheduled · ${state.scheduled.length}`} actions={<IconButton icon={LightningIcon} title="Fire all now" disabled={state.scheduled.length === 0} onClick={() => run('fireAll')} />}>
|
|
144
|
+
<List empty="Nothing scheduled">
|
|
145
|
+
{state.scheduled.map(({ request, next }) => (
|
|
146
|
+
<ListItem
|
|
147
|
+
key={request.identifier}
|
|
148
|
+
title={heading(request.content, request.identifier)}
|
|
149
|
+
detail={`${when(next)} · ${triggerLabel(request.trigger)}`}
|
|
150
|
+
actions={
|
|
151
|
+
<>
|
|
152
|
+
<IconButton icon={PaperPlaneTiltIcon} title={`Fire ${request.identifier} now`} onClick={() => run('fire', { identifier: request.identifier })} />
|
|
153
|
+
<IconButton icon={XIcon} title={`Cancel ${request.identifier}`} onClick={() => run('cancel', { identifier: request.identifier })} />
|
|
154
|
+
</>
|
|
155
|
+
}
|
|
156
|
+
/>
|
|
157
|
+
))}
|
|
158
|
+
</List>
|
|
159
|
+
</Section>
|
|
160
|
+
<Section
|
|
161
|
+
title={`Delivered · ${state.delivered.length}`}
|
|
162
|
+
actions={<IconButton icon={TrashIcon} title="Clear notification centre" disabled={state.delivered.length === 0} onClick={() => run('dismiss')} />}
|
|
163
|
+
>
|
|
164
|
+
<List empty="None">
|
|
165
|
+
{[...state.delivered].reverse().map(({ request, date }) => (
|
|
166
|
+
<ListItem
|
|
167
|
+
key={request.identifier}
|
|
168
|
+
title={heading(request.content, request.identifier)}
|
|
169
|
+
detail={`${when(date)}${request.trigger?.type === 'push' ? ' · push' : ''}`}
|
|
170
|
+
actions={<IconButton icon={XIcon} title={`Dismiss ${request.identifier}`} onClick={() => run('dismiss', { identifier: request.identifier })} />}
|
|
171
|
+
/>
|
|
172
|
+
))}
|
|
173
|
+
</List>
|
|
174
|
+
</Section>
|
|
175
|
+
<Field label="Badge">
|
|
176
|
+
<Row>
|
|
177
|
+
<NumberField label="Badge" value={state.badge} min={0} onChange={(badge) => set({ badge: Math.max(0, Math.floor(badge)) })} />
|
|
178
|
+
<Button onClick={() => run('clearBadge')}>Clear</Button>
|
|
179
|
+
</Row>
|
|
180
|
+
</Field>
|
|
181
|
+
<Section title="Push tokens">
|
|
182
|
+
<Field label={platform === 'ios' ? 'APNs device token' : 'FCM device token'}>
|
|
183
|
+
<Copyable label="device token" value={deviceToken} />
|
|
184
|
+
</Field>
|
|
185
|
+
<Field label="Expo push token">
|
|
186
|
+
<Copyable label="Expo push token" value={expoPushToken(deviceToken, configProjectId(app().config))} />
|
|
187
|
+
</Field>
|
|
188
|
+
</Section>
|
|
189
|
+
</>
|
|
190
|
+
)
|
|
191
|
+
},
|
|
192
|
+
})
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The phone's side of expo-notifications, kept by the notifications plugin: what the app scheduled (with when each
|
|
3
|
+
* next fires), what sits in the notification centre, the badge, and the app's channels and categories. The shell
|
|
4
|
+
* module keeps the timers; the override reads and changes this state. Requests, notifications and responses have
|
|
5
|
+
* expo-notifications' shapes.
|
|
6
|
+
*/
|
|
7
|
+
export type Json = Record<string, unknown>
|
|
8
|
+
export type NotificationRequest = { identifier: string; content: Json; trigger: Json | null }
|
|
9
|
+
export type Scheduled = { request: NotificationRequest; next: number }
|
|
10
|
+
export type Delivered = { date: number; request: NotificationRequest }
|
|
11
|
+
|
|
12
|
+
export type NotificationsState = {
|
|
13
|
+
scheduled: Scheduled[]
|
|
14
|
+
delivered: Delivered[]
|
|
15
|
+
badge: number
|
|
16
|
+
/** The response to the banner tapped in this run of the app. */
|
|
17
|
+
lastResponse: Json | null
|
|
18
|
+
categories: Record<string, Json>
|
|
19
|
+
/** Android's notification channels and their groups. */
|
|
20
|
+
channels: Record<string, Json>
|
|
21
|
+
channelGroups: Record<string, Json>
|
|
22
|
+
/** Bumped when the app unregisters, so the device token it gets next is a new one. */
|
|
23
|
+
tokenGeneration: number
|
|
24
|
+
/** How many remote pushes were sent, which numbers their identifiers. */
|
|
25
|
+
pushCount: number
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const INITIAL_NOTIFICATIONS: NotificationsState = {
|
|
29
|
+
scheduled: [],
|
|
30
|
+
delivered: [],
|
|
31
|
+
badge: 0,
|
|
32
|
+
lastResponse: null,
|
|
33
|
+
categories: {},
|
|
34
|
+
channels: {},
|
|
35
|
+
channelGroups: {},
|
|
36
|
+
tokenGeneration: 0,
|
|
37
|
+
pushCount: 0,
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** A remote push, as the panel and `web-sim run notifications push` send it. */
|
|
41
|
+
export type Push = {
|
|
42
|
+
title?: string
|
|
43
|
+
subtitle?: string
|
|
44
|
+
body?: string
|
|
45
|
+
data?: Json
|
|
46
|
+
badge?: number
|
|
47
|
+
/** Android. */
|
|
48
|
+
channelId?: string
|
|
49
|
+
sound?: string | null
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export const DEFAULT_ACTION = 'expo.modules.notifications.actions.DEFAULT'
|
|
53
|
+
|
|
54
|
+
export function clone<T>(value: T): T {
|
|
55
|
+
return value === undefined || value === null ? value : (JSON.parse(JSON.stringify(value)) as T)
|
|
56
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { Platform } from 'web-sim/runtime'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The device's push tokens: made from the app and the platform, so the same app on the same simulated device keeps
|
|
5
|
+
* its token across reloads, as a device keeps it per install. Unregistering bumps `generation` for a new one.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
function hash(text: string, seed: number): number {
|
|
9
|
+
let h = seed
|
|
10
|
+
for (let i = 0; i < text.length; i++) {
|
|
11
|
+
h = Math.imul(h ^ text.charCodeAt(i), 2654435761)
|
|
12
|
+
}
|
|
13
|
+
h = Math.imul(h ^ (h >>> 15), 2246822507)
|
|
14
|
+
return (h ^ (h >>> 13)) >>> 0
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function characters(seed: string, alphabet: string, length: number): string {
|
|
18
|
+
let out = ''
|
|
19
|
+
for (let i = 0; out.length < length; i++) {
|
|
20
|
+
let value = hash(seed, i + 1)
|
|
21
|
+
for (let j = 0; j < 4 && out.length < length; j++) {
|
|
22
|
+
out += alphabet[value % alphabet.length]
|
|
23
|
+
value = Math.floor(value / alphabet.length)
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return out
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const BASE64URL = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'
|
|
30
|
+
const ALPHANUMERIC = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
|
|
31
|
+
|
|
32
|
+
/** APNs: 32 bytes as hex. FCM: an instance id, a colon, then 'APA91b' and the registration. */
|
|
33
|
+
export function devicePushToken(app: string, platform: Platform, generation: number): string {
|
|
34
|
+
const seed = `${app}:${platform}:${generation}`
|
|
35
|
+
return platform === 'ios' ? characters(seed, '0123456789abcdef', 64) : `${characters(seed, BASE64URL, 22)}:APA91b${characters(`${seed}:fcm`, BASE64URL, 134)}`
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** `ExponentPushToken[...]`, the same for the same device token and project. Push sent to it goes nowhere. */
|
|
39
|
+
export function expoPushToken(deviceToken: string, projectId: string | undefined): string {
|
|
40
|
+
return `ExponentPushToken[${characters(`${deviceToken}:${projectId ?? ''}`, ALPHANUMERIC, 22)}]`
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** The EAS project id in the app's config, which `getExpoPushTokenAsync` uses when it is given none. */
|
|
44
|
+
export function configProjectId(config: Record<string, any> | undefined): string | undefined {
|
|
45
|
+
const id = config?.extra?.eas?.projectId
|
|
46
|
+
return typeof id === 'string' ? id : undefined
|
|
47
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { fromWallTime, phoneTimeZone, toWallTime } from 'web-sim/runtime'
|
|
2
|
+
import type { Json } from './state.ts'
|
|
3
|
+
|
|
4
|
+
/** What `scheduleNotificationAsync` puts in a date component the trigger leaves out. */
|
|
5
|
+
const PLACEHOLDER = -9999
|
|
6
|
+
|
|
7
|
+
export { phoneTimeZone }
|
|
8
|
+
|
|
9
|
+
export function repeats(trigger: Json): boolean {
|
|
10
|
+
switch (trigger.type) {
|
|
11
|
+
case 'timeInterval':
|
|
12
|
+
case 'calendar':
|
|
13
|
+
return trigger.repeats === true
|
|
14
|
+
case 'daily':
|
|
15
|
+
case 'weekly':
|
|
16
|
+
case 'monthly':
|
|
17
|
+
case 'yearly':
|
|
18
|
+
return true
|
|
19
|
+
default:
|
|
20
|
+
return false
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function component(trigger: Json, key: string): number | undefined {
|
|
25
|
+
const value = trigger[key]
|
|
26
|
+
return typeof value === 'number' && value !== PLACEHOLDER ? value : undefined
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* When a parsed trigger next fires after `after`, or null when it never will. Calendar-like triggers are matched
|
|
31
|
+
* minute by minute over the next year and a bit, in the phone's time zone.
|
|
32
|
+
*/
|
|
33
|
+
export function nextDate(trigger: Json, after: number, timeZone: string = phoneTimeZone()): number | null {
|
|
34
|
+
const match = (want: Components, second?: number) => matchMinute(after, want, timeZone, second)
|
|
35
|
+
switch (trigger.type) {
|
|
36
|
+
case 'timeInterval':
|
|
37
|
+
return after + (trigger.seconds as number) * 1000
|
|
38
|
+
case 'date': {
|
|
39
|
+
const at = trigger.timestamp as number
|
|
40
|
+
return at > after ? at : null
|
|
41
|
+
}
|
|
42
|
+
case 'daily':
|
|
43
|
+
return match({ hour: component(trigger, 'hour'), minute: component(trigger, 'minute') })
|
|
44
|
+
case 'weekly':
|
|
45
|
+
return match({
|
|
46
|
+
weekday: component(trigger, 'weekday'),
|
|
47
|
+
hour: component(trigger, 'hour'),
|
|
48
|
+
minute: component(trigger, 'minute'),
|
|
49
|
+
})
|
|
50
|
+
case 'monthly':
|
|
51
|
+
return match({ day: component(trigger, 'day'), hour: component(trigger, 'hour'), minute: component(trigger, 'minute') })
|
|
52
|
+
case 'yearly': {
|
|
53
|
+
// 0-based, as JavaScript's Date counts months.
|
|
54
|
+
const month = component(trigger, 'month')
|
|
55
|
+
return match({
|
|
56
|
+
month: month === undefined ? undefined : month + 1,
|
|
57
|
+
day: component(trigger, 'day'),
|
|
58
|
+
hour: component(trigger, 'hour'),
|
|
59
|
+
minute: component(trigger, 'minute'),
|
|
60
|
+
})
|
|
61
|
+
}
|
|
62
|
+
case 'calendar':
|
|
63
|
+
return match(
|
|
64
|
+
{
|
|
65
|
+
year: component(trigger, 'year'),
|
|
66
|
+
month: component(trigger, 'month'),
|
|
67
|
+
day: component(trigger, 'day'),
|
|
68
|
+
weekday: component(trigger, 'weekday'),
|
|
69
|
+
hour: component(trigger, 'hour'),
|
|
70
|
+
minute: component(trigger, 'minute'),
|
|
71
|
+
},
|
|
72
|
+
component(trigger, 'second') ?? 0,
|
|
73
|
+
)
|
|
74
|
+
default:
|
|
75
|
+
return null
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Month 1-12, weekday 1-7 from Sunday, as iOS's date components count them. */
|
|
80
|
+
type Components = { year?: number; month?: number; day?: number; weekday?: number; hour?: number; minute?: number }
|
|
81
|
+
|
|
82
|
+
function matchMinute(after: number, want: Components, timeZone: string, second = 0): number | null {
|
|
83
|
+
// Walked in the zone's wall-clock time, read with the UTC methods.
|
|
84
|
+
const start = toWallTime(timeZone, after)
|
|
85
|
+
const date = new Date(start)
|
|
86
|
+
date.setUTCSeconds(second, 0)
|
|
87
|
+
if (date.getTime() <= start) {
|
|
88
|
+
date.setUTCMinutes(date.getUTCMinutes() + 1)
|
|
89
|
+
}
|
|
90
|
+
for (let step = 0; step < 60 * 24 * 400; step++) {
|
|
91
|
+
if (
|
|
92
|
+
(want.year === undefined || date.getUTCFullYear() === want.year) &&
|
|
93
|
+
(want.month === undefined || date.getUTCMonth() + 1 === want.month) &&
|
|
94
|
+
(want.day === undefined || date.getUTCDate() === want.day) &&
|
|
95
|
+
(want.weekday === undefined || date.getUTCDay() + 1 === want.weekday) &&
|
|
96
|
+
(want.hour === undefined || date.getUTCHours() === want.hour) &&
|
|
97
|
+
(want.minute === undefined || date.getUTCMinutes() === want.minute)
|
|
98
|
+
) {
|
|
99
|
+
const at = fromWallTime(timeZone, date.getTime())
|
|
100
|
+
if (at > after) {
|
|
101
|
+
return at
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
date.setUTCMinutes(date.getUTCMinutes() + 1)
|
|
105
|
+
}
|
|
106
|
+
return null
|
|
107
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { ShieldCheckIcon } from '@phosphor-icons/react'
|
|
2
|
+
import { app } from 'web-sim/runtime'
|
|
3
|
+
import { defineShellPlugin, ListItem, List, Select } from 'web-sim/shell'
|
|
4
|
+
import { INITIAL_PERMISSIONS, PERMISSIONS, type PermissionsState } from './state.ts'
|
|
5
|
+
|
|
6
|
+
const STATUSES = [
|
|
7
|
+
{ value: 'undetermined', label: 'Not asked' },
|
|
8
|
+
{ value: 'granted', label: 'Allowed' },
|
|
9
|
+
{ value: 'denied', label: 'Denied' },
|
|
10
|
+
] as const
|
|
11
|
+
|
|
12
|
+
export default defineShellPlugin<PermissionsState>({
|
|
13
|
+
id: 'permissions',
|
|
14
|
+
title: 'Permissions',
|
|
15
|
+
icon: ShieldCheckIcon,
|
|
16
|
+
initialState: INITIAL_PERMISSIONS,
|
|
17
|
+
// A permission changing is worth seeing in the plain `web-sim logs`.
|
|
18
|
+
logState: 'info',
|
|
19
|
+
actions: {
|
|
20
|
+
/** Every permission back to not asked, as the toolbar's reset does. */
|
|
21
|
+
reset: (context) => context.set(INITIAL_PERMISSIONS),
|
|
22
|
+
grantAll: (context) => context.set(Object.fromEntries(Object.keys(INITIAL_PERMISSIONS).map((kind) => [kind, 'granted'])) as PermissionsState),
|
|
23
|
+
},
|
|
24
|
+
Panel: ({ state, set, sim }) => {
|
|
25
|
+
const dependencies = app().dependencies ?? []
|
|
26
|
+
const shown = PERMISSIONS.filter(
|
|
27
|
+
(permission) =>
|
|
28
|
+
(permission.platform === undefined || permission.platform === sim.device.platform) &&
|
|
29
|
+
(dependencies.length === 0 || permission.packages.some((name) => dependencies.includes(name))),
|
|
30
|
+
)
|
|
31
|
+
return (
|
|
32
|
+
<List empty="No permissions">
|
|
33
|
+
{shown.map((permission) => (
|
|
34
|
+
<ListItem
|
|
35
|
+
key={permission.kind}
|
|
36
|
+
title={permission.label}
|
|
37
|
+
actions={
|
|
38
|
+
<Select
|
|
39
|
+
label={permission.label}
|
|
40
|
+
value={state[permission.kind]}
|
|
41
|
+
options={[...STATUSES]}
|
|
42
|
+
onChange={(status) => set({ [permission.kind]: status } as Partial<PermissionsState>)}
|
|
43
|
+
/>
|
|
44
|
+
}
|
|
45
|
+
/>
|
|
46
|
+
))}
|
|
47
|
+
</List>
|
|
48
|
+
)
|
|
49
|
+
},
|
|
50
|
+
})
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { PermissionKind, PermissionStatus } from '../../src/runtime/index.ts'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* What the user has answered for each permission. The shell's `getPermission` / `setPermission` read and write it, so
|
|
5
|
+
* every module's permission calls, the OS prompts, the panel and the CLI agree.
|
|
6
|
+
*/
|
|
7
|
+
export type PermissionsState = Record<PermissionKind, PermissionStatus>
|
|
8
|
+
|
|
9
|
+
export const INITIAL_PERMISSIONS: PermissionsState = {
|
|
10
|
+
camera: 'undetermined',
|
|
11
|
+
microphone: 'undetermined',
|
|
12
|
+
photos: 'undetermined',
|
|
13
|
+
location: 'undetermined',
|
|
14
|
+
'location-background': 'undetermined',
|
|
15
|
+
contacts: 'undetermined',
|
|
16
|
+
calendar: 'undetermined',
|
|
17
|
+
reminders: 'undetermined',
|
|
18
|
+
notifications: 'undetermined',
|
|
19
|
+
tracking: 'undetermined',
|
|
20
|
+
motion: 'undetermined',
|
|
21
|
+
'face-id': 'undetermined',
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Each permission, its label, the packages that ask for it, and the platform it exists on (both when absent). */
|
|
25
|
+
export const PERMISSIONS: { kind: PermissionKind; label: string; packages: string[]; platform?: 'ios' | 'android' }[] = [
|
|
26
|
+
{ kind: 'camera', label: 'Camera', packages: ['expo-camera', 'expo-image-picker'] },
|
|
27
|
+
{ kind: 'microphone', label: 'Microphone', packages: ['expo-camera', 'expo-audio', 'expo-av'] },
|
|
28
|
+
{ kind: 'photos', label: 'Photos', packages: ['expo-image-picker', 'expo-media-library'] },
|
|
29
|
+
{ kind: 'location', label: 'Location', packages: ['expo-location'] },
|
|
30
|
+
{ kind: 'location-background', label: 'Location (always)', packages: ['expo-location'] },
|
|
31
|
+
{ kind: 'contacts', label: 'Contacts', packages: ['expo-contacts'] },
|
|
32
|
+
{ kind: 'calendar', label: 'Calendar', packages: ['expo-calendar'] },
|
|
33
|
+
{ kind: 'reminders', label: 'Reminders', packages: ['expo-calendar'], platform: 'ios' },
|
|
34
|
+
{ kind: 'notifications', label: 'Notifications', packages: ['expo-notifications'] },
|
|
35
|
+
{ kind: 'tracking', label: 'Tracking', packages: ['expo-tracking-transparency'], platform: 'ios' },
|
|
36
|
+
{ kind: 'motion', label: 'Motion & Fitness', packages: ['expo-sensors', 'expo-location'], platform: 'ios' },
|
|
37
|
+
{ kind: 'face-id', label: 'Face ID', packages: ['expo-local-authentication'], platform: 'ios' },
|
|
38
|
+
]
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { appendLog, type CapturedPhoto, platform, plugin } from 'web-sim/runtime'
|
|
2
|
+
import {
|
|
3
|
+
addPhoto,
|
|
4
|
+
albumNumber,
|
|
5
|
+
createAlbum,
|
|
6
|
+
deleteAlbums,
|
|
7
|
+
deletePhotos,
|
|
8
|
+
editAlbum,
|
|
9
|
+
type LibraryAlbum,
|
|
10
|
+
libraryAlbums as albumsOf,
|
|
11
|
+
type LibraryPhoto,
|
|
12
|
+
libraryPhotos as photosOf,
|
|
13
|
+
photoDataUrl as dataUrlOf,
|
|
14
|
+
photoNumbers,
|
|
15
|
+
setFavorite,
|
|
16
|
+
} from './library.ts'
|
|
17
|
+
import { INITIAL_PHOTOS, type PhotosState } from './state.ts'
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The library as the app's modules use it: by the platform's asset and album ids, over the photos plugin's state.
|
|
21
|
+
* What the app changes is logged for the panel.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
export type { LibraryAlbum, LibraryPhoto }
|
|
25
|
+
|
|
26
|
+
const photos = () => plugin<PhotosState>('photos', INITIAL_PHOTOS)
|
|
27
|
+
|
|
28
|
+
function log(text: string) {
|
|
29
|
+
photos().set({ log: appendLog(photos().get().log, text) })
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function libraryPhotos(): LibraryPhoto[] {
|
|
33
|
+
return photosOf(photos().get(), platform())
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function libraryPhoto(id: string): LibraryPhoto | null {
|
|
37
|
+
return libraryPhotos().find((photo) => photo.id === id) ?? null
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function libraryAlbums(): LibraryAlbum[] {
|
|
41
|
+
return albumsOf(photos().get(), platform())
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export const photoDataUrl: (photo: LibraryPhoto, options?: { maxSize?: number; quality?: number }) => Promise<CapturedPhoto> = dataUrlOf
|
|
45
|
+
|
|
46
|
+
/** Saves an image to the library, as the app does, into one of the user's albums if `albumId` names one. */
|
|
47
|
+
export async function saveToLibrary(uri: string, albumId?: string): Promise<LibraryPhoto> {
|
|
48
|
+
const album = albumId === undefined ? undefined : albumNumber(photos().get(), platform(), albumId)
|
|
49
|
+
const saved = await addPhoto(photos(), uri, { by: 'app', album })
|
|
50
|
+
const photo = libraryPhotos().find((candidate) => candidate.n === saved.n)!
|
|
51
|
+
log(`Saved ${photo.filename}`)
|
|
52
|
+
return photo
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function deleteFromLibrary(ids: string[]): void {
|
|
56
|
+
const ns = photoNumbers(photos().get(), platform(), ids)
|
|
57
|
+
deletePhotos(photos(), ns)
|
|
58
|
+
log(ns.length === 1 ? 'Deleted a photo' : `Deleted ${ns.length} photos`)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function setLibraryFavorite(id: string, favorite: boolean): void {
|
|
62
|
+
const [n] = photoNumbers(photos().get(), platform(), [id])
|
|
63
|
+
if (n !== undefined) {
|
|
64
|
+
setFavorite(photos(), n, favorite)
|
|
65
|
+
log(`${favorite ? 'Favourited' : 'Unfavourited'} ${libraryPhoto(id)?.filename ?? id}`)
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function createLibraryAlbum(title: string, assetIds: string[]): LibraryAlbum {
|
|
70
|
+
const album = createAlbum(photos(), title, photoNumbers(photos().get(), platform(), assetIds))
|
|
71
|
+
log(`Created album ${title}`)
|
|
72
|
+
return libraryAlbums().find((candidate) => candidate.n === album.n)!
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function deleteLibraryAlbums(ids: string[]): void {
|
|
76
|
+
const state = photos().get()
|
|
77
|
+
const ns = ids.map((id) => albumNumber(state, platform(), id)).filter((n) => n !== undefined)
|
|
78
|
+
const titles = state.albums.filter((album) => ns.includes(album.n)).map((album) => album.title)
|
|
79
|
+
deleteAlbums(photos(), ns)
|
|
80
|
+
if (titles.length > 0) {
|
|
81
|
+
log(`Deleted album ${titles.join(', ')}`)
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Adds photos to, or removes them from, one of the user's albums. The smart albums and folders are made, not kept. */
|
|
86
|
+
export function editLibraryAlbum(id: string, change: { add?: string[]; remove?: string[] }): void {
|
|
87
|
+
const state = photos().get()
|
|
88
|
+
const n = albumNumber(state, platform(), id)
|
|
89
|
+
if (n === undefined) {
|
|
90
|
+
return
|
|
91
|
+
}
|
|
92
|
+
editAlbum(photos(), n, { add: photoNumbers(state, platform(), change.add ?? []), remove: photoNumbers(state, platform(), change.remove ?? []) })
|
|
93
|
+
log(`Changed album ${state.albums.find((album) => album.n === n)?.title}`)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export type LibraryChange = { hasIncrementalChanges: boolean; insertedAssets?: string[]; deletedAssets?: string[]; updatedAssets?: string[] }
|
|
97
|
+
|
|
98
|
+
/** The library's changes, from the app or the panel, as the OS reports them. */
|
|
99
|
+
export function onLibraryChange(listener: (change: LibraryChange) => void): () => void {
|
|
100
|
+
return photos().watch((next, previous) => {
|
|
101
|
+
if (next.photos === previous.photos) {
|
|
102
|
+
return
|
|
103
|
+
}
|
|
104
|
+
const os = platform()
|
|
105
|
+
const before = new Map(photosOf(previous, os).map((photo) => [photo.id, photo]))
|
|
106
|
+
const after = new Map(photosOf(next, os).map((photo) => [photo.id, photo]))
|
|
107
|
+
const insertedAssets = [...after.keys()].filter((id) => !before.has(id))
|
|
108
|
+
const deletedAssets = [...before.keys()].filter((id) => !after.has(id))
|
|
109
|
+
const updatedAssets = [...after.values()].filter((photo) => before.has(photo.id) && before.get(photo.id)!.favorite !== photo.favorite).map((photo) => photo.id)
|
|
110
|
+
if (insertedAssets.length + deletedAssets.length + updatedAssets.length > 0) {
|
|
111
|
+
listener({ hasIncrementalChanges: true, insertedAssets, deletedAssets, updatedAssets })
|
|
112
|
+
}
|
|
113
|
+
})
|
|
114
|
+
}
|