Settings.js 8.5 KB

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