-
Notifications
You must be signed in to change notification settings - Fork 584
/
Copy pathCustomKeyPage.js
184 lines (172 loc) · 5.45 KB
/
CustomKeyPage.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
/**
* 添加Trending语言,Popular 关键字
* @flow
* **/
import React, {Component} from 'react';
import {
StyleSheet,
ScrollView,
View,
Image,
Alert
} from 'react-native'
import CheckBox from 'react-native-check-box'
import NavigationBar from '../../common/NavigationBar'
import BaseCommon from '../../common/BaseCommon'
import LanguageDao,{FLAG_LANGUAGE} from '../../expand/dao/LanguageDao'
import ArrayUtils from '../../util/ArrayUtils'
import {MORE_MENU} from '../../common/MoreMenu'
import ViewUtils from '../../util/ViewUtils'
export default class CustomKeyPage extends Component {
constructor(props) {
super(props);
this.baseCommon=new BaseCommon({...props,backPress:(e)=>this.onBackPress(e)});
this.changeValues=[];
this.isRemoveKey=this.props.menuType===MORE_MENU.Remove_Key;
this.state = {
dataArray: []
}
}
componentDidMount() {
this.languageDao = new LanguageDao(this.props.flag);
this.loadData();
this.baseCommon.componentDidMount();
}
componentWillUnmount(){
this.baseCommon.componentWillUnmount();
}
onBackPress(e){
this.onBack();
return true;
}
loadData() {
this.languageDao.fetch().then((data)=> {
this.setState({
dataArray: data
})
}).catch((error)=> {
console.log(error);
});
}
onClick(data) {
if(!this.isRemoveKey)data.checked = !data.checked;
ArrayUtils.updateArray(this.changeValues,data)
}
onSave() {
if (this.changeValues.length ===0){
this.props.navigator.pop();
return;
}
if(this.isRemoveKey){
for(let i=0,l=this.changeValues.length;i<l;i++){
ArrayUtils.remove(this.state.dataArray,this.changeValues[i]);
}
}
this.languageDao.save(this.state.dataArray);
if(this.props.flag===FLAG_LANGUAGE.flag_language){
this.props.homeComponent.changedValues.my.languageChange=true;
}else{
this.props.homeComponent.changedValues.my.keyChange=true;
}
if(this.props.fromPage){
this.props.homeComponent.onReStart(this.props.fromPage)
}else {
this.props.navigator.pop();
}
}
onBack() {
if (this.changeValues.length > 0) {
Alert.alert(
'Confirm Exit',
'Do you want to save your changes before exitting?',
[
{
text: 'No', onPress: () => {
this.props.navigator.pop();
}
}, {
text: 'Yes', onPress: () => {
this.onSave();
}
}
]
)
} else {
this.props.navigator.pop();
}
}
renderView() {
if (!this.state.dataArray || this.state.dataArray.length === 0)return;
var len = this.state.dataArray.length;
var views = [];
for (var i = 0, l = len - 2; i < l; i += 2) {
views.push(
<View key={i}>
<View style={styles.item}>
{this.renderCheckBox(this.state.dataArray[i])}
{this.renderCheckBox(this.state.dataArray[i + 1])}
</View>
<View style={styles.line}/>
</View>
)
}
views.push(
<View key={len - 1}>
<View style={styles.item}>
{len % 2 === 0 ? this.renderCheckBox(this.state.dataArray[len - 2]) : null}
{this.renderCheckBox(this.state.dataArray[len - 1])}
</View>
</View>
)
return views;
}
renderCheckBox(data) {
let leftText = data.name;
let isChecked=this.isRemoveKey?false:data.checked;
return (
<CheckBox
style={{flex: 1, padding: 10}}
onClick={()=>this.onClick(data)}
isChecked={isChecked}
leftText={leftText}
checkedImage={<Image source={require('../../page/my/img/ic_check_box.png')} style={this.props.theme.styles.tabBarSelectedIcon}/>}
unCheckedImage={<Image source={require('../../page/my/img/ic_check_box_outline_blank.png')} style={this.props.theme.styles.tabBarSelectedIcon}/>}
/>);
}
render() {
let rightButtonTitle=this.isRemoveKey? 'Remove':'Save';
let rightButtonConfig = {
title: rightButtonTitle,
handler:()=>this.onSave(),
tintColor:'white',
};
let navigationBar =
<NavigationBar
title={this.props.menuType}
leftButton={ViewUtils.getLeftButton(()=>this.onBack())}
style={this.props.theme.styles.navBar}
rightButton={rightButtonConfig}/>;
return (
<View style={styles.container}>
{navigationBar}
<ScrollView>
{this.renderView()}
</ScrollView>
</View>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#f3f2f2'
},
item: {
flexDirection: 'row',
},
line: {
flex: 1,
height: 0.3,
backgroundColor: 'darkgray',
},
})