-
Notifications
You must be signed in to change notification settings - Fork 112
/
FABMenu.tsx
239 lines (212 loc) · 7.39 KB
/
FABMenu.tsx
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/// Floating Action Button component.
/// The main-menu
///
import React from 'react';
import {
Alert,
StyleSheet,
Platform
} from 'react-native';
import {
Icon,
Button,
SpeedDial
} from 'react-native-elements'
import { NavigationContext } from '@react-navigation/native';
import BackgroundGeolocation from "../react-native-background-geolocation";
import ENV from "../ENV";
import {COLORS, SOUNDS} from './lib/config';
import SettingsService from './lib/SettingsService';
const ACTION_BUTTON_OFFSET_Y = (Platform.OS === 'ios') ? 90 : 60;
interface Props {
onResetOdometer:Function
};
const FABMenu = (props:Props) => {
const [isOpen, setIsOpen] = React.useState(false);
const [isEmailingLog, setIsEmailingLog] = React.useState(false);
const [isResettingOdometer, setIsResettingOdometer] = React.useState(false);
const [isSyncing, setIsSyncing] = React.useState(false);
const [isDestroyingLocations, setIsDestroyingLocations] = React.useState(false);
const navigation = props.navigation;
const settingsService = SettingsService.getInstance();
/// FAB Menu handler.
const onClickMainMenu = (open:boolean) => {
setIsOpen(open);
settingsService.playSound((open) ? 'OPEN' : 'CLOSE');
}
/// FABItem handler.
const onClickAction = (command:string) => {
settingsService.playSound('BUTTON_CLICK');
switch(command) {
case 'settings':
settingsService.playSound('OPEN');
navigation.navigate('Settings');
return;
case 'resetOdometer':
resetOdometer();
break;
case 'emailLog':
emailLog();
break;
case 'sync':
sync();
break;
case 'destroyLocations':
destroyLocations();
break;
case 'requestPermission':
requestPermission();
break;
}
}
/// Reset the odometer.
const resetOdometer = async () => {
setIsResettingOdometer(true);
BackgroundGeolocation.setOdometer(0).then(location => {
setIsResettingOdometer(false);
if (props.onResetOdometer) {
props.onResetOdometer(location);
}
settingsService.toast('Reset odometer success');
}).catch(error => {
setIsResettingOdometer(false);
settingsService.toast('Reset odometer failure: ' + error);
});
}
/// Email the logs.
const emailLog = async () => {
// First fetch the email from settingsService.
settingsService.getEmail((email:string) => {
if (!email) { return; } // <-- [Cancel] returns null
// Confirm email
settingsService.yesNo('Email log', 'Use email address: ' + email + '?', () => {
// Here we go...
setIsEmailingLog(true);
BackgroundGeolocation.logger.emailLog(email).then((succes) => {
console.log('[emailLog] success');
setIsEmailingLog(false);
}).catch((error) => {
setIsEmailingLog(false);
settingsService.toast("Email log failure: " + error);
});
}, () => {
// User said [NO]: The want to change their email. Clear it and recursively restart the process.
settingsService.set('email', null);
emailLog();
});
});
}
/// Initiate HTTP Upload.
const sync = async () => {
const count = await BackgroundGeolocation.getCount();
if (!count) {
settingsService.alert('Locations database is empty');
return;
}
settingsService.confirm('Confirm Sync', 'Sync ' + count + ' records?', () => {
setIsSyncing(true);
BackgroundGeolocation.sync().then((rs) => {
settingsService.playSound('MESSAGE_SENT');
setIsSyncing(false);
}).catch((error:string) => {
settingsService.toast('Sync error: ' + error);
setIsSyncing(false);
});
});
}
/// Clear all queued locations from the plugin's SQLite database.
const destroyLocations = async () => {
const count = await BackgroundGeolocation.getCount();
if (!count) {
settingsService.toast('Locations database is empty');
return;
}
settingsService.confirm('Confirm Delete', 'Destroy ' + count + ' records?', () => {
setIsDestroyingLocations(true);
BackgroundGeolocation.destroyLocations().then(() => {
setIsDestroyingLocations(false);
settingsService.toast('Destroyed ' + count + ' records');
}).catch((error:string) => {
setIsDestroyingLocations(false);
settingsService.toast('Destroy locations error: ' + error, 'LONG');
});
});
}
/// Initiate permission request.
const requestPermission = async () => {
const providerState = await BackgroundGeolocation.getProviderState();
Alert.alert("Request Location Permission", `Current authorization status: ${providerState.status}`, [
{text: 'When in Use', onPress: () => {doRequestPermission('WhenInUse')}},
{text: 'Always', onPress: () => {doRequestPermission('Always')}},
], { cancelable: false });
}
const doRequestPermission = async (request) => {
await BackgroundGeolocation.setConfig({locationAuthorizationRequest: request});
const status = await BackgroundGeolocation.requestPermission();
console.log(`[requestPermission] status: ${status}`);
setTimeout(() => {
Alert.alert("Request Permission Result", `Authorization status: ${status}`, [
{text: 'Ok', onPress: () => {}},
], { cancelable: false });
}, 10);
}
return (
<SpeedDial
isOpen={isOpen}
color={COLORS.gold}
style={styles.speedDial}
icon={<Icon name="add-sharp" color={COLORS.black} type="ionicon" />}
openIcon={<Icon name="close-sharp" color={COLORS.black} type="ionicon" />}
onOpen={() => onClickMainMenu(true)}
onClose={() => onClickMainMenu(false)}
transitionDuration={0}
overlayColor="transparent"
>
<SpeedDial.Action
title="Destroy locations"
onPress={() => onClickAction('destroyLocations')}
icon={<Icon name="trash-sharp" type='ionicon' style={styles.itemIcon} />}
color={COLORS.gold}
/>
<SpeedDial.Action
icon={(!isSyncing) ? <Icon name="cloud-upload-sharp" type='ionicon' style={styles.itemIcon} /> : <Icon name="spinner" type='font-awesome' style={styles.itemIcon} />}
title="Sync"
onPress={() => onClickAction('sync')}
color={COLORS.gold}
/>
<SpeedDial.Action
title="Email logs"
onPress={() => onClickAction('emailLog')}
icon={<Icon name="mail-sharp" type='ionicon' style={styles.itemIcon} />}
color={COLORS.gold}
/>
<SpeedDial.Action
icon={(!isResettingOdometer) ? <Icon name="speedometer-sharp" type='ionicon' style={styles.itemIcon} /> : <Icon name="spinner" type='font-awesome' style={styles.itemIcon} />}
onPress={() => onClickAction('resetOdometer')}
title="Reset odometer"
color={COLORS.gold}
/>
<SpeedDial.Action
title="Request permission"
onPress={() => onClickAction('requestPermission')}
icon={<Icon name="lock-open-sharp" type='ionicon' style={styles.itemIcon} />}
color={COLORS.gold}
/>
<SpeedDial.Action
title="Config"
onPress={() => onClickAction('settings')}
icon={<Icon name="cog-sharp" type='ionicon' style={styles.itemIcon} />}
color={COLORS.gold}
/>
</SpeedDial>
)
}
export default FABMenu;
var styles = StyleSheet.create({
speedDial: {
bottom: ACTION_BUTTON_OFFSET_Y
},
itemIcon: {
fontSize: 24
}
});