ChargeAdapter.js 7.6 KB

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