react-native-permission-handler 0.1.0 → 0.2.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/README.md +183 -47
- package/dist/chunk-NFEGQTCC.mjs +27 -0
- package/dist/chunk-NFEGQTCC.mjs.map +1 -0
- package/dist/chunk-WZJOIVOM.mjs +49 -0
- package/dist/chunk-WZJOIVOM.mjs.map +1 -0
- package/dist/engines/expo.d.mts +18 -0
- package/dist/engines/expo.d.ts +18 -0
- package/dist/engines/expo.js +58 -0
- package/dist/engines/expo.js.map +1 -0
- package/dist/engines/expo.mjs +35 -0
- package/dist/engines/expo.mjs.map +1 -0
- package/dist/engines/rnp.d.mts +5 -0
- package/dist/engines/rnp.d.ts +5 -0
- package/dist/engines/rnp.js +52 -0
- package/dist/engines/rnp.js.map +1 -0
- package/dist/engines/rnp.mjs +10 -0
- package/dist/engines/rnp.mjs.map +1 -0
- package/dist/index.d.mts +7 -107
- package/dist/index.d.ts +7 -107
- package/dist/index.js +127 -58
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +98 -70
- package/dist/index.mjs.map +1 -1
- package/dist/types-QXyq8VnD.d.mts +122 -0
- package/dist/types-QXyq8VnD.d.ts +122 -0
- package/package.json +27 -2
- package/src/components/permission-gate.tsx +10 -3
- package/src/engines/expo.test.ts +122 -0
- package/src/engines/expo.ts +45 -0
- package/src/engines/resolve.test.ts +85 -0
- package/src/engines/resolve.ts +11 -0
- package/src/engines/rnp-fallback.ts +23 -0
- package/src/engines/rnp.test.ts +96 -0
- package/src/engines/rnp.ts +33 -0
- package/src/engines/use-engine.ts +10 -0
- package/src/hooks/use-multiple-permissions.test.ts +94 -54
- package/src/hooks/use-multiple-permissions.ts +52 -39
- package/src/hooks/use-permission-handler.test.ts +59 -49
- package/src/hooks/use-permission-handler.ts +11 -40
- package/src/index.ts +3 -0
- package/src/types.ts +20 -3
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
package/dist/index.d.mts
CHANGED
|
@@ -1,110 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { P as PermissionEngine, a as PermissionFlowState, b as PermissionFlowEvent, c as PermissionHandlerConfig, d as PermissionHandlerResult, M as MultiplePermissionsConfig, e as MultiplePermissionsResult, f as PermissionCallbacks, g as PrePromptConfig, B as BlockedPromptConfig } from './types-QXyq8VnD.mjs';
|
|
2
|
+
export { h as MultiPermissionEntry, i as PermissionStatus } from './types-QXyq8VnD.mjs';
|
|
2
3
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
3
4
|
import { ReactNode } from 'react';
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
* States of the permission flow state machine.
|
|
7
|
-
*/
|
|
8
|
-
type PermissionFlowState = "idle" | "checking" | "prePrompt" | "requesting" | "granted" | "denied" | "blocked" | "blockedPrompt" | "openingSettings" | "recheckingAfterSettings" | "unavailable";
|
|
9
|
-
/**
|
|
10
|
-
* Events that drive state transitions.
|
|
11
|
-
*/
|
|
12
|
-
type PermissionFlowEvent = {
|
|
13
|
-
type: "CHECK";
|
|
14
|
-
} | {
|
|
15
|
-
type: "CHECK_RESULT";
|
|
16
|
-
status: PermissionStatus;
|
|
17
|
-
} | {
|
|
18
|
-
type: "PRE_PROMPT_CONFIRM";
|
|
19
|
-
} | {
|
|
20
|
-
type: "PRE_PROMPT_DISMISS";
|
|
21
|
-
} | {
|
|
22
|
-
type: "REQUEST_RESULT";
|
|
23
|
-
status: PermissionStatus;
|
|
24
|
-
} | {
|
|
25
|
-
type: "OPEN_SETTINGS";
|
|
26
|
-
} | {
|
|
27
|
-
type: "SETTINGS_RETURN";
|
|
28
|
-
} | {
|
|
29
|
-
type: "RECHECK_RESULT";
|
|
30
|
-
status: PermissionStatus;
|
|
31
|
-
};
|
|
32
|
-
/**
|
|
33
|
-
* Configuration for the pre-prompt modal.
|
|
34
|
-
*/
|
|
35
|
-
interface PrePromptConfig {
|
|
36
|
-
title: string;
|
|
37
|
-
message: string;
|
|
38
|
-
confirmLabel?: string;
|
|
39
|
-
cancelLabel?: string;
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* Configuration for the blocked-prompt modal.
|
|
43
|
-
*/
|
|
44
|
-
interface BlockedPromptConfig {
|
|
45
|
-
title: string;
|
|
46
|
-
message: string;
|
|
47
|
-
settingsLabel?: string;
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Callbacks for analytics and side effects.
|
|
51
|
-
*/
|
|
52
|
-
interface PermissionCallbacks {
|
|
53
|
-
onGrant?: () => void;
|
|
54
|
-
onDeny?: () => void;
|
|
55
|
-
onBlock?: () => void;
|
|
56
|
-
onSettingsReturn?: (granted: boolean) => void;
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* Configuration for usePermissionHandler.
|
|
60
|
-
*/
|
|
61
|
-
interface PermissionHandlerConfig extends PermissionCallbacks {
|
|
62
|
-
permission: Permission | "notifications";
|
|
63
|
-
prePrompt: PrePromptConfig;
|
|
64
|
-
blockedPrompt: BlockedPromptConfig;
|
|
65
|
-
autoCheck?: boolean;
|
|
66
|
-
recheckOnForeground?: boolean;
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Return type of usePermissionHandler.
|
|
70
|
-
*/
|
|
71
|
-
interface PermissionHandlerResult {
|
|
72
|
-
state: PermissionFlowState;
|
|
73
|
-
nativeStatus: PermissionStatus | null;
|
|
74
|
-
isGranted: boolean;
|
|
75
|
-
isDenied: boolean;
|
|
76
|
-
isBlocked: boolean;
|
|
77
|
-
isChecking: boolean;
|
|
78
|
-
isUnavailable: boolean;
|
|
79
|
-
request: () => void;
|
|
80
|
-
check: () => void;
|
|
81
|
-
dismiss: () => void;
|
|
82
|
-
openSettings: () => void;
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Configuration for a single permission within useMultiplePermissions.
|
|
86
|
-
*/
|
|
87
|
-
interface MultiPermissionEntry extends PermissionCallbacks {
|
|
88
|
-
permission: Permission | "notifications";
|
|
89
|
-
prePrompt: PrePromptConfig;
|
|
90
|
-
blockedPrompt: BlockedPromptConfig;
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* Configuration for useMultiplePermissions.
|
|
94
|
-
*/
|
|
95
|
-
interface MultiplePermissionsConfig {
|
|
96
|
-
permissions: MultiPermissionEntry[];
|
|
97
|
-
strategy: "sequential" | "parallel";
|
|
98
|
-
onAllGranted?: () => void;
|
|
99
|
-
}
|
|
100
|
-
/**
|
|
101
|
-
* Return type of useMultiplePermissions.
|
|
102
|
-
*/
|
|
103
|
-
interface MultiplePermissionsResult {
|
|
104
|
-
statuses: Record<string, PermissionFlowState>;
|
|
105
|
-
allGranted: boolean;
|
|
106
|
-
request: () => void;
|
|
107
|
-
}
|
|
6
|
+
declare function setDefaultEngine(engine: PermissionEngine): void;
|
|
108
7
|
|
|
109
8
|
declare function transition(state: PermissionFlowState, event: PermissionFlowEvent): PermissionFlowState;
|
|
110
9
|
|
|
@@ -113,7 +12,8 @@ declare function usePermissionHandler(config: PermissionHandlerConfig): Permissi
|
|
|
113
12
|
declare function useMultiplePermissions(config: MultiplePermissionsConfig): MultiplePermissionsResult;
|
|
114
13
|
|
|
115
14
|
interface PermissionGateProps extends PermissionCallbacks {
|
|
116
|
-
permission:
|
|
15
|
+
permission: string;
|
|
16
|
+
engine?: PermissionEngine;
|
|
117
17
|
prePrompt: PrePromptConfig;
|
|
118
18
|
blockedPrompt: BlockedPromptConfig;
|
|
119
19
|
children: ReactNode;
|
|
@@ -128,7 +28,7 @@ interface PermissionGateProps extends PermissionCallbacks {
|
|
|
128
28
|
onOpenSettings: () => void;
|
|
129
29
|
}) => ReactNode;
|
|
130
30
|
}
|
|
131
|
-
declare function PermissionGate({ permission, prePrompt, blockedPrompt, children, fallback, renderPrePrompt, renderBlockedPrompt, onGrant, onDeny, onBlock, onSettingsReturn, }: PermissionGateProps): react_jsx_runtime.JSX.Element;
|
|
31
|
+
declare function PermissionGate({ permission, engine, prePrompt, blockedPrompt, children, fallback, renderPrePrompt, renderBlockedPrompt, onGrant, onDeny, onBlock, onSettingsReturn, }: PermissionGateProps): react_jsx_runtime.JSX.Element;
|
|
132
32
|
|
|
133
33
|
interface DefaultPrePromptProps extends PrePromptConfig {
|
|
134
34
|
visible: boolean;
|
|
@@ -143,4 +43,4 @@ interface DefaultBlockedPromptProps extends BlockedPromptConfig {
|
|
|
143
43
|
}
|
|
144
44
|
declare function DefaultBlockedPrompt({ visible, title, message, settingsLabel, onOpenSettings, }: DefaultBlockedPromptProps): react_jsx_runtime.JSX.Element;
|
|
145
45
|
|
|
146
|
-
export {
|
|
46
|
+
export { BlockedPromptConfig, DefaultBlockedPrompt, type DefaultBlockedPromptProps, DefaultPrePrompt, type DefaultPrePromptProps, MultiplePermissionsConfig, MultiplePermissionsResult, PermissionCallbacks, PermissionEngine, PermissionFlowEvent, PermissionFlowState, PermissionGate, type PermissionGateProps, PermissionHandlerConfig, PermissionHandlerResult, PrePromptConfig, setDefaultEngine, transition, useMultiplePermissions, usePermissionHandler };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,110 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { P as PermissionEngine, a as PermissionFlowState, b as PermissionFlowEvent, c as PermissionHandlerConfig, d as PermissionHandlerResult, M as MultiplePermissionsConfig, e as MultiplePermissionsResult, f as PermissionCallbacks, g as PrePromptConfig, B as BlockedPromptConfig } from './types-QXyq8VnD.js';
|
|
2
|
+
export { h as MultiPermissionEntry, i as PermissionStatus } from './types-QXyq8VnD.js';
|
|
2
3
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
3
4
|
import { ReactNode } from 'react';
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
* States of the permission flow state machine.
|
|
7
|
-
*/
|
|
8
|
-
type PermissionFlowState = "idle" | "checking" | "prePrompt" | "requesting" | "granted" | "denied" | "blocked" | "blockedPrompt" | "openingSettings" | "recheckingAfterSettings" | "unavailable";
|
|
9
|
-
/**
|
|
10
|
-
* Events that drive state transitions.
|
|
11
|
-
*/
|
|
12
|
-
type PermissionFlowEvent = {
|
|
13
|
-
type: "CHECK";
|
|
14
|
-
} | {
|
|
15
|
-
type: "CHECK_RESULT";
|
|
16
|
-
status: PermissionStatus;
|
|
17
|
-
} | {
|
|
18
|
-
type: "PRE_PROMPT_CONFIRM";
|
|
19
|
-
} | {
|
|
20
|
-
type: "PRE_PROMPT_DISMISS";
|
|
21
|
-
} | {
|
|
22
|
-
type: "REQUEST_RESULT";
|
|
23
|
-
status: PermissionStatus;
|
|
24
|
-
} | {
|
|
25
|
-
type: "OPEN_SETTINGS";
|
|
26
|
-
} | {
|
|
27
|
-
type: "SETTINGS_RETURN";
|
|
28
|
-
} | {
|
|
29
|
-
type: "RECHECK_RESULT";
|
|
30
|
-
status: PermissionStatus;
|
|
31
|
-
};
|
|
32
|
-
/**
|
|
33
|
-
* Configuration for the pre-prompt modal.
|
|
34
|
-
*/
|
|
35
|
-
interface PrePromptConfig {
|
|
36
|
-
title: string;
|
|
37
|
-
message: string;
|
|
38
|
-
confirmLabel?: string;
|
|
39
|
-
cancelLabel?: string;
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* Configuration for the blocked-prompt modal.
|
|
43
|
-
*/
|
|
44
|
-
interface BlockedPromptConfig {
|
|
45
|
-
title: string;
|
|
46
|
-
message: string;
|
|
47
|
-
settingsLabel?: string;
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Callbacks for analytics and side effects.
|
|
51
|
-
*/
|
|
52
|
-
interface PermissionCallbacks {
|
|
53
|
-
onGrant?: () => void;
|
|
54
|
-
onDeny?: () => void;
|
|
55
|
-
onBlock?: () => void;
|
|
56
|
-
onSettingsReturn?: (granted: boolean) => void;
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* Configuration for usePermissionHandler.
|
|
60
|
-
*/
|
|
61
|
-
interface PermissionHandlerConfig extends PermissionCallbacks {
|
|
62
|
-
permission: Permission | "notifications";
|
|
63
|
-
prePrompt: PrePromptConfig;
|
|
64
|
-
blockedPrompt: BlockedPromptConfig;
|
|
65
|
-
autoCheck?: boolean;
|
|
66
|
-
recheckOnForeground?: boolean;
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Return type of usePermissionHandler.
|
|
70
|
-
*/
|
|
71
|
-
interface PermissionHandlerResult {
|
|
72
|
-
state: PermissionFlowState;
|
|
73
|
-
nativeStatus: PermissionStatus | null;
|
|
74
|
-
isGranted: boolean;
|
|
75
|
-
isDenied: boolean;
|
|
76
|
-
isBlocked: boolean;
|
|
77
|
-
isChecking: boolean;
|
|
78
|
-
isUnavailable: boolean;
|
|
79
|
-
request: () => void;
|
|
80
|
-
check: () => void;
|
|
81
|
-
dismiss: () => void;
|
|
82
|
-
openSettings: () => void;
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Configuration for a single permission within useMultiplePermissions.
|
|
86
|
-
*/
|
|
87
|
-
interface MultiPermissionEntry extends PermissionCallbacks {
|
|
88
|
-
permission: Permission | "notifications";
|
|
89
|
-
prePrompt: PrePromptConfig;
|
|
90
|
-
blockedPrompt: BlockedPromptConfig;
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* Configuration for useMultiplePermissions.
|
|
94
|
-
*/
|
|
95
|
-
interface MultiplePermissionsConfig {
|
|
96
|
-
permissions: MultiPermissionEntry[];
|
|
97
|
-
strategy: "sequential" | "parallel";
|
|
98
|
-
onAllGranted?: () => void;
|
|
99
|
-
}
|
|
100
|
-
/**
|
|
101
|
-
* Return type of useMultiplePermissions.
|
|
102
|
-
*/
|
|
103
|
-
interface MultiplePermissionsResult {
|
|
104
|
-
statuses: Record<string, PermissionFlowState>;
|
|
105
|
-
allGranted: boolean;
|
|
106
|
-
request: () => void;
|
|
107
|
-
}
|
|
6
|
+
declare function setDefaultEngine(engine: PermissionEngine): void;
|
|
108
7
|
|
|
109
8
|
declare function transition(state: PermissionFlowState, event: PermissionFlowEvent): PermissionFlowState;
|
|
110
9
|
|
|
@@ -113,7 +12,8 @@ declare function usePermissionHandler(config: PermissionHandlerConfig): Permissi
|
|
|
113
12
|
declare function useMultiplePermissions(config: MultiplePermissionsConfig): MultiplePermissionsResult;
|
|
114
13
|
|
|
115
14
|
interface PermissionGateProps extends PermissionCallbacks {
|
|
116
|
-
permission:
|
|
15
|
+
permission: string;
|
|
16
|
+
engine?: PermissionEngine;
|
|
117
17
|
prePrompt: PrePromptConfig;
|
|
118
18
|
blockedPrompt: BlockedPromptConfig;
|
|
119
19
|
children: ReactNode;
|
|
@@ -128,7 +28,7 @@ interface PermissionGateProps extends PermissionCallbacks {
|
|
|
128
28
|
onOpenSettings: () => void;
|
|
129
29
|
}) => ReactNode;
|
|
130
30
|
}
|
|
131
|
-
declare function PermissionGate({ permission, prePrompt, blockedPrompt, children, fallback, renderPrePrompt, renderBlockedPrompt, onGrant, onDeny, onBlock, onSettingsReturn, }: PermissionGateProps): react_jsx_runtime.JSX.Element;
|
|
31
|
+
declare function PermissionGate({ permission, engine, prePrompt, blockedPrompt, children, fallback, renderPrePrompt, renderBlockedPrompt, onGrant, onDeny, onBlock, onSettingsReturn, }: PermissionGateProps): react_jsx_runtime.JSX.Element;
|
|
132
32
|
|
|
133
33
|
interface DefaultPrePromptProps extends PrePromptConfig {
|
|
134
34
|
visible: boolean;
|
|
@@ -143,4 +43,4 @@ interface DefaultBlockedPromptProps extends BlockedPromptConfig {
|
|
|
143
43
|
}
|
|
144
44
|
declare function DefaultBlockedPrompt({ visible, title, message, settingsLabel, onOpenSettings, }: DefaultBlockedPromptProps): react_jsx_runtime.JSX.Element;
|
|
145
45
|
|
|
146
|
-
export {
|
|
46
|
+
export { BlockedPromptConfig, DefaultBlockedPrompt, type DefaultBlockedPromptProps, DefaultPrePrompt, type DefaultPrePromptProps, MultiplePermissionsConfig, MultiplePermissionsResult, PermissionCallbacks, PermissionEngine, PermissionFlowEvent, PermissionFlowState, PermissionGate, type PermissionGateProps, PermissionHandlerConfig, PermissionHandlerResult, PrePromptConfig, setDefaultEngine, transition, useMultiplePermissions, usePermissionHandler };
|
package/dist/index.js
CHANGED
|
@@ -3,6 +3,9 @@ var __defProp = Object.defineProperty;
|
|
|
3
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __esm = (fn, res) => function __init() {
|
|
7
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
8
|
+
};
|
|
6
9
|
var __export = (target, all) => {
|
|
7
10
|
for (var name in all)
|
|
8
11
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -17,18 +20,62 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
20
|
};
|
|
18
21
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
22
|
|
|
23
|
+
// src/engines/rnp.ts
|
|
24
|
+
var rnp_exports = {};
|
|
25
|
+
__export(rnp_exports, {
|
|
26
|
+
createRNPEngine: () => createRNPEngine
|
|
27
|
+
});
|
|
28
|
+
function createRNPEngine() {
|
|
29
|
+
return {
|
|
30
|
+
async check(permission) {
|
|
31
|
+
if (permission === "notifications") {
|
|
32
|
+
const result = await (0, import_react_native_permissions.checkNotifications)();
|
|
33
|
+
return result.status;
|
|
34
|
+
}
|
|
35
|
+
return await (0, import_react_native_permissions.check)(permission);
|
|
36
|
+
},
|
|
37
|
+
async request(permission) {
|
|
38
|
+
if (permission === "notifications") {
|
|
39
|
+
const result = await (0, import_react_native_permissions.requestNotifications)(["alert", "badge", "sound"]);
|
|
40
|
+
return result.status;
|
|
41
|
+
}
|
|
42
|
+
return await (0, import_react_native_permissions.request)(permission);
|
|
43
|
+
},
|
|
44
|
+
async openSettings() {
|
|
45
|
+
await (0, import_react_native_permissions.openSettings)();
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
var import_react_native_permissions;
|
|
50
|
+
var init_rnp = __esm({
|
|
51
|
+
"src/engines/rnp.ts"() {
|
|
52
|
+
"use strict";
|
|
53
|
+
import_react_native_permissions = require("react-native-permissions");
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
|
|
20
57
|
// src/index.ts
|
|
21
58
|
var index_exports = {};
|
|
22
59
|
__export(index_exports, {
|
|
23
60
|
DefaultBlockedPrompt: () => DefaultBlockedPrompt,
|
|
24
61
|
DefaultPrePrompt: () => DefaultPrePrompt,
|
|
25
62
|
PermissionGate: () => PermissionGate,
|
|
63
|
+
setDefaultEngine: () => setDefaultEngine,
|
|
26
64
|
transition: () => transition,
|
|
27
65
|
useMultiplePermissions: () => useMultiplePermissions,
|
|
28
66
|
usePermissionHandler: () => usePermissionHandler
|
|
29
67
|
});
|
|
30
68
|
module.exports = __toCommonJS(index_exports);
|
|
31
69
|
|
|
70
|
+
// src/engines/resolve.ts
|
|
71
|
+
var defaultEngine = null;
|
|
72
|
+
function setDefaultEngine(engine) {
|
|
73
|
+
defaultEngine = engine;
|
|
74
|
+
}
|
|
75
|
+
function getDefaultEngine() {
|
|
76
|
+
return defaultEngine;
|
|
77
|
+
}
|
|
78
|
+
|
|
32
79
|
// src/core/state-machine.ts
|
|
33
80
|
function transition(state, event) {
|
|
34
81
|
switch (state) {
|
|
@@ -107,11 +154,34 @@ function transition(state, event) {
|
|
|
107
154
|
// src/hooks/use-permission-handler.ts
|
|
108
155
|
var import_react = require("react");
|
|
109
156
|
var import_react_native = require("react-native");
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
157
|
+
|
|
158
|
+
// src/engines/rnp-fallback.ts
|
|
159
|
+
var cachedFallback = null;
|
|
160
|
+
function getRNPFallbackEngine() {
|
|
161
|
+
if (cachedFallback) return cachedFallback;
|
|
162
|
+
try {
|
|
163
|
+
const mod = (init_rnp(), __toCommonJS(rnp_exports));
|
|
164
|
+
const { createRNPEngine: createRNPEngine2 } = mod;
|
|
165
|
+
cachedFallback = createRNPEngine2();
|
|
166
|
+
return cachedFallback;
|
|
167
|
+
} catch {
|
|
168
|
+
throw new Error(
|
|
169
|
+
"react-native-permission-handler: No permission engine configured. Either pass an `engine` in your hook config, call setDefaultEngine(), or install react-native-permissions as a peer dependency."
|
|
170
|
+
);
|
|
171
|
+
}
|
|
113
172
|
}
|
|
173
|
+
|
|
174
|
+
// src/engines/use-engine.ts
|
|
175
|
+
function resolveEngine(configEngine) {
|
|
176
|
+
if (configEngine) return configEngine;
|
|
177
|
+
const global = getDefaultEngine();
|
|
178
|
+
if (global) return global;
|
|
179
|
+
return getRNPFallbackEngine();
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// src/hooks/use-permission-handler.ts
|
|
114
183
|
function usePermissionHandler(config) {
|
|
184
|
+
const engine = resolveEngine(config.engine);
|
|
115
185
|
const [flowState, setFlowState] = (0, import_react.useState)("idle");
|
|
116
186
|
const [nativeStatus, setNativeStatus] = (0, import_react.useState)(null);
|
|
117
187
|
const isRequesting = (0, import_react.useRef)(false);
|
|
@@ -121,13 +191,7 @@ function usePermissionHandler(config) {
|
|
|
121
191
|
const checkPermission = (0, import_react.useCallback)(async () => {
|
|
122
192
|
setFlowState((s) => transition(s, { type: "CHECK" }));
|
|
123
193
|
try {
|
|
124
|
-
|
|
125
|
-
if (isNotifications(permission)) {
|
|
126
|
-
const result = await (0, import_react_native_permissions.checkNotifications)();
|
|
127
|
-
status = result.status;
|
|
128
|
-
} else {
|
|
129
|
-
status = await (0, import_react_native_permissions.check)(permission);
|
|
130
|
-
}
|
|
194
|
+
const status = await engine.check(permission);
|
|
131
195
|
setNativeStatus(status);
|
|
132
196
|
setFlowState((s) => {
|
|
133
197
|
const next = transition(s, { type: "CHECK_RESULT", status });
|
|
@@ -137,19 +201,13 @@ function usePermissionHandler(config) {
|
|
|
137
201
|
} catch {
|
|
138
202
|
setFlowState("idle");
|
|
139
203
|
}
|
|
140
|
-
}, [permission, onGrant]);
|
|
204
|
+
}, [engine, permission, onGrant]);
|
|
141
205
|
const requestPermission = (0, import_react.useCallback)(async () => {
|
|
142
206
|
if (isRequesting.current) return;
|
|
143
207
|
isRequesting.current = true;
|
|
144
208
|
setFlowState((s) => transition(s, { type: "PRE_PROMPT_CONFIRM" }));
|
|
145
209
|
try {
|
|
146
|
-
|
|
147
|
-
if (isNotifications(permission)) {
|
|
148
|
-
const result = await (0, import_react_native_permissions.requestNotifications)(["alert", "badge", "sound"]);
|
|
149
|
-
status = result.status;
|
|
150
|
-
} else {
|
|
151
|
-
status = await (0, import_react_native_permissions.request)(permission);
|
|
152
|
-
}
|
|
210
|
+
const status = await engine.request(permission);
|
|
153
211
|
setNativeStatus(status);
|
|
154
212
|
setFlowState((s) => {
|
|
155
213
|
const next = transition(s, { type: "REQUEST_RESULT", status });
|
|
@@ -163,7 +221,7 @@ function usePermissionHandler(config) {
|
|
|
163
221
|
} finally {
|
|
164
222
|
isRequesting.current = false;
|
|
165
223
|
}
|
|
166
|
-
}, [permission, onGrant, onDeny, onBlock]);
|
|
224
|
+
}, [engine, permission, onGrant, onDeny, onBlock]);
|
|
167
225
|
const dismiss = (0, import_react.useCallback)(() => {
|
|
168
226
|
setFlowState((s) => transition(s, { type: "PRE_PROMPT_DISMISS" }));
|
|
169
227
|
onDeny?.();
|
|
@@ -172,22 +230,16 @@ function usePermissionHandler(config) {
|
|
|
172
230
|
setFlowState((s) => transition(s, { type: "OPEN_SETTINGS" }));
|
|
173
231
|
waitingForSettings.current = true;
|
|
174
232
|
try {
|
|
175
|
-
await
|
|
233
|
+
await engine.openSettings();
|
|
176
234
|
} catch {
|
|
177
235
|
waitingForSettings.current = false;
|
|
178
236
|
setFlowState("blockedPrompt");
|
|
179
237
|
}
|
|
180
|
-
}, []);
|
|
238
|
+
}, [engine]);
|
|
181
239
|
const recheckAfterSettings = (0, import_react.useCallback)(async () => {
|
|
182
240
|
setFlowState((s) => transition(s, { type: "SETTINGS_RETURN" }));
|
|
183
241
|
try {
|
|
184
|
-
|
|
185
|
-
if (isNotifications(permission)) {
|
|
186
|
-
const result = await (0, import_react_native_permissions.checkNotifications)();
|
|
187
|
-
status = result.status;
|
|
188
|
-
} else {
|
|
189
|
-
status = await (0, import_react_native_permissions.check)(permission);
|
|
190
|
-
}
|
|
242
|
+
const status = await engine.check(permission);
|
|
191
243
|
setNativeStatus(status);
|
|
192
244
|
setFlowState((s) => {
|
|
193
245
|
const next = transition(s, { type: "RECHECK_RESULT", status });
|
|
@@ -198,7 +250,7 @@ function usePermissionHandler(config) {
|
|
|
198
250
|
} catch {
|
|
199
251
|
setFlowState("blockedPrompt");
|
|
200
252
|
}
|
|
201
|
-
}, [permission, onGrant, onSettingsReturn]);
|
|
253
|
+
}, [engine, permission, onGrant, onSettingsReturn]);
|
|
202
254
|
(0, import_react.useEffect)(() => {
|
|
203
255
|
if (autoCheck) {
|
|
204
256
|
checkPermission();
|
|
@@ -231,32 +283,30 @@ function usePermissionHandler(config) {
|
|
|
231
283
|
|
|
232
284
|
// src/hooks/use-multiple-permissions.ts
|
|
233
285
|
var import_react2 = require("react");
|
|
234
|
-
var import_react_native_permissions2 = require("react-native-permissions");
|
|
235
|
-
function isNotifications2(permission) {
|
|
236
|
-
return permission === "notifications";
|
|
237
|
-
}
|
|
238
|
-
async function checkOne(entry) {
|
|
239
|
-
if (isNotifications2(entry.permission)) {
|
|
240
|
-
const result = await (0, import_react_native_permissions2.checkNotifications)();
|
|
241
|
-
return result.status;
|
|
242
|
-
}
|
|
243
|
-
return (0, import_react_native_permissions2.check)(entry.permission);
|
|
244
|
-
}
|
|
245
|
-
async function requestOne(entry) {
|
|
246
|
-
if (isNotifications2(entry.permission)) {
|
|
247
|
-
const result = await (0, import_react_native_permissions2.requestNotifications)(["alert", "badge", "sound"]);
|
|
248
|
-
return result.status;
|
|
249
|
-
}
|
|
250
|
-
return (0, import_react_native_permissions2.request)(entry.permission);
|
|
251
|
-
}
|
|
252
286
|
function permissionKey(entry) {
|
|
253
287
|
return String(entry.permission);
|
|
254
288
|
}
|
|
255
289
|
function isGrantedStatus(status) {
|
|
256
290
|
return status === "granted" || status === "limited";
|
|
257
291
|
}
|
|
292
|
+
function statusToFlowState(status) {
|
|
293
|
+
switch (status) {
|
|
294
|
+
case "granted":
|
|
295
|
+
case "limited":
|
|
296
|
+
return "granted";
|
|
297
|
+
case "blocked":
|
|
298
|
+
return "blockedPrompt";
|
|
299
|
+
case "unavailable":
|
|
300
|
+
return "unavailable";
|
|
301
|
+
case "denied":
|
|
302
|
+
return "prePrompt";
|
|
303
|
+
default:
|
|
304
|
+
return "idle";
|
|
305
|
+
}
|
|
306
|
+
}
|
|
258
307
|
function useMultiplePermissions(config) {
|
|
259
|
-
const
|
|
308
|
+
const engine = resolveEngine(config.engine);
|
|
309
|
+
const { permissions, strategy, autoCheck = true, onAllGranted } = config;
|
|
260
310
|
const [statuses, setStatuses] = (0, import_react2.useState)(() => {
|
|
261
311
|
const initial = {};
|
|
262
312
|
for (const entry of permissions) {
|
|
@@ -274,13 +324,13 @@ function useMultiplePermissions(config) {
|
|
|
274
324
|
};
|
|
275
325
|
try {
|
|
276
326
|
if (strategy === "sequential") {
|
|
277
|
-
await runSequential(permissions, update);
|
|
327
|
+
await runSequential(permissions, engine, update);
|
|
278
328
|
} else {
|
|
279
|
-
await runParallel(permissions, update);
|
|
329
|
+
await runParallel(permissions, engine, update);
|
|
280
330
|
}
|
|
281
331
|
let allDone = true;
|
|
282
332
|
for (const entry of permissions) {
|
|
283
|
-
const finalStatus = await
|
|
333
|
+
const finalStatus = await engine.check(entry.permission);
|
|
284
334
|
if (!isGrantedStatus(finalStatus)) {
|
|
285
335
|
allDone = false;
|
|
286
336
|
break;
|
|
@@ -292,18 +342,34 @@ function useMultiplePermissions(config) {
|
|
|
292
342
|
} finally {
|
|
293
343
|
isRunning.current = false;
|
|
294
344
|
}
|
|
295
|
-
}, [permissions, strategy, onAllGranted]);
|
|
345
|
+
}, [permissions, strategy, engine, onAllGranted]);
|
|
346
|
+
(0, import_react2.useEffect)(() => {
|
|
347
|
+
if (!autoCheck) return;
|
|
348
|
+
let cancelled = false;
|
|
349
|
+
async function checkAll() {
|
|
350
|
+
for (const entry of permissions) {
|
|
351
|
+
const key = permissionKey(entry);
|
|
352
|
+
const status = await engine.check(entry.permission);
|
|
353
|
+
if (cancelled) return;
|
|
354
|
+
setStatuses((prev) => ({ ...prev, [key]: statusToFlowState(status) }));
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
checkAll();
|
|
358
|
+
return () => {
|
|
359
|
+
cancelled = true;
|
|
360
|
+
};
|
|
361
|
+
}, []);
|
|
296
362
|
return {
|
|
297
363
|
statuses,
|
|
298
364
|
allGranted,
|
|
299
365
|
request: requestAll
|
|
300
366
|
};
|
|
301
367
|
}
|
|
302
|
-
async function runSequential(permissions, updateStatus) {
|
|
368
|
+
async function runSequential(permissions, engine, updateStatus) {
|
|
303
369
|
for (const entry of permissions) {
|
|
304
370
|
const key = permissionKey(entry);
|
|
305
371
|
updateStatus(key, "checking");
|
|
306
|
-
const checkStatus = await
|
|
372
|
+
const checkStatus = await engine.check(entry.permission);
|
|
307
373
|
if (isGrantedStatus(checkStatus)) {
|
|
308
374
|
updateStatus(key, "granted");
|
|
309
375
|
entry.onGrant?.();
|
|
@@ -319,7 +385,7 @@ async function runSequential(permissions, updateStatus) {
|
|
|
319
385
|
break;
|
|
320
386
|
}
|
|
321
387
|
updateStatus(key, "requesting");
|
|
322
|
-
const requestStatus = await
|
|
388
|
+
const requestStatus = await engine.request(entry.permission);
|
|
323
389
|
if (isGrantedStatus(requestStatus)) {
|
|
324
390
|
updateStatus(key, "granted");
|
|
325
391
|
entry.onGrant?.();
|
|
@@ -334,12 +400,12 @@ async function runSequential(permissions, updateStatus) {
|
|
|
334
400
|
}
|
|
335
401
|
}
|
|
336
402
|
}
|
|
337
|
-
async function runParallel(permissions, updateStatus) {
|
|
403
|
+
async function runParallel(permissions, engine, updateStatus) {
|
|
338
404
|
const checkResults = await Promise.all(
|
|
339
405
|
permissions.map(async (entry) => {
|
|
340
406
|
const key = permissionKey(entry);
|
|
341
407
|
updateStatus(key, "checking");
|
|
342
|
-
const status = await
|
|
408
|
+
const status = await engine.check(entry.permission);
|
|
343
409
|
return { entry, key, status };
|
|
344
410
|
})
|
|
345
411
|
);
|
|
@@ -361,7 +427,7 @@ async function runParallel(permissions, updateStatus) {
|
|
|
361
427
|
continue;
|
|
362
428
|
}
|
|
363
429
|
updateStatus(key, "requesting");
|
|
364
|
-
const requestStatus = await
|
|
430
|
+
const requestStatus = await engine.request(entry.permission);
|
|
365
431
|
if (isGrantedStatus(requestStatus)) {
|
|
366
432
|
updateStatus(key, "granted");
|
|
367
433
|
entry.onGrant?.();
|
|
@@ -519,6 +585,7 @@ var styles2 = import_react_native3.StyleSheet.create({
|
|
|
519
585
|
var import_jsx_runtime3 = require("react/jsx-runtime");
|
|
520
586
|
function PermissionGate({
|
|
521
587
|
permission,
|
|
588
|
+
engine,
|
|
522
589
|
prePrompt,
|
|
523
590
|
blockedPrompt,
|
|
524
591
|
children,
|
|
@@ -532,6 +599,7 @@ function PermissionGate({
|
|
|
532
599
|
}) {
|
|
533
600
|
const handler = usePermissionHandler({
|
|
534
601
|
permission,
|
|
602
|
+
engine,
|
|
535
603
|
prePrompt,
|
|
536
604
|
blockedPrompt,
|
|
537
605
|
onGrant,
|
|
@@ -591,6 +659,7 @@ function PermissionGate({
|
|
|
591
659
|
DefaultBlockedPrompt,
|
|
592
660
|
DefaultPrePrompt,
|
|
593
661
|
PermissionGate,
|
|
662
|
+
setDefaultEngine,
|
|
594
663
|
transition,
|
|
595
664
|
useMultiplePermissions,
|
|
596
665
|
usePermissionHandler
|