ChargeAdapter.js 7.5 KB

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