-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppInner.js
141 lines (126 loc) · 4.43 KB
/
AppInner.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
/* eslint-disable prettier/prettier */
import React, { useEffect, useRef } from 'react';
import {
SafeAreaView,
useColorScheme,
StyleSheet,
Platform,
} from 'react-native';
import {connect} from 'react-redux';
import {Colors} from 'react-native/Libraries/NewAppScreen';
import {
useReachability,
watchEvents,
} from 'react-native-watch-connectivity';
import NavBar from './src/components/NavBar';
import {
updateCurrentGameFromWatch,
} from './src/actions/CurrentGameActions/CurrentGameActions';
import {
setGamesFromWatch,
} from './src/actions/GameActions/GameActions';
import {
completeSet,
fixSetCountFromWatch,
} from './src/actions/SetActions/SetActions';
import {updateAppleWatchReachable} from './src/actions/WatchActions/WatchActions';
import analytics from '@react-native-firebase/analytics';
const App = ({
updateIsAppleWatchReachable,
setCurrentGameScoreFromWatch,
setGamesScoreFromWatch,
setSetScoreFromWatch,
sets,
fixSetCount,
}) => {
// We need to setup a reference to sets for the watch listener callback.
// Without it, the callback will only use the original sets object. If sets
// changes, the callback will net get the updates. Using a ref fixes that.
const stateRef = useRef();
stateRef.currentSets = sets;
// See if the apple watch is reachable
// eslint-disable-next-line react-hooks/rules-of-hooks
const reachable = Platform.OS === 'ios' ? useReachability() : false;
useEffect(() => {
updateIsAppleWatchReachable({isReachable: reachable});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [reachable]);
// Listen for communication from the apple watch
useEffect(() => {
if (Platform.OS !== 'ios') {
return;
}
watchEvents.addListener('message', async (messageFromWatch) => {
if (messageFromWatch.score) {
const score = JSON.parse(messageFromWatch.score);
await analytics().logEvent('watchUpdatingScore', {
messageFromWatch,
score,
});
setCurrentGameScoreFromWatch(score);
setGamesScoreFromWatch({home: score.homeTeamGames, visitor: score.visitorTeamGames});
const homeSetsSum = stateRef.currentSets.filter(set => set.isHomeWinner).length;
const visitorSetsSum = stateRef.currentSets.filter(set => !set.isHomeWinner).length;
if (homeSetsSum !== score.homeTeamSets || visitorSetsSum !== score.visitorTeamSets) {
let homeSets = [];
for (let i = 0; i < score.homeTeamSets; i++) {
homeSets.push({isHomeWinner: true, home: 0, visitor: 0});
}
let visitorSets = [];
for (let i = 0; i < score.visitorTeamSets; i++) {
visitorSets.push({isHomeWinner: false, home: 0, visitor: 0});
}
await analytics().logEvent('watchFixingSets', {
messageFromWatch,
score,
sets,
homeSets,
visitorSets,
});
fixSetCount({sets: [...homeSets, ...visitorSets]});
}
}
if (messageFromWatch.completeSet) {
const set = JSON.parse(messageFromWatch.completeSet);
await analytics().logEvent('watchCompletingSet', {
messageFromWatch,
set,
});
setSetScoreFromWatch({
home: set.home,
visitor: set.visitor,
isHomeWinner: set.isHomeWinner,
});
setGamesScoreFromWatch({home: 0, visitor: 0});
}
});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const isDarkMode = useColorScheme() === 'dark';
const backgroundStyle = {
backgroundColor: isDarkMode ? Colors.darker : Colors.lighter,
};
return (
<SafeAreaView style={[backgroundStyle, styles.safeAreaView]}>
<NavBar />
</SafeAreaView>
);
};
const styles = StyleSheet.create({
safeAreaView: {
flexGrow: 1,
justifyContent: 'center',
alignContent: 'center',
},
});
const mapStateToProps = state => ({
sets: state.sets.sets,
});
const mapDispatchToProps = dispatch => ({
updateIsAppleWatchReachable: ({isReachable}) => dispatch(updateAppleWatchReachable({isReachable})),
setCurrentGameScoreFromWatch: (payload) => dispatch(updateCurrentGameFromWatch(payload)),
setGamesScoreFromWatch: (payload) => dispatch(setGamesFromWatch(payload)),
setSetScoreFromWatch: (payload) => dispatch(completeSet(payload)),
fixSetCount: (payload) => dispatch(fixSetCountFromWatch(payload)),
});
export default connect(mapStateToProps, mapDispatchToProps)(App);