ChargeAdapter.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. utils.setBackClick([this.props?.route?.name, PagerList.tabCharge, PagerList.tabInfo, PagerList.tabReserve], this.backPage)
  67. this.props.navigation.addListener('beforeRemove', (e) => {
  68. this.hideDialog();
  69. });
  70. this.props.navigation.addListener('focus', () => {
  71. if (this.isHide && this.state.showContent) {
  72. this.updateStationInfo();
  73. } else {
  74. //this.canShowLoginDialog();
  75. }
  76. this.isHide = false;
  77. });
  78. this.props.navigation.addListener('blur', () => {
  79. this.isHide = true;
  80. });
  81. const params = this.props.route.params;
  82. if (params.action && params.stationInfo) {
  83. this.action = params.action
  84. this.setState({
  85. stationInfo: params.stationInfo
  86. }, () => {
  87. this.setPageTitle();
  88. this.canShowLoginDialog();
  89. });
  90. }
  91. BackHandler.addEventListener('hardwareBackPress', this.backPage)
  92. PagerUtil.addOnRefresh(this);
  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(getFocusedRouteNameFromRoute(this.props.route));
  164. this.setState({
  165. refreshing: false,
  166. stationInfo: info
  167. }, () => {
  168. this.setPageTitle();
  169. });
  170. if (!PagerUtil.ENABLE_NEW_UI && this.action == 'qr') {
  171. setTimeout(() => {
  172. this.changeTab(1)
  173. }, 300);
  174. }
  175. }
  176. }).catch((err) => {
  177. toastLong(err);
  178. this.setState({
  179. refreshing: false
  180. })
  181. });
  182. }
  183. setPageTitle() {
  184. if (!this.titleName) {
  185. this.titleName = this.state.stationInfo.name;
  186. this.props.navigation.setOptions({
  187. headerTitle: () => (<HeaderTitle title={this.titleName}/>),
  188. headerRight: () => (
  189. <Pressable
  190. style={Styles.backIcon}
  191. android_ripple={rippleLessIcon}
  192. onPress={() => utils.directMaps(this.state.stationInfo.latitude, this.state.stationInfo.longitude, this.state.stationInfo.address)}>
  193. <MaterialIcons
  194. name='directions'
  195. size={24}
  196. color={pageTitleTint}
  197. style={Styles.iconOpacity}
  198. />
  199. </Pressable>
  200. )
  201. })
  202. }
  203. }
  204. getAvailable(type) {
  205. const all = this.state.stationInfo.allConnector;
  206. if (all) {
  207. if (type == 'box') {
  208. return all.boxAvailable + '/' + all.boxAll;
  209. } else {
  210. return all.available + '/' + all.all;
  211. }
  212. } else {
  213. return '0/0';
  214. }
  215. }
  216. onPullRefresh() {
  217. this.updateStationInfo();
  218. }
  219. onBackRefresh() {
  220. this.onPullRefresh();
  221. }
  222. onEnterStation() {
  223. this.changeTab(1);
  224. }
  225. changeTab(index) {
  226. console.log("[changeTab]", index);
  227. this.props.navigation.navigate(this.pageAdapter[index]?.name);
  228. /*this.setState({
  229. tabIndex: index
  230. })*/
  231. }
  232. render() {
  233. const Tab = createMaterialTopTabNavigator();
  234. return (
  235. <ScrollView
  236. style={styles.container}
  237. keyboardShouldPersistTaps={'handled'}
  238. refreshControl={
  239. <RefreshControl
  240. {...MyRefreshProps()}
  241. refreshing={this.state.refreshing}
  242. onRefresh={() => this.onPullRefresh()}
  243. />
  244. }>
  245. <Tab.Navigator
  246. style={{minHeight: $vht(100)}}
  247. screenOptions={{
  248. lazy: false,
  249. lazyPreloadDistance: 1,
  250. ...this.tabBarStyle
  251. }}
  252. initialRouteName={PagerList.tabCharge}>
  253. { this.pageAdapter.map((item, index) =>
  254. <Tab.Screen
  255. key={index}
  256. name={item.name}
  257. component={item.component}
  258. options={{
  259. title: item.title,
  260. tabBarAllowFontScaling: false
  261. }}
  262. />
  263. )}
  264. </Tab.Navigator>
  265. </ScrollView>
  266. );
  267. }
  268. }
  269. const styles = StyleSheet.create({
  270. container: {
  271. backgroundColor: colorLight
  272. },
  273. tabStyle: {
  274. backgroundColor: app.isWhitelabel ? colorLight : colorPrimary
  275. },
  276. indicator: {
  277. backgroundColor: app.isWhitelabel ? colorPrimary : colorLight
  278. }
  279. })
  280. //export const ChargeStyles = styles;