joplin/ReactNativeClient/lib/components/screens/config.js

135 lines
3.9 KiB
JavaScript
Raw Normal View History

2017-07-23 18:26:50 +00:00
import React, { Component } from 'react';
import { View, Switch, Slider, StyleSheet, Picker, Text, Button } from 'react-native';
2017-07-23 18:26:50 +00:00
import { connect } from 'react-redux'
import { ScreenHeader } from 'lib/components/screen-header.js';
2017-07-24 21:29:40 +00:00
import { _, setLocale } from 'lib/locale.js';
2017-07-23 18:26:50 +00: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 21:29:40 +00: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,
2017-07-30 21:04:26 +00:00
fontSize: globalStyle.fontSize,
2017-07-24 21:29:40 +00:00
},
settingControl: {
color: globalStyle.color,
2017-07-24 21:29:40 +00:00
},
2017-07-30 21:04:26 +00:00
pickerItem: {
fontSize: globalStyle.fontSize,
}
2017-07-23 18:26:50 +00:00
}
2017-07-30 21:04:26 +00:00
styles.switchSettingText = Object.assign({}, styles.settingText);
styles.switchSettingText.width = '80%';
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-30 21:04:26 +00:00
styles.switchSettingControl.width = '20%';
2017-07-23 18:26:50 +00:00
styles = StyleSheet.create(styles);
class ConfigScreenComponent extends BaseScreenComponent {
static navigationOptions(options) {
return { header: null };
}
2017-07-31 20:51:24 +00:00
settingToComponent(key, value) {
2017-07-23 18:26:50 +00:00
let output = null;
const updateSettingValue = (key, value) => {
2017-07-31 20:51:24 +00:00
Setting.setValue(key, value);
2017-07-23 18:26:50 +00:00
}
2017-07-31 20:51:24 +00:00
const md = Setting.settingMetadata(key);
if (md.isEnum) {
// The Picker component doesn't work properly with int values, so
// convert everything to string (Setting.setValue will convert
// back to the correct type.
value = value.toString();
2017-07-23 18:26:50 +00:00
let items = [];
2017-07-31 20:51:24 +00:00
const settingOptions = md.options();
2017-07-23 18:26:50 +00:00
for (let k in settingOptions) {
if (!settingOptions.hasOwnProperty(k)) continue;
2017-07-31 20:51:24 +00:00
items.push(<Picker.Item label={settingOptions[k]} value={k.toString()} key={k}/>);
2017-07-23 18:26:50 +00:00
}
return (
2017-07-24 21:29:40 +00:00
<View key={key} style={styles.settingContainer}>
2017-07-31 20:51:24 +00:00
<Text key="label" style={styles.settingText}>{md.label()}</Text>
2017-07-24 21:29:40 +00:00
<Picker key="control" style={styles.settingControl} selectedValue={value} onValueChange={(itemValue, itemIndex) => updateSettingValue(key, itemValue)} >
2017-07-23 18:26:50 +00:00
{ items }
</Picker>
</View>
);
2017-07-31 20:51:24 +00:00
} else if (md.type == Setting.TYPE_BOOL) {
return (
<View key={key} style={styles.switchSettingContainer}>
2017-07-31 20:51:24 +00:00
<Text key="label" style={styles.switchSettingText}>{md.label()}</Text>
<Switch key="control" style={styles.switchSettingControl} value={value} onValueChange={(value) => updateSettingValue(key, value)} />
</View>
);
2017-07-31 20:51:24 +00:00
} else if (md.type == Setting.TYPE_INT) {
2017-07-25 21:55:26 +00:00
return (
<View key={key} style={styles.settingContainer}>
2017-07-31 20:51:24 +00:00
<Text key="label" style={styles.settingText}>{md.label()}</Text>
<Slider key="control" style={styles.settingControl} value={value} onValueChange={(value) => updateSettingValue(key, value)} />
2017-07-25 21:55:26 +00:00
</View>
);
} else {
2017-07-23 18:26:50 +00:00
//throw new Error('Unsupported setting type: ' + setting.type);
}
return output;
}
render() {
2017-07-31 20:51:24 +00:00
const settings = this.props.settings;
2017-07-23 18:26:50 +00:00
let settingComps = [];
for (let key in settings) {
2017-07-24 21:29:40 +00:00
if (key == 'sync.target') continue;
2017-07-23 18:26:50 +00:00
if (!settings.hasOwnProperty(key)) continue;
2017-07-31 20:51:24 +00:00
if (!Setting.isPublic(key)) continue;
2017-07-23 18:26:50 +00:00
const comp = this.settingToComponent(key, settings[key]);
if (!comp) continue;
settingComps.push(comp);
}
return (
<View style={this.styles().screen}>
2017-07-24 21:58:14 +00:00
<ScreenHeader title={_('Configuration')}/>
2017-07-23 18:26:50 +00:00
<View style={styles.body}>
{ settingComps }
</View>
</View>
);
}
}
const ConfigScreen = connect(
(state) => {
2017-07-31 20:51:24 +00:00
return { settings: state.settings };
2017-07-23 18:26:50 +00:00
}
)(ConfigScreenComponent)
export { ConfigScreen };