ChargeAdapter.js 7.5 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, RefreshControl, ScrollView, StyleSheet } from 'react-native';
  8. import Charge from './TabCharge';
  9. import Reserve from './TabReserve';
  10. import TabInfos from './TabInfos';
  11. import { MyRefreshProps } from '../../components/ThemesConfig';
  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: TabInfos
  40. }, {
  41. title: $t('charging.tabCharge'),
  42. name: "Charge",
  43. component: Charge
  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: app.isWhitelabel ? textPrimary : colorLight,
  59. tabBarInactiveTintColor: app.isWhitelabel ? textSecondary : "#E0E0E0"
  60. }
  61. this.action = "";
  62. this.isHide = false;
  63. this.titleName = "";
  64. }
  65. componentDidMount() {
  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. <ScrollView
  235. style={styles.container}
  236. keyboardShouldPersistTaps={'handled'}
  237. refreshControl={
  238. <RefreshControl
  239. {...MyRefreshProps()}
  240. refreshing={this.state.refreshing}
  241. onRefresh={() => this.onPullRefresh()}
  242. />
  243. }>
  244. <Tab.Navigator
  245. style={{minHeight: $vht(100)}}
  246. screenOptions={{
  247. lazy: false,
  248. lazyPreloadDistance: 1,
  249. ...this.tabBarStyle
  250. }}>
  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. </ScrollView>
  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;