-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppWrapper.js
89 lines (81 loc) · 3.14 KB
/
AppWrapper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import React, { useEffect, useState } from 'react';
import App from './App';
import codePush from 'react-native-code-push';
import { Platform } from 'react-native';
import {
CODEPUSH_ANDROID_PRODUCTION_DEPLOYMENT_KEY,
CODEPUSH_IOS_PRODUCTION_DEPLOYMENT_KEY,
} from './src/utils/constants';
const codePushOptions = { checkFrequency: codePush.CheckFrequency.MANUAL };
const AppWithCodePush = () => {
const [progress, setProgress] = useState(false);
const [codePushType, setCodepushType] = useState('background');
const checkUpdateAvailable = async () => {
const update = await codePush.checkForUpdate();
console.log(update);
if (update) {
return true;
} else return false;
};
const syncCodePush = (type = 'immediate') => {
codePush.sync(
{
deploymentKey:
Platform.OS === 'ios'
? CODEPUSH_IOS_PRODUCTION_DEPLOYMENT_KEY
: CODEPUSH_ANDROID_PRODUCTION_DEPLOYMENT_KEY,
updateDialog: false,
installMode: type === 'immediate' ?
codePush.InstallMode.IMMEDIATE :
codePush.InstallMode.ON_NEXT_RESUME,
},
(syncStatus) => {
switch (syncStatus) {
//Can put log to see these stages and do some action meanwhile
case codePush.SyncStatus.CHECKING_FOR_UPDATE:
break;
case codePush.SyncStatus.DOWNLOADING_PACKAGE:
break;
case codePush.SyncStatus.INSTALLING_UPDATE:
break;
case codePush.SyncStatus.UPDATE_INSTALLED:
break;
}
}
);
}
useEffect(() => {
// This function should check if an update is available
codePush.getUpdateMetadata().then((metadata) => {
if (metadata.label == 'PERMIT') setCodepushType('permit');
else setCodepushType('background');
});
if (codePushType == 'permit') {
try {
const update = checkUpdateAvailable();
if (update) {
// This is to show loading state and prompt user if
// they want to install updates
setProgress(true);
if (codePushType === 'permit') {
//show dialog box and if presses on a button 'permit'
const dialog = true
if (dialog) { syncCodePush(); }
else {
//do something when no codepush downloaded here
}
}
}
} catch (error) {
console.error('CodePush update error:', error);
} finally {
setProgress(false);
}
}
else if (codePushType === 'background') {
syncCodePush('background');
}
}, []);
return <App />;
};
export default __DEV__ ? App : codePush(codePushOptions)(AppWithCodePush);