Settings.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /**
  2. * 设置页面
  3. * @邠心vbe on 2022/12/9
  4. */
  5. import React, { Component } from 'react';
  6. import { View, Text, StyleSheet, Switch, Pressable } from 'react-native';
  7. import apiUser from '../api/apiUser';
  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. userInfo: {
  41. notifyLowBalance: false,
  42. notifyChargingComplete: false,
  43. notifyPromotionsOffers: false
  44. },
  45. intervalList: [10, 15, 20, 30, 60, 90, 120],
  46. switchStyle: isIOS ? { false: "#B2B2B2", true: colorAccent } : null,
  47. };
  48. this.changed = false;
  49. //global.colorAccent = "#00A9FF"
  50. //global.colorPrimary = "#00A9FF"
  51. }
  52. componentDidMount() {
  53. this.props.navigation.addListener('blur', () => {
  54. //console.log("保存设置");
  55. this.saveSettings();
  56. });
  57. this.props.navigation.addListener('focus', () => {
  58. getUserInfo(info => {
  59. this.setState({
  60. userInfo: info
  61. });
  62. }, true);
  63. });
  64. SettingUtil.getSettings(res => {
  65. const info = this.state.settings;
  66. for (let item in info) {
  67. if (res[item] !== undefined) {
  68. info[item] = res[item]
  69. }
  70. }
  71. this.setState({
  72. settings: info
  73. })
  74. })
  75. }
  76. changeSwitch(key, value) {
  77. this.state.settings[key] = value
  78. //console.log('changeSwitch', item);
  79. this.setState({
  80. settings: this.state.settings
  81. })
  82. }
  83. changeNotifySwitch(key, value) {
  84. userInfo[key] = value;
  85. this.setState({
  86. userInfo: userInfo
  87. });
  88. this.changed = true;
  89. }
  90. changeSettings(key, value) {
  91. this.state.settings[key] = value
  92. this.setState({
  93. settings: this.state.settings
  94. })
  95. }
  96. saveSettings() {
  97. global.settingsInfo = this.state.settings
  98. storage.setStorageJson(SETTINGS_KEY, this.state.settings)
  99. if (this.changed) {
  100. apiUser.setNotifySwitch(this.state.userInfo);
  101. }
  102. }
  103. render() {
  104. return (
  105. <View style={ui.flex1}>
  106. <Text style={styles.title}>Notification</Text>
  107. <Button
  108. style={styles.itemButton}
  109. viewStyle={styles.itemView}
  110. onClick={() => this.changeNotifySwitch("notifyChargingComplete", !userInfo.notifyChargingComplete)}>
  111. <Text style={styles.buttonText}>Notify me when charging complete</Text>
  112. <Switch
  113. value={this.state.userInfo.notifyChargingComplete}
  114. trackColor={this.state.switchStyle}
  115. onValueChange={v => this.changeNotifySwitch("notifyChargingComplete", v)}/>
  116. </Button>
  117. <Button
  118. style={styles.itemButton}
  119. viewStyle={styles.itemView}
  120. onClick={() => this.changeNotifySwitch("notifyLowBalance", !userInfo.notifyLowBalance)}>
  121. <Text style={styles.buttonText}>Notify me when wallet is low balance</Text>
  122. <Switch
  123. value={this.state.userInfo.notifyLowBalance}
  124. trackColor={isIOS ? { false: "#B2B2B2", true: colorAccent } : null}
  125. onValueChange={v => this.changeNotifySwitch("notifyLowBalance", v)}/>
  126. </Button>
  127. <Button
  128. style={styles.itemButton}
  129. viewStyle={styles.itemView}
  130. onClick={() => this.changeNotifySwitch("notifyPromotionsOffers", !userInfo.notifyPromotionsOffers)}>
  131. <Text style={styles.buttonText}>Notify me for promotions and offers</Text>
  132. <Switch
  133. value={this.state.userInfo.notifyPromotionsOffers}
  134. trackColor={isIOS ? { false: "#B2B2B2", true: colorAccent } : null}
  135. onValueChange={v => this.changeNotifySwitch("notifyPromotionsOffers", v)}/>
  136. </Button>
  137. <Text style={styles.title}>Maps</Text>
  138. <Button
  139. style={styles.itemButton}
  140. viewStyle={styles.itemView}
  141. onClick={() => this.changeSwitch('alwaysLocation', !this.state.settings.alwaysLocation)}>
  142. <Text style={styles.buttonText}>Always show my locations</Text>
  143. <Switch
  144. value={this.state.settings.alwaysLocation}
  145. trackColor={this.state.switchStyle}
  146. onValueChange={v => this.changeSwitch('alwaysLocation', v)}/>
  147. </Button>
  148. <View style={{height: 56}}>
  149. <Pressable style={styles.dropdownView} android_ripple={ripple}>
  150. <Text style={styles.buttonText}>Auto refresh interval</Text>
  151. <Text style={styles.settingText}>{this.state.settings.refreshInterval}s</Text>
  152. </Pressable>
  153. <Dropdown
  154. style={styles.hideItem}
  155. list={this.state.intervalList}
  156. title="Refresh interval"
  157. suffixList="s"
  158. showText={false}
  159. showIcon={false}
  160. value={this.state.settings.refreshInterval}
  161. rippleStyle={ripple}
  162. onChange={v => this.changeSettings("refreshInterval", v)}
  163. />
  164. </View>
  165. </View>
  166. );
  167. }
  168. }
  169. const styles = StyleSheet.create({
  170. title: {
  171. color: '#888',
  172. fontSize: 12,
  173. ...$padding(8, 16, 4),
  174. backgroundColor: colorLight
  175. },
  176. itemButton: {
  177. borderRadius: 0,
  178. backgroundColor: colorLight
  179. },
  180. itemView: {
  181. flex: 1,
  182. height: 56,
  183. paddingLeft: 16,
  184. paddingRight: 16,
  185. alignItems: 'center',
  186. flexDirection: 'row'
  187. },
  188. buttonText: {
  189. flex: 1,
  190. color: textPrimary,
  191. fontSize: 16
  192. },
  193. settingText: {
  194. color: '#666',
  195. fontSize: 15
  196. },
  197. dropdownView: {
  198. top: 0,
  199. left: 0,
  200. right: 0,
  201. height: 56,
  202. paddingLeft: 16,
  203. paddingRight: 16,
  204. position: 'absolute',
  205. alignItems: 'center',
  206. flexDirection: 'row',
  207. backgroundColor: colorLight
  208. },
  209. hideItem: {
  210. flex: 1,
  211. height: 56,
  212. paddingLeft: 16,
  213. paddingRight: 16,
  214. alignItems: 'center',
  215. flexDirection: 'row'
  216. }
  217. })