-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrowPage.js
413 lines (388 loc) · 17.5 KB
/
ArrowPage.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
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import React, { Component, Fragment } from 'react';
import {
View,
SafeAreaView,
Text,
PixelRatio,
TextInput,
TouchableOpacity,
ScrollView
} from 'react-native';
// import Swipeout from 'react-native-swipeout';
import Qs from 'qs'; // use encode false when creating query arguments
import Icon from 'react-native-vector-icons/MaterialIcons';
import PlaceInfoIcon from 'react-native-vector-icons/SimpleLineIcons';
import AutoHeightImage from 'react-native-auto-height-image';
import ArrowPageModel from './ArrowPageModel';
import SwipeNavigationPageModel from './SwipeNavigationPageModel';
import { GooglePlacesAutocomplete } from 'react-native-google-places-autocomplete';
import { scale, moderateScale } from 'react-native-size-matters';
import { inject, observer } from 'mobx-react';
import { toJS, reaction } from 'mobx';
import Utils from './Utils';
import PlacePageModel from './PlacePageModel';
import Pagination from './Pagination';
import * as Animatable from 'react-native-animatable';
import Geolocation from 'react-native-geolocation-service';
let self;
class ArrowPage extends Component {
static values = {
iconSize: 25
}
static navigationOptions = ({ navigation }) => {
const { state } = navigation;
// const title = state.params ? `${state.params.title}` : 'Arrow';
return {
title: 'Arrow',
headerRight: ArrowPage.headerRight(navigation),
};
};
static headerRight(navigation) {
const { iconSize } = ArrowPage.values;
return (
<View
style={{
alignItems: 'center',
justifyContent: 'space-evenly',
width: scale(100),
height: '100%',
flexDirection: 'row',
paddingRight: scale(6),
}}
>
{this.renderPlaceInfoButton(navigation, iconSize)}
<TouchableOpacity
onPress={() => {
navigation.navigate('Settings');
}}
>
<Icon
name={'settings'}
size={iconSize}
style={{ width: iconSize, height: iconSize }}
/>
</TouchableOpacity>
</View>
);
}
static renderPlaceButton(navigation, iconSize) {
const shouldRender = ArrowPageModel.getInstance().isShowingDirection;
return shouldRender ? (
<TouchableOpacity
onPress={() => {
navigation.navigate('Place');
}}
>
<Icon
name={'place'}
size={iconSize}
style={{ width: iconSize, height: iconSize }}
/>
</TouchableOpacity>
) : (
<View style={{ width: iconSize, height: iconSize }} /> // render empty on the area so that settings button don't replaces itself
);
}
static renderPlaceInfoButton(navigation, iconSize) {
const shouldRender = SwipeNavigationPageModel.getInstance().showPlaceInfoButton;
return shouldRender ? (
<Animatable.View ref={(ref) => ArrowPage.animateAndDisappear(ref)} >
<TouchableOpacity
onPress={() => {
self.navigateInfoPlace();
}}
>
<PlaceInfoIcon
name={'info'}
size={iconSize}
style={{ width: iconSize, height: iconSize }}
/>
</TouchableOpacity>
</Animatable.View>
)
:
ArrowPage.renderPlaceButton(navigation, iconSize);
}
static avoidCancel = [];
static animateAndDisappear(ref) {
if (ref) {
const avoidCancel = ArrowPage.avoidCancel;
avoidCancel.push(ref);
ref.flash(2000).then((fulfilled) => {
});
ref.bounceIn(3000).then(() => {
setTimeout(() => {
const index = avoidCancel.indexOf(ref);
avoidCancel.splice(index, 1);
if (avoidCancel.length === 0) { // cancel with time only when it has not already been cancelled by long hold on new item
SwipeNavigationPageModel.getInstance().showPlaceInfoButton = false;
}
}, 1500);
});
}
}
constructor() {
super();
self = this;
}
componentDidMount() {
this.willBlurListener = this.props.navigation.addListener('willBlur', () => {
console.log('will blur');
});
this.didBlurListener = this.props.navigation.addListener('didBlur', () => {
// SwipeNavigationPageModel.getInstance().tabBarSwipeEnabled = false; did not work
SwipeNavigationPageModel.getInstance().showListViewOnReturn = false;
this.googlePlacesAutocomplete._onBlur();
});
this.willFocusListener = this.props.navigation.addListener('willFocus', () => {
console.log('willFocus: ' + this.props.swipeNavigationPageModel.showListViewOnReturn);
if (this.props.swipeNavigationPageModel.showListViewOnReturn) { // if placepage user should return to listview again if it was opened
this.googlePlacesAutocomplete.listViewDisplayed = true;
this.googlePlacesAutocomplete.triggerFocus();
}
SwipeNavigationPageModel.getInstance().index = 1;
});
this.reactionOnShowPlaceButton = reaction(() => this.props.swipeNavigationPageModel.showPlaceInfoButton, (show, reaction) => {
this.updateHeaderRight();
}, {});
this.reactionOnIsShowingDirection = reaction(() => this.props.arrowPageModel.isShowingDirection, (show, reaction) => {
this.updateHeaderRight();
}, {});
/* need to fix swipe out issue: https://github.com/mobxjs/mobx-react/issues/510
this.reactionOnScrollEnabled = reaction(() => this.props.swipeNavigationPageModel.scrollEnabled, (scrollEnabled, reaction) => {
console.log('reaction');
const { setParams } = this.props.navigation;
setParams({ swipeEnabled: scrollEnabled });
}, {});
*/
}
componentWillUnmount() {
this.didBlurListener.remove();
this.willFocusListener.remove();
this.didFocusListener.remove();
this.reactionOnShowPlaceButton();
this.reactionOnIsShowingDirection();
// this.reactionOnScrollEnabled();
}
updateHeaderRight() {
const { setParams } = this.props.navigation;
// setParams({ title });
setParams({});
}
navigateInfoPlace() { // show listview on return if it was open
const showListViewOnReturn = this.googlePlacesAutocomplete.state.listViewDisplayed;
this.props.navigation.navigate('Place', { pageType: 'infoPlace', showListViewOnReturn });
}
renderSwipeoutButtons(data) {
console.log('render swipeoutButtons'); // https://github.com/dancormier/react-native-swipeout/issues/327
/*
return [
{
text: 'press',
onPress: () => {
console.log('rowData: ' + JSON.stringify(rowData));
fetch('https://maps.googleapis.com/maps/api/place/details/json?' + Qs.stringify({
key: Utils.getInstance().key,
placeid: rowData.place_id
// language: this.props.query.language,
})).then((response) => response.json()).then((details) => {
console.log('details: ' + JSON.stringify(details));
});
// console.log('details: ' + JSON.stringify(this.selectedItem.details));
}
}
];*/
return [
{
component: (
<View style={{ flex: 1, justifyContent: 'center', alignSelf: 'center' }}>
<TouchableOpacity
onPress={() => {
fetch('https://maps.googleapis.com/maps/api/place/details/json?' + Qs.stringify({
key: Utils.getInstance().key,
placeid: data.place_id,
language: 'en'
})).then(response => response.json()).then(details => {
PlacePageModel.getInstance().setPlace({ data, details: details.result });
this.navigateInfoPlace();
});
}}
>
<PlaceInfoIcon name={'info'} size={26} />
</TouchableOpacity>
</View>
)
}
];
}
render() {
return (
<ScrollView
//style={{ flex: 1 }} // had to change back to scroll view after removing react-native-swiper so that deselecting textinput works
contentContainerStyle={{ flex: 1 }}
keyboardDismissMode={'none'}
keyboardShouldPersistTaps={'handled'} // neccesary to prevent listview items to have to be pressed twice
scrollEnabled={false}
>
<GooglePlacesAutocomplete
ref={(ref) => { this.googlePlacesAutocomplete = ref; }}
placeholder='search'
textInputProps={{ onFocus: () => {
Geolocation.getCurrentPosition((position) => {
console.log('position: ' + JSON.stringify(position));
const pageModel = this.props.arrowPageModel;
pageModel.location.latitude = position.coords.latitude;
pageModel.location.longitude = position.coords.longitude;
}, (error) => {
console.log(error);
console.warn(error);
});
},
clearButtonMode: 'never',
spellCheck: false,
autoCorrect: false,
rejectResponderTermination: true
}}
query={{
key: Utils.getInstance().key,
radius: this.props.arrowPageModel.getRadius(),
location: this.props.arrowPageModel.getLocationAsString(),
strictbounds: this.props.arrowPageModel.getRadius() ? 'strictbounds' : undefined,
sessiontoken: 'aqse34fr5hnj78l9g4s2svfbm377912kde'
}}
/* can't use this
https://github.com/FaridSafi/react-native-google-places-autocomplete/issues/373
*/
GooglePlacesSearchQuery={{
location: '57.708870,11.974560', // Burggrevegatan, 41103 Göteborg (Stampen) Stampen, Göteborg Göteborg Sverige
radius: this.props.arrowPageModel.getRadius()
// types: 'cafe'
}}
styles={{
// to place suggestions on top
container: {
flex: 1,
position: 'absolute',
width: '100%',
zIndex: 1,
},
listView: {
flex: 1,
backgroundColor: 'white'
},
// just for styling
textInputContainer: {
backgroundColor: 'white',
height: 44,
borderTopColor: '#7e7e7e',
borderBottomColor: '#b5b5b5',
borderTopWidth: 1 / PixelRatio.get(),
borderBottomWidth: 1 / PixelRatio.get(),
flexDirection: 'row',
},
row: {
justifyContent: 'flex-end',
alignItems: 'center',
backgroundColor: '#f9f9f9'
},
poweredContainer: {
justifyContent: 'flex-end',
alignItems: 'center',
backgroundColor: '#efefef',
}
}}
predefinedPlaces={this.props.arrowPageModel.predefinedPlaces}
predefinedPlacesAlwaysVisible={true}
listViewDisplayed={false}
fetchDetails={true}
onPress={(data, details) => {
console.log('press');
console.log(JSON.stringify(data), JSON.stringify(details));
ArrowPageModel.getInstance().setDestination(details.geometry.location);
PlacePageModel.getInstance().setPlace({ data, details });
}}
onLongPress={(data, details) => {
console.log('long hold: ' + JSON.stringify(data), JSON.stringify(details));
SwipeNavigationPageModel.getInstance().showPlaceInfoButton = false; // cancel previous animation when long pressing fast on different items
PlacePageModel.getInstance().setPlace({ data, details }); // need to set
SwipeNavigationPageModel.getInstance().showPlaceInfoButton = true;
}}
onClear={() => {
ArrowPageModel.getInstance().setDestination(null);
}}
renderSwipeoutButtons={(rowData) => this.renderSwipeoutButtons(rowData)}
buttonWidth={scale(55)}
// api={'GooglePlacesSearch'} can't use GooglePlacesSearch beacuse shows too many results: https://stackoverflow.com/questions/55440295/flatlist-with-position-absolute-dont-scroll
/>
<View style={{ height: 44, width: '100%' }} />
<View style={{ alignItems: 'flex-end', paddingTop: moderateScale(11), paddingRight: moderateScale(11) }}>
<TouchableOpacity
disabled={!this.props.arrowPageModel.isShowingDirection}
onPress={() => {
this.googlePlacesAutocomplete.setAddressText('');
ArrowPageModel.getInstance().setDestination('');
}}
>
<Icon name={'clear'} size={scale(33)} color={this.props.arrowPageModel.isShowingDirection ? 'black' : 'transparent'} />
</TouchableOpacity>
</View>
<View style={{ flex: 1, width: '93%', alignItems: 'center', alignSelf: 'center', justifyContent: 'center' }}>
<View style={{ height: scale(200) * (3 / 4) }}>
<AutoHeightImage
source={require('./arrow.png')}
width={scale(200)}
style={{
transform: [{ rotate: this.props.arrowPageModel.rotate }]
}}
/>
</View>
<Text style={{ fontSize: moderateScale(20) }}>{this.props.arrowPageModel.distance} meters</Text>
</View>
<View style={{ width: '93%', alignSelf: 'center' }}>
<View style={{ flexDirection: 'row', justifyContent: 'space-between' }}>
<Text>Left</Text>
<Text>Right</Text>
</View>
</View>
</ScrollView>
);
}
/*
total is amount of tabs in AppNavigation.js .wont be using tabs to due swipe problem
<Pagination total={2} index={this.props.swipeNavigationPageModel.index} />
*/
}
export default inject('arrowPageModel', 'swipeNavigationPageModel')(observer(ArrowPage));
/**
* in googleplacesautcomplete
* make following:
* setAddressText = address => {
this.setState({ text: address })
if(!address) {
this.props.onClear();
}
}
textinput:
onChangeText={(text) => {
this._handleChangeText(text);
if (!text) {
this.props.onClear();
console.log('text cleared');
}
}}
so that unselect/clear is detected
onsubmitediting should close listview:
onSubmitEditing={() => {
this.props.onSubmitEditing()
this.setState({listViewDisplayed: false});
}}
hiding listview when keyboard dismissed:
_keyboardDidHide() {
console.log('hid keyboard');
this.setState({ listViewDisplayed: false });
componentWillMound
this.keyboardDidHideListener = Keyboard.addListener('keyboardDidHide', this._keyboardDidHide);
componentWillUnmount
this.keyboardDidHideListener.remove();
}
*/