| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- /**
- * 通知功能页面适配器
- * @邠心vbe on 2023/08/17
- */
- import { createMaterialTopTabNavigator } from '@react-navigation/material-top-tabs';
- import React, { Component } from 'react';
- import { StyleSheet, View } from 'react-native';
- import { PageList } from '../Router';
- import ListAlerts from './ListAlerts';
- import ListNews from './ListNews';
- import ListCampaign from './ListCampaign';
- import app from '../../../app.json';
- import utils from '../../utils/utils';
- import apiNotification from '../../api/apiNotification';
- import TextView from '../../components/TextView';
- import AlertUtil from './AlertUtil';
- export default class Notification extends Component {
- constructor(props) {
- super(props);
- this.state = {
- refreshing: false,
- countInfo: {},
- pageAdapter: []
- };
- this.pageAdapterAll = [{
- title: $t('notification.tabCampaign'),
- name: "Campaigns",
- component: ListCampaign
- }, {
- title: $t('notification.tabAlerts'),
- name: "Alerts",
- component: ListAlerts
- }, {
- title: AlertUtil.isPromotion ? $t('notification.tabPromotions') : $t("notification.tabArticles"),
- name: "News",
- component: ListNews
- }]
- this.pageAdapter = [{
- title: $t('notification.tabAlerts'),
- name: "Alerts",
- component: ListAlerts
- }, {
- title: AlertUtil.isPromotion ? $t('notification.tabPromotions') : $t("notification.tabArticles"),
- name: "News",
- component: ListNews
- }]
- this.tabBarStyle = {
- tabBarStyle: styles.tabStyle,
- tabBarPressColor: rippleColor,
- tabBarScrollEnabled: false,
- tabBarIndicatorStyle: styles.indicator,
- tabBarActiveTintColor: app.isWhitelabel ? textPrimary : colorLight,
- tabBarInactiveTintColor: app.isWhitelabel ? textSecondary : "#E0E0E0"
- }
- this.isHide = false;
- }
- componentDidMount() {
- if (app.notifications.showCampaigns && app.notifications.showArticle) {
- this.setState({
- pageAdapter: this.pageAdapterAll
- }, () => {
- setTimeout(() => {
- this.init();
- }, 300);
- });
- } else if (app.notifications.showArticle) {
- this.setState({
- pageAdapter: this.pageAdapter
- }, () => {
- setTimeout(() => {
- this.init();
- }, 300);
- });
- } else {
- this.init();
- }
-
- this.props.navigation.addListener('focus', () => {
- if (this.isHide) {
- this.isHide = false;
- this.getTotalCount();
- }
- });
- this.props.navigation.addListener('blur', () => {
- this.isHide = true;
- });
- }
- componentWillUnmount() {
- AlertUtil.release();
- //BackHandler.removeEventListener("hardwareBackPress", this.backPage)
- }
- init() {
- utils.setBackClick([this.props?.route?.name, "Campaigns", "Alerts", "Promotions"], this.backPage)
- this.getTotalCount();
- //BackHandler.addEventListener('hardwareBackPress', this.backPage)
- AlertUtil.setOnRefreshListener(() => {
- this.getTotalCount();
- })
- }
- backPage = () => {
- //const params = this.props.route.params;
- if (!this.isHide) {
- startPage(PageList.home);
- return true;
- }
- }
- getTotalCount() {
- apiNotification.getTabDotCount().then(res => {
- if (res.data) {
- this.setState({
- countInfo: res.data
- })
- AlertUtil.setBadgeCount(res.data)
- }
- }).catch(err => {
- if (res.data) {
- this.setState({
- countInfo: {}
- })
- AlertUtil.setBadgeCount()
- }
- })
- }
- getBadgeText(index) {
- let count = 0;
- switch (index) {
- case 0:
- if (app.notifications.showCampaigns) {
- count = this.state.countInfo?.campaignUnreadCount;
- } else {
- count = this.state.countInfo?.alertUnreadCount;
- }
- break;
- case 1:
- if (app.notifications.showCampaigns) {
- count = this.state.countInfo?.alertUnreadCount;
- } else {
- count = this.state.countInfo?.newsUnreadCount;
- }
- break
- case 2:
- count = this.state.countInfo?.newsUnreadCount;
- break;
- }
- if (count > 0) {
- return <TextView style={styles.badgeText}>{count}</TextView>
- } else {
- return <></>
- }
- }
- render() {
- const Tab = createMaterialTopTabNavigator();
- if (this.state.pageAdapter.length > 0) {
- return (
- <Tab.Navigator
- style={styles.container}
- screenOptions={{
- lazy: false,
- lazyPreloadDistance: 1,
- ...this.tabBarStyle
- }}
- backBehavior={() => this.backPage()}>
- { this.state.pageAdapter.map((item, index) =>
- <Tab.Screen
- key={index}
- name={item.name}
- component={item.component}
- options={{
- title: item.title,
- tabBarAllowFontScaling: false,
- tabBarBadge: () => this.getBadgeText(index)
- }}
- />
- )}
- </Tab.Navigator>
- );
- } else {
- return <ListAlerts {...this.props} showUnread={true}/>
- }
- }
- }
- const styles = StyleSheet.create({
- container: {
- flex: 1,
- backgroundColor: colorLight
- },
- tabStyle: {
- backgroundColor: app.isWhitelabel ? colorLight : colorPrimary
- },
- indicator: {
- backgroundColor: app.isWhitelabel ? colorPrimary : colorLight
- },
- badgeText: {
- width: 20,
- height: 20,
- color: textLight,
- fontSize: 10,
- marginTop: 4,
- marginRight: 8,
- borderRadius: 30,
- fontWeight: 'bold',
- alignItems: 'center',
- flexDirection: 'row',
- justifyContent: 'center',
- backgroundColor: "#FF3B30"
- }
- })
|