2017-07-23 20:26:50 +02:00
|
|
|
import React, { Component } from 'react';
|
2017-07-26 19:49:01 +02:00
|
|
|
import { View, Switch, Slider, StyleSheet, Picker, Text, Button } from 'react-native';
|
2017-07-23 20:26:50 +02:00
|
|
|
import { connect } from 'react-redux'
|
|
|
|
import { ScreenHeader } from 'lib/components/screen-header.js';
|
2017-07-24 23:29:40 +02:00
|
|
|
import { _, setLocale } from 'lib/locale.js';
|
2017-07-23 20:26:50 +02:00
|
|
|
import { BaseScreenComponent } from 'lib/components/base-screen.js';
|
|
|
|
import { globalStyle } from 'lib/components/global-style.js';
|
|
|
|
import { Setting } from 'lib/models/setting.js';
|
|
|
|
|
|
|
|
let styles = {
|
2017-07-24 23:29:40 +02:00
|
|
|
settingContainer: {
|
|
|
|
borderBottomWidth: 1,
|
|
|
|
borderBottomColor: globalStyle.dividerColor,
|
|
|
|
paddingTop: globalStyle.marginTop,
|
|
|
|
paddingBottom: globalStyle.marginBottom,
|
|
|
|
paddingLeft: globalStyle.marginLeft,
|
|
|
|
paddingRight: globalStyle.marginRight,
|
|
|
|
},
|
|
|
|
settingText: {
|
|
|
|
fontWeight: 'bold',
|
|
|
|
color: globalStyle.color,
|
|
|
|
},
|
|
|
|
settingControl: {
|
2017-07-26 19:49:01 +02:00
|
|
|
color: globalStyle.color,
|
2017-07-24 23:29:40 +02:00
|
|
|
},
|
2017-07-23 20:26:50 +02:00
|
|
|
}
|
|
|
|
|
2017-07-26 19:49:01 +02:00
|
|
|
styles.switchSettingContainer = Object.assign({}, styles.settingContainer);
|
|
|
|
styles.switchSettingContainer.flexDirection = 'row';
|
|
|
|
styles.switchSettingContainer.justifyContent = 'space-between';
|
|
|
|
|
|
|
|
styles.switchSettingControl = Object.assign({}, styles.settingControl);
|
|
|
|
delete styles.switchSettingControl.color;
|
|
|
|
|
2017-07-23 20:26:50 +02:00
|
|
|
styles = StyleSheet.create(styles);
|
|
|
|
|
|
|
|
class ConfigScreenComponent extends BaseScreenComponent {
|
|
|
|
|
|
|
|
static navigationOptions(options) {
|
|
|
|
return { header: null };
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.state = {
|
|
|
|
values: {},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillMount() {
|
|
|
|
const settings = Setting.publicSettings(Setting.value('appType'));
|
|
|
|
|
|
|
|
let values = {};
|
|
|
|
for (let key in settings) {
|
|
|
|
if (!settings.hasOwnProperty(key)) continue;
|
|
|
|
values[key] = settings[key].value;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setState({ values: values });
|
|
|
|
}
|
|
|
|
|
|
|
|
settingToComponent(key, setting) {
|
|
|
|
let output = null;
|
|
|
|
|
|
|
|
const updateSettingValue = (key, value) => {
|
|
|
|
let values = this.state.values;
|
|
|
|
values[key] = value;
|
|
|
|
this.setState({ values: values });
|
2017-07-24 23:29:40 +02:00
|
|
|
this.saveSettings();
|
2017-07-23 20:26:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const value = this.state.values[key];
|
|
|
|
|
2017-07-25 23:55:26 +02:00
|
|
|
if (setting.isEnum) {
|
2017-07-23 20:26:50 +02:00
|
|
|
let items = [];
|
|
|
|
const settingOptions = setting.options();
|
|
|
|
for (let k in settingOptions) {
|
|
|
|
if (!settingOptions.hasOwnProperty(k)) continue;
|
|
|
|
items.push(<Picker.Item label={settingOptions[k]} value={k} key={k}/>);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2017-07-24 23:29:40 +02:00
|
|
|
<View key={key} style={styles.settingContainer}>
|
|
|
|
<Text key="label" style={styles.settingText}>{setting.label()}</Text>
|
|
|
|
<Picker key="control" style={styles.settingControl} selectedValue={value} onValueChange={(itemValue, itemIndex) => updateSettingValue(key, itemValue)} >
|
2017-07-23 20:26:50 +02:00
|
|
|
{ items }
|
|
|
|
</Picker>
|
|
|
|
</View>
|
|
|
|
);
|
2017-07-25 23:55:26 +02:00
|
|
|
} else if (setting.type == Setting.TYPE_BOOL) {
|
2017-07-26 19:49:01 +02:00
|
|
|
return (
|
|
|
|
<View key={key} style={styles.switchSettingContainer}>
|
|
|
|
<Text key="label" style={styles.settingText}>{setting.label()}</Text>
|
|
|
|
<Switch key="control" style={styles.switchSettingControl} value={value} onValueChange={(value) => updateSettingValue(key, value)} />
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
} else if (setting.type == Setting.TYPE_INT) {
|
2017-07-25 23:55:26 +02:00
|
|
|
return (
|
|
|
|
<View key={key} style={styles.settingContainer}>
|
|
|
|
<Text key="label" style={styles.settingText}>{setting.label()}</Text>
|
2017-07-26 19:49:01 +02:00
|
|
|
<Slider key="control" style={styles.settingControl} value={value} onValueChange={(value) => updateSettingValue(key, value)} />
|
2017-07-25 23:55:26 +02:00
|
|
|
</View>
|
|
|
|
);
|
2017-07-26 19:49:01 +02:00
|
|
|
} else {
|
2017-07-23 20:26:50 +02:00
|
|
|
//throw new Error('Unsupported setting type: ' + setting.type);
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2017-07-24 23:29:40 +02:00
|
|
|
saveSettings() {
|
2017-07-23 20:26:50 +02:00
|
|
|
const values = this.state.values;
|
|
|
|
for (let key in values) {
|
|
|
|
if (!values.hasOwnProperty(key)) continue;
|
|
|
|
Setting.setValue(key, values[key]);
|
|
|
|
}
|
|
|
|
Setting.saveAll();
|
2017-07-24 23:29:40 +02:00
|
|
|
|
|
|
|
setLocale(Setting.value('locale'));
|
2017-07-23 20:26:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const settings = Setting.publicSettings(Setting.value('appType'));
|
|
|
|
|
|
|
|
let settingComps = [];
|
|
|
|
for (let key in settings) {
|
2017-07-24 23:29:40 +02:00
|
|
|
if (key == 'sync.target') continue;
|
|
|
|
|
2017-07-23 20:26:50 +02:00
|
|
|
if (!settings.hasOwnProperty(key)) continue;
|
|
|
|
const comp = this.settingToComponent(key, settings[key]);
|
|
|
|
if (!comp) continue;
|
|
|
|
settingComps.push(comp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<View style={this.styles().screen}>
|
2017-07-24 23:58:14 +02:00
|
|
|
<ScreenHeader title={_('Configuration')}/>
|
2017-07-23 20:26:50 +02:00
|
|
|
<View style={styles.body}>
|
|
|
|
{ settingComps }
|
|
|
|
</View>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const ConfigScreen = connect(
|
|
|
|
(state) => {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
)(ConfigScreenComponent)
|
|
|
|
|
|
|
|
export { ConfigScreen };
|