Settings.js 8.4 KB

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