ChargeAdapter.js 8.1 KB

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