ChargeAdapter.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /**
  2. * 充电桩ViewPager适配器
  3. * @邠心vbe on 2023/02/06
  4. */
  5. import React, { Component } from 'react';
  6. import {createMaterialTopTabNavigator} from '@react-navigation/material-top-tabs';
  7. import { View, BackHandler, Pressable, StyleSheet } from 'react-native';
  8. import TabInfos from './TabInfos';
  9. import TabCharge from './TabCharge';
  10. import Reserve from './TabReserve';
  11. import TabInfosV3 from '../chargeV3/TabInfos';
  12. import QRResult from '../charge/QRResult';
  13. import apiStation from '../../api/apiStation';
  14. import PagerUtil from './PagerUtil';
  15. import utils from '../../utils/utils';
  16. import Toolbar, { Styles } from '../../components/Toolbar';
  17. import { getFocusedRouteNameFromRoute } from '@react-navigation/core';
  18. import app from '../../../app.json';
  19. import HeaderTitle from '../../components/HeaderTitle';
  20. import { PageList } from '../Router';
  21. export const PagerList = {
  22. "tabInfo": "Info",
  23. "tabCharge": "Charge",
  24. "tabReserve": "Reserve",
  25. "tabExplore": "Explore"
  26. }
  27. export default class ChargeAdapter extends Component {
  28. constructor(props) {
  29. super(props);
  30. this.state = {
  31. showTab: true,
  32. refreshing: false,
  33. showContent: false,
  34. showLoginDialog: false,
  35. stationInfo: {}
  36. }
  37. this.pageAdapter = [{
  38. title: $t('charging.tabInfo'),
  39. name: "Info",
  40. component: app.isLumiWhitelabel ? TabInfosV3 : TabInfos
  41. }, {
  42. title: $t('charging.tabCharge'),
  43. name: "Charge",
  44. component: TabCharge
  45. }, {
  46. title: $t('charging.tabReserve'),
  47. name: "Reserve",
  48. component: Reserve
  49. }/*, {
  50. title: $t('charging.tabExplore'),
  51. name: "Explore",
  52. component: Explore
  53. }*/]
  54. this.tabBarStyle = {
  55. tabBarStyle: styles.tabStyle,
  56. tabBarPressColor: rippleColor,
  57. tabBarScrollEnabled: this.pageAdapter.length > 3,
  58. tabBarIndicatorStyle: styles.indicator,
  59. tabBarActiveTintColor: tabBarTextActive,
  60. tabBarInactiveTintColor: tabBarTextInactive
  61. }
  62. this.action = "";
  63. this.isHide = false;
  64. this.titleName = "";
  65. this.backHandler = undefined;
  66. }
  67. componentDidMount() {
  68. utils.setBackClick([this.props?.route?.name, PagerList.tabCharge, PagerList.tabInfo, PagerList.tabReserve], this.backPage)
  69. this.props.navigation.addListener('beforeRemove', (e) => {
  70. this.hideDialog();
  71. });
  72. this.props.navigation.addListener('focus', () => {
  73. if (this.isHide && this.state.showContent) {
  74. this.updateStationInfo();
  75. } else {
  76. //this.canShowLoginDialog();
  77. }
  78. this.isHide = false;
  79. });
  80. this.props.navigation.addListener('blur', () => {
  81. this.isHide = true;
  82. });
  83. const params = this.props.route.params;
  84. if (params.action && params.stationInfo) {
  85. this.action = params.action
  86. this.setState({
  87. stationInfo: params.stationInfo
  88. }, () => {
  89. this.setPageTitle();
  90. this.canShowLoginDialog();
  91. });
  92. }
  93. this.backHandler = BackHandler.addEventListener('hardwareBackPress', this.backPage)
  94. PagerUtil.addOnRefresh(this);
  95. PagerUtil.setSelectedVoucher({});
  96. // setTimeout(() => {
  97. // this.changeTab(1)
  98. // }, 200);
  99. }
  100. componentDidUpdate() {
  101. /*if (this.state.tabIndex !== this.props.route.state.index) {
  102. this.setState({
  103. tabIndex: this.props.route.state.index
  104. })
  105. }*/
  106. var act = this.props.route.params.action;
  107. if (act !== this.state.action) {
  108. this.setState({
  109. action: act
  110. });
  111. }
  112. }
  113. componentWillUnmount() {
  114. QRResult.clearResult();
  115. PagerUtil.onDestory();
  116. if (this.backHandler) {
  117. this.backHandler.remove();
  118. }
  119. }
  120. backPage = () => {
  121. const params = this.props.route.params;
  122. if (params.from && !this.isHide) {
  123. if (params.from == PageList.home) {
  124. startPage(params.from);
  125. } else {
  126. goBack2();
  127. }
  128. return true;
  129. }
  130. }
  131. canShowLoginDialog() {
  132. if (isLogin()) {
  133. this.setState({
  134. showContent: true
  135. });
  136. //if (this.action !== 'view')
  137. this.updateStationInfo();
  138. } else {
  139. this.setState({
  140. showLoginDialog: true
  141. });
  142. }
  143. }
  144. hideDialog(close) {
  145. this.setState({
  146. showLoginDialog: false
  147. });
  148. if (close) {
  149. goBack();
  150. }
  151. }
  152. updateStationInfo() {
  153. navigator.geolocation.getCurrentPosition(location => {
  154. let params = {
  155. latitude: location.coords.latitude,
  156. longitude: location.coords.longitude,
  157. }
  158. this.getStationInfo(params)
  159. }, err => {
  160. this.getStationInfo()
  161. });
  162. }
  163. getStationInfo(location={}) {
  164. apiStation.getStationRate({
  165. sitePk: this.state.stationInfo.id,
  166. lat: location?.latitude,
  167. lng: location?.longitude
  168. }).then(res => {
  169. if (res.data.sitePk) {
  170. var info = utils.getSiteInfo(res.data);
  171. PagerUtil.setStationInfo(info);
  172. PagerUtil.setRefreshing();
  173. //PagerUtil.setRefreshing(getFocusedRouteNameFromRoute(this.props.route));
  174. this.setState({
  175. refreshing: false,
  176. stationInfo: info
  177. }, () => {
  178. this.setPageTitle();
  179. });
  180. if (PagerUtil.isInnerScanQR()) {
  181. if (QRResult.haveResult()) {
  182. this.changeTab(1);
  183. //PagerUtil.setRefreshing("Charge");
  184. }
  185. PagerUtil.ofInnerScanQR();
  186. } else if (!PagerUtil.ENABLE_NEW_UI && this.action == 'qr') {
  187. setTimeout(() => {
  188. this.changeTab(1)
  189. }, 300);
  190. }
  191. }
  192. }).catch((err) => {
  193. toastLong(err);
  194. this.setState({
  195. refreshing: false
  196. })
  197. });
  198. }
  199. setPageTitle() {
  200. if (!this.titleName) {
  201. this.titleName = this.state.stationInfo.name;
  202. this.props.navigation.setOptions({
  203. headerTitle: () => (<HeaderTitle title={this.titleName}/>),
  204. headerRight: () => (
  205. <Pressable
  206. style={Styles.backIcon}
  207. android_ripple={rippleLessIcon}
  208. onPress={() => utils.directMaps(this.state.stationInfo.latitude, this.state.stationInfo.longitude, this.state.stationInfo.address)}>
  209. <MaterialIcons
  210. name='directions'
  211. size={24}
  212. color={pageTitleTint}
  213. style={Styles.iconOpacity}
  214. />
  215. </Pressable>
  216. )
  217. })
  218. }
  219. }
  220. getAvailable(type) {
  221. const all = this.state.stationInfo.allConnector;
  222. if (all) {
  223. if (type == 'box') {
  224. return all.boxAvailable + '/' + all.boxAll;
  225. } else {
  226. return all.available + '/' + all.all;
  227. }
  228. } else {
  229. return '0/0';
  230. }
  231. }
  232. onPullRefresh() {
  233. this.updateStationInfo();
  234. }
  235. onBackRefresh() {
  236. this.onPullRefresh();
  237. }
  238. onEnterStation() {
  239. this.changeTab(1);
  240. }
  241. changeTab(index) {
  242. console.log("[changeTab]", index);
  243. this.props.navigation.navigate(this.pageAdapter[index]?.name);
  244. /*this.setState({
  245. tabIndex: index
  246. })*/
  247. }
  248. render() {
  249. const Tab = createMaterialTopTabNavigator();
  250. return (
  251. <View style={styles.container}>
  252. <Toolbar
  253. title={this.state.stationInfo?.name ?? "Charging Site"}
  254. rightIcon={() => (
  255. <Pressable
  256. style={Styles.backIcon}
  257. android_ripple={rippleLessIcon}
  258. onPress={() => utils.directMaps(this.state.stationInfo.latitude, this.state.stationInfo.longitude, this.state.stationInfo.address)}>
  259. <MaterialIcons
  260. name='directions'
  261. size={24}
  262. color={pageTitleTint}
  263. style={Styles.iconOpacity}
  264. />
  265. </Pressable>
  266. )}
  267. />
  268. <Tab.Navigator
  269. style={styles.container}
  270. screenOptions={{
  271. lazy: false,
  272. lazyPreloadDistance: 1,
  273. ...this.tabBarStyle
  274. }}
  275. backBehavior={() => this.backPage()}
  276. initialRouteName={PagerList.tabCharge}>
  277. { this.pageAdapter.map((item, index) =>
  278. <Tab.Screen
  279. key={index}
  280. name={item.name}
  281. component={item.component}
  282. options={{
  283. title: item.title,
  284. tabBarAllowFontScaling: false
  285. }}
  286. />
  287. )}
  288. </Tab.Navigator>
  289. </View>
  290. );
  291. }
  292. }
  293. const styles = StyleSheet.create({
  294. container: {
  295. flex: 1,
  296. backgroundColor: colorLight
  297. },
  298. tabStyle: {
  299. backgroundColor: app.isWhitelabel ? colorLight : colorPrimary
  300. },
  301. indicator: {
  302. backgroundColor: app.isWhitelabel ? colorPrimary : colorLight
  303. }
  304. })
  305. //export const ChargeStyles = styles;