Skip to content

Commit

Permalink
add NavigationUtil for reset and change ToastUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard-Cao committed May 5, 2017
1 parent 5ba63dd commit bccecd8
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 37 deletions.
18 changes: 9 additions & 9 deletions app/pages/Category.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ import AV from 'leancloud-storage';
import store from 'react-native-simple-store';
import GridView from '../components/GridView';
import Button from '../components/Button';
import { toastShort } from '../utils/ToastUtil';
import ToastUtil from '../utils/ToastUtil';
import NavigationUtil from '../utils/NavigationUtil';

let tempTypeIds = [];
let maxCategory = 5; // 默认最多5个类别,远端可配置
Expand All @@ -46,7 +47,7 @@ class Category extends React.Component {
super(props);
this.renderItem = this.renderItem.bind(this);
this.onActionSelected = this.onActionSelected.bind(this);
this.resetRoute = this.resetRoute.bind(this);
this.routeMain = this.routeMain.bind(this);
this.onRefresh = this.onRefresh.bind(this);
this.state = {
typeIds: tempTypeIds
Expand Down Expand Up @@ -98,32 +99,31 @@ class Category extends React.Component {
}

onSelectCatagory() {
const { navigate } = this.props.navigation;
if (this.state.typeIds.length === 0) {
Alert.alert('提示', '您确定不选择任何分类吗?', [
{ text: '取消', style: 'cancel' },
{
text: '确定',
onPress: () => {
store.save('typeIds', this.state.typeIds);
navigate('Home');
NavigationUtil.reset(this.props.navigation, 'Home');
}
}
]);
} else {
store.save('typeIds', this.state.typeIds);
store.save('isInit', true);
navigate('Home');
NavigationUtil.reset(this.props.navigation, 'Home');
}
}

onActionSelected() {
if (tempTypeIds.length > maxCategory) {
toastShort(`不要超过${maxCategory}个类别哦`);
ToastUtil.showShort(`不要超过${maxCategory}个类别哦`);
return;
}
if (tempTypeIds.length < 1) {
toastShort('不要少于1个类别哦');
ToastUtil.showShort('不要少于1个类别哦');
}
const { navigate } = this.props.navigation;
InteractionManager.runAfterInteractions(() => {
Expand All @@ -135,12 +135,12 @@ class Category extends React.Component {
navigate('Main');
return;
}
store.save('typeIds', this.state.typeIds).then(this.resetRoute);
store.save('typeIds', this.state.typeIds).then(this.routeMain);
});
});
}

resetRoute() {
routeMain() {
const { navigate } = this.props.navigation;
DeviceEventEmitter.emit('changeCategory', this.state.typeIds);
navigate('Main');
Expand Down
6 changes: 3 additions & 3 deletions app/pages/Feedback.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { StyleSheet, TextInput, View, Keyboard } from 'react-native';
import AV from 'leancloud-storage';
import DeviceInfo from 'react-native-device-info';
import Icon from 'react-native-vector-icons/Ionicons';
import { toastShort } from '../utils/ToastUtil';
import ToastUtil from '../utils/ToastUtil';

let feedbackText;

Expand Down Expand Up @@ -56,7 +56,7 @@ class Feedback extends React.Component {

onActionSelected() {
if (feedbackText === undefined || feedbackText.replace(/\s+/g, '') === '') {
toastShort('请填写建议内容哦~');
ToastUtil.showShort('请填写建议内容哦~');
} else {
const feedback = AV.Object.new('Feedback');
feedback.set('manufacturer', DeviceInfo.getManufacturer());
Expand All @@ -66,7 +66,7 @@ class Feedback extends React.Component {
feedback.set('appVersion', DeviceInfo.getVersion());
feedback.set('feedback', feedbackText);
feedback.save();
toastShort('您的问题已反馈,我们会及时跟进处理');
ToastUtil.showShort('您的问题已反馈,我们会及时跟进处理');
this.textInput.clear();
Keyboard.dismiss();
}
Expand Down
4 changes: 2 additions & 2 deletions app/pages/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import ScrollableTabView, {
} from 'react-native-scrollable-tab-view';
import store from 'react-native-simple-store';
import LoadingView from '../components/LoadingView';
import { toastShort } from '../utils/ToastUtil';
import ToastUtil from '../utils/ToastUtil';
import { formatStringWithHtml } from '../utils/FormatUtil';

require('moment/locale/zh-cn');
Expand Down Expand Up @@ -103,7 +103,7 @@ class Main extends React.Component {
!nextProps.read.isRefreshing
) {
if (nextProps.read.noMore) {
toastShort('没有更多数据了');
ToastUtil.showShort('没有更多数据了');
const index = this.state.typeIds.indexOf(currentLoadMoreTypeId);
if (index >= 0) {
pages[index] -= 1;
Expand Down
8 changes: 2 additions & 6 deletions app/pages/Splash.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { Dimensions, Animated } from 'react-native';
import store from 'react-native-simple-store';
import { registerApp } from 'react-native-wechat';
import AV from 'leancloud-storage';
import { NavigationActions } from 'react-navigation';
import NavigationUtil from '../utils/NavigationUtil';

const maxHeight = Dimensions.get('window').height;
const maxWidth = Dimensions.get('window').width;
Expand Down Expand Up @@ -54,11 +54,7 @@ class Splash extends React.Component {
if (!isInit) {
navigate('Category', { isFirst: true });
} else {
const resetAction = NavigationActions.reset({
index: 0,
actions: [NavigationActions.navigate({ routeName: 'Home' })]
});
this.props.navigation.dispatch(resetAction);
NavigationUtil.reset(this.props.navigation, 'Home');
}
});
}, 1000);
Expand Down
17 changes: 8 additions & 9 deletions app/pages/WebViewPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import React from 'react';
import {
StyleSheet,
WebView,
BackAndroid,
BackHandler,
Dimensions,
Text,
Image,
Expand All @@ -31,7 +31,7 @@ import {

import * as WeChat from 'react-native-wechat';
import Icon from 'react-native-vector-icons/Ionicons';
import { toastShort } from '../utils/ToastUtil';
import ToastUtil from '../utils/ToastUtil';
import LoadingView from '../components/LoadingView';
import { formatStringWithHtml } from '../utils/FormatUtil';

Expand Down Expand Up @@ -70,11 +70,11 @@ class WebViewPage extends React.Component {

componentDidMount() {
this.props.navigation.setParams({ handleShare: this.onActionSelected });
BackAndroid.addEventListener('hardwareBackPress', this.goBack);
BackHandler.addEventListener('hardwareBackPress', this.goBack);
}

componentWillUnmount() {
BackAndroid.removeEventListener('hardwareBackPress', this.goBack);
BackHandler.removeEventListener('hardwareBackPress', this.goBack);
}

onActionSelected() {
Expand Down Expand Up @@ -134,10 +134,10 @@ class WebViewPage extends React.Component {
type: 'news',
webpageUrl: params.article.url
}).catch((error) => {
toastShort(error.message, true);
ToastUtil.showShort(error.message, true);
});
} else {
toastShort('没有安装微信软件,请您安装微信之后再试', true);
ToastUtil.showShort('没有安装微信软件,请您安装微信之后再试', true);
}
});
}}
Expand All @@ -162,10 +162,10 @@ class WebViewPage extends React.Component {
type: 'news',
webpageUrl: params.article.url
}).catch((error) => {
toastShort(error.message, true);
ToastUtil.showShort(error.message, true);
});
} else {
toastShort('没有安装微信软件,请您安装微信之后再试', true);
ToastUtil.showShort('没有安装微信软件,请您安装微信之后再试', true);
}
});
}}
Expand Down Expand Up @@ -204,7 +204,6 @@ class WebViewPage extends React.Component {
ref={(ref) => {
this.webview = ref;
}}
automaticallyAdjustContentInsets={false}
style={styles.base}
source={{ uri: params.article.url }}
javaScriptEnabled
Expand Down
6 changes: 3 additions & 3 deletions app/sagas/category.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import { put, take, call, fork } from 'redux-saga/effects';
import store from 'react-native-simple-store';
import * as types from '../constants/ActionTypes';
import { toastShort } from '../utils/ToastUtil';
import ToastUtil from '../utils/ToastUtil';
import { request } from '../utils/RequestUtil';
import { WEXIN_ARTICLE_TYPE } from '../constants/Urls';
import { fetchTypeList, receiveTypeList } from '../actions/category';
Expand All @@ -32,11 +32,11 @@ export function* requestTypeList() {
yield call(store.save, 'typeList', typeList.showapi_res_body.typeList);
const errorMessage = typeList.showapi_res_error;
if (errorMessage && errorMessage !== '') {
yield toastShort(errorMessage);
yield ToastUtil.showShort(errorMessage);
}
} catch (error) {
yield put(receiveTypeList([]));
yield toastShort('网络发生错误,请重试');
yield ToastUtil.showShort('网络发生错误,请重试');
}
}

Expand Down
6 changes: 3 additions & 3 deletions app/sagas/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import { put, take, call, fork } from 'redux-saga/effects';

import * as types from '../constants/ActionTypes';
import { toastShort } from '../utils/ToastUtil';
import ToastUtil from '../utils/ToastUtil';
import { request } from '../utils/RequestUtil';
import { WEXIN_ARTICLE_LIST } from '../constants/Urls';
import { fetchArticleList, receiveArticleList } from '../actions/read';
Expand All @@ -46,11 +46,11 @@ export function* requestArticleList(
);
const errorMessage = articleList.showapi_res_error;
if (errorMessage && errorMessage !== '') {
yield toastShort(errorMessage);
yield ToastUtil.showShort(errorMessage);
}
} catch (error) {
yield put(receiveArticleList([], typeId));
toastShort('网络发生错误,请重试');
ToastUtil.showShort('网络发生错误,请重试');
}
}

Expand Down
30 changes: 30 additions & 0 deletions app/utils/NavigationUtil.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
*
* Copyright 2016-present reading
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import { NavigationActions } from 'react-navigation';

const reset = (navigation, routeName) => {
const resetAction = NavigationActions.reset({
index: 0,
actions: [NavigationActions.navigate({ routeName })]
});
navigation.dispatch(resetAction);
};

export default {
reset
};
12 changes: 10 additions & 2 deletions app/utils/ToastUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,26 @@
*/
import { Alert, ToastAndroid, Platform } from 'react-native';

export const toastShort = (content, isAlert) => {
const showShort = (content, isAlert) => {
if (!content) {
return;
}
if (isAlert || Platform.OS === 'ios') {
Alert.alert('提示', content.toString());
} else {
ToastAndroid.show(content.toString(), ToastAndroid.SHORT);
}
};

export const toastLong = (content, isAlert) => {
const showLong = (content, isAlert) => {
if (isAlert || Platform.OS === 'ios') {
Alert.alert('提示', content.toString());
} else {
ToastAndroid.show(content.toString(), ToastAndroid.LONG);
}
};

export default {
showShort,
showLong
};

0 comments on commit bccecd8

Please sign in to comment.