Settings.js 9.1 KB

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