ChargeAdapter.js 6.8 KB

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