forked from credentials/irma_mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
197 lines (170 loc) · 5.35 KB
/
Copy pathindex.js
File metadata and controls
197 lines (170 loc) · 5.35 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import { Linking, AppState, StatusBar, Platform, NativeModules } from 'react-native';
const { IrmaVersion } = NativeModules;
import moment from 'moment';
import { initStore } from 'store';
import parseIrmaUrl from 'lib/parseIrmaUrl';
import ignoreWarnings from 'lib/ignoreWarnings';
import {
Navigation,
componentAppearanceChangedListener,
registerScreens,
setCredentialDashboardRoot,
setDefaultOptions,
setEnrollmentRoot,
showAppUnlockModal,
startSessionAndNavigate,
} from 'lib/navigation';
// Ignore specific deprecation warnings or harmless issues
if (__DEV__)
ignoreWarnings();
// Initialize store
const store = initStore();
// // Main entry point of application on boot
let hasSetInitialScreen = false;
Navigation.events().registerAppLaunchedListener( () => {
hasSetInitialScreen = false;
StatusBar.setBarStyle('light-content');
setDefaultOptions();
registerScreens();
componentAppearanceChangedListener();
handleInitialUrl();
AppState.addEventListener('change', appStateChangeListener);
store.subscribe(initialScreenStoreListener);
store.subscribe(minimumVersionStoreListener);
Linking.addEventListener('url', handleUrl);
});
// // Initialization helper functions
// Function that handles initial incoming url, and saves it in store
const handleInitialUrl = () => {
Linking.getInitialURL().then( irmaUrl => {
store.dispatch({
type: 'Navigation.SetInitialSessionPointer',
initialSessionPointer: parseIrmaUrl(irmaUrl),
});
}).catch( () => {
store.dispatch({
type: 'Navigation.SetInitialSessionPointer',
initialSessionPointer: null,
});
});
};
// Function that reponds to any non-initial incoming url
const handleUrl = (event) => {
const sessionPointer = parseIrmaUrl(event.url);
if (!sessionPointer)
return;
startSessionAndNavigate({sessionPointer, exitAfter: true, setRoot: false});
};
// Sets the initial screen as soon as the irmagobridge enrollment reducer
// has been loaded. Also set the unlockModal if necessary.
const initialScreenStoreListener = () => {
if (hasSetInitialScreen)
return;
const {
appUnlock: {
isAuthenticated,
},
enrollment: {
enrolledSchemeManagerIds,
loaded: enrollmentLoaded,
unenrolledSchemeManagerIds,
},
navigation: {
initialSessionPointerLoaded,
initialSessionPointer,
},
} = store.getState();
// Do nothing until enrollment and initial URL session pointer status has loaded
if (!enrollmentLoaded || !initialSessionPointerLoaded)
return;
// We'll be sure to set the initial screen now, so flag that
hasSetInitialScreen = true;
// If we should enroll, ignore any other circumstance and redirect to Enrollment
if (unenrolledSchemeManagerIds.length > 0) {
setEnrollmentRoot();
} else {
// If there was an initial session pointer, load up a stack with the session started
// Otherwise show the dashboard
if (initialSessionPointer)
startSessionAndNavigate({sessionPointer: initialSessionPointer, exitAfter: true, setRoot: true});
else
setCredentialDashboardRoot();
// Show AppUnlock modal on top of screen if not authenticated
if (enrolledSchemeManagerIds.length > 0 && !isAuthenticated)
showAppUnlockModal({showModalAnimation: false});
}
};
// Check version requirements
const minimumVersionStoreListener = () => {
const {
irmaConfiguration: {
loaded,
schemeManagers,
showingUpdate,
},
} = store.getState();
if (!loaded || showingUpdate)
return;
let minBuild = 0;
for (const scheme in schemeManagers) {
if (Platform.OS === 'android')
minBuild = Math.max(minBuild, schemeManagers[scheme].MinimumAppVersion.Android);
if (Platform.OS === 'ios')
minBuild = Math.max(minBuild, schemeManagers[scheme].MinimumAppVersion.IOS);
}
let myVersion = parseInt(IrmaVersion.build, 10);
if (Platform.OS === 'android') {
while (myVersion > 1024*1024)
myVersion -= 1024*1024;
}
if (minBuild > myVersion) {
store.dispatch({
type: 'IrmaConfiguration.ShowingUpdate',
});
}
};
// Event handler for becoming active (called from appStateChange)
const onAppBecomesActive = () => {
const {
enrollment: {
enrolledSchemeManagerIds,
},
appUnlock: {
lastForegroundedTime,
},
irmaConfiguration: {
lastUpdateTime,
},
} = store.getState();
// If we were active more than 5 minutes ago, show AppUnlock again (if enrolled)
if (moment(lastForegroundedTime).isBefore(moment().subtract(5, 'minutes'))) {
store.dispatch({
type: 'AppUnlock.Lock',
});
if (enrolledSchemeManagerIds.length > 0)
showAppUnlockModal({showModalAnimation: false});
}
// Update configuration when becoming active, but at most once every 6 hours
if (moment(lastUpdateTime).isBefore(moment().subtract(6, 'hours'))) {
store.dispatch({
type: 'IrmaBridge.UpdateSchemes',
});
}
}
// Function that listens for change in foreground/background appState,
// records that state and calls event handler when app changes to foreground
const appStateChangeListener = (appState) => {
const {
appUnlock: {
appState: previousAppState,
},
} = store.getState();
if (previousAppState !== 'active' && appState === 'active') {
onAppBecomesActive()
}
// Record the current state
store.dispatch({
type: 'AppUnlock.AppStateChanged',
appState,
});
};