Skip to content

Commit 4391c0a

Browse files
author
soliury
committed
refactor: can run with toast
1 parent 2a9662e commit 4391c0a

11 files changed

Lines changed: 215 additions & 7 deletions

File tree

src/actions/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import * as home from './home';
22
import * as user from './user';
3+
import * as utils from './utils';
34

45

56
export default {
67
...home,
7-
...user
8+
...user,
9+
...utils
810
};
911

1012

src/actions/utils.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { createAction } from 'redux-actions';
2+
import * as types from '../constants/ActionTypes';
3+
4+
5+
export const toast = createAction(types.TOAST, (text, timeout)=> {
6+
return {
7+
text,
8+
timeout,
9+
id: new Date().getTime()
10+
}
11+
});

src/components/base/Toast.js

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import React,{
2+
Component,
3+
PropTypes,
4+
View,
5+
StyleSheet,
6+
Text,
7+
Dimensions,
8+
Animated
9+
} from 'react-native';
10+
11+
const { height, width } = Dimensions.get('window');
12+
const toastWidth = width * 0.7;
13+
const defaultText = 'Toast';
14+
const defaultTimeout = 2000;
15+
16+
17+
class Toast extends Component {
18+
static propTypes = {
19+
duration: PropTypes.number
20+
};
21+
22+
23+
static defaultProps = {
24+
duration: 300
25+
};
26+
27+
28+
constructor(props) {
29+
super(props);
30+
this.state = {
31+
fadeAnim: new Animated.Value(0.4),
32+
show: false,
33+
text: defaultText,
34+
timeout: defaultTimeout
35+
};
36+
}
37+
38+
39+
componentWillUnmount() {
40+
clearTimeout(this.timeout);
41+
}
42+
43+
44+
show(text = defaultText, timeout = defaultTimeout) {
45+
const { duration } = this.props;
46+
Animated.timing(this.state.fadeAnim, {
47+
toValue: 1,
48+
duration: duration
49+
}).start();
50+
51+
this.setState({
52+
show: true,
53+
text,
54+
timeout
55+
});
56+
57+
this.timeout = setTimeout(()=> {
58+
Animated.timing(this.state.fadeAnim, {
59+
toValue: 0,
60+
duration: duration
61+
}).start(()=> {
62+
this.setState({
63+
show: false
64+
});
65+
});
66+
}, timeout - duration);
67+
}
68+
69+
70+
render() {
71+
const opacity = {
72+
opacity: this.state.fadeAnim
73+
};
74+
if (!this.state.show) return null;
75+
return (
76+
<Animated.View style={[styles.container, this.props.style, opacity]}>
77+
<Text style={styles.text}>
78+
{this.state.text}
79+
</Text>
80+
</Animated.View>
81+
)
82+
}
83+
}
84+
85+
const styles = StyleSheet.create({
86+
container: {
87+
position: 'absolute',
88+
backgroundColor: 'rgba(0,0,0,0.8)',
89+
borderRadius: 5,
90+
flexDirection: 'row',
91+
justifyContent: 'center',
92+
alignItems: 'center',
93+
width: toastWidth,
94+
left: (width - toastWidth) / 2,
95+
top: (height - 60) / 2,
96+
padding: 20
97+
},
98+
text: {
99+
flex: 1,
100+
color: 'white',
101+
fontSize: 16,
102+
textAlign: 'center'
103+
}
104+
});
105+
106+
107+
export default Toast;

src/constants/ActionTypes.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,9 @@ export const GET_USER_FROM_STORAGE = 'GET_USER_FROM_STORAGE';
1212
export const UPDATE_CLIENT_USER_INFO = 'UPDATE_CLIENT_USER_INFO';
1313
export const LOGOUT = 'LOGOUT';
1414
export const GET_USER_INFO = 'GET_USER_INFO';
15+
16+
17+
// utils
18+
export const TOAST = 'TOAST';
19+
export const OPEN_TOAST = 'OPEN_TOAST';
20+
export const CLOSE_TOAST = 'CLOSE_TOAST';

src/layouts/Home.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Home extends Component {
2424
source={{ uri: config.bgImgUri }}>
2525

2626
<Text onPress={()=>{
27-
this.props.router.toAbout();
27+
this.props.actions.toast('测试');
2828
}}>
2929
toAbout
3030
</Text>

src/layouts/QRCode.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class QRCode extends Component {
3131
VibrationIOS.vibrate();
3232
actions.checkToken(result.data, ()=> {
3333
router.pop();
34+
actions.toast('登陆成功');
3435
});
3536
router.pop();
3637
}

src/layouts/Utils.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,50 @@
11
import React,{
22
Component,
3-
View
3+
View,
4+
StyleSheet,
5+
Text
46
} from 'react-native';
7+
import Toast from '../components/base/Toast';
58

69

710
class Utils extends Component {
11+
constructor(props) {
12+
super(props);
13+
}
14+
15+
816
componentDidMount() {
917
const { actions } = this.props;
1018
//actions.getUserFromStorage();
1119
}
1220

1321

22+
componentWillReceiveProps(nextProps) {
23+
if (this.props.toast.id !== nextProps.toast.id) {
24+
this.toast.show(nextProps.toast.text, nextProps.toast.timeout);
25+
}
26+
}
27+
28+
1429
render() {
15-
return null;
30+
return (
31+
<Toast ref={ (view)=> this.toast=view } style={styles.container}/>
32+
);
1633
}
1734
}
1835

1936

37+
const styles = StyleSheet.create({
38+
container: {
39+
position: 'absolute'
40+
}
41+
});
42+
43+
2044
export const LayoutComponent = Utils;
2145
export function mapStateToProps(state) {
46+
const { utils = {} } = state;
2247
return {
23-
util: state.util
48+
...utils
2449
}
2550
}

src/reducers/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import { combineReducers } from 'redux';
22
import home from './home';
33
import user from './user';
44
import userUI from './userUI';
5+
import utils from './utils';
56

67
export default combineReducers({
78
home,
89
user,
9-
userUI
10+
userUI,
11+
utils
1012
});

src/reducers/utils.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import * as types from '../constants/ActionTypes';
2+
3+
4+
const initialState = {
5+
toast: {
6+
text: null,
7+
timeout: 2000,
8+
id: null
9+
}
10+
};
11+
12+
13+
export default function (state = initialState, action) {
14+
const { payload ={} } = action;
15+
switch (action.type) {
16+
case types.TOAST:
17+
return {
18+
...state,
19+
toast: {
20+
...state.toast,
21+
...payload
22+
}
23+
};
24+
default :
25+
return state;
26+
}
27+
}

src/store/configureStore.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ import { createStore, applyMiddleware } from 'redux';
22
import thunkMiddleware from 'redux-thunk';
33
import promiseMiddleware from './promiseMiddleware';
44
import asyncActionCallbackMiddleware from './asyncActionCallbackMiddleware';
5+
import utilsMiddleware from './utilsMiddleware';
56
import logger from 'redux-logger';
67
import reducers from '../reducers';
78

89

910
var middlewares = [
1011
thunkMiddleware,
1112
promiseMiddleware,
12-
asyncActionCallbackMiddleware
13+
asyncActionCallbackMiddleware,
14+
utilsMiddleware
1315
];
1416

1517

0 commit comments

Comments
 (0)