Skip to content
This repository was archived by the owner on Nov 12, 2019. It is now read-only.

Commit 229fe3d

Browse files
committed
add 瞎推荐 page
1 parent 2339f5b commit 229fe3d

File tree

14 files changed

+159
-73
lines changed

14 files changed

+159
-73
lines changed

js/App.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import React from 'react';
88
import {Navigator} from 'react-native';
99
import {store} from './store/index';
1010
import MainPage from './containers/MainPage';
11-
import Test from './containers/MoreTab/GirlsPage'
11+
import Test from './containers/DiscoveryTab/GirlsPage'
1212

1313
export default class App extends React.Component{
1414
render(){

js/actions/requestHomeData.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import * as types from './actionTypes';
77
import fetchUrl from '../constants/fetchUrl';
8+
import fetchWithTimeout from '../utils/fetchWithTimeout';
89
import {getYesterdayFromDate} from '../utils/getDate';
910
import HomeDataDAO from '../dao/HomeDataDAO';
1011
import Toast from 'react-native-root-toast';
@@ -24,6 +25,12 @@ function receiveData(json, date){
2425
}
2526
}
2627

28+
function fetchFailure() {
29+
return {
30+
type: types.FETCH_HOME_DATA_FAILURE
31+
};
32+
}
33+
2734
function isValidData(responseData) {
2835
if(responseData.category.length > 0)
2936
return true;
@@ -33,13 +40,13 @@ function isValidData(responseData) {
3340
export function fetchData(date) {
3441
const url = fetchUrl.daily + date;
3542
return (dispatch) => {
36-
//dispatch(requestData());
43+
dispatch(requestData());
3744
var dao = new HomeDataDAO();
3845
dao.fetchLocalData(date).then((localData) => {
3946
Toast.show('已是最新数据了', {position: px2dp(-80)});
4047
dispatch(receiveData(localData, date));
4148
}, (localData)=>{
42-
fetch(url)
49+
fetchWithTimeout(5000, fetch(url))
4350
.then(response => response.json())
4451
.then(json => {
4552
if(isValidData(json)){
@@ -57,7 +64,10 @@ export function fetchData(date) {
5764
dispatch(receiveData(localData, date));
5865
}
5966
}
60-
});
67+
}).catch((error)=>{
68+
Toast.show('获取数据失败', {position: px2dp(-80)});
69+
dispatch(fetchFailure());
70+
});
6171
});
6272

6373
}

js/actions/requestRandomData.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import * as TYPES from './actionTypes';
77
import fetchUrl from '../constants/fetchUrl';
88
import fetchWithTimeout from '../utils/fetchWithTimeout';
9+
import RandomDataDAO from '../dao/RandomDataDAO';
910

1011
function fetchSuccess(json) {
1112
return {
@@ -45,6 +46,21 @@ function fetchMoreDataFailure() {
4546
};
4647
}
4748

49+
/**
50+
* for save cellular network data and improve performance,
51+
* this random data is only manually refreshed
52+
*/
53+
export function fetchLocalRandomData() {
54+
return (dispatch) => {
55+
let dao = new RandomDataDAO();
56+
dao.fetchLocalData().then((result)=>{
57+
dispatch(fetchSuccess(result));
58+
}, (error)=>{
59+
dispatch(fetchRandomData());
60+
});
61+
};
62+
}
63+
4864
export function fetchRandomData(isMoreData=false) {
4965
var results = [];
5066
const randomCategory = ['Android/1','iOS/1','前端/1','休息视频/1','拓展资源/1','App/1','瞎推荐/1'];
@@ -58,17 +74,20 @@ export function fetchRandomData(isMoreData=false) {
5874
results = results.concat(json.results);
5975

6076
if(index >= 10) {
61-
if (isMoreData)
77+
if (isMoreData) {
6278
dispatch(fetchMoreDataSuccess(results));
63-
else
79+
}else {
80+
let dao = new RandomDataDAO();
81+
dao.saveData(results);
6482
dispatch(fetchSuccess(results));
83+
}
6584
}else
6685
fetchCategoryData(dispatch);
6786
}).catch((error) => {
68-
if(isMoreData)
69-
dispatch(fetchMoreDataFailure());
70-
else
71-
dispatch(fetchFailure());
87+
if(isMoreData)
88+
dispatch(fetchMoreDataFailure());
89+
else
90+
dispatch(fetchFailure());
7291
});
7392
}
7493

js/components/ListViewForHome.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ class ListViewForHome extends Component{
2121
constructor(props){
2222
super(props);
2323
this.ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
24-
this.tabIcon = ['logo-android','logo-apple','logo-chrome','ios-film','ios-book','ios-apps'];
25-
this.tabColor = ['rgb(141,192,89)','#000','rgb(51,154,237)', 'rgb(154,53,172)','rgb(65,87,175)','rgb(249,89,58)'];
24+
this.tabIcon = ['logo-android','logo-apple','logo-chrome','ios-film','ios-book','ios-apps','ios-radio'];
25+
this.tabColor = ['rgb(141,192,89)','#000','rgb(51,154,237)', '#9370db','#00ced1','rgb(249,89,58)','#ffa500'];
2626
}
2727

2828
render(){
@@ -112,6 +112,8 @@ class ListViewForHome extends Component{
112112
return 4;
113113
case 'App':
114114
return 5;
115+
case '瞎推荐':
116+
return 6;
115117
}
116118
}
117119
}

js/containers/DiscoveryTab/index.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {bindActionCreators} from 'redux';
1010
import * as Actions from '../../actions/requestRandomData';
1111
import Icon from 'react-native-vector-icons/Ionicons';
1212
import TextListPage from './TextListPage';
13+
import GirlsPage from './GirlsPage';
1314
import theme from '../../constants/theme';
1415
import NavigationBar from '../../components/NavigationBar';
1516
import px2dp from '../../utils/px2dp';
@@ -20,22 +21,18 @@ import WebViewPage from '../../containers/WebViewPage';
2021
class DiscoveryFragment extends Component{
2122
constructor(props){
2223
super(props);
23-
this.tabNames = [['Android','iOS','前端'],['App','休息视频','拓展资源']];
24-
this.tabIcon = [['logo-android','logo-apple','logo-chrome'],['ios-apps','ios-film','ios-book']];
25-
this.tabColor = [['rgb(141,192,89)','#000','rgb(51,154,237)'],['rgb(249,89,58)','rgb(154,53,172)','rgb(65,87,175)']];
24+
this.tabNames = [['Android','iOS','前端','App'],['休息视频','拓展资源','瞎推荐','福利']];
25+
this.tabIcon = [['logo-android','logo-apple','logo-chrome','ios-apps'],['ios-film','ios-book','ios-radio','ios-images']];
26+
this.tabColor = [['rgb(141,192,89)','#000','rgb(51,154,237)','rgb(249,89,58)'],['#9370db','#00ced1','#ffa500','lightpink']];
2627
this.ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
2728
}
2829

29-
_fetchData(){
30-
this.props.actions.fetchRandomData();
31-
}
32-
3330
componentDidMount(){
34-
this._fetchData();
31+
this.props.actions.fetchLocalRandomData();
3532
}
3633

3734
_onRefresh(){
38-
this._fetchData();
35+
this.props.actions.fetchRandomData();
3936
}
4037

4138
_onEndReached(){
@@ -110,6 +107,14 @@ class DiscoveryFragment extends Component{
110107
)
111108
}
112109

110+
_renderBtnContent(i, index){
111+
return(
112+
<View style={{width:px2dp(50), height:px2dp(50), alignItems:'center', justifyContent:'center'}}>
113+
<Avatar icon={this.tabIcon[i][index]} width={px2dp(50)} backgroundColor={this.tabColor[i][index]}/>
114+
</View>
115+
);
116+
}
117+
113118
_renderFooter(){
114119
if(this.props.isRenderFooter) {
115120
return (
@@ -187,16 +192,11 @@ class DiscoveryFragment extends Component{
187192
return time.substring(0, 10);
188193
}
189194

190-
_renderBtnContent(i, index){
191-
return(
192-
<View style={{width:px2dp(60), height:px2dp(60), alignItems:'center', justifyContent:'center'}}>
193-
<Avatar icon={this.tabIcon[i][index]} width={px2dp(60)} backgroundColor={this.tabColor[i][index]}/>
194-
</View>
195-
);
196-
}
197-
198195
_itemPressCallback(title){
199-
this._pushScene(TextListPage, title);
196+
if(title === '福利')
197+
this._pushScene(GirlsPage, title);
198+
else
199+
this._pushScene(TextListPage, title);
200200
}
201201

202202
_pushScene(component, title){

js/containers/HomeTab/index.js

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -44,38 +44,46 @@ class HomeFragment extends Component{
4444
title="玩命加载中..."
4545
/>}
4646
>
47-
{/*prevent the length of data is equal to 0*/}
48-
{this.props.hasData && Info.getCategoryList(dataSource).length > 0 ?
49-
<View>
50-
<View style={{height: this.imageHeight, width: theme.screenWidth}}>
51-
<ImageView
52-
imgUrl={Info.getFuLiUrl(dataSource)}
53-
labelTime={this.props.dataTime}/>
54-
</View>
55-
<View style={styles.scrollContents}>
56-
{this.props.displayOrder.map((item, i) => {
57-
if(item !== '福利' && Info.getTargetList(dataSource, item) != null)
58-
return(
59-
<ListViewForHome
60-
key={i}
61-
navigator={this.props.navigator}
62-
dataSource={Info.getTargetList(dataSource, item)}
63-
headerTitle={item}/>
64-
);
65-
})}
66-
<View style={[styles.footer, {backgroundColor: rowItemBackgroundColor, borderTopColor: segmentColor}]}>
67-
<TouchableOpacity
68-
onPress={this._onPress.bind(this, 1)}
69-
activeOpacity={theme.touchableOpacityActiveOpacity}>
70-
<View style={styles.bottomBtn}>
71-
<Text style={styles.btnLabel}>没看够?试试往期干货吧</Text>
72-
</View>
73-
</TouchableOpacity>
74-
</View>
75-
</View>
47+
{(this.props.error && !this.props.hasData) ?
48+
<View style={styles.indicator}>
49+
<Text style={{color: this.props.tabIconColor}}>Ooops, 获取数据失败~ </Text>
7650
</View>
7751
:
78-
null
52+
((this.props.hasData && Info.getCategoryList(dataSource).length > 0) ?
53+
<View>
54+
<View style={{height: this.imageHeight, width: theme.screenWidth}}>
55+
<ImageView
56+
imgUrl={Info.getFuLiUrl(dataSource)}
57+
labelTime={this.props.dataTime}/>
58+
</View>
59+
<View style={styles.scrollContents}>
60+
{this.props.displayOrder.map((item, i) => {
61+
if (item !== '福利' && Info.getTargetList(dataSource, item) != null)
62+
return (
63+
<ListViewForHome
64+
key={i}
65+
navigator={this.props.navigator}
66+
dataSource={Info.getTargetList(dataSource, item)}
67+
headerTitle={item}/>
68+
);
69+
})}
70+
<View style={[styles.footer, {
71+
backgroundColor: rowItemBackgroundColor,
72+
borderTopColor: segmentColor
73+
}]}>
74+
<TouchableOpacity
75+
onPress={this._onPress.bind(this, 1)}
76+
activeOpacity={theme.touchableOpacityActiveOpacity}>
77+
<View style={styles.bottomBtn}>
78+
<Text style={styles.btnLabel}>没看够?试试往期干货吧</Text>
79+
</View>
80+
</TouchableOpacity>
81+
</View>
82+
</View>
83+
</View>
84+
:
85+
null
86+
)
7987
}
8088
</ScrollView>
8189
</View>
@@ -175,6 +183,13 @@ const styles = StyleSheet.create({
175183
btnLabel: {
176184
color: '#fff',
177185
fontSize: px2dp(16)
186+
},
187+
indicator: {
188+
flexDirection: 'row',
189+
width: theme.screenWidth,
190+
alignItems: 'center',
191+
justifyContent: 'center',
192+
marginTop: px2dp(20)
178193
}
179194
});
180195

@@ -184,6 +199,7 @@ const mapStateToProps = (state) => {
184199
hasData: state.homeData.hasData,
185200
dataSource: state.homeData.dataSource,
186201
dataTime: state.homeData.dataTime,
202+
error: state.homeData.error,
187203
mainThemeColor: state.settingState.colorScheme.mainThemeColor,
188204
pageBackgroundColor: state.settingState.colorScheme.pageBackgroundColor,
189205
rowItemBackgroundColor: state.settingState.colorScheme.rowItemBackgroundColor,

js/containers/MoreTab/index.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import ThemeColorPage from './ThemeColorPage';
2323
import OrderContentPage from './OrderContentPage';
2424
import AboutGankPage from './AboutGankPage';
2525
import AboutAuthorPage from './AboutAuthor/index';
26-
import GirlsPage from './GirlsPage';
2726

2827
class MoreFragment extends Component{
2928
constructor(props){
@@ -55,15 +54,13 @@ class MoreFragment extends Component{
5554
{this._renderTitleContent()}
5655
</TouchableHighlight>
5756
}
58-
<View style={[styles.block, {borderTopColor: segmentColor, borderBottomColor: segmentColor}]}>
59-
<RowItem title="福利" icon="md-images" iconColor='lightpink' renderSegment={false} onPress={this._itemClickCallback.bind(this, 1)}/>
60-
</View>
6157
<View style={[styles.block, {borderTopColor: segmentColor, borderBottomColor: segmentColor}]}>
6258
<RowItem title="首页内容展示顺序" icon="md-reorder" iconColor='lightskyblue' onPress={this._itemClickCallback.bind(this, 2)}/>
6359
<RowItem title="主题颜色" icon="ios-color-palette" iconColor={colors.orange} onPress={this._itemClickCallback.bind(this, 3)}/>
6460
{/*<RowItem title="选择语言 / Language" icon="md-globe" iconColor={colors.purple} onPress={this._itemClickCallback.bind(this, 3)}/>*/}
6561
<RowItemWithSwitcher title="夜间模式" icon="md-moon" iconColor="#7b68ee" switcherValue={isOpenNightMode} onValueChange={(value) => actions.changeNightMode(value)}/>
66-
<RowItemWithSwitcher title="显示列表缩略图" icon="md-browsers" iconColor='plum' switcherValue={isOpenThumbnail} onValueChange={(value) => actions.changeShowThumbnail(value)} renderSegment={false}/>
62+
<RowItemWithSwitcher title="显示列表缩略图" icon="md-browsers" iconColor='plum' switcherValue={isOpenThumbnail} onValueChange={(value) => actions.changeShowThumbnail(value)} />
63+
<RowItemWithSwitcher title="自动刷新首页数据" icon="md-refresh" iconColor='#ffd700' switcherValue={isOpenThumbnail} onValueChange={(value) => actions.changeShowThumbnail(value)} renderSegment={false}/>
6764
</View>
6865
<View style={[styles.block, {borderTopColor: segmentColor, borderBottomColor: segmentColor}]}>
6966
<RowItem title="关于作者" icon="md-happy" iconColor="#9acd32" renderSegment={false} onPress={this._itemClickCallback.bind(this, 4)}/>

js/dao/FavouriteDataDAO.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44
'use strict';
55

6-
const FAVOR_DATA = 'favorData';
6+
const FAVOR_DATA = '@FavorData';
77

88
import {AsyncStorage} from 'react-native';
99

js/dao/HomeDataDAO.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44
'use strict';
55

6-
const HOME_DATA = 'homeData';
6+
const HOME_DATA = '@HomeData';
77

88
import {AsyncStorage} from 'react-native';
99

@@ -54,8 +54,4 @@ export default class HomeDataDAO{
5454
});
5555
});
5656
}
57-
58-
removeLocalData(){
59-
AsyncStorage.removeItem(HOME_DATA, (error)=>{});
60-
}
6157
}

0 commit comments

Comments
 (0)