Skip to content

Commit 170ceb0

Browse files
author
soliury
committed
refactor
1 parent e8a12ed commit 170ceb0

4 files changed

Lines changed: 48 additions & 22 deletions

File tree

src/components/ScrollableTabs.js

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
import React, {Component, PropTypes} from 'react';
2-
import {View, Dimensions, StyleSheet, TouchableOpacity, Text, Platform, ScrollView, Animated, ViewPagerAndroid} from 'react-native';
2+
import {
3+
View,
4+
Dimensions,
5+
StyleSheet,
6+
TouchableOpacity,
7+
Text,
8+
Platform,
9+
ScrollView,
10+
Animated,
11+
ViewPagerAndroid
12+
} from 'react-native';
313
import PureRenderMixin from 'react-addons-pure-render-mixin';
414

515

@@ -32,9 +42,9 @@ class ScrollableTabs extends Component {
3242
x: new Animated.Value(-offset)
3343
};
3444
this.state.x.addListener((e)=> {
35-
if (e.value % (this.space + tabNavItemWidth) == 0) {
45+
if (e.value % (this.space + tabNavItemWidth) === 0) {
3646
let index = Math.abs(e.value / (this.space + tabNavItemWidth));
37-
typeof this.props.onPageChangedAndAnimateEnd == 'function' && this.props.onPageChangedAndAnimateEnd(index);
47+
typeof this.props.onPageChangedAndAnimateEnd === 'function' && this.props.onPageChangedAndAnimateEnd(index, this._isScrolling());
3848
}
3949
});
4050
this._navs = {};
@@ -82,18 +92,31 @@ class ScrollableTabs extends Component {
8292
}
8393

8494

95+
_isScrolling() {
96+
return ()=> this._scrolling;
97+
}
98+
8599
_onScroll(e) {
86100
const {x} = e.nativeEvent.contentOffset;
87101
this._animateScroll(x);
88102
}
89103

90104

91105
_onMomentumScrollBegin(e) {
106+
this._scrolling = true;
107+
const offsetX = e.nativeEvent.contentOffset.x;
108+
this._animateScroll(offsetX);
109+
}
110+
111+
112+
_onMomentumScrollEnd(e) {
113+
this._scrolling = true;
92114
const offsetX = e.nativeEvent.contentOffset.x;
93115
const page = parseInt(offsetX / width, 10);
94116
this._animateScroll(offsetX);
95117
if (page !== this.index) {
96-
typeof this.props.onPageChanged == 'function' && this.props.onPageChanged(page);
118+
this._scrolling = false;
119+
typeof this.props.onPageChanged === 'function' && this.props.onPageChanged(page, this._isScrolling());
97120
}
98121
this.index = page;
99122
}
@@ -102,10 +125,10 @@ class ScrollableTabs extends Component {
102125
_onPageSelected(e) {
103126
const {position} = e.nativeEvent;
104127
this.index = position;
105-
if (position == undefined) {
106-
return
128+
if (position === undefined) {
129+
return;
107130
}
108-
typeof this.props.onPageChanged == 'function' && this.props.onPageChanged(position);
131+
typeof this.props.onPageChanged === 'function' && this.props.onPageChanged(position, this._isScrolling());
109132
}
110133

111134

@@ -133,7 +156,7 @@ class ScrollableTabs extends Component {
133156
_getActiveNavItemStyle(opacity) {
134157
return {
135158
borderTopColor: 'rgba(241,196,15,' + opacity + ')'
136-
}
159+
};
137160
}
138161

139162

@@ -155,7 +178,7 @@ class ScrollableTabs extends Component {
155178

156179
</View>
157180
</TouchableOpacity>
158-
)
181+
);
159182
});
160183
}
161184

@@ -168,8 +191,8 @@ class ScrollableTabs extends Component {
168191
style={ styles.page }>
169192
{ pageContent }
170193
</View>
171-
)
172-
})
194+
);
195+
});
173196
}
174197

175198

@@ -204,7 +227,7 @@ class ScrollableTabs extends Component {
204227
{ this._renderChildren() }
205228

206229
</ScrollView>
207-
)
230+
);
208231
}
209232
return (
210233
<ViewPagerAndroid
@@ -217,7 +240,7 @@ class ScrollableTabs extends Component {
217240
{ this._renderChildren() }
218241

219242
</ViewPagerAndroid>
220-
)
243+
);
221244
}
222245

223246

@@ -241,7 +264,7 @@ class ScrollableTabs extends Component {
241264
{ this._renderPageScroll() }
242265

243266
</View>
244-
)
267+
);
245268
}
246269
}
247270

src/layouts/Home.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ class Home extends Component {
1919
}
2020

2121

22-
_onPageChanged(page) {
22+
_onPageChanged(page, isScrolling) {
2323
const {actions, topic, ui} = this.props;
2424
const tab = Tabs.tabs[page];
2525
if (topic[tab] && ui[tab] && !ui[tab].flag) {
26-
actions.updateTopicsByTab(tab);
26+
setTimeout(()=> {
27+
isScrolling() && actions.updateTopicsByTab(tab);
28+
}, 16);
2729
}
2830
}
2931

@@ -89,5 +91,5 @@ export function mapStateToProps(state) {
8991
message: state.message,
9092
topic: state.topic,
9193
ui: state.home
92-
}
94+
};
9395
}

src/services/request.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import config from '../configs';
33

44

55
const urlPrefix = config.domain + config.apiPath;
6+
const isDebuggingInChrome = __DEV__ && !!window.navigator.userAgent;
67

78

89
function filterJSON(res) {
@@ -12,7 +13,7 @@ function filterJSON(res) {
1213

1314
function filterStatus(res) {
1415
if (res.status >= 200 && res.status < 300) {
15-
return res
16+
return res;
1617
}
1718
else {
1819
let error = new Error(res.statusText);
@@ -29,7 +30,7 @@ export function get(url, params) {
2930
url += `?${qs.stringify(params)}`;
3031
}
3132

32-
if (__DEV__) {
33+
if (isDebuggingInChrome) {
3334
console.info(`GET: `, url);
3435
console.info(`Params: `, params)
3536
}
@@ -43,9 +44,9 @@ export function get(url, params) {
4344
export function post(url, body) {
4445
url = urlPrefix + url;
4546

46-
if (__DEV__) {
47+
if (isDebuggingInChrome) {
4748
console.info(`POST: `, url);
48-
console.info(`Body: `, body)
49+
console.info(`Body: `, body);
4950
}
5051

5152
return fetch(url, {

src/store/configureStore.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ let middlewares = [
2727

2828

2929
if (isDebuggingInChrome) {
30-
middlewares.push(logger);
30+
// middlewares.push(logger);
3131
}
3232

3333

0 commit comments

Comments
 (0)