Settings.js 9.1 KB

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