-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathApp.js
More file actions
389 lines (287 loc) · 9.15 KB
/
Copy pathApp.js
File metadata and controls
389 lines (287 loc) · 9.15 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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
// react and react native imports
import React, { Component } from 'react';
import { View, FlatList, TouchableOpacity, Text, StyleSheet, StatusBar, Platform, Vibration } from 'react-native';
// the Flic2 module
import Flic2 from 'react-native-flic2';
// icons
import { FontAwesomeIcon } from '@fortawesome/react-native-fontawesome';
import { faPause, faPlay, faTrash, faEdit, faBatteryQuarter, faBatteryFull } from '@fortawesome/free-solid-svg-icons';
// plugins to make it more fancy
import prompt from 'react-native-prompt-android';
import { request as requestPermission, PERMISSIONS } from 'react-native-permissions';
import * as Animatable from 'react-native-animatable';
import Toast from 'react-native-root-toast';
export default class App extends Component {
constructor(props) {
// man
super(props);
// init state
this.state = {
buttons: [],
scanning: false,
};
// bindings
this.didReceiveButtonClickFunction = this.didReceiveButtonClick.bind(this);
this.onScanResultFunction = this.onScanResult.bind(this);
this.onInitializedFunction = this.onInitialized.bind(this);
}
componentDidMount() {
if (typeof Flic2.isInitialized === 'function' && Flic2.isInitialized() === true) {
this.onInitialized();
} else {
Flic2.addListener('managerInitialized', this.onInitializedFunction);
}
// listen bindings
Flic2.addListener('didReceiveButtonClick', this.didReceiveButtonClickFunction);
Flic2.addListener('scanResult', this.onScanResultFunction);
}
componentWillUnmount() {
// remove bindings
Flic2.removeListener('buttonEvent', this.handleButtonEventFunction);
Flic2.removeListener('scanResult', this.onScanResultFunction);
}
onInitialized() {
// connect to all known buttons
Flic2.connectAllKnownButtons();
// get the buttons
this.getButtons();
}
async getButtons() {
// async calls for init
this.setState({
buttons: await Flic2.getButtons(),
});
}
async forgetAllButtons() {
await Flic2.forgetAllButtons();
this.getButtons();
}
async startScan() {
// check os
if (Platform.OS === 'android') {
// on android we need the permission ACCESS_FINE_LOCATION first
// we are just going to assume the permission is granted after calling this
// in your real application, please create an actual permission check here
await requestPermission(PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION);
}
// set to scanning
this.setState({
scanning: true,
});
// go!
Flic2.startScan();
}
stopScan() {
this.setState({
scanning: false,
});
Flic2.stopScan();
}
connectButton(button) {
// connect it
button.connect(button);
// update our button list
this.getButtons();
}
disconnectButton(button) {
// disconnect it
button.disconnect(button);
// update our button list
this.getButtons();
}
forgetButton(button) {
// forget it
button.forget();
// update our button list
this.getButtons();
}
editButtonName(button) {
// use the prompt to change the name
prompt(
'Edit Flic nickname',
'Choose a name you will recognize',
[
{ text: 'Cancel', style: 'cancel' },
{
text: 'OK',
onPress: value => {
// save
button.setName(value);
// get new buttons
this.getButtons();
},
},
],
{
type: 'plain-text',
cancelable: true,
defaultValue: button.getName(),
}
);
}
onScanResult(data) {
if (data.event === 'completion') {
this.setState({
scanning: false,
});
// check
if (data.error === false) {
alert('The button has been added');
this.getButtons();
} else {
if (data.result === Flic2.constants.SCAN_RESULT_ERROR_ALREADY_CONNECTED_TO_ANOTHER_DEVICE) {
alert('This button is already connected to another device');
} else
if (data.result === Flic2.constants.SCAN_RESULT_ERROR_NO_PUBLIC_BUTTON_DISCOVERED) {
alert('No buttons found');
} else {
alert(`Could not connect\n\nError code: ${data.result}`);
}
}
}
}
didReceiveButtonClick(eventData) {
console.log('Received click event', eventData);
// update list
this.getButtons();
// do something with the click like showing a notification
Toast.show(`Button ${eventData.button.getName()} has been pressed ${eventData.button.getPressCount()} times`);
// wobble
// we do this extensive check because when you develop the app with live reload, the _logoRef will break.
if (typeof this._logoRef !== 'undefined' && this._logoRef !== null && typeof this._logoRef.wobble === 'function') {
// wobble wobble
this._logoRef.wobble();
}
// vibrate
Vibration.vibrate(200);
}
getBatteryIcon(batteryPercentage) {
if (batteryPercentage === true) {
return faBatteryFull;
} else {
return faBatteryQuarter;
}
}
render() {
return (
<View style={style.container}>
<StatusBar barStyle="light-content" />
{/* eslint-disable-next-line */}
<Animatable.Image ref={ image => this._logoRef = image } style={style.logo} useNativeDriver={true} source={require('./images/flic-logo.png')} />
{/* Scan button */}
{this.state.scanning === false ?
<TouchableOpacity onPress={this.startScan.bind(this)}>
<View style={style.button}><Text style={style.buttonText}>Start scan</Text></View>
</TouchableOpacity>
:
<TouchableOpacity onPress={this.stopScan.bind(this)}>
<View style={style.button}><Text style={style.buttonText}>Scanning... (click to cancel)</Text></View>
</TouchableOpacity> }
<TouchableOpacity onPress={this.forgetAllButtons.bind(this)}>
<View style={style.button}><Text style={style.buttonText}>Forget all buttons</Text></View>
</TouchableOpacity>
<View style={style.buttonContainer}>
<Text style={style.heading}>Button list:</Text>
{this.state.buttons.length > 0 ?
<FlatList
data={this.state.buttons}
keyExtractor={item => item.uuid}
renderItem={row => {
// define button
const button = row.item;
// eslint-disable-next-line react-native/no-inline-styles
return <View style={[style.listItem, { borderColor: button.getIsReady() ? '#006e1a' : '#b00000'}]}>
<FontAwesomeIcon style={style.icon} icon={this.getBatteryIcon(button.getBatteryLevelIsOk())} size={16} />
<Text style={style.pressCount}>{button.getPressCount()}</Text>
<Text style={style.listItemText}>{button.getName()}</Text>
<View style={style.icons}>
{button.getIsReady() === true ?
<TouchableOpacity onPress={this.disconnectButton.bind(this, button)}><FontAwesomeIcon icon={faPause} size={16} /></TouchableOpacity>
:
<TouchableOpacity onPress={this.connectButton.bind(this, button)}><FontAwesomeIcon icon={faPlay} size={16} /></TouchableOpacity>
}
<TouchableOpacity onPress={this.forgetButton.bind(this, button)}><FontAwesomeIcon icon={faTrash} size={16} /></TouchableOpacity>
<TouchableOpacity onPress={this.editButtonName.bind(this, button)}><FontAwesomeIcon icon={faEdit} size={16} /></TouchableOpacity>
</View>
</View>;
}}
/> : <Text>There are no buttons paired to this app. Click 'start scan' and hold your flic button to add a new button.</Text>}
</View>
</View>
);
}
}
// define stylesheet
const style = StyleSheet.create({
// container
container: {
paddingTop: 20,
padding: 10,
backgroundColor: '#45454d',
flex: 1,
},
// logo
logo: {
width: 100,
alignSelf: 'center',
resizeMode: 'contain',
},
// button
button: {
padding: 10,
borderRadius: 10,
backgroundColor: '#ff0089',
marginTop: 15,
},
buttonText: {
color: 'white',
textAlign: 'center',
fontSize: 20,
},
// button container
buttonContainer: {
padding: 10,
backgroundColor: 'rgba(255, 255, 255, 1)',
borderRadius: 10,
marginTop: 20,
},
// heading
heading: {
fontSize: 20,
fontWeight: 'bold',
marginBottom: 10,
},
// list item
listItem: {
padding: 20,
paddingLeft: 15,
paddingRight: 15,
flex: 1,
flexDirection: 'row',
justifyContent: 'flex-start',
alignItems: 'center',
borderRadius: 10,
marginBottom: 10,
backgroundColor: '#f3f9ff',
borderWidth: 2,
},
listItemText: {
flex: 1,
},
pressCount: {
width: 25,
color: 'rgba(40, 40, 40, 0.5)',
fontSize: 10,
},
// icons
icons: {
width: 70,
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
alignSelf: 'flex-end',
},
icon: {
marginRight: 7,
},
});