ChargeAdapter.js 7.6 KB

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