Settings.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /**
  2. * 设置页面
  3. * @邠心vbe on 2022/12/9
  4. */
  5. import React, { Component } from 'react';
  6. import { View, Text, StyleSheet } from 'react-native';
  7. import apiUser from '../api/apiUser';
  8. import Button from '../components/Button';
  9. import Dropdown from '../components/Dropdown';
  10. import Switch from '../components/Switch';
  11. import { i18nUtil } from '../i18n';
  12. import routeUtil from '../utils/routeUtil';
  13. import storage, { getStorage, setStorage } from '../utils/storage';
  14. import app from '../../app.json';
  15. export const SETTINGS_KEY = 'CHARGE_SETTINGS'
  16. export const SettingUtil = {
  17. getSettings: (back) => {
  18. if (!global.settingsInfo) {
  19. storage.getStorage(SETTINGS_KEY).then(sto => {
  20. if (sto) {
  21. const res = JSON.parse(sto);
  22. global.settingsInfo = res;
  23. back(global.settingsInfo)
  24. } else {
  25. back({
  26. alwaysLocation: true,
  27. refreshInterval: 10
  28. })
  29. }
  30. })
  31. } else {
  32. back(global.settingsInfo)
  33. }
  34. }
  35. }
  36. export default class Settings extends Component {
  37. constructor(props) {
  38. super(props);
  39. this.state = {
  40. settings: {
  41. alwaysLocation: true,
  42. refreshInterval: undefined
  43. },
  44. userInfo: {
  45. notifyLowBalance: false,
  46. notifyChargingComplete: false,
  47. notifyPromotionsOffers: false
  48. },
  49. localeList: [],
  50. currentLocale: "",
  51. intervalList: [10, 15, 20, 30, 60, 90, 120],
  52. //switchStyle: isIOS ? { false: "#B2B2B2", true: colorAccent } : null,
  53. };
  54. this.changed = false;
  55. //global.colorAccent = "#00A9FF"
  56. //global.colorPrimary = "#00A9FF"
  57. }
  58. componentDidMount() {
  59. this.props.navigation.addListener('blur', () => {
  60. //console.log("保存设置");
  61. this.saveSettings();
  62. });
  63. this.props.navigation.addListener('focus', () => {
  64. getUserInfo(info => {
  65. this.setState({
  66. userInfo: info
  67. });
  68. }, true);
  69. });
  70. this.getLocaleValues();
  71. SettingUtil.getSettings(res => {
  72. const info = this.state.settings;
  73. for (let item in info) {
  74. if (res[item] !== undefined) {
  75. info[item] = res[item]
  76. }
  77. }
  78. this.setState({
  79. settings: info
  80. })
  81. })
  82. }
  83. getLocaleValues() {
  84. const locales = [{
  85. name: $t("default"),
  86. value: ""//i18nUtil.getDefaultLocale()
  87. }];
  88. for (let item of i18nUtil.locales) {
  89. if (item.enable) {
  90. locales.push({
  91. name: item.name,
  92. value: item.value
  93. })
  94. }
  95. }
  96. this.setState({
  97. localeList: locales
  98. });
  99. getStorage(i18nUtil.SETTING_KEY).then(res => {
  100. if (res) {
  101. this.setState({
  102. currentLocale: res
  103. })
  104. }
  105. })
  106. console.log("getLocaleValues", locales);
  107. }
  108. changeSwitch(key, value) {
  109. this.state.settings[key] = value
  110. //console.log('changeSwitch', item);
  111. this.setState({
  112. settings: this.state.settings
  113. })
  114. }
  115. changeNotifySwitch(key, value) {
  116. userInfo[key] = value;
  117. this.setState({
  118. userInfo: userInfo
  119. });
  120. this.changed = true;
  121. }
  122. changeSettings(key, value) {
  123. this.state.settings[key] = value
  124. this.setState({
  125. settings: this.state.settings
  126. })
  127. console.log("设置数据", this.state.settings);
  128. }
  129. saveSettings() {
  130. global.settingsInfo = this.state.settings
  131. storage.setStorageJson(SETTINGS_KEY, this.state.settings)
  132. if (this.changed) {
  133. apiUser.setNotifySwitch(this.state.userInfo);
  134. }
  135. }
  136. changeLocale(locale) {
  137. if (locale !== this.state.currentLocale) {
  138. /*this.setState({
  139. currentLocale: locale
  140. })*/
  141. if (locale == "") {
  142. i18nUtil.setlocale(i18nUtil.getDefaultLocale());
  143. } else {
  144. i18nUtil.setlocale(locale);
  145. }
  146. setStorage(i18nUtil.SETTING_KEY, locale);
  147. routeUtil.resetToHome(this.props);
  148. }
  149. }
  150. render() {
  151. return (
  152. <View style={ui.flex1}>
  153. { app.modules.i18n &&
  154. <View style={styles.menuView}>
  155. <Text style={styles.buttonText}>{$t('settings.language')}</Text>
  156. <Dropdown
  157. list={this.state.localeList}
  158. //title={$t('settings.language')}
  159. nameKey="name"
  160. valueKey="value"
  161. value={this.state.currentLocale}
  162. onChange={l => this.changeLocale(l)}
  163. style={styles.localeSelect}
  164. showIcon={false}
  165. textStyle={styles.settingText}
  166. />
  167. </View>
  168. }
  169. <Text style={styles.title}>{$t('settings.notification')}</Text>
  170. <Button
  171. style={styles.itemButton}
  172. viewStyle={styles.itemView}
  173. onClick={() => this.changeNotifySwitch("notifyChargingComplete", !userInfo.notifyChargingComplete)}>
  174. <Text style={styles.buttonText}>{$t('settings.notifyChargingComplete')}</Text>
  175. <Switch
  176. value={this.state.userInfo.notifyChargingComplete}
  177. onValueChange={v => this.changeNotifySwitch("notifyChargingComplete", v)}/>
  178. </Button>
  179. <Button
  180. style={styles.itemButton}
  181. viewStyle={styles.itemView}
  182. onClick={() => this.changeNotifySwitch("notifyLowBalance", !userInfo.notifyLowBalance)}>
  183. <Text style={styles.buttonText}>{$t('settings.notifyLowBalance')}</Text>
  184. <Switch
  185. value={this.state.userInfo.notifyLowBalance}
  186. onValueChange={v => this.changeNotifySwitch("notifyLowBalance", v)}/>
  187. </Button>
  188. <Button
  189. style={styles.itemButton}
  190. viewStyle={styles.itemView}
  191. onClick={() => this.changeNotifySwitch("notifyPromotionsOffers", !userInfo.notifyPromotionsOffers)}>
  192. <Text style={styles.buttonText}>{$t('settings.notifyPromotionsOffers')}</Text>
  193. <Switch
  194. value={this.state.userInfo.notifyPromotionsOffers}
  195. onValueChange={v => this.changeNotifySwitch("notifyPromotionsOffers", v)}/>
  196. </Button>
  197. <Text style={styles.title}>{$t('settings.maps')}</Text>
  198. <Button
  199. style={styles.itemButton}
  200. viewStyle={styles.itemView}
  201. onClick={() => this.changeSwitch('alwaysLocation', !this.state.settings.alwaysLocation)}>
  202. <Text style={styles.buttonText}>{$t('settings.showMyLocations')}</Text>
  203. <Switch
  204. value={this.state.settings.alwaysLocation}
  205. onValueChange={v => this.changeSwitch('alwaysLocation', v)}/>
  206. </Button>
  207. <View style={styles.menuView}>
  208. <Text style={styles.buttonText}>{$t('settings.autoRefreshInterval')}</Text>
  209. <Dropdown
  210. style={styles.localeSelect}
  211. list={this.state.intervalList}
  212. title={$t('settings.refreshInterval')}
  213. suffixList={$t('settings.seconds')}
  214. suffixText={$t('settings.seconds')}
  215. showIcon={false}
  216. autoSelect={false}
  217. value={this.state.settings.refreshInterval}
  218. textStyle={styles.settingText}
  219. onChange={v => this.changeSettings("refreshInterval", v)}
  220. />
  221. </View>
  222. </View>
  223. );
  224. }
  225. }
  226. const styles = StyleSheet.create({
  227. title: {
  228. color: '#888',
  229. fontSize: 10,
  230. marginTop: 1,
  231. ...$padding(8, 16, 4),
  232. backgroundColor: colorLight
  233. },
  234. itemButton: {
  235. borderRadius: 0,
  236. backgroundColor: colorLight
  237. },
  238. itemView: {
  239. flex: 1,
  240. height: 56,
  241. paddingLeft: 16,
  242. paddingRight: 16,
  243. alignItems: 'center',
  244. flexDirection: 'row'
  245. },
  246. menuView: {
  247. height: 56,
  248. paddingLeft: 16,
  249. paddingRight: 16,
  250. overflow: 'hidden',
  251. alignItems: 'center',
  252. flexDirection: 'row',
  253. backgroundColor: colorLight
  254. },
  255. buttonText: {
  256. flex: 1,
  257. color: textPrimary,
  258. fontSize: 16
  259. },
  260. settingText: {
  261. color: '#888',
  262. fontSize: 14,
  263. textAlign: 'right'
  264. },
  265. localeSelect: {
  266. top: 0,
  267. left: 0,
  268. right: 0,
  269. bottom: 0,
  270. paddingLeft: 16,
  271. paddingRight: 16,
  272. position: "absolute",
  273. alignItems: "center",
  274. flexDirection: "row",
  275. justifyContent: 'flex-end'
  276. }
  277. })