ChargeAdapter.js 7.5 KB

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