-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.android.js
141 lines (121 loc) · 3.27 KB
/
index.android.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
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
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
DeviceEventEmitter,
Alert
} from 'react-native';
import Beacons from 'react-native-beacons-manager';
import { createStore } from 'redux';
import { connect, Provider } from 'react-redux';
import Tabs from './Components/Tabs';
import SplashScreen from './Components/SplashScreen';
import store from './store';
async function establishBeacons(){
Beacons.detectIBeacons(); // Android only fn
//Beacons.requestWhenInUseAuthorization(); // ios only fn
const region = {
identifier: 'WGB',
uuid: 'EBD21AB7-C471-770B-E4DF-70EE82026A17'
};
try {
await Beacons.startRangingBeaconsInRegion(region.identifier, region.uuid);
console.log(`Beacons ranging started successfully`);
} catch (error) {
console.log(`Beacons ranging not started, error: ${error}`);
}
// Update app state with ordered detected beacons
// whenever beacons range
DeviceEventEmitter.addListener('beaconsDidRange', (data) => {
if(data.beacons.length > 0){
function compare(a,b) { //by distance
if (a.distance < b.distance)
return -1;
if (a.distance > b.distance)
return 1;
return 0;
}
data.beacons.sort(compare);
store.dispatch({type:'BEACONS_UPDATE', beacons:data.beacons});
}
});
}
establishBeacons();
ready = false;
async function getProjectsFromAPI() {
let response = await fetch('https://colmfyp.netsoc.co/projects.json');
let data = await response.json();
return data;
}
async function initProjects(){
try{
// Change this, but note - JSON endpoint MUST BE HTTPS!
let data = await getProjectsFromAPI();
// Get saved projects from storage
s = await AsyncStorage.getItem('savedProjects');
// if nothing found, use empty array instead
s = s === null ? [] : JSON.parse(s);
//repeat
d = await AsyncStorage.getItem('doneProjects');
d = d === null ? [] : JSON.parse(d);
//update state
store.dispatch({type:'PROJECTS_UPDATE', projects:data.projects});
store.dispatch({type:'ROOMS_UPDATE', rooms:data.rooms});
ready = true;
} catch(error) {
Alert.alert(
'Network Error',
'There\'s been an issue downloading event information. Make sure you\'re connected to the internet and try again.',
[
{text: 'Try Again', onPress: () => initProjects()},
],
{ cancelable: false }
);
}
}
initProjects();
export default class Mizen extends Component {
constructor(props) {
super(props);
this.state = {};
setTimeout(() => this.readyCheck(), 250); // Keep checking until it is!
}
readyCheck(){
this.setState({ready: ready});
if(!this.state.ready){
setTimeout(() => this.readyCheck(), 250);
}
}
render() {
if(this.state.ready){
return (
<Tabs store={store} />
);
} else {
return(<SplashScreen />);
}
}
}
const styles = StyleSheet.create({
container: {
flex: 1
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
AppRegistry.registerComponent('Mizen', () => Mizen);