ChargeAdapter.js 6.9 KB

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