-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
index.ios.js
126 lines (120 loc) · 3.49 KB
/
index.ios.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
'use strict';
import React, { Component } from 'react';
import {
AppStateIOS,
AppRegistry,
StyleSheet,
Modal,
NavigatorIOS,
View,
Linking,
} from 'react-native';
import StoryActions from './actions/StoryActions';
import StoriesView from './views/StoriesView';
import AboutView from './views/AboutView';
import CommentsView from './views/CommentsView';
import colors from './colors';
const styles = StyleSheet.create({
container: {
flex: 1,
},
wrapper: {
backgroundColor: colors.viewBackgroundColor,
},
});
class App extends Component {
constructor(props){
super(props);
this.state = {
currentAppState: AppStateIOS.currentState,
isAboutVisible: false,
};
this._reloadCount = 0;
this._reloadCountTimeout = null;
this._handleAppStateChange = this._handleAppStateChange.bind(this);
this._handleOpenURL = this._handleOpenURL.bind(this);
}
componentDidMount(){
AppStateIOS.addEventListener('change', this._handleAppStateChange);
Linking.addEventListener('url', this._handleOpenURL);
}
componentWillUnmount(){
AppStateIOS.removeEventListener('change', this._handleAppStateChange);
Linking.removeEventListener('url', this._handleOpenURL);
}
_handleAppStateChange(currentAppState){
if (currentAppState == 'active' && this.state.currentAppState != currentAppState){
StoryActions.fetchStoriesIfExpired();
}
this.setState({
currentAppState,
});
}
_handleOpenURL(e){
const {url} = e;
if (!url) return;
const id = (url.match(/item\?id=([a-z\d]+)/i) || [,null])[1];
if (!id) return;
this.refs.nav.push({
component: CommentsView,
wrapperStyle: styles.wrapper,
passProps: {
data: {id}
},
});
}
_showAbout(){
this.setState({
isAboutVisible: true,
});
}
_hideAbout(){
this.setState({
isAboutVisible: false,
});
}
render(){
const {isAboutVisible} = this.state;
return (
<View style={styles.container}>
<View style={styles.container} pointerEvents={isAboutVisible ? 'none' : 'auto'}>
<NavigatorIOS
ref="nav"
style={styles.container}
initialRoute={{
title: 'HackerWeb',
backButtonTitle: 'News',
component: StoriesView,
leftButtonTitle: 'About',
onLeftButtonPress: this._showAbout.bind(this),
rightButtonIcon: require('./images/refresh-icon.png'),
onRightButtonPress: () => {
// For the compulsive-type of people who likes to press Reload multiple times
this._reloadCount++;
clearTimeout(this._reloadCountTimeout);
if (this._reloadCount >= 3){
StoryActions.flush();
this._reloadCount = 0;
} else {
this._reloadCountTimeout = setTimeout(() => this._reloadCount = 0, 3000);
}
StoryActions.fetchStories();
},
}}/>
</View>
<Modal animationType="slide" visible={isAboutVisible}>
<NavigatorIOS
style={styles.container}
initialRoute={{
title: 'About',
component: AboutView,
wrapperStyle: styles.wrapper,
leftButtonTitle: 'Close',
onLeftButtonPress: this._hideAbout.bind(this),
}}/>
</Modal>
</View>
);
}
}
AppRegistry.registerComponent('HackerWeb', () => App);