-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathApp.js
145 lines (121 loc) · 3.61 KB
/
App.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
142
143
144
145
import React, { Component } from 'react';
import {
View,
Dimensions,
AppRegistry
} from 'react-native';
import { ARKit } from 'react-native-arkit';
// redux
import { Provider, connect } from 'react-redux';
import store from './redux/store';
import {
selectAnchors,
selectCurrentAnchor,
selectPaintingPosition,
selectShouldShowRegularPreview,
selectShouldShowAlternativePreview
} from './redux/reducers';
import { PaintingActionCreators } from './redux/actions';
// components
import Painting from './app/Painting';
import Controls from './app/Controls';
import PaintingsList from './app/PaintingsList';
import { Preview, PreviewAlternative } from './app/Preview';
// helpers
const screen = Dimensions.get('window');
// redux map
const mapStateToProps = state => ({
anchors: selectAnchors(state),
currentAnchor: selectCurrentAnchor(state),
paintingPosition: selectPaintingPosition(state),
showRegularPreview: selectShouldShowRegularPreview(state),
showAlternativePreview: selectShouldShowAlternativePreview(state)
});
const mapDispatchToProps = dispatch => ({
updateAnchorsInfo(anchors) {
dispatch(PaintingActionCreators.updateAnchorsInfo(anchors));
},
checkIfPaintingAllowed(canHangAPainting) {
dispatch(PaintingActionCreators.checkIfPaintingAllowed(canHangAPainting));
},
});
class ReactNativeARKit extends Component {
onPlaneDetected = (anchor) => {
this.updateAnchorInfo(anchor);
this.checkWetherPossibleToHangAPainting(anchor);
}
onPlaneUpdated = (anchor) => {
this.updateAnchorInfo(anchor);
}
updateAnchorInfo = (anchor) => {
const {
anchors,
updateAnchorsInfo
} = this.props;
anchors[anchor.id] = anchor;
updateAnchorsInfo(anchors);
}
checkWetherPossibleToHangAPainting = async (anchor) => {
const { checkIfPaintingAllowed } = this.props;
const point = {
x: screen.width / 2,
y: screen.height / 2
};
const result = await ARKit.hitTestPlanes(
point,
ARKit.ARHitTestResultType.ExistingPlaneUsingExtent
);
const canHangAPainting = !!(result &&
result.results &&
result.results.length);
checkIfPaintingAllowed(canHangAPainting);
}
_setActiveImage = (image) => {
this.setState({activeImage: image});
}
render() {
const {
currentAnchor,
paintingPosition,
showRegularPreview,
showAlternativePreview
} = this.props;
return (
<Provider store={store}>
<View style={{ flex: 1 }}>
<ARKit
style={{ flex: 1 }}
planeDetection={ARKit.ARPlaneDetection.Vertical}
lightEstimationEnabled
onPlaneDetected={this.onPlaneDetected}
onPlaneUpdated={this.onPlaneUpdated}
onAnchorDetected={this.onPlaneDetected}
onAnchorUpdated={this.onPlaneUpdated}
onARKitError={console.log} // if arkit could not be initialized (e.g. missing permissions), you will get notified here
>
{!!currentAnchor && !!paintingPosition && <Painting />}
</ARKit>
{showRegularPreview && <Preview />}
{showAlternativePreview && <PreviewAlternative />}
{
!!currentAnchor &&
<Controls>
<PaintingsList />
</Controls>
}
</View>
</Provider>
);
}
}
ReactNativeARKit = connect(mapStateToProps, mapDispatchToProps)(ReactNativeARKit);
class App extends Component {
render() {
return (
<Provider store={store}>
<ReactNativeARKit />
</Provider>
);
}
}
export default App;