ChargeAdapter.js 7.2 KB

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