ChargeAdapter.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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: app.isWhitelabel ? textPrimary : colorLight,
  58. tabBarInactiveTintColor: app.isWhitelabel ? textSecondary : "#E0E0E0"
  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(getFocusedRouteNameFromRoute(this.props.route));
  163. this.setState({
  164. refreshing: false,
  165. stationInfo: info
  166. }, () => {
  167. this.setPageTitle();
  168. });
  169. if (!PagerUtil.ENABLE_NEW_UI && this.action == 'qr') {
  170. setTimeout(() => {
  171. this.changeTab(1)
  172. }, 300);
  173. }
  174. }
  175. }).catch((err) => {
  176. toastLong(err);
  177. this.setState({
  178. refreshing: false
  179. })
  180. });
  181. }
  182. setPageTitle() {
  183. if (!this.titleName) {
  184. this.titleName = this.state.stationInfo.name;
  185. this.props.navigation.setOptions({
  186. headerTitle: () => (<HeaderTitle title={this.titleName}/>),
  187. headerRight: () => (
  188. <Pressable
  189. style={Styles.backIcon}
  190. android_ripple={rippleLessIcon}
  191. onPress={() => utils.directMaps(this.state.stationInfo.latitude, this.state.stationInfo.longitude, this.state.stationInfo.address)}>
  192. <MaterialIcons
  193. name='directions'
  194. size={24}
  195. color={pageTitleTint}
  196. style={Styles.iconOpacity}
  197. />
  198. </Pressable>
  199. )
  200. })
  201. }
  202. }
  203. getAvailable(type) {
  204. const all = this.state.stationInfo.allConnector;
  205. if (all) {
  206. if (type == 'box') {
  207. return all.boxAvailable + '/' + all.boxAll;
  208. } else {
  209. return all.available + '/' + all.all;
  210. }
  211. } else {
  212. return '0/0';
  213. }
  214. }
  215. onPullRefresh() {
  216. this.updateStationInfo();
  217. }
  218. onBackRefresh() {
  219. this.onPullRefresh();
  220. }
  221. onEnterStation() {
  222. this.changeTab(1);
  223. }
  224. changeTab(index) {
  225. console.log("[changeTab]", index);
  226. this.props.navigation.navigate(this.pageAdapter[index]?.name);
  227. /*this.setState({
  228. tabIndex: index
  229. })*/
  230. }
  231. render() {
  232. const Tab = createMaterialTopTabNavigator();
  233. return (
  234. <Tab.Navigator
  235. style={styles.container}
  236. screenOptions={{
  237. lazy: false,
  238. lazyPreloadDistance: 1,
  239. ...this.tabBarStyle
  240. }}
  241. backBehavior={() => this.backPage()}
  242. initialRouteName={PagerList.tabCharge}>
  243. { this.pageAdapter.map((item, index) =>
  244. <Tab.Screen
  245. key={index}
  246. name={item.name}
  247. component={item.component}
  248. options={{
  249. title: item.title,
  250. tabBarAllowFontScaling: false
  251. }}
  252. />
  253. )}
  254. </Tab.Navigator>
  255. );
  256. }
  257. }
  258. const styles = StyleSheet.create({
  259. container: {
  260. backgroundColor: colorLight
  261. },
  262. tabStyle: {
  263. backgroundColor: app.isWhitelabel ? colorLight : colorPrimary
  264. },
  265. indicator: {
  266. backgroundColor: app.isWhitelabel ? colorPrimary : colorLight
  267. }
  268. })
  269. //export const ChargeStyles = styles;