ChargeAdapter.js 7.3 KB

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