Add slider and save value in LocalStorage

This commit is contained in:
Julia Friesel
2018-08-21 14:26:54 +02:00
parent 7a1ef388d3
commit c6b490b2a4
6 changed files with 116 additions and 14 deletions
+5
View File
@@ -29,5 +29,10 @@ export const settings = {
success: { success: {
message: 'Data successfully imported' message: 'Data successfully imported'
} }
},
tempScale: {
segmentTitle: 'Change temperature scale',
min: 'Min',
max: 'Max'
} }
} }
+59 -3
View File
@@ -3,21 +3,28 @@ import {
View, View,
Button, Button,
ScrollView, ScrollView,
Alert Alert,
Text
} from 'react-native' } from 'react-native'
import Slider from '@ptomasroos/react-native-multi-slider'
import Share from 'react-native-share' import Share from 'react-native-share'
import { DocumentPicker, DocumentPickerUtil } from 'react-native-document-picker' import { DocumentPicker, DocumentPickerUtil } from 'react-native-document-picker'
import rnfs from 'react-native-fs' import rnfs from 'react-native-fs'
import styles from '../styles/index' import styles from '../styles/index'
import config from '../config'
import { settings as labels } from './labels' import { settings as labels } from './labels'
import getDataAsCsvDataUri from '../lib/import-export/export-to-csv' import getDataAsCsvDataUri from '../lib/import-export/export-to-csv'
import importCsv from '../lib/import-export/import-from-csv' import importCsv from '../lib/import-export/import-from-csv'
import { getTempScale, saveTempScale } from '../local-storage'
export default class Settings extends Component { export default class Settings extends Component {
render() { render() {
return ( return (
<ScrollView> <ScrollView>
<View style={styles.settingsSegment}>
<Text>{labels.tempScale.segmentTitle}</Text>
<TempSlider/>
</View>
<View style={styles.homeButtons}> <View style={styles.homeButtons}>
<View style={styles.homeButton}> <View style={styles.homeButton}>
<Button <Button
@@ -37,6 +44,55 @@ export default class Settings extends Component {
} }
} }
class TempSlider extends Component {
constructor(props) {
super(props)
this.state = {
min: config.temperatureScale.low,
max: config.temperatureScale.high
}
this.getStoredScale()
}
async getStoredScale() {
const storedScale = await getTempScale()
if (!storedScale) return
this.setState(storedScale)
}
onValuesChange = (values) => {
this.setState({
min: values[0],
max: values[1]
})
}
onValuesChangeFinish = (values) => {
this.setState({
min: values[0],
max: values[1]
})
saveTempScale(this.state)
}
render() {
return (
<View>
<Text>{`${labels.tempScale.min} ${this.state.min}`}</Text>
<Text>{`${labels.tempScale.max} ${this.state.max}`}</Text>
<Slider
values={[this.state.min, this.state.max]}
min={config.temperatureScale.min}
max={config.temperatureScale.max}
step={0.5}
onValuesChange={this.onValuesChange}
onValuesChangeFinish={this.onValuesChangeFinish}
/>
</View>
)
}
}
async function openShareDialogAndExport() { async function openShareDialogAndExport() {
let data let data
try { try {
@@ -117,4 +173,4 @@ function alertError(msg) {
function importError(msg) { function importError(msg) {
const postFixed = `${msg}\n\n${labels.import.errors.postFix}` const postFixed = `${msg}\n\n${labels.import.errors.postFix}`
alertError(postFixed) alertError(postFixed)
} }
+11
View File
@@ -0,0 +1,11 @@
import { AsyncStorage } from 'react-native'
export async function getTempScale() {
const result = await AsyncStorage.getItem('tempScale')
if (!result) return result
return JSON.parse(result)
}
export async function saveTempScale(scale) {
return AsyncStorage.setItem('tempScale', JSON.stringify(scale))
}
+35 -11
View File
@@ -723,6 +723,11 @@
} }
} }
}, },
"@ptomasroos/react-native-multi-slider": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/@ptomasroos/react-native-multi-slider/-/react-native-multi-slider-1.0.0.tgz",
"integrity": "sha512-NpX22rQLArg9widwMzGf7XsInTDf6mfY/D1XaDVjglNkVphj3NSN37+nF6MofArCxC++1P+jHv0SGWbmJQwy4g=="
},
"abbrev": { "abbrev": {
"version": "1.1.1", "version": "1.1.1",
"resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz", "resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz",
@@ -3727,7 +3732,8 @@
}, },
"ansi-regex": { "ansi-regex": {
"version": "2.1.1", "version": "2.1.1",
"bundled": true "bundled": true,
"optional": true
}, },
"aproba": { "aproba": {
"version": "1.2.0", "version": "1.2.0",
@@ -3745,11 +3751,13 @@
}, },
"balanced-match": { "balanced-match": {
"version": "1.0.0", "version": "1.0.0",
"bundled": true "bundled": true,
"optional": true
}, },
"brace-expansion": { "brace-expansion": {
"version": "1.1.11", "version": "1.1.11",
"bundled": true, "bundled": true,
"optional": true,
"requires": { "requires": {
"balanced-match": "^1.0.0", "balanced-match": "^1.0.0",
"concat-map": "0.0.1" "concat-map": "0.0.1"
@@ -3762,15 +3770,18 @@
}, },
"code-point-at": { "code-point-at": {
"version": "1.1.0", "version": "1.1.0",
"bundled": true "bundled": true,
"optional": true
}, },
"concat-map": { "concat-map": {
"version": "0.0.1", "version": "0.0.1",
"bundled": true "bundled": true,
"optional": true
}, },
"console-control-strings": { "console-control-strings": {
"version": "1.1.0", "version": "1.1.0",
"bundled": true "bundled": true,
"optional": true
}, },
"core-util-is": { "core-util-is": {
"version": "1.0.2", "version": "1.0.2",
@@ -3873,7 +3884,8 @@
}, },
"inherits": { "inherits": {
"version": "2.0.3", "version": "2.0.3",
"bundled": true "bundled": true,
"optional": true
}, },
"ini": { "ini": {
"version": "1.3.5", "version": "1.3.5",
@@ -3883,6 +3895,7 @@
"is-fullwidth-code-point": { "is-fullwidth-code-point": {
"version": "1.0.0", "version": "1.0.0",
"bundled": true, "bundled": true,
"optional": true,
"requires": { "requires": {
"number-is-nan": "^1.0.0" "number-is-nan": "^1.0.0"
} }
@@ -3895,17 +3908,20 @@
"minimatch": { "minimatch": {
"version": "3.0.4", "version": "3.0.4",
"bundled": true, "bundled": true,
"optional": true,
"requires": { "requires": {
"brace-expansion": "^1.1.7" "brace-expansion": "^1.1.7"
} }
}, },
"minimist": { "minimist": {
"version": "0.0.8", "version": "0.0.8",
"bundled": true "bundled": true,
"optional": true
}, },
"minipass": { "minipass": {
"version": "2.2.4", "version": "2.2.4",
"bundled": true, "bundled": true,
"optional": true,
"requires": { "requires": {
"safe-buffer": "^5.1.1", "safe-buffer": "^5.1.1",
"yallist": "^3.0.0" "yallist": "^3.0.0"
@@ -3922,6 +3938,7 @@
"mkdirp": { "mkdirp": {
"version": "0.5.1", "version": "0.5.1",
"bundled": true, "bundled": true,
"optional": true,
"requires": { "requires": {
"minimist": "0.0.8" "minimist": "0.0.8"
} }
@@ -3994,7 +4011,8 @@
}, },
"number-is-nan": { "number-is-nan": {
"version": "1.0.1", "version": "1.0.1",
"bundled": true "bundled": true,
"optional": true
}, },
"object-assign": { "object-assign": {
"version": "4.1.1", "version": "4.1.1",
@@ -4004,6 +4022,7 @@
"once": { "once": {
"version": "1.4.0", "version": "1.4.0",
"bundled": true, "bundled": true,
"optional": true,
"requires": { "requires": {
"wrappy": "1" "wrappy": "1"
} }
@@ -4079,7 +4098,8 @@
}, },
"safe-buffer": { "safe-buffer": {
"version": "5.1.1", "version": "5.1.1",
"bundled": true "bundled": true,
"optional": true
}, },
"safer-buffer": { "safer-buffer": {
"version": "2.1.2", "version": "2.1.2",
@@ -4109,6 +4129,7 @@
"string-width": { "string-width": {
"version": "1.0.2", "version": "1.0.2",
"bundled": true, "bundled": true,
"optional": true,
"requires": { "requires": {
"code-point-at": "^1.0.0", "code-point-at": "^1.0.0",
"is-fullwidth-code-point": "^1.0.0", "is-fullwidth-code-point": "^1.0.0",
@@ -4126,6 +4147,7 @@
"strip-ansi": { "strip-ansi": {
"version": "3.0.1", "version": "3.0.1",
"bundled": true, "bundled": true,
"optional": true,
"requires": { "requires": {
"ansi-regex": "^2.0.0" "ansi-regex": "^2.0.0"
} }
@@ -4164,11 +4186,13 @@
}, },
"wrappy": { "wrappy": {
"version": "1.0.2", "version": "1.0.2",
"bundled": true "bundled": true,
"optional": true
}, },
"yallist": { "yallist": {
"version": "3.0.2", "version": "3.0.2",
"bundled": true "bundled": true,
"optional": true
} }
} }
}, },
+1
View File
@@ -15,6 +15,7 @@
"lint": "eslint components lib test" "lint": "eslint components lib test"
}, },
"dependencies": { "dependencies": {
"@ptomasroos/react-native-multi-slider": "^1.0.0",
"assert": "^1.4.1", "assert": "^1.4.1",
"csvtojson": "^2.0.8", "csvtojson": "^2.0.8",
"date-range": "0.0.2", "date-range": "0.0.2",
+5
View File
@@ -88,5 +88,10 @@ export default StyleSheet.create({
margin: 30, margin: 30,
textAlign: 'left', textAlign: 'left',
textAlignVertical: 'center' textAlignVertical: 'center'
},
settingsSegment: {
backgroundColor: 'lightgrey',
padding: 10,
marginTop: 10
} }
}) })