react-native-3rddigital-appupdate 1.0.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.
@@ -0,0 +1,92 @@
1
+ import axios from 'axios';
2
+ import { Platform } from 'react-native';
3
+ import ReactNativeBlobUtil from 'react-native-blob-util';
4
+ import DeviceInfo from 'react-native-device-info';
5
+ import hotUpdate from 'react-native-ota-hot-update';
6
+ import { AppAlertDialog, type DialogOptions } from './AppAlertDialog';
7
+ import { AppLoader, type LoaderOptions } from './AppLoader';
8
+
9
+ const API_URL = 'https://dev.3rddigital.com/appupdate-api/api/';
10
+
11
+ export type OTAUpdateProps = {
12
+ key: string;
13
+ iosPackage: string;
14
+ androidPackage: string;
15
+ loaderOptions?: LoaderOptions;
16
+ dialogOptions?: Omit<DialogOptions, 'onConfirm' | 'onCancel'>;
17
+ };
18
+
19
+ export const checkOTAUpdate = async ({
20
+ key,
21
+ iosPackage,
22
+ androidPackage,
23
+ loaderOptions,
24
+ dialogOptions,
25
+ }: OTAUpdateProps) => {
26
+ try {
27
+ const response = await axios.get(
28
+ `${API_URL}projects/get-bundle?key=${key}&iosPackage=${iosPackage}&androidPackage=${androidPackage}`
29
+ );
30
+
31
+ const currentVersion = await hotUpdate.getCurrentVersion();
32
+ const data =
33
+ Platform.OS === 'android' ? response?.data?.android : response?.data?.ios;
34
+ const version = data?.version ?? 0;
35
+ const forceUpdate = data?.forceUpdate ?? false;
36
+ const url = data?.url ?? '';
37
+ const bundleId = data?.bundleId ?? '';
38
+
39
+ if (version <= currentVersion) return;
40
+
41
+ const downloadAndReport = () => {
42
+ AppLoader.show(loaderOptions);
43
+ hotUpdate.downloadBundleUri(ReactNativeBlobUtil, url, version, {
44
+ updateSuccess: () => {
45
+ axios
46
+ .post(
47
+ `${API_URL}bundles/${bundleId}/count`,
48
+ { status: 'success' },
49
+ { headers: { 'Content-Type': 'application/json' } }
50
+ )
51
+ .finally(() => AppLoader.hide());
52
+ },
53
+ updateFail: (error) => {
54
+ axios
55
+ .post(
56
+ `${API_URL}bundles/${bundleId}/count`,
57
+ {
58
+ status: 'failure',
59
+ error: JSON.stringify(error),
60
+ deviceInfo: {
61
+ model: DeviceInfo.getModel(),
62
+ brand: DeviceInfo.getBrand(),
63
+ systemName: DeviceInfo.getSystemName(),
64
+ systemVersion: DeviceInfo.getSystemVersion(),
65
+ },
66
+ },
67
+ { headers: { 'Content-Type': 'application/json' } }
68
+ )
69
+ .finally(() => AppLoader.hide());
70
+ },
71
+ restartAfterInstall: true,
72
+ restartDelay: 1000,
73
+ });
74
+ };
75
+
76
+ if (forceUpdate) {
77
+ downloadAndReport();
78
+ } else {
79
+ AppAlertDialog.showMessage({
80
+ title: 'Update Available!',
81
+ message: 'A newer version is ready to install.',
82
+ confirmText: 'Update',
83
+ cancelText: 'Cancel',
84
+ onConfirm: downloadAndReport,
85
+ onCancel: () => {},
86
+ ...dialogOptions,
87
+ });
88
+ }
89
+ } catch (err) {
90
+ console.warn('OTA update check failed:', err);
91
+ }
92
+ };
package/src/index.tsx ADDED
@@ -0,0 +1,2 @@
1
+ export { checkOTAUpdate, type OTAUpdateProps } from './checkOTAUpdate';
2
+ export { OTAProvider } from './OTAProvider';