| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- /**
- * 充电桩ViewPager适配器
- * @邠心vbe on 2023/02/06
- */
- import React, { Component } from 'react';
- import {createMaterialTopTabNavigator} from '@react-navigation/material-top-tabs';
- import { BackHandler, Pressable, RefreshControl, ScrollView, StyleSheet } from 'react-native';
- import Charge from './TabCharge';
- import Reserve from './TabReserve';
- import TabInfos from './TabInfos';
- import { MyRefreshProps } from '../../components/ThemesConfig';
- import { QRResult } from '../charge/QRScan';
- import apiStation from '../../api/apiStation';
- import PagerUtil from './PagerUtil';
- import utils from '../../utils/utils';
- import { Styles } from '../../components/Toolbar';
- import { getFocusedRouteNameFromRoute } from '@react-navigation/core';
- export const PagerList = {
- "tabInfo": "Info",
- "tabCharge": "Charge",
- "tabReserve": "Reserve",
- "tabExplore": "Explore"
- }
- export default class ChargeAdapter extends Component {
- constructor(props) {
- super(props);
- this.state = {
- showTab: true,
- refreshing: false,
- showContent: false,
- showLoginDialog: false,
- stationInfo: {}
- }
- this.pageAdapter = [{
- title: "Info",
- component: TabInfos
- }, {
- title: "Charge",
- component: Charge
- }/*, {
- title: "Reserve",
- component: Reserve
- }/*, {
- title: "Explore",
- component: Reserve
- }*/]
- this.tabBarStyle = {
- style: styles.tabStyle,
- pressColor: rippleColor,
- scrollEnabled: this.pageAdapter.length > 3,
- indicatorStyle: styles.indicator,
- activeTintColor: textPrimary,
- inactiveTintColor: textSecondary //"#E0E0E0",
- }
- this.isHide = false;
- this.titleName = "";
- }
- componentDidMount() {
- this.props.navigation.addListener('beforeRemove', (e) => {
- this.hideDialog();
- });
- this.props.navigation.addListener('focus', () => {
- if (this.isHide && this.state.showContent) {
- this.updateStationInfo();
- } else {
- //this.canShowLoginDialog();
- }
- this.isHide = false;
- });
- this.props.navigation.addListener('blur', () => {
- this.isHide = true;
- });
- const params = this.props.route.params;
- if (params.action && params.stationInfo) {
- this.action = params.action
- this.setState({
- stationInfo: params.stationInfo
- }, () => {
- this.setPageTitle();
- this.canShowLoginDialog();
- });
- }
- BackHandler.addEventListener('hardwareBackPress', this.backPage)
- PagerUtil.addOnRefresh(this);
- // setTimeout(() => {
- // this.changeTab(1)
- // }, 200);
- }
- componentDidUpdate() {
- /*if (this.state.tabIndex !== this.props.route.state.index) {
- this.setState({
- tabIndex: this.props.route.state.index
- })
- }*/
- var act = this.props.route.params.action;
- if (act !== this.state.action) {
- this.setState({
- action: act
- });
- if (act == 'qr') {
- //this.onEnterStation();
- }
- }
- }
- componentWillUnmount() {
- QRResult.clearResult();
- PagerUtil.onDestory();
- BackHandler.removeEventListener("hardwareBackPress", this.backPage)
- }
- backPage = () => {
- const params = this.props.route.params;
- if (params.from && !this.isHide) {
- startPage(params.from);
- return true;
- }
- }
- canShowLoginDialog() {
- if (isLogin()) {
- this.setState({
- showContent: true
- });
- //if (this.action !== 'view')
- this.updateStationInfo();
- if (this.action == 'qr') {
- // setTimeout(() => {
- // this.changeTab(1)
- // }, 300);
- }
- } else {
- this.setState({
- showLoginDialog: true
- });
- }
- }
- hideDialog(close) {
- this.setState({
- showLoginDialog: false
- });
- if (close) {
- goBack();
- }
- }
- updateStationInfo() {
- navigator.geolocation.getCurrentPosition(location => {
- let params = {
- lat: location.coords.latitude,
- lng: location.coords.longitude,
- sitePk: this.state.stationInfo.id
- }
- apiStation.getStationRate(params).then(res => {
- if (res.data.sitePk) {
- var info = utils.getSiteInfo(res.data);
- PagerUtil.setStationInfo(info);
- PagerUtil.setRefreshing(getFocusedRouteNameFromRoute(this.props.route));
- this.setState({
- refreshing: false,
- stationInfo: info
- }, () => {
- this.setPageTitle();
- });
- }
- }).catch(err => {
- toastLong(err);
- this.setState({
- refreshing: false
- })
- });
- });
- }
- setPageTitle() {
- if (!this.titleName) {
- this.titleName = this.state.stationInfo.name;
- this.props.navigation.setOptions({
- headerTitle: this.titleName,
- headerRight: () => (
- <Pressable
- style={Styles.backIcon}
- android_ripple={rippleLessIcon}
- onPress={() => utils.directMaps(this.state.stationInfo.latitude, this.state.stationInfo.longitude, this.state.stationInfo.address)}>
- <MaterialIcons
- name='directions'
- size={24}
- color={colorLight}
- style={Styles.iconOpacity}
- />
- </Pressable>
- )
- })
- }
- }
- getAvailable(type) {
- const all = this.state.stationInfo.allConnector;
- if (all) {
- if (type == 'box') {
- return all.boxAvailable + '/' + all.boxAll;
- } else {
- return all.available + '/' + all.all;
- }
- } else {
- return '0/0';
- }
- }
- onPullRefresh() {
- this.updateStationInfo();
- }
- onBackRefresh() {
- this.onPullRefresh();
- }
- onEnterStation() {
- this.changeTab(1);
- }
- changeTab(index) {
- this.props.navigation.navigate(this.pageAdapter[index]?.title);
- /*this.setState({
- tabIndex: index
- })*/
- }
-
- render() {
- const Tab = createMaterialTopTabNavigator();
- return (
- <ScrollView
- style={styles.container}
- keyboardShouldPersistTaps={'handled'}
- refreshControl={
- <RefreshControl
- {...MyRefreshProps}
- refreshing={this.state.refreshing}
- onRefresh={() => this.onPullRefresh()}
- />
- }>
- <Tab.Navigator
- style={{minHeight: $vht(100)}}
- tabBarOptions={this.tabBarStyle}
- lazy={false}
- lazyPreloadDistance={1}>
- { this.pageAdapter.map((item, index) =>
- <Tab.Screen
- key={index}
- name={item.title}
- component={item.component}
- />
- )}
- </Tab.Navigator>
- </ScrollView>
- );
- }
- }
- const styles = StyleSheet.create({
- container: {
- backgroundColor: colorLight
- },
- tabStyle: {
- backgroundColor: colorLight //colorPrimary
- },
- indicator: {
- backgroundColor: colorAccent //colorLight
- }
- })
-
- //export const ChargeStyles = styles;
|