Settings.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /**
  2. * 设置页面
  3. * @邠心vbe on 2022/12/9
  4. */
  5. import React, { Component } from 'react';
  6. import { View, Text, StyleSheet, Switch } from 'react-native';
  7. import { Value } from 'react-native-reanimated';
  8. import Button from '../components/Button';
  9. import Dropdown from '../components/Dropdown';
  10. import storage from '../utils/storage';
  11. export const SETTINGS_KEY = 'CHARGE_SETTINGS'
  12. export const SettingUtil = {
  13. getSettings: (back) => {
  14. if (!global.settingsInfo) {
  15. storage.getStorage(SETTINGS_KEY).then(sto => {
  16. if (sto) {
  17. const res = JSON.parse(sto);
  18. global.settingsInfo = res;
  19. back(global.settingsInfo)
  20. } else {
  21. back({
  22. alwaysLocation: true,
  23. refreshInterval: 10
  24. })
  25. }
  26. })
  27. } else {
  28. back(global.settingsInfo)
  29. }
  30. }
  31. }
  32. export default class Settings extends Component {
  33. constructor(props) {
  34. super(props);
  35. this.state = {
  36. settings: {
  37. alwaysLocation: true,
  38. refreshInterval: 10
  39. },
  40. intervalList: [10, 15, 20, 30, 60, 90, 120]
  41. };
  42. }
  43. componentDidMount() {
  44. this.props.navigation.addListener('blur', () => {
  45. //console.log("保存设置");
  46. global.settingsInfo = this.state.settings
  47. storage.setStorageJson(SETTINGS_KEY, this.state.settings)
  48. });
  49. SettingUtil.getSettings(res => {
  50. const info = this.state.settings;
  51. for (let item in info) {
  52. if (res[item] !== undefined) {
  53. info[item] = res[item]
  54. }
  55. }
  56. this.setState({
  57. settings: info
  58. })
  59. })
  60. }
  61. changeSwitch(key, value) {
  62. this.state.settings[key] = value
  63. //console.log('changeSwitch', item);
  64. this.setState({
  65. settings: this.state.settings
  66. })
  67. }
  68. changeSettings(key, value) {
  69. this.state.settings[key] = value
  70. this.setState({
  71. settings: this.state.settings
  72. })
  73. }
  74. render() {
  75. return (
  76. <View style={ui.flex1}>
  77. <Text style={styles.title}>Maps</Text>
  78. <Button
  79. style={styles.itemButton}
  80. viewStyle={styles.itemView}
  81. onClick={() => this.changeSwitch('alwaysLocation', !this.state.settings.alwaysLocation)}>
  82. <Text style={styles.buttonText}>Always show my locations</Text>
  83. <Switch
  84. value={this.state.settings.alwaysLocation}
  85. trackColor={isIOS ? { false: "#B2B2B2", true: colorAccent } : null}
  86. onValueChange={v => {
  87. this.changeSwitch('alwaysLocation', v);
  88. }}/>
  89. </Button>
  90. <View style={{height: 56}}>
  91. <View style={styles.dropdownView}>
  92. <Text style={styles.buttonText}>Auto refresh interval</Text>
  93. <Text style={styles.settingText}>{this.state.settings.refreshInterval}s</Text>
  94. </View>
  95. <Dropdown
  96. style={styles.hideItem}
  97. list={this.state.intervalList}
  98. title="Refresh interval"
  99. suffixList="s"
  100. value={this.state.settings.refreshInterval}
  101. rippleStyle={ripple}
  102. onChange={v => this.changeSettings("refreshInterval", v)}
  103. />
  104. </View>
  105. </View>
  106. );
  107. }
  108. }
  109. const styles = StyleSheet.create({
  110. title: {
  111. color: '#888',
  112. fontSize: 12,
  113. ...$padding(8, 16, 4),
  114. backgroundColor: 'white'
  115. },
  116. itemButton: {
  117. borderRadius: 0,
  118. backgroundColor: 'white'
  119. },
  120. itemView: {
  121. flex: 1,
  122. height: 56,
  123. paddingLeft: 16,
  124. paddingRight: 16,
  125. alignItems: 'center',
  126. flexDirection: 'row'
  127. },
  128. buttonText: {
  129. flex: 1,
  130. color: '#333',
  131. fontSize: 16
  132. },
  133. settingText: {
  134. color: '#666',
  135. fontSize: 15
  136. },
  137. dropdownView: {
  138. top: 0,
  139. left: 0,
  140. right: 0,
  141. height: 56,
  142. paddingLeft: 16,
  143. paddingRight: 16,
  144. position: 'absolute',
  145. alignItems: 'center',
  146. flexDirection: 'row',
  147. backgroundColor: 'white'
  148. },
  149. hideItem: {
  150. flex: 1,
  151. height: 56,
  152. opacity: 0,
  153. paddingLeft: 16,
  154. paddingRight: 16,
  155. alignItems: 'center',
  156. flexDirection: 'row'
  157. }
  158. })