@imksh/create 1.0.32 → 1.0.34
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/package.json +1 -1
- package/templates/layers/react-native/base/package.json +32 -35
- package/templates/layers/react-native/base/src/app/_layout.tsx +1 -5
- package/templates/layers/react-native/base/src/app/screens/doc-viewer.tsx +378 -0
- package/templates/layers/react-native/base/src/components/common/ConfirmationToast.tsx +2 -5
- package/templates/layers/react-native/base/src/components/common/GoogleAuthButton.tsx +33 -54
- package/templates/layers/react-native/base/src/components/ui/BiometricLock.tsx +3 -3
- package/templates/layers/react-native/base/src/components/ui/CustomTabBar.tsx +14 -9
- package/templates/layers/react-native/base/src/services/permission.service.ts +168 -42
- package/templates/layers/react-native/base/src/store/useNotificationStore.ts +2 -2
- package/templates/layers/react-native/base/src/types/common.type.ts +17 -0
- package/templates/layers/react-native/base/src/utils/env.ts +47 -0
- package/templates/layers/react-native/base/src/utils/fileDownload.ts +2 -2
- package/templates/presets/production-fullstack/overrides/mobile/package.json +32 -32
- package/templates/presets/production-fullstack/overrides/mobile/src/services/permission.service.ts +168 -44
- package/templates/presets/production-fullstack/overrides/mobile/src/store/useNotificationStore.ts +16 -0
- package/templates/presets/production-fullstack/overrides/mobile/src/utils/env.ts +47 -0
- package/templates/layers/react-native/base/src/app/doc-viewer.tsx +0 -193
package/templates/presets/production-fullstack/overrides/mobile/src/services/permission.service.ts
CHANGED
|
@@ -1,13 +1,36 @@
|
|
|
1
|
-
import { Platform, Alert } from "react-native";
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
1
|
+
import { Platform, Alert, Linking } from "react-native";
|
|
2
|
+
import { isExpoGo, hasNativeModule } from "@/utils/env";
|
|
3
|
+
|
|
4
|
+
export const RESULTS = {
|
|
5
|
+
UNAVAILABLE: "unavailable",
|
|
6
|
+
DENIED: "denied",
|
|
7
|
+
BLOCKED: "blocked",
|
|
8
|
+
GRANTED: "granted",
|
|
9
|
+
LIMITED: "limited",
|
|
10
|
+
} as const;
|
|
11
|
+
|
|
12
|
+
export type PermissionStatus = (typeof RESULTS)[keyof typeof RESULTS];
|
|
13
|
+
export type Permission = string;
|
|
14
|
+
|
|
15
|
+
export const openSettings = async (): Promise<void> => {
|
|
16
|
+
try {
|
|
17
|
+
await Linking.openSettings();
|
|
18
|
+
} catch (e) {
|
|
19
|
+
console.warn("Failed to open device settings", e);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const getRNPModule = () => {
|
|
24
|
+
if (isExpoGo() || !hasNativeModule("RNPermissions")) {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
try {
|
|
28
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
29
|
+
return require("react-native-permissions");
|
|
30
|
+
} catch {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
};
|
|
11
34
|
|
|
12
35
|
export type AppPermissionType =
|
|
13
36
|
| "camera"
|
|
@@ -27,6 +50,12 @@ class PermissionService {
|
|
|
27
50
|
* Helper to determine the correct native Permission object based on platform
|
|
28
51
|
*/
|
|
29
52
|
private getNativePermission(type: AppPermissionType): Permission | null {
|
|
53
|
+
const rnp = getRNPModule();
|
|
54
|
+
if (!rnp?.PERMISSIONS) {
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
const { PERMISSIONS } = rnp;
|
|
58
|
+
|
|
30
59
|
if (Platform.OS === "ios") {
|
|
31
60
|
switch (type) {
|
|
32
61
|
case "camera":
|
|
@@ -34,10 +63,7 @@ class PermissionService {
|
|
|
34
63
|
case "audio":
|
|
35
64
|
return PERMISSIONS.IOS.MICROPHONE;
|
|
36
65
|
case "image":
|
|
37
|
-
return PERMISSIONS.IOS.PHOTO_LIBRARY;
|
|
38
66
|
case "file":
|
|
39
|
-
// iOS doesn't have a broad "file system" permission for the app sandbox
|
|
40
|
-
// Usually handled via DocumentPicker or PhotoLibrary depending on use case.
|
|
41
67
|
return PERMISSIONS.IOS.PHOTO_LIBRARY;
|
|
42
68
|
case "location":
|
|
43
69
|
return PERMISSIONS.IOS.LOCATION_WHEN_IN_USE;
|
|
@@ -50,11 +76,7 @@ class PermissionService {
|
|
|
50
76
|
case "nearbydevice":
|
|
51
77
|
return PERMISSIONS.IOS.BLUETOOTH;
|
|
52
78
|
case "notification":
|
|
53
|
-
// handled uniquely by react-native-permissions using requestNotifications
|
|
54
|
-
return null;
|
|
55
79
|
case "phone":
|
|
56
|
-
// iOS doesn't require explicit permission to use Linking.openURL('tel:...')
|
|
57
|
-
return null;
|
|
58
80
|
default:
|
|
59
81
|
return null;
|
|
60
82
|
}
|
|
@@ -77,13 +99,10 @@ class PermissionService {
|
|
|
77
99
|
case "calendar":
|
|
78
100
|
return PERMISSIONS.ANDROID.READ_CALENDAR;
|
|
79
101
|
case "nearbydevice":
|
|
80
|
-
// Required for Android 12+
|
|
81
102
|
return PERMISSIONS.ANDROID.BLUETOOTH_CONNECT;
|
|
82
103
|
case "phone":
|
|
83
104
|
return PERMISSIONS.ANDROID.CALL_PHONE;
|
|
84
105
|
case "notification":
|
|
85
|
-
// handled uniquely by checkNotifications/requestNotifications
|
|
86
|
-
return null;
|
|
87
106
|
default:
|
|
88
107
|
return null;
|
|
89
108
|
}
|
|
@@ -91,24 +110,101 @@ class PermissionService {
|
|
|
91
110
|
return null;
|
|
92
111
|
}
|
|
93
112
|
|
|
113
|
+
/**
|
|
114
|
+
* Fallback for checking permissions in Expo Go using Expo APIs
|
|
115
|
+
*/
|
|
116
|
+
private async checkExpoPermission(type: AppPermissionType): Promise<PermissionStatus> {
|
|
117
|
+
try {
|
|
118
|
+
if (type === "notification") {
|
|
119
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
120
|
+
const Notifications = require("expo-notifications");
|
|
121
|
+
const { status } = await Notifications.getPermissionsAsync();
|
|
122
|
+
return status === "granted" ? RESULTS.GRANTED : RESULTS.DENIED;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (type === "location" || type === "locationAlways") {
|
|
126
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
127
|
+
const Location = require("expo-location");
|
|
128
|
+
const { status } = await Location.getForegroundPermissionsAsync();
|
|
129
|
+
return status === "granted" ? RESULTS.GRANTED : RESULTS.DENIED;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (type === "camera" || type === "image") {
|
|
133
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
134
|
+
const ImagePicker = require("expo-image-picker");
|
|
135
|
+
const { status } =
|
|
136
|
+
type === "camera"
|
|
137
|
+
? await ImagePicker.getCameraPermissionsAsync()
|
|
138
|
+
: await ImagePicker.getMediaLibraryPermissionsAsync();
|
|
139
|
+
return status === "granted" ? RESULTS.GRANTED : RESULTS.DENIED;
|
|
140
|
+
}
|
|
141
|
+
} catch {
|
|
142
|
+
return RESULTS.GRANTED;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return RESULTS.GRANTED;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Fallback for requesting permissions in Expo Go using Expo APIs
|
|
150
|
+
*/
|
|
151
|
+
private async requestExpoPermission(type: AppPermissionType): Promise<PermissionStatus> {
|
|
152
|
+
try {
|
|
153
|
+
if (type === "notification") {
|
|
154
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
155
|
+
const Notifications = require("expo-notifications");
|
|
156
|
+
const { status } = await Notifications.requestPermissionsAsync();
|
|
157
|
+
return status === "granted" ? RESULTS.GRANTED : RESULTS.DENIED;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if (type === "location" || type === "locationAlways") {
|
|
161
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
162
|
+
const Location = require("expo-location");
|
|
163
|
+
const { status } = await Location.requestForegroundPermissionsAsync();
|
|
164
|
+
return status === "granted" ? RESULTS.GRANTED : RESULTS.DENIED;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (type === "camera" || type === "image") {
|
|
168
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
169
|
+
const ImagePicker = require("expo-image-picker");
|
|
170
|
+
const { status } =
|
|
171
|
+
type === "camera"
|
|
172
|
+
? await ImagePicker.requestCameraPermissionsAsync()
|
|
173
|
+
: await ImagePicker.requestMediaLibraryPermissionsAsync();
|
|
174
|
+
return status === "granted" ? RESULTS.GRANTED : RESULTS.DENIED;
|
|
175
|
+
}
|
|
176
|
+
} catch {
|
|
177
|
+
return RESULTS.GRANTED;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
return RESULTS.GRANTED;
|
|
181
|
+
}
|
|
182
|
+
|
|
94
183
|
/**
|
|
95
184
|
* Check the current status of a permission without asking the user
|
|
96
185
|
*/
|
|
97
186
|
async checkPermission(type: AppPermissionType): Promise<PermissionStatus> {
|
|
187
|
+
const rnp = getRNPModule();
|
|
188
|
+
if (isExpoGo() || !rnp) {
|
|
189
|
+
return this.checkExpoPermission(type);
|
|
190
|
+
}
|
|
191
|
+
|
|
98
192
|
if (type === "notification") {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
193
|
+
try {
|
|
194
|
+
const { status } = await rnp.checkNotifications();
|
|
195
|
+
return status as PermissionStatus;
|
|
196
|
+
} catch {
|
|
197
|
+
return this.checkExpoPermission(type);
|
|
198
|
+
}
|
|
103
199
|
}
|
|
104
|
-
|
|
200
|
+
|
|
105
201
|
const nativePerm = this.getNativePermission(type);
|
|
106
202
|
if (!nativePerm) {
|
|
107
|
-
// If the permission doesn't exist on this platform (e.g. Phone on iOS), we consider it granted
|
|
108
203
|
return RESULTS.GRANTED;
|
|
109
204
|
}
|
|
205
|
+
|
|
110
206
|
try {
|
|
111
|
-
return await check(nativePerm);
|
|
207
|
+
return (await rnp.check(nativePerm as any)) as PermissionStatus;
|
|
112
208
|
} catch (error) {
|
|
113
209
|
console.error(`Error checking permission ${type}:`, error);
|
|
114
210
|
return RESULTS.UNAVAILABLE;
|
|
@@ -119,19 +215,27 @@ class PermissionService {
|
|
|
119
215
|
* Request a permission from the user
|
|
120
216
|
*/
|
|
121
217
|
async requestPermission(type: AppPermissionType): Promise<PermissionStatus> {
|
|
218
|
+
const rnp = getRNPModule();
|
|
219
|
+
if (isExpoGo() || !rnp) {
|
|
220
|
+
return this.requestExpoPermission(type);
|
|
221
|
+
}
|
|
222
|
+
|
|
122
223
|
if (type === "notification") {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
224
|
+
try {
|
|
225
|
+
const { status } = await rnp.requestNotifications(["alert", "sound", "badge"]);
|
|
226
|
+
return status as PermissionStatus;
|
|
227
|
+
} catch {
|
|
228
|
+
return this.requestExpoPermission(type);
|
|
229
|
+
}
|
|
127
230
|
}
|
|
128
231
|
|
|
129
232
|
const nativePerm = this.getNativePermission(type);
|
|
130
233
|
if (!nativePerm) {
|
|
131
234
|
return RESULTS.GRANTED;
|
|
132
235
|
}
|
|
236
|
+
|
|
133
237
|
try {
|
|
134
|
-
return await request(nativePerm);
|
|
238
|
+
return (await rnp.request(nativePerm as any)) as PermissionStatus;
|
|
135
239
|
} catch (error) {
|
|
136
240
|
console.error(`Error requesting permission ${type}:`, error);
|
|
137
241
|
return RESULTS.UNAVAILABLE;
|
|
@@ -152,7 +256,14 @@ class PermissionService {
|
|
|
152
256
|
}
|
|
153
257
|
|
|
154
258
|
/**
|
|
155
|
-
*
|
|
259
|
+
* Opens the OS Settings screen for the app
|
|
260
|
+
*/
|
|
261
|
+
async openSettings(): Promise<void> {
|
|
262
|
+
return openSettings();
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Helper to show an alert and open the OS Settings screen for the app if permission is blocked
|
|
156
267
|
*/
|
|
157
268
|
async openSettingsForApp(
|
|
158
269
|
title = "Permission Required",
|
|
@@ -160,52 +271,65 @@ class PermissionService {
|
|
|
160
271
|
) {
|
|
161
272
|
Alert.alert(title, message, [
|
|
162
273
|
{ text: "Cancel", style: "cancel" },
|
|
163
|
-
{
|
|
274
|
+
{
|
|
275
|
+
text: "Open Settings",
|
|
276
|
+
onPress: () => {
|
|
277
|
+
openSettings();
|
|
278
|
+
},
|
|
279
|
+
},
|
|
164
280
|
]);
|
|
165
281
|
}
|
|
282
|
+
|
|
166
283
|
/**
|
|
167
284
|
* Utility to register and fetch the Expo Push Token for this device.
|
|
168
285
|
* This uses the unified PermissionService to request the notification permission.
|
|
169
286
|
* NOTE: This will only return a token on physical devices.
|
|
170
287
|
*/
|
|
171
288
|
async registerForPushNotificationsAsync(): Promise<string | null> {
|
|
289
|
+
// In Expo Go on Android, FCM push notifications are not supported
|
|
290
|
+
if (isExpoGo() && Platform.OS === "android") {
|
|
291
|
+
return null;
|
|
292
|
+
}
|
|
293
|
+
|
|
172
294
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
173
295
|
const Device = require("expo-device");
|
|
174
296
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
175
297
|
const Notifications = require("expo-notifications");
|
|
176
298
|
|
|
177
299
|
if (!Device.isDevice) {
|
|
178
|
-
console.log("Must use physical device for Push Notifications");
|
|
179
300
|
return null;
|
|
180
301
|
}
|
|
181
302
|
|
|
182
303
|
const hasPermission = await this.requirePermission("notification");
|
|
183
304
|
if (!hasPermission) {
|
|
184
|
-
console.log("User denied push notification permissions");
|
|
185
305
|
return null;
|
|
186
306
|
}
|
|
187
307
|
|
|
188
308
|
// Set channel for Android
|
|
189
309
|
if (Platform.OS === "android") {
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
310
|
+
try {
|
|
311
|
+
await Notifications.setNotificationChannelAsync("default", {
|
|
312
|
+
name: "default",
|
|
313
|
+
importance: Notifications.AndroidImportance.MAX,
|
|
314
|
+
vibrationPattern: [0, 250, 250, 250],
|
|
315
|
+
lightColor: "#FF231F7C",
|
|
316
|
+
});
|
|
317
|
+
} catch {
|
|
318
|
+
// Channel setup may fail in Expo Go
|
|
319
|
+
}
|
|
196
320
|
}
|
|
197
321
|
|
|
198
322
|
try {
|
|
199
323
|
const projectId =
|
|
200
324
|
require("../../app.json")?.expo?.extra?.eas?.projectId;
|
|
201
|
-
|
|
325
|
+
|
|
202
326
|
const tokenData = await Notifications.getExpoPushTokenAsync({
|
|
203
327
|
projectId,
|
|
204
328
|
});
|
|
205
329
|
|
|
206
330
|
return tokenData.data;
|
|
207
331
|
} catch (e) {
|
|
208
|
-
console.
|
|
332
|
+
console.warn("Failed to get push token", e);
|
|
209
333
|
return null;
|
|
210
334
|
}
|
|
211
335
|
}
|
package/templates/presets/production-fullstack/overrides/mobile/src/store/useNotificationStore.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { create } from 'zustand';
|
|
2
|
+
import { Platform } from 'react-native';
|
|
2
3
|
import { getSocket } from '../lib/socket';
|
|
3
4
|
import * as Notifications from 'expo-notifications';
|
|
5
|
+
import { isExpoGo } from '../utils/env';
|
|
4
6
|
|
|
5
7
|
export interface NotificationState {
|
|
6
8
|
notifications: any[];
|
|
@@ -24,7 +26,21 @@ export const useNotificationStore = create<NotificationState>((set, get) => ({
|
|
|
24
26
|
},
|
|
25
27
|
|
|
26
28
|
registerForPushNotificationsAsync: async () => {
|
|
29
|
+
// In Expo Go on Android, FCM push notifications are not supported
|
|
30
|
+
if (isExpoGo() && Platform.OS === 'android') {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
|
|
27
34
|
try {
|
|
35
|
+
if (Platform.OS === 'android') {
|
|
36
|
+
await Notifications.setNotificationChannelAsync('default', {
|
|
37
|
+
name: 'default',
|
|
38
|
+
importance: Notifications.AndroidImportance.MAX,
|
|
39
|
+
vibrationPattern: [0, 250, 250, 250],
|
|
40
|
+
lightColor: '#FF231F7C',
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
28
44
|
const { status: existingStatus } = await Notifications.getPermissionsAsync();
|
|
29
45
|
let finalStatus = existingStatus;
|
|
30
46
|
if (existingStatus !== 'granted') {
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import Constants, { ExecutionEnvironment } from "expo-constants";
|
|
2
|
+
import { TurboModuleRegistry, NativeModules } from "react-native";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Checks whether a specific native module or TurboModule is linked in the native binary.
|
|
6
|
+
* Uses non-throwing lookups on both TurboModuleRegistry and NativeModules.
|
|
7
|
+
*/
|
|
8
|
+
export const hasNativeModule = (name: string): boolean => {
|
|
9
|
+
try {
|
|
10
|
+
if (TurboModuleRegistry?.get && TurboModuleRegistry.get(name) != null) {
|
|
11
|
+
return true;
|
|
12
|
+
}
|
|
13
|
+
if (NativeModules && NativeModules[name] != null) {
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
} catch {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
return false;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Returns true if the app is currently running inside Expo Go.
|
|
24
|
+
* Robust against early bundle evaluation where Constants.appOwnership may be null/undefined.
|
|
25
|
+
*/
|
|
26
|
+
export const isExpoGo = (): boolean => {
|
|
27
|
+
try {
|
|
28
|
+
// Legacy appOwnership check (SDK < 50 or when populated)
|
|
29
|
+
if (Constants.appOwnership === "expo") {
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
// Modern execution environment check (SDK 50+)
|
|
33
|
+
if (Constants.executionEnvironment === ExecutionEnvironment.StoreClient) {
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
// Fallback: Check if custom third-party native modules absent in Expo Go are missing
|
|
37
|
+
const hasRNPermissions = hasNativeModule("RNPermissions");
|
|
38
|
+
const hasRNGoogleSignin = hasNativeModule("RNGoogleSignin");
|
|
39
|
+
|
|
40
|
+
if (!hasRNPermissions && !hasRNGoogleSignin) {
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
} catch {
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
return false;
|
|
47
|
+
};
|
|
@@ -1,193 +0,0 @@
|
|
|
1
|
-
import React, { useState, useEffect } from "react";
|
|
2
|
-
import {
|
|
3
|
-
View,
|
|
4
|
-
StyleSheet,
|
|
5
|
-
Dimensions,
|
|
6
|
-
} from "react-native";
|
|
7
|
-
import { useLocalSearchParams, useRouter } from "expo-router";
|
|
8
|
-
import { useSafeAreaInsets } from "react-native-safe-area-context";
|
|
9
|
-
import Pdf from "react-native-pdf";
|
|
10
|
-
import { useColorScheme } from "nativewind";
|
|
11
|
-
import { Colors } from "../constants/Colors";
|
|
12
|
-
import ScreenHeader from "../components/common/ScreenHeader";
|
|
13
|
-
import { Txt } from "../components/common/Typography";
|
|
14
|
-
import Input from "../components/ui/inputs/Input";
|
|
15
|
-
import Button from "../components/ui/buttons/Button";
|
|
16
|
-
import Container from "../components/ui/layout/Container";
|
|
17
|
-
import { Ionicons } from "@expo/vector-icons";
|
|
18
|
-
import ProgressBar from "../components/ui/feedback/ProgressBar";
|
|
19
|
-
import * as FileSystem from "expo-file-system/legacy";
|
|
20
|
-
|
|
21
|
-
export default function DocViewerScreen() {
|
|
22
|
-
const router = useRouter();
|
|
23
|
-
const { url, name } = useLocalSearchParams<{
|
|
24
|
-
url: string;
|
|
25
|
-
name?: string;
|
|
26
|
-
}>();
|
|
27
|
-
|
|
28
|
-
const insets = useSafeAreaInsets();
|
|
29
|
-
const { colorScheme } = useColorScheme();
|
|
30
|
-
const isDark = colorScheme === "dark";
|
|
31
|
-
const colors = Colors[isDark ? "dark" : "light"];
|
|
32
|
-
|
|
33
|
-
const [totalPages, setTotalPages] = useState(0);
|
|
34
|
-
const [currentPage, setCurrentPage] = useState(1);
|
|
35
|
-
|
|
36
|
-
const [password, setPassword] = useState("");
|
|
37
|
-
const [isPasswordRequired, setIsPasswordRequired] = useState(false);
|
|
38
|
-
const [inputPassword, setInputPassword] = useState("");
|
|
39
|
-
const [errorMsg, setErrorMsg] = useState("");
|
|
40
|
-
|
|
41
|
-
const [localUri, setLocalUri] = useState<string | null>(null);
|
|
42
|
-
const [downloadProgress, setDownloadProgress] = useState(0);
|
|
43
|
-
const [downloadError, setDownloadError] = useState<string | null>(null);
|
|
44
|
-
|
|
45
|
-
useEffect(() => {
|
|
46
|
-
if (!url) return;
|
|
47
|
-
|
|
48
|
-
if (url.startsWith('http')) {
|
|
49
|
-
const downloadFile = async () => {
|
|
50
|
-
try {
|
|
51
|
-
const filename = `doc_${Date.now()}.pdf`;
|
|
52
|
-
const fileUri = FileSystem.documentDirectory + filename;
|
|
53
|
-
|
|
54
|
-
const downloadResumable = FileSystem.createDownloadResumable(
|
|
55
|
-
url,
|
|
56
|
-
fileUri,
|
|
57
|
-
{},
|
|
58
|
-
(progressEvent) => {
|
|
59
|
-
const progress = progressEvent.totalBytesWritten / progressEvent.totalBytesExpectedToWrite;
|
|
60
|
-
setDownloadProgress(progress);
|
|
61
|
-
}
|
|
62
|
-
);
|
|
63
|
-
|
|
64
|
-
const result = await downloadResumable.downloadAsync();
|
|
65
|
-
if (result && result.uri) {
|
|
66
|
-
setLocalUri(result.uri);
|
|
67
|
-
} else {
|
|
68
|
-
setDownloadError('Failed to download document');
|
|
69
|
-
}
|
|
70
|
-
} catch (error) {
|
|
71
|
-
console.error('Download error:', error);
|
|
72
|
-
setDownloadError('Error downloading document');
|
|
73
|
-
}
|
|
74
|
-
};
|
|
75
|
-
downloadFile();
|
|
76
|
-
} else {
|
|
77
|
-
setLocalUri(url);
|
|
78
|
-
}
|
|
79
|
-
}, [url]);
|
|
80
|
-
|
|
81
|
-
if (!url) {
|
|
82
|
-
return (
|
|
83
|
-
<View style={{ flex: 1, backgroundColor: colors.base100 }}>
|
|
84
|
-
<ScreenHeader title="Document Viewer" onBack={() => router.back()} />
|
|
85
|
-
<View className="flex-1 items-center justify-center p-4">
|
|
86
|
-
<Ionicons name="alert-circle-outline" size={48} color={colors.baseContent} style={{ opacity: 0.5 }} />
|
|
87
|
-
<Txt variant="base" className="mt-4 text-center opacity-70">
|
|
88
|
-
No document URL provided.
|
|
89
|
-
</Txt>
|
|
90
|
-
</View>
|
|
91
|
-
</View>
|
|
92
|
-
);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
const handlePasswordSubmit = () => {
|
|
96
|
-
setPassword(inputPassword);
|
|
97
|
-
setIsPasswordRequired(false);
|
|
98
|
-
setErrorMsg("");
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
return (
|
|
102
|
-
<View style={{ flex: 1, backgroundColor: colors.base100 }}>
|
|
103
|
-
<ScreenHeader
|
|
104
|
-
title={name || 'Document Viewer'}
|
|
105
|
-
subtitle={totalPages > 0 ? `Page ${currentPage} of ${totalPages}` : 'Loading...'}
|
|
106
|
-
onBack={() => router.back()}
|
|
107
|
-
/>
|
|
108
|
-
|
|
109
|
-
{isPasswordRequired ? (
|
|
110
|
-
<Container className="flex-1 justify-center items-center px-6">
|
|
111
|
-
<View className="p-6 rounded-3xl w-full" style={{ backgroundColor: colors.base200 }}>
|
|
112
|
-
<Txt variant="xl" className="font-bold text-center mb-2">Password Protected</Txt>
|
|
113
|
-
<Txt variant="base" className="text-center text-base-content/70 mb-6">
|
|
114
|
-
This document requires a password to open.
|
|
115
|
-
</Txt>
|
|
116
|
-
<Input
|
|
117
|
-
placeholder="Enter password"
|
|
118
|
-
value={inputPassword}
|
|
119
|
-
onChangeText={setInputPassword}
|
|
120
|
-
secureTextEntry
|
|
121
|
-
className="mb-4"
|
|
122
|
-
/>
|
|
123
|
-
{errorMsg ? (
|
|
124
|
-
<Txt variant="caption" className="text-error text-center mb-4">{errorMsg}</Txt>
|
|
125
|
-
) : null}
|
|
126
|
-
<Button
|
|
127
|
-
label="Unlock Document"
|
|
128
|
-
variant="primary"
|
|
129
|
-
isFullWidth
|
|
130
|
-
onPress={handlePasswordSubmit}
|
|
131
|
-
/>
|
|
132
|
-
</View>
|
|
133
|
-
</Container>
|
|
134
|
-
) : (
|
|
135
|
-
<View style={styles.pdfContainer}>
|
|
136
|
-
{!localUri && !downloadError ? (
|
|
137
|
-
<View className="flex-1 items-center justify-center w-full relative">
|
|
138
|
-
<View className="w-3/4 max-w-sm absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 bg-base-100 p-6 rounded-2xl shadow-lg border border-base-300">
|
|
139
|
-
<ProgressBar progress={downloadProgress * 100} label="Downloading Document" />
|
|
140
|
-
</View>
|
|
141
|
-
</View>
|
|
142
|
-
) : downloadError ? (
|
|
143
|
-
<View className="flex-1 items-center justify-center p-4">
|
|
144
|
-
<Ionicons name="close-circle-outline" size={48} color={colors.error} style={{ opacity: 0.8 }} />
|
|
145
|
-
<Txt variant="base" className="mt-4 text-center text-error font-semibold">
|
|
146
|
-
{downloadError}
|
|
147
|
-
</Txt>
|
|
148
|
-
<Button
|
|
149
|
-
label="Go Back"
|
|
150
|
-
variant="outline"
|
|
151
|
-
style={{ marginTop: 20 }}
|
|
152
|
-
onPress={() => router.back()}
|
|
153
|
-
/>
|
|
154
|
-
</View>
|
|
155
|
-
) : (
|
|
156
|
-
<Pdf
|
|
157
|
-
source={{ uri: localUri!, cache: false }}
|
|
158
|
-
password={password}
|
|
159
|
-
onLoadComplete={(numberOfPages) => {
|
|
160
|
-
setTotalPages(numberOfPages);
|
|
161
|
-
}}
|
|
162
|
-
onPageChanged={(page) => {
|
|
163
|
-
setCurrentPage(page);
|
|
164
|
-
}}
|
|
165
|
-
onError={(error: unknown) => {
|
|
166
|
-
const err = error as { message?: string };
|
|
167
|
-
if (err?.message?.toLowerCase().includes('password')) {
|
|
168
|
-
setIsPasswordRequired(true);
|
|
169
|
-
setErrorMsg('Incorrect password. Please try again.');
|
|
170
|
-
}
|
|
171
|
-
}}
|
|
172
|
-
trustAllCerts={false}
|
|
173
|
-
style={[styles.pdf, { backgroundColor: colors.base200 }]}
|
|
174
|
-
/>
|
|
175
|
-
)}
|
|
176
|
-
</View>
|
|
177
|
-
)}
|
|
178
|
-
</View>
|
|
179
|
-
);
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
const styles = StyleSheet.create({
|
|
183
|
-
pdfContainer: {
|
|
184
|
-
flex: 1,
|
|
185
|
-
justifyContent: "flex-start",
|
|
186
|
-
alignItems: "center",
|
|
187
|
-
},
|
|
188
|
-
pdf: {
|
|
189
|
-
flex: 1,
|
|
190
|
-
width: Dimensions.get("window").width,
|
|
191
|
-
height: Dimensions.get("window").height,
|
|
192
|
-
},
|
|
193
|
-
});
|