-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
204 lines (185 loc) · 5.55 KB
/
index.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
* 手势解锁组件
* @author Leo Wang([email protected])
*/
import React, {Component} from 'react'
import {
Animated,
AsyncStorage,
View,
Text,
Dimensions
} from 'react-native';
var kCore = require('k-core');
var {
LifeStage,
errorHandler
} = kCore;
var screenWidth = Dimensions.get('window').width;
var styles = require('./styles');
var GridView = require('./GridView');
var STORAGE_KEY = '@k-react-native-swipe-unlock:saved-password';
class SwipeUnlocker extends Component {
constructor(props) {
super(props);
this.lifeStage = new LifeStage({
stages: [
'DEFAULT',
'SET', 'SET_AGAIN', 'SET_FAILED', 'SET_SUCCESSFUL',
'LOCKED', 'UNLOCK_ERROR', 'UNLOCKED'
]
});
this.initBehavior();
this.state = {
moved: new Animated.Value(0)
};
}
initBehavior() {
var me = this;
me.lifeStage.on('transfer', me.processStageTransferred.bind(me));
me.lifeStage.on('set_successful', (e) => {
AsyncStorage.setItem(STORAGE_KEY, me.password)
.then(() => {
me.lifeStage.switchTo('UNLOCKED');
})
.catch((error) => errorHandler(error.message))
.done();
});
me.lifeStage.on('unlocked', () => {
me.onUnlocked();
});
}
onUnlocked() {
var me = this;
Animated.timing( // 支持: spring, decay, timing,过渡的动画方式
me.state.moved,
{
toValue: screenWidth, // 目标值
duration: 150
}
).start(() => {
me.props.onUnlocked && me.props.onUnlocked();
}); // 开始
}
processStageTransferred(e) {
this.setState({
stage: e.to
});
}
componentWillMount() {
var me = this;
if (me.props.stage !== 'DEFAULT') {
me.lifeStage.switchTo(me.props.stage);
}
else {
AsyncStorage.getItem(STORAGE_KEY)
.then((value) => {
if (value !== null) {
// 获取到了,说明 set 过了
me.password = value;
me.lifeStage.switchTo('LOCKED');
}
else {
me.lifeStage.switchTo('SET');
}
})
.catch((error) => errorHandler(error.message))
.done();
}
}
onEachFinished(values) {
if (values.length === 0) {
return;
}
var currentStage = this.lifeStage.current();
switch (currentStage) {
case 'SET':
this.firstValue = values.join('-');
this.lifeStage.next();
case 'SET_FAILED':
this.firstValue = values.join('-');
this.lifeStage.switchTo('SET_AGAIN');
break;
case 'SET_AGAIN':
var secondValue = values.join('-');
if (secondValue === this.firstValue) {
this.password = secondValue;
this.lifeStage.switchTo('SET_SUCCESSFUL');
}
else {
this.lifeStage.switchTo('SET_FAILED');
}
break;
case 'LOCKED':
case 'UNLOCK_ERROR':
if (values.join('-') === this.password) {
this.lifeStage.switchTo('UNLOCKED');
}
else {
this.lifeStage.switchTo('UNLOCK_ERROR');
}
default:
break;
}
}
getMessage() {
var message = this.props.message || {};
switch(this.lifeStage.current()) {
case 'DEFAULT':
return '请稍候';
case 'SET':
return message.set || '摸一次密码';
case 'SET_AGAIN':
return message.setAgain || '再摸一次密码';
case 'SET_SUCCESSFUL':
return message.setSuccessful || '设置成功';
case 'SET_FAILED':
return message.setError || '两次摸密码不一致,请重新设置';
case 'LOCKED':
return message.locked || '先摸摸看解锁';
case 'UNLOCK_ERROR':
return message.unlockError || '解锁失败';
case 'UNLOCKED':
return message.unlocked || '解锁成功';
default:
return '亲,是不是用错了?不认识这个状态。'
}
}
getTitleStyle() {
switch(this.lifeStage.current()) {
case 'SET_SUCCESSFUL':
case 'UNLOCKED':
return styles.okTitle;
case 'SET_FAILED':
case 'UNLOCK_ERROR':
return styles.errorTitle;
default:
return styles.normalTitle;
}
}
render() {
return (
<Animated.View
style={[
styles.container,
{
transform: [
{translateY: this.state.moved}
]
}
]}>
<View style={styles.header}>
<Text style={[styles.title, this.getTitleStyle()]}>{this.getMessage()}</Text>
</View>
<GridView onFinished={this.onEachFinished.bind(this)}></GridView>
</Animated.View>
);
}
}
SwipeUnlocker.propTypes = {
stage: React.PropTypes.string
};
SwipeUnlocker.defaultProps = {
stage: 'DEFAULT'
};
module.exports = SwipeUnlocker;