@codual/react-native-ui 0.0.0-stage → 0.1.0
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/LICENSE +202 -0
- package/NOTICE +2 -0
- package/README.md +18 -2
- package/dist/announce.d.ts +2 -0
- package/dist/announce.js +8 -0
- package/dist/charts/data.d.ts +32 -0
- package/dist/charts/data.js +13 -0
- package/dist/charts/labels.d.ts +54 -0
- package/dist/charts/labels.js +91 -0
- package/dist/charts/machine.d.ts +62 -0
- package/dist/charts/machine.js +144 -0
- package/dist/charts/points.d.ts +23 -0
- package/dist/charts/points.js +46 -0
- package/dist/charts/summary.d.ts +29 -0
- package/dist/charts/summary.js +45 -0
- package/dist/charts/use-chart-interaction.d.ts +62 -0
- package/dist/charts/use-chart-interaction.js +79 -0
- package/dist/forms/async-checks.d.ts +34 -0
- package/dist/forms/async-checks.js +77 -0
- package/dist/forms/field-errors.d.ts +8 -0
- package/dist/forms/field-errors.js +16 -0
- package/dist/forms/form-types.d.ts +43 -0
- package/dist/forms/form-types.js +2 -0
- package/dist/forms/machine.d.ts +63 -0
- package/dist/forms/machine.js +251 -0
- package/dist/forms/numbers.d.ts +5 -0
- package/dist/forms/numbers.js +83 -0
- package/dist/forms/rules.d.ts +47 -0
- package/dist/forms/rules.js +72 -0
- package/dist/forms/server-errors.d.ts +17 -0
- package/dist/forms/server-errors.js +22 -0
- package/dist/forms/use-form.d.ts +49 -0
- package/dist/forms/use-form.js +61 -0
- package/dist/index.d.ts +50 -0
- package/dist/index.js +94 -0
- package/dist/lists/PagedList.d.ts +47 -0
- package/dist/lists/PagedList.js +81 -0
- package/dist/lists/controller.d.ts +23 -0
- package/dist/lists/controller.js +20 -0
- package/dist/lists/inverted-list.d.ts +23 -0
- package/dist/lists/inverted-list.js +60 -0
- package/dist/lists/machine.d.ts +81 -0
- package/dist/lists/machine.js +252 -0
- package/dist/lists/newest-end.d.ts +30 -0
- package/dist/lists/newest-end.js +58 -0
- package/dist/lists/paged-content.d.ts +46 -0
- package/dist/lists/paged-content.js +65 -0
- package/dist/lists/paged-list-types.d.ts +56 -0
- package/dist/lists/paged-list-types.js +2 -0
- package/dist/lists/system-footer.d.ts +28 -0
- package/dist/lists/system-footer.js +50 -0
- package/dist/lists/use-paged-list.d.ts +25 -0
- package/dist/lists/use-paged-list.js +34 -0
- package/dist/permissions/access.d.ts +30 -0
- package/dist/permissions/access.js +2 -0
- package/dist/permissions/android-permission.d.ts +52 -0
- package/dist/permissions/android-permission.js +143 -0
- package/dist/permissions/permission.d.ts +69 -0
- package/dist/permissions/permission.js +201 -0
- package/dist/permissions/setup.d.ts +25 -0
- package/dist/permissions/setup.js +41 -0
- package/dist/permissions/status.d.ts +29 -0
- package/dist/permissions/status.js +18 -0
- package/dist/permissions/use-permission.d.ts +23 -0
- package/dist/permissions/use-permission.js +29 -0
- package/dist/purchases/Paywall.d.ts +27 -0
- package/dist/purchases/Paywall.js +28 -0
- package/dist/purchases/paywall-content.d.ts +20 -0
- package/dist/purchases/paywall-content.js +34 -0
- package/dist/purchases/purchases.d.ts +92 -0
- package/dist/purchases/purchases.js +236 -0
- package/dist/purchases/status.d.ts +62 -0
- package/dist/purchases/status.js +25 -0
- package/dist/purchases/store.d.ts +71 -0
- package/dist/purchases/store.js +2 -0
- package/dist/purchases/system-views.d.ts +49 -0
- package/dist/purchases/system-views.js +91 -0
- package/dist/purchases/use-purchases.d.ts +3 -0
- package/dist/purchases/use-purchases.js +8 -0
- package/dist/react-native-iap/iap-purchases.d.ts +24 -0
- package/dist/react-native-iap/iap-purchases.js +90 -0
- package/dist/react-native-iap/iap-store.d.ts +56 -0
- package/dist/react-native-iap/iap-store.js +249 -0
- package/dist/react-native-iap/iap-types.d.ts +12 -0
- package/dist/react-native-iap/iap-types.js +11 -0
- package/dist/react-native-iap/index.d.ts +31 -0
- package/dist/react-native-iap/index.js +24 -0
- package/dist/react-native-permissions/index.d.ts +24 -0
- package/dist/react-native-permissions/index.js +88 -0
- package/dist/screen-states/ScreenStates.d.ts +38 -0
- package/dist/screen-states/ScreenStates.js +69 -0
- package/dist/screen-states/errors.d.ts +9 -0
- package/dist/screen-states/errors.js +24 -0
- package/dist/screen-states/machine.d.ts +97 -0
- package/dist/screen-states/machine.js +172 -0
- package/dist/screen-states/system-views.d.ts +40 -0
- package/dist/screen-states/system-views.js +66 -0
- package/dist/screen-states/use-screen-states.d.ts +23 -0
- package/dist/screen-states/use-screen-states.js +41 -0
- package/dist/search/SearchResults.d.ts +22 -0
- package/dist/search/SearchResults.js +25 -0
- package/dist/search/announcements.d.ts +17 -0
- package/dist/search/announcements.js +31 -0
- package/dist/search/labels.d.ts +15 -0
- package/dist/search/labels.js +14 -0
- package/dist/search/machine.d.ts +65 -0
- package/dist/search/machine.js +241 -0
- package/dist/search/recent-search-sync.d.ts +32 -0
- package/dist/search/recent-search-sync.js +87 -0
- package/dist/search/recent-searches.d.ts +22 -0
- package/dist/search/recent-searches.js +40 -0
- package/dist/search/system-views.d.ts +11 -0
- package/dist/search/system-views.js +16 -0
- package/dist/search/types.d.ts +51 -0
- package/dist/search/types.js +2 -0
- package/dist/search/use-search.d.ts +73 -0
- package/dist/search/use-search.js +123 -0
- package/dist/selection/config.d.ts +34 -0
- package/dist/selection/config.js +13 -0
- package/dist/selection/labels.d.ts +19 -0
- package/dist/selection/labels.js +23 -0
- package/dist/selection/machine.d.ts +80 -0
- package/dist/selection/machine.js +274 -0
- package/dist/selection/snapshot.d.ts +54 -0
- package/dist/selection/snapshot.js +45 -0
- package/dist/selection/state.d.ts +25 -0
- package/dist/selection/state.js +24 -0
- package/dist/selection/system-views.d.ts +11 -0
- package/dist/selection/system-views.js +22 -0
- package/dist/selection/use-selection.d.ts +73 -0
- package/dist/selection/use-selection.js +100 -0
- package/dist/sheets/machine.d.ts +101 -0
- package/dist/sheets/machine.js +213 -0
- package/dist/sheets/use-sheet.d.ts +78 -0
- package/dist/sheets/use-sheet.js +103 -0
- package/dist/victory-native/index.d.ts +37 -0
- package/dist/victory-native/index.js +41 -0
- package/package.json +94 -4
- package/react-native-iap/package.json +4 -0
- package/react-native-permissions/package.json +4 -0
- package/victory-native/package.json +4 -0
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { PermissionStatus } from "./status";
|
|
2
|
+
/**
|
|
3
|
+
* One system permission behind `usePermission`: `androidPermission("camera")`, `createReactNativePermissionsAccess`
|
|
4
|
+
* from `@codual/react-native-ui/react-native-permissions`, the app's own, or a fake in tests. A rejection is
|
|
5
|
+
* reported as unavailable.
|
|
6
|
+
*/
|
|
7
|
+
export interface PermissionAccess {
|
|
8
|
+
/** Names what the permission remembers, such as a declined explanation. */
|
|
9
|
+
readonly id: string;
|
|
10
|
+
/** Whether the system asks again once the status is limited or provisional. False unless the adapter says. */
|
|
11
|
+
readonly canAskForFullAccess?: boolean;
|
|
12
|
+
/** Reads the status without prompting. */
|
|
13
|
+
readonly status: () => Promise<PermissionStatus>;
|
|
14
|
+
/**
|
|
15
|
+
* Shows the system prompt and answers with the status after it. Called only once the user accepts the explanation,
|
|
16
|
+
* and only while the system can still ask.
|
|
17
|
+
*/
|
|
18
|
+
readonly request: () => Promise<PermissionStatus>;
|
|
19
|
+
/** Opens the page in Settings where the user turns it on; React Native's `Linking.openSettings()` if left out. */
|
|
20
|
+
readonly openSettings?: () => Promise<void>;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Flags kept across launches: a declined explanation, and on Android each earlier "no". `permissionMemory(storage)`
|
|
24
|
+
* makes one from MMKV or another synchronous store; a `Set` keeps them for one run, as tests want.
|
|
25
|
+
*/
|
|
26
|
+
export interface PermissionMemory {
|
|
27
|
+
readonly has: (key: string) => boolean;
|
|
28
|
+
readonly add: (key: string) => void;
|
|
29
|
+
readonly delete: (key: string) => void;
|
|
30
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { type Permission as AndroidManifestPermission } from "react-native";
|
|
2
|
+
import type { PermissionAccess, PermissionMemory } from "./access";
|
|
3
|
+
declare const BUILT_IN: {
|
|
4
|
+
readonly camera: {
|
|
5
|
+
readonly id: "camera";
|
|
6
|
+
readonly full: () => "android.permission.CAMERA"[];
|
|
7
|
+
readonly partial: () => never[];
|
|
8
|
+
};
|
|
9
|
+
readonly microphone: {
|
|
10
|
+
readonly id: "microphone";
|
|
11
|
+
readonly full: () => "android.permission.RECORD_AUDIO"[];
|
|
12
|
+
readonly partial: () => never[];
|
|
13
|
+
};
|
|
14
|
+
readonly contacts: {
|
|
15
|
+
readonly id: "contacts";
|
|
16
|
+
readonly full: () => "android.permission.READ_CONTACTS"[];
|
|
17
|
+
readonly partial: () => never[];
|
|
18
|
+
};
|
|
19
|
+
readonly "location-when-in-use": {
|
|
20
|
+
readonly id: "location.whenInUse";
|
|
21
|
+
readonly full: () => "android.permission.ACCESS_FINE_LOCATION"[];
|
|
22
|
+
readonly partial: () => "android.permission.ACCESS_COARSE_LOCATION"[];
|
|
23
|
+
};
|
|
24
|
+
readonly photos: {
|
|
25
|
+
readonly id: "photos";
|
|
26
|
+
readonly full: (sdk: number) => ("android.permission.READ_EXTERNAL_STORAGE" | "android.permission.READ_MEDIA_IMAGES")[];
|
|
27
|
+
readonly partial: (sdk: number) => "android.permission.READ_MEDIA_VISUAL_USER_SELECTED"[];
|
|
28
|
+
};
|
|
29
|
+
readonly notifications: {
|
|
30
|
+
readonly id: "notifications";
|
|
31
|
+
readonly full: (sdk: number) => "android.permission.POST_NOTIFICATIONS"[];
|
|
32
|
+
readonly partial: () => never[];
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
export type AndroidPermissionName = keyof typeof BUILT_IN;
|
|
36
|
+
/** Another permission, granted when every one of `permissions` is: `{ id: "bluetooth", permissions: [...] }`. */
|
|
37
|
+
export interface CustomAndroidPermission {
|
|
38
|
+
readonly id: string;
|
|
39
|
+
readonly permissions: readonly AndroidManifestPermission[];
|
|
40
|
+
}
|
|
41
|
+
export interface AndroidPermissionOptions {
|
|
42
|
+
/** Where each "no" is remembered; `configurePermissions`'s by default. */
|
|
43
|
+
readonly memory?: PermissionMemory;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* A permission on Android, through React Native's own `PermissionsAndroid`. Its "never ask again" answer is the same
|
|
47
|
+
* for a closed prompt and for a system that won't ask any more, and its status is only granted or not, so each "no"
|
|
48
|
+
* is remembered: the first is denied, the next "never ask again" blocked. `PermissionsAndroid` can't read the
|
|
49
|
+
* manifest: a permission missing from it reads as blocked.
|
|
50
|
+
*/
|
|
51
|
+
export declare function androidPermission(permission: AndroidPermissionName | CustomAndroidPermission, options?: AndroidPermissionOptions): PermissionAccess;
|
|
52
|
+
export {};
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.androidPermission = androidPermission;
|
|
4
|
+
const react_native_1 = require("react-native");
|
|
5
|
+
const setup_1 = require("./setup");
|
|
6
|
+
const status_1 = require("./status");
|
|
7
|
+
const ANDROID_13 = 33;
|
|
8
|
+
const ANDROID_14 = 34;
|
|
9
|
+
const NAMES = react_native_1.PermissionsAndroid.PERMISSIONS;
|
|
10
|
+
const none = () => [];
|
|
11
|
+
const BUILT_IN = {
|
|
12
|
+
camera: { id: "camera", full: () => [NAMES.CAMERA], partial: none },
|
|
13
|
+
microphone: { id: "microphone", full: () => [NAMES.RECORD_AUDIO], partial: none },
|
|
14
|
+
contacts: { id: "contacts", full: () => [NAMES.READ_CONTACTS], partial: none },
|
|
15
|
+
"location-when-in-use": {
|
|
16
|
+
id: "location.whenInUse",
|
|
17
|
+
full: () => [NAMES.ACCESS_FINE_LOCATION],
|
|
18
|
+
partial: () => [NAMES.ACCESS_COARSE_LOCATION],
|
|
19
|
+
},
|
|
20
|
+
photos: {
|
|
21
|
+
id: "photos",
|
|
22
|
+
full: (sdk) => [sdk >= ANDROID_13 ? NAMES.READ_MEDIA_IMAGES : NAMES.READ_EXTERNAL_STORAGE],
|
|
23
|
+
partial: (sdk) => (sdk >= ANDROID_14 ? [NAMES.READ_MEDIA_VISUAL_USER_SELECTED] : []),
|
|
24
|
+
},
|
|
25
|
+
// Before Android 13 there is no prompt; PermissionsAndroid can't tell notifications turned off in Settings.
|
|
26
|
+
notifications: {
|
|
27
|
+
id: "notifications",
|
|
28
|
+
full: (sdk) => (sdk >= ANDROID_13 ? [NAMES.POST_NOTIFICATIONS] : []),
|
|
29
|
+
partial: none,
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* A permission on Android, through React Native's own `PermissionsAndroid`. Its "never ask again" answer is the same
|
|
34
|
+
* for a closed prompt and for a system that won't ask any more, and its status is only granted or not, so each "no"
|
|
35
|
+
* is remembered: the first is denied, the next "never ask again" blocked. `PermissionsAndroid` can't read the
|
|
36
|
+
* manifest: a permission missing from it reads as blocked.
|
|
37
|
+
*/
|
|
38
|
+
function androidPermission(permission, options = {}) {
|
|
39
|
+
const description = typeof permission === "string"
|
|
40
|
+
? BUILT_IN[permission]
|
|
41
|
+
: { id: permission.id, full: () => permission.permissions, partial: none };
|
|
42
|
+
return new AndroidPermissionAccess(description, options.memory);
|
|
43
|
+
}
|
|
44
|
+
function declines(id) {
|
|
45
|
+
return {
|
|
46
|
+
denied: `ai.codual.ui.permissions.${id}.denied`,
|
|
47
|
+
blocked: `ai.codual.ui.permissions.${id}.blocked`,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
class AndroidPermissionAccess {
|
|
51
|
+
description;
|
|
52
|
+
ownMemory;
|
|
53
|
+
id;
|
|
54
|
+
whole;
|
|
55
|
+
upgrade;
|
|
56
|
+
constructor(description, ownMemory) {
|
|
57
|
+
this.description = description;
|
|
58
|
+
this.ownMemory = ownMemory;
|
|
59
|
+
this.id = description.id;
|
|
60
|
+
this.whole = declines(description.id);
|
|
61
|
+
this.upgrade = declines(`${description.id}.full`);
|
|
62
|
+
}
|
|
63
|
+
/** Android asks again for the rest of a partial grant, until the user turns that down twice. */
|
|
64
|
+
get canAskForFullAccess() {
|
|
65
|
+
return this.description.partial(sdk()).length > 0 && !this.memory.has(this.upgrade.blocked);
|
|
66
|
+
}
|
|
67
|
+
status = async () => {
|
|
68
|
+
const granted = await this.grant((name) => react_native_1.PermissionsAndroid.check(name));
|
|
69
|
+
if (granted)
|
|
70
|
+
return granted;
|
|
71
|
+
if (this.memory.has(this.whole.blocked))
|
|
72
|
+
return status_1.BLOCKED;
|
|
73
|
+
return this.memory.has(this.whole.denied) ? status_1.DENIED : status_1.NOT_DETERMINED;
|
|
74
|
+
};
|
|
75
|
+
request = async () => {
|
|
76
|
+
const isUpgrade = (await this.grant((name) => react_native_1.PermissionsAndroid.check(name))) === status_1.LIMITED;
|
|
77
|
+
const requested = this.requested();
|
|
78
|
+
const asked = isUpgrade ? this.description.full(sdk()) : requested;
|
|
79
|
+
const answers = await react_native_1.PermissionsAndroid.requestMultiple([...requested]);
|
|
80
|
+
// No answers: the prompt was canceled, which is no "no".
|
|
81
|
+
if (requested.every((name) => answers[name] === undefined))
|
|
82
|
+
return this.status();
|
|
83
|
+
const granted = await this.grant(async (name) => answers[name] === react_native_1.PermissionsAndroid.RESULTS.GRANTED);
|
|
84
|
+
if (granted && (granted !== status_1.LIMITED || !isUpgrade))
|
|
85
|
+
return granted;
|
|
86
|
+
const declined = isUpgrade ? this.upgrade : this.whole;
|
|
87
|
+
const willAskAgain = asked.some((name) => answers[name] === react_native_1.PermissionsAndroid.RESULTS.DENIED);
|
|
88
|
+
// "Never ask again" after an earlier "no": the system won't ask any more.
|
|
89
|
+
if (!willAskAgain && this.memory.has(declined.denied)) {
|
|
90
|
+
this.memory.add(declined.blocked);
|
|
91
|
+
return isUpgrade ? status_1.LIMITED : status_1.BLOCKED;
|
|
92
|
+
}
|
|
93
|
+
// A "no" it will ask again after, or the first "never ask again", which a closed prompt answers too.
|
|
94
|
+
this.memory.add(declined.denied);
|
|
95
|
+
return isUpgrade ? status_1.LIMITED : status_1.DENIED;
|
|
96
|
+
};
|
|
97
|
+
openSettings = async () => {
|
|
98
|
+
const applicationId = (0, setup_1.androidApplicationId)();
|
|
99
|
+
if (this.description.id !== "notifications" || applicationId === undefined)
|
|
100
|
+
return react_native_1.Linking.openSettings();
|
|
101
|
+
await react_native_1.Linking.sendIntent("android.settings.APP_NOTIFICATION_SETTINGS", [
|
|
102
|
+
{ key: "android.provider.extra.APP_PACKAGE", value: applicationId },
|
|
103
|
+
]);
|
|
104
|
+
};
|
|
105
|
+
get memory() {
|
|
106
|
+
return this.ownMemory ?? (0, setup_1.configuredMemory)();
|
|
107
|
+
}
|
|
108
|
+
requested() {
|
|
109
|
+
return [...this.description.full(sdk()), ...this.description.partial(sdk())];
|
|
110
|
+
}
|
|
111
|
+
/** Granted or limited, forgetting the "no"s it makes stale; undefined when neither. */
|
|
112
|
+
async grant(isGranted) {
|
|
113
|
+
const status = await this.grantLevel(isGranted);
|
|
114
|
+
if (status === undefined)
|
|
115
|
+
return undefined;
|
|
116
|
+
this.forget(this.whole);
|
|
117
|
+
if (status === status_1.GRANTED)
|
|
118
|
+
this.forget(this.upgrade);
|
|
119
|
+
return status;
|
|
120
|
+
}
|
|
121
|
+
async grantLevel(isGranted) {
|
|
122
|
+
if (await everyOf(this.description.full(sdk()), isGranted))
|
|
123
|
+
return status_1.GRANTED;
|
|
124
|
+
if (await someOf(this.description.partial(sdk()), isGranted))
|
|
125
|
+
return status_1.LIMITED;
|
|
126
|
+
return undefined;
|
|
127
|
+
}
|
|
128
|
+
forget(declined) {
|
|
129
|
+
this.memory.delete(declined.denied);
|
|
130
|
+
this.memory.delete(declined.blocked);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
function sdk() {
|
|
134
|
+
return typeof react_native_1.Platform.Version === "number" ? react_native_1.Platform.Version : 0;
|
|
135
|
+
}
|
|
136
|
+
async function everyOf(items, test) {
|
|
137
|
+
const answers = await Promise.all(items.map(test));
|
|
138
|
+
return answers.every(Boolean);
|
|
139
|
+
}
|
|
140
|
+
async function someOf(items, test) {
|
|
141
|
+
const answers = await Promise.all(items.map(test));
|
|
142
|
+
return answers.some(Boolean);
|
|
143
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { PermissionAccess, PermissionMemory } from "./access";
|
|
2
|
+
import { type PermissionPhase, type PermissionStatus } from "./status";
|
|
3
|
+
export interface PermissionSnapshot {
|
|
4
|
+
/** The system's status, or undefined until the first read answers. */
|
|
5
|
+
readonly status: PermissionStatus | undefined;
|
|
6
|
+
readonly phase: PermissionPhase;
|
|
7
|
+
}
|
|
8
|
+
export interface PermissionOptions {
|
|
9
|
+
/** Where a declined explanation is remembered, so `suggest` never asks twice in a row; `configurePermissions`'s by default. */
|
|
10
|
+
readonly memory?: PermissionMemory;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* One permission, asked for at the right moment: the same as `usePermission`, for code outside components. Creating
|
|
14
|
+
* it reads the status and never prompts; it reads again each time the app becomes active while anything subscribes.
|
|
15
|
+
*/
|
|
16
|
+
export declare function createPermission(access: PermissionAccess, options?: PermissionOptions): Permission;
|
|
17
|
+
export declare class Permission {
|
|
18
|
+
private readonly access;
|
|
19
|
+
private readonly ownMemory;
|
|
20
|
+
private snapshot;
|
|
21
|
+
private readonly listeners;
|
|
22
|
+
private flow;
|
|
23
|
+
private readsStarted;
|
|
24
|
+
/** Reads that started at or before this one answer too late to count: a newer read, or the system prompt, decided. */
|
|
25
|
+
private newestApplied;
|
|
26
|
+
private appState;
|
|
27
|
+
/** The running flow asked for full access, so a limited or provisional status doesn't end it. */
|
|
28
|
+
private isForFullAccess;
|
|
29
|
+
private readonly firstRead;
|
|
30
|
+
constructor(access: PermissionAccess, ownMemory: PermissionMemory | undefined);
|
|
31
|
+
readonly subscribe: (listener: () => void) => (() => void);
|
|
32
|
+
readonly getSnapshot: () => PermissionSnapshot;
|
|
33
|
+
/**
|
|
34
|
+
* For the user's tap on a feature that needs the permission: the explanation, the system prompt once it's accepted,
|
|
35
|
+
* or the Settings guidance once the system won't ask. Resolves with the status when the flow ends; at once when it's
|
|
36
|
+
* already usable, restricted or unavailable. A second ask while a flow runs joins it.
|
|
37
|
+
*/
|
|
38
|
+
readonly ask: () => Promise<PermissionStatus>;
|
|
39
|
+
/**
|
|
40
|
+
* For a moment the app chooses, such as the end of onboarding: the explanation only while the system has never been
|
|
41
|
+
* asked and the user has never declined it. Otherwise it shows nothing and resolves with the status.
|
|
42
|
+
*/
|
|
43
|
+
readonly suggest: () => Promise<PermissionStatus>;
|
|
44
|
+
/**
|
|
45
|
+
* For a feature that needs more than a limited or provisional grant: the explanation where the system asks again,
|
|
46
|
+
* the Settings guidance where it won't. Any other status goes as `ask` does.
|
|
47
|
+
*/
|
|
48
|
+
readonly askForFullAccess: () => Promise<PermissionStatus>;
|
|
49
|
+
/** The explanation's button: shows the system prompt. Does nothing unless the explanation is showing. */
|
|
50
|
+
readonly accept: () => void;
|
|
51
|
+
/** The user closed the explanation or the Settings guidance without going on. A declined explanation is remembered. */
|
|
52
|
+
readonly decline: () => void;
|
|
53
|
+
/** The Settings guidance's button: opens the app's page in Settings. The guidance stays until the app returns. */
|
|
54
|
+
readonly openSettings: () => Promise<void>;
|
|
55
|
+
/** Reads the status again: when it is created, and when the app becomes active while anything subscribes. */
|
|
56
|
+
readonly refresh: () => Promise<void>;
|
|
57
|
+
private get memory();
|
|
58
|
+
private get declinedKey();
|
|
59
|
+
private knownStatus;
|
|
60
|
+
/**
|
|
61
|
+
* Whether the explanation or guidance still has something to do after a fresh read: an explanation while the system
|
|
62
|
+
* can ask, guidance while only Settings can help, either while a full-access flow has only part of it.
|
|
63
|
+
*/
|
|
64
|
+
private stillNeedsTheShownFlow;
|
|
65
|
+
private start;
|
|
66
|
+
private requestAccess;
|
|
67
|
+
private finish;
|
|
68
|
+
private update;
|
|
69
|
+
}
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Permission = void 0;
|
|
4
|
+
exports.createPermission = createPermission;
|
|
5
|
+
const react_native_1 = require("react-native");
|
|
6
|
+
const setup_1 = require("./setup");
|
|
7
|
+
const status_1 = require("./status");
|
|
8
|
+
/** Never used in practice: every read sets a status before a flow can end. */
|
|
9
|
+
const UNREAD = (0, status_1.unavailable)("The status couldn't be read.");
|
|
10
|
+
/**
|
|
11
|
+
* One permission, asked for at the right moment: the same as `usePermission`, for code outside components. Creating
|
|
12
|
+
* it reads the status and never prompts; it reads again each time the app becomes active while anything subscribes.
|
|
13
|
+
*/
|
|
14
|
+
function createPermission(access, options = {}) {
|
|
15
|
+
return new Permission(access, options.memory);
|
|
16
|
+
}
|
|
17
|
+
class Permission {
|
|
18
|
+
access;
|
|
19
|
+
ownMemory;
|
|
20
|
+
snapshot = { status: undefined, phase: "idle" };
|
|
21
|
+
listeners = new Set();
|
|
22
|
+
flow;
|
|
23
|
+
readsStarted = 0;
|
|
24
|
+
/** Reads that started at or before this one answer too late to count: a newer read, or the system prompt, decided. */
|
|
25
|
+
newestApplied = 0;
|
|
26
|
+
appState;
|
|
27
|
+
/** The running flow asked for full access, so a limited or provisional status doesn't end it. */
|
|
28
|
+
isForFullAccess = false;
|
|
29
|
+
firstRead;
|
|
30
|
+
constructor(access, ownMemory) {
|
|
31
|
+
this.access = access;
|
|
32
|
+
this.ownMemory = ownMemory;
|
|
33
|
+
this.firstRead = this.refresh();
|
|
34
|
+
}
|
|
35
|
+
subscribe = (listener) => {
|
|
36
|
+
this.listeners.add(listener);
|
|
37
|
+
if (this.appState === undefined) {
|
|
38
|
+
// A permission shared by screens may have changed while none was showing it.
|
|
39
|
+
void this.refresh();
|
|
40
|
+
this.appState = react_native_1.AppState.addEventListener("change", (state) => {
|
|
41
|
+
if (state === "active")
|
|
42
|
+
void this.refresh();
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
return () => {
|
|
46
|
+
this.listeners.delete(listener);
|
|
47
|
+
if (this.listeners.size > 0)
|
|
48
|
+
return;
|
|
49
|
+
this.appState?.remove();
|
|
50
|
+
this.appState = undefined;
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
getSnapshot = () => this.snapshot;
|
|
54
|
+
/**
|
|
55
|
+
* For the user's tap on a feature that needs the permission: the explanation, the system prompt once it's accepted,
|
|
56
|
+
* or the Settings guidance once the system won't ask. Resolves with the status when the flow ends; at once when it's
|
|
57
|
+
* already usable, restricted or unavailable. A second ask while a flow runs joins it.
|
|
58
|
+
*/
|
|
59
|
+
ask = async () => {
|
|
60
|
+
const current = await this.knownStatus();
|
|
61
|
+
if (this.flow)
|
|
62
|
+
return this.flow.result;
|
|
63
|
+
switch (current.status) {
|
|
64
|
+
case "not-determined":
|
|
65
|
+
case "denied":
|
|
66
|
+
return this.start("explaining");
|
|
67
|
+
case "blocked":
|
|
68
|
+
return this.start("settings-guidance");
|
|
69
|
+
default:
|
|
70
|
+
return current;
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* For a moment the app chooses, such as the end of onboarding: the explanation only while the system has never been
|
|
75
|
+
* asked and the user has never declined it. Otherwise it shows nothing and resolves with the status.
|
|
76
|
+
*/
|
|
77
|
+
suggest = async () => {
|
|
78
|
+
const current = await this.knownStatus();
|
|
79
|
+
if (this.flow)
|
|
80
|
+
return this.flow.result;
|
|
81
|
+
if (current.status !== "not-determined" || this.memory.has(this.declinedKey))
|
|
82
|
+
return current;
|
|
83
|
+
return this.start("explaining");
|
|
84
|
+
};
|
|
85
|
+
/**
|
|
86
|
+
* For a feature that needs more than a limited or provisional grant: the explanation where the system asks again,
|
|
87
|
+
* the Settings guidance where it won't. Any other status goes as `ask` does.
|
|
88
|
+
*/
|
|
89
|
+
askForFullAccess = async () => {
|
|
90
|
+
const current = await this.knownStatus();
|
|
91
|
+
if (this.flow)
|
|
92
|
+
return this.flow.result;
|
|
93
|
+
if (current.status !== "limited" && current.status !== "provisional")
|
|
94
|
+
return this.ask();
|
|
95
|
+
this.isForFullAccess = true;
|
|
96
|
+
return this.start(this.access.canAskForFullAccess ? "explaining" : "settings-guidance");
|
|
97
|
+
};
|
|
98
|
+
/** The explanation's button: shows the system prompt. Does nothing unless the explanation is showing. */
|
|
99
|
+
accept = () => {
|
|
100
|
+
if (this.snapshot.phase !== "explaining")
|
|
101
|
+
return;
|
|
102
|
+
this.update({ phase: "requesting" });
|
|
103
|
+
void this.requestAccess();
|
|
104
|
+
};
|
|
105
|
+
/** The user closed the explanation or the Settings guidance without going on. A declined explanation is remembered. */
|
|
106
|
+
decline = () => {
|
|
107
|
+
switch (this.snapshot.phase) {
|
|
108
|
+
case "explaining":
|
|
109
|
+
this.memory.add(this.declinedKey);
|
|
110
|
+
this.finish();
|
|
111
|
+
return;
|
|
112
|
+
case "settings-guidance":
|
|
113
|
+
this.finish();
|
|
114
|
+
return;
|
|
115
|
+
default:
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
/** The Settings guidance's button: opens the app's page in Settings. The guidance stays until the app returns. */
|
|
120
|
+
openSettings = async () => {
|
|
121
|
+
if (this.snapshot.phase !== "settings-guidance")
|
|
122
|
+
return;
|
|
123
|
+
await (this.access.openSettings?.() ?? react_native_1.Linking.openSettings());
|
|
124
|
+
};
|
|
125
|
+
/** Reads the status again: when it is created, and when the app becomes active while anything subscribes. */
|
|
126
|
+
refresh = async () => {
|
|
127
|
+
const read = ++this.readsStarted;
|
|
128
|
+
const answer = await outcome(() => this.access.status());
|
|
129
|
+
if (read <= this.newestApplied)
|
|
130
|
+
return;
|
|
131
|
+
this.newestApplied = read;
|
|
132
|
+
this.update({ status: answer });
|
|
133
|
+
const { phase } = this.snapshot;
|
|
134
|
+
const isShowing = phase === "explaining" || phase === "settings-guidance";
|
|
135
|
+
if (isShowing && !this.stillNeedsTheShownFlow(answer))
|
|
136
|
+
this.finish();
|
|
137
|
+
};
|
|
138
|
+
get memory() {
|
|
139
|
+
return this.ownMemory ?? (0, setup_1.configuredMemory)();
|
|
140
|
+
}
|
|
141
|
+
get declinedKey() {
|
|
142
|
+
return `ai.codual.ui.permissions.${this.access.id}.declined`;
|
|
143
|
+
}
|
|
144
|
+
async knownStatus() {
|
|
145
|
+
if (this.snapshot.status)
|
|
146
|
+
return this.snapshot.status;
|
|
147
|
+
await this.firstRead;
|
|
148
|
+
return this.snapshot.status ?? UNREAD;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Whether the explanation or guidance still has something to do after a fresh read: an explanation while the system
|
|
152
|
+
* can ask, guidance while only Settings can help, either while a full-access flow has only part of it.
|
|
153
|
+
*/
|
|
154
|
+
stillNeedsTheShownFlow(answer) {
|
|
155
|
+
if (this.isForFullAccess)
|
|
156
|
+
return answer.status === "limited" || answer.status === "provisional";
|
|
157
|
+
if (this.snapshot.phase === "explaining")
|
|
158
|
+
return answer.status === "not-determined" || answer.status === "denied";
|
|
159
|
+
return answer.status === "blocked";
|
|
160
|
+
}
|
|
161
|
+
start(phase) {
|
|
162
|
+
let end = () => { };
|
|
163
|
+
const result = new Promise((resolve) => {
|
|
164
|
+
end = resolve;
|
|
165
|
+
});
|
|
166
|
+
this.flow = { result, end };
|
|
167
|
+
this.update({ phase });
|
|
168
|
+
return result;
|
|
169
|
+
}
|
|
170
|
+
async requestAccess() {
|
|
171
|
+
const answer = await outcome(() => this.access.request());
|
|
172
|
+
// Every read that started before the system answered is older than its answer.
|
|
173
|
+
this.newestApplied = this.readsStarted;
|
|
174
|
+
this.update({ status: answer });
|
|
175
|
+
// A prompt closed without answering (still not determined) is no "no".
|
|
176
|
+
if (answer.status === "denied" || answer.status === "blocked")
|
|
177
|
+
this.memory.add(this.declinedKey);
|
|
178
|
+
this.finish();
|
|
179
|
+
}
|
|
180
|
+
finish() {
|
|
181
|
+
const ended = this.flow;
|
|
182
|
+
this.flow = undefined;
|
|
183
|
+
this.isForFullAccess = false;
|
|
184
|
+
this.update({ phase: "idle" });
|
|
185
|
+
ended?.end(this.snapshot.status ?? UNREAD);
|
|
186
|
+
}
|
|
187
|
+
update(change) {
|
|
188
|
+
this.snapshot = { ...this.snapshot, ...change };
|
|
189
|
+
for (const listener of this.listeners)
|
|
190
|
+
listener();
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
exports.Permission = Permission;
|
|
194
|
+
async function outcome(body) {
|
|
195
|
+
try {
|
|
196
|
+
return await body();
|
|
197
|
+
}
|
|
198
|
+
catch (error) {
|
|
199
|
+
return (0, status_1.unavailable)(error instanceof Error ? error.message : String(error));
|
|
200
|
+
}
|
|
201
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { PermissionMemory } from "./access";
|
|
2
|
+
export interface PermissionsSetup {
|
|
3
|
+
/** Where declines are kept across launches, so a "no" is never asked again: `permissionMemory(storage)`. */
|
|
4
|
+
readonly memory: PermissionMemory;
|
|
5
|
+
/** The app's Android application ID, so notifications open their own Settings page rather than the app's. */
|
|
6
|
+
readonly androidApplicationId?: string;
|
|
7
|
+
}
|
|
8
|
+
/** A synchronous store of booleans: MMKV (`new MMKV()` before version 4, `createMMKV()` after) or one like it. */
|
|
9
|
+
export type BooleanStorage = {
|
|
10
|
+
readonly getBoolean: (key: string) => boolean | undefined;
|
|
11
|
+
readonly set: (key: string, value: boolean) => void;
|
|
12
|
+
} & ({
|
|
13
|
+
readonly remove: (key: string) => unknown;
|
|
14
|
+
} | {
|
|
15
|
+
readonly delete: (key: string) => void;
|
|
16
|
+
});
|
|
17
|
+
/**
|
|
18
|
+
* Sets where every permission keeps its declines; call it once at launch, before any permission is used. Without it
|
|
19
|
+
* they are kept for one run only, so after a relaunch a "no" is asked again, against the behavior's rules.
|
|
20
|
+
*/
|
|
21
|
+
export declare function configurePermissions(next: PermissionsSetup): void;
|
|
22
|
+
/** Memory over `storage`, such as MMKV. */
|
|
23
|
+
export declare function permissionMemory(storage: BooleanStorage): PermissionMemory;
|
|
24
|
+
export declare function configuredMemory(): PermissionMemory;
|
|
25
|
+
export declare function androidApplicationId(): string | undefined;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.configurePermissions = configurePermissions;
|
|
4
|
+
exports.permissionMemory = permissionMemory;
|
|
5
|
+
exports.configuredMemory = configuredMemory;
|
|
6
|
+
exports.androidApplicationId = androidApplicationId;
|
|
7
|
+
let setup;
|
|
8
|
+
let hasWarned = false;
|
|
9
|
+
const runMemory = new Set();
|
|
10
|
+
/**
|
|
11
|
+
* Sets where every permission keeps its declines; call it once at launch, before any permission is used. Without it
|
|
12
|
+
* they are kept for one run only, so after a relaunch a "no" is asked again, against the behavior's rules.
|
|
13
|
+
*/
|
|
14
|
+
function configurePermissions(next) {
|
|
15
|
+
setup = next;
|
|
16
|
+
}
|
|
17
|
+
/** Memory over `storage`, such as MMKV. */
|
|
18
|
+
function permissionMemory(storage) {
|
|
19
|
+
return {
|
|
20
|
+
has: (key) => storage.getBoolean(key) === true,
|
|
21
|
+
add: (key) => storage.set(key, true),
|
|
22
|
+
delete: (key) => {
|
|
23
|
+
if ("remove" in storage)
|
|
24
|
+
storage.remove(key);
|
|
25
|
+
else
|
|
26
|
+
storage.delete(key);
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
function configuredMemory() {
|
|
31
|
+
if (setup)
|
|
32
|
+
return setup.memory;
|
|
33
|
+
if (!hasWarned) {
|
|
34
|
+
hasWarned = true;
|
|
35
|
+
console.warn("Codual UI permissions: call configurePermissions({ memory }) at launch, or declines are forgotten on relaunch.");
|
|
36
|
+
}
|
|
37
|
+
return runMemory;
|
|
38
|
+
}
|
|
39
|
+
function androidApplicationId() {
|
|
40
|
+
return setup?.androidApplicationId;
|
|
41
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/** A permission's status as the system reports it, read without prompting. */
|
|
2
|
+
export type PermissionStatus = {
|
|
3
|
+
/**
|
|
4
|
+
* `not-determined`: never asked, its prompt still to come. `limited`: part of it (some photos, an approximate
|
|
5
|
+
* location). `provisional`: iOS notifications delivered quietly. `denied`: a "no" the system will ask again
|
|
6
|
+
* after (Android). `blocked`: a "no" only Settings can change. `restricted`: the device doesn't let the user
|
|
7
|
+
* grant it.
|
|
8
|
+
*/
|
|
9
|
+
readonly status: "not-determined" | "granted" | "limited" | "provisional" | "denied" | "blocked" | "restricted";
|
|
10
|
+
} | {
|
|
11
|
+
/** It can't be asked for here: the adapter failed, or isn't set up. The reason is for the developer. */
|
|
12
|
+
readonly status: "unavailable";
|
|
13
|
+
readonly reason: string;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* The flow the user started: `explaining` while the app shows its explanation (its button calls `accept`),
|
|
17
|
+
* `requesting` while the system prompt is up, `settings-guidance` while the app shows how to turn it on in Settings
|
|
18
|
+
* (its button calls `openSettings`).
|
|
19
|
+
*/
|
|
20
|
+
export type PermissionPhase = "idle" | "explaining" | "requesting" | "settings-guidance";
|
|
21
|
+
export declare const NOT_DETERMINED: PermissionStatus;
|
|
22
|
+
export declare const GRANTED: PermissionStatus;
|
|
23
|
+
export declare const LIMITED: PermissionStatus;
|
|
24
|
+
export declare const PROVISIONAL: PermissionStatus;
|
|
25
|
+
export declare const DENIED: PermissionStatus;
|
|
26
|
+
export declare const BLOCKED: PermissionStatus;
|
|
27
|
+
export declare function unavailable(reason: string): PermissionStatus;
|
|
28
|
+
/** Granted, limited or provisional: the feature runs. */
|
|
29
|
+
export declare function isUsable(status: PermissionStatus | undefined): boolean;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BLOCKED = exports.DENIED = exports.PROVISIONAL = exports.LIMITED = exports.GRANTED = exports.NOT_DETERMINED = void 0;
|
|
4
|
+
exports.unavailable = unavailable;
|
|
5
|
+
exports.isUsable = isUsable;
|
|
6
|
+
exports.NOT_DETERMINED = { status: "not-determined" };
|
|
7
|
+
exports.GRANTED = { status: "granted" };
|
|
8
|
+
exports.LIMITED = { status: "limited" };
|
|
9
|
+
exports.PROVISIONAL = { status: "provisional" };
|
|
10
|
+
exports.DENIED = { status: "denied" };
|
|
11
|
+
exports.BLOCKED = { status: "blocked" };
|
|
12
|
+
function unavailable(reason) {
|
|
13
|
+
return { status: "unavailable", reason };
|
|
14
|
+
}
|
|
15
|
+
/** Granted, limited or provisional: the feature runs. */
|
|
16
|
+
function isUsable(status) {
|
|
17
|
+
return status?.status === "granted" || status?.status === "limited" || status?.status === "provisional";
|
|
18
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { PermissionAccess } from "./access";
|
|
2
|
+
import { Permission, type PermissionOptions, type PermissionSnapshot } from "./permission";
|
|
3
|
+
import { type PermissionStatus } from "./status";
|
|
4
|
+
export interface PermissionController extends PermissionSnapshot {
|
|
5
|
+
/** Whether the feature can run now: granted, limited or provisional. */
|
|
6
|
+
readonly isUsable: boolean;
|
|
7
|
+
readonly ask: () => Promise<PermissionStatus>;
|
|
8
|
+
readonly suggest: () => Promise<PermissionStatus>;
|
|
9
|
+
readonly askForFullAccess: () => Promise<PermissionStatus>;
|
|
10
|
+
readonly accept: () => void;
|
|
11
|
+
readonly decline: () => void;
|
|
12
|
+
readonly openSettings: () => Promise<void>;
|
|
13
|
+
readonly refresh: () => Promise<void>;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* One permission, asked for at the right moment. It reads the status on mount and never prompts; `ask` shows the
|
|
17
|
+
* app's explanation (`phase === "explaining"`), and only `accept` shows the system prompt; once the system won't ask
|
|
18
|
+
* again, it shows the app's Settings guidance instead. It reads again each time the app becomes active.
|
|
19
|
+
*
|
|
20
|
+
* Pass an adapter to keep one for the component (the first one passed is kept), or a permission from
|
|
21
|
+
* `createPermission` to share it between components.
|
|
22
|
+
*/
|
|
23
|
+
export declare function usePermission(source: PermissionAccess | Permission, options?: PermissionOptions): PermissionController;
|