Notification.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /**
  2. * 通知功能页面适配器
  3. * @邠心vbe on 2023/08/17
  4. */
  5. import { createMaterialTopTabNavigator } from '@react-navigation/material-top-tabs';
  6. import React, { Component } from 'react';
  7. import { StyleSheet } from 'react-native';
  8. import { PageList } from '../Router';
  9. import ListAlerts from './ListAlerts';
  10. import ListNews from './ListNews';
  11. import ListCampaign from './ListCampaign';
  12. import app from '../../../app.json';
  13. import utils from '../../utils/utils';
  14. import apiNotification from '../../api/apiNotification';
  15. import TextView from '../../components/TextView';
  16. import AlertUtil from './AlertUtil';
  17. export default class Notification extends Component {
  18. constructor(props) {
  19. super(props);
  20. this.state = {
  21. refreshing: false,
  22. countInfo: {}
  23. };
  24. this.pageAdapter = [/*{
  25. title: $t('notification.tabCampaign'),
  26. name: "Campaigns",
  27. component: ListCampaign
  28. }, */{
  29. title: $t('notification.tabAlerts'),
  30. name: "Alerts",
  31. component: ListAlerts
  32. }, {
  33. title: $t('notification.tabPromotions'),
  34. name: "News",
  35. component: ListNews
  36. }]
  37. this.tabBarStyle = {
  38. tabBarStyle: styles.tabStyle,
  39. tabBarPressColor: rippleColor,
  40. tabBarScrollEnabled: false,
  41. tabBarIndicatorStyle: styles.indicator,
  42. tabBarActiveTintColor: app.isWhitelabel ? textPrimary : colorLight,
  43. tabBarInactiveTintColor: app.isWhitelabel ? textSecondary : "#E0E0E0"
  44. }
  45. this.isHide = false;
  46. }
  47. componentDidMount() {
  48. utils.setBackClick([this.props?.route?.name, "Campaigns", "Alerts", "Promotions"], this.backPage)
  49. this.props.navigation.addListener('focus', () => {
  50. if (this.isHide) {
  51. this.isHide = false;
  52. this.getTotalCount();
  53. }
  54. });
  55. this.props.navigation.addListener('blur', () => {
  56. this.isHide = true;
  57. });
  58. this.getTotalCount();
  59. //BackHandler.addEventListener('hardwareBackPress', this.backPage)
  60. }
  61. componentWillUnmount() {
  62. //BackHandler.removeEventListener("hardwareBackPress", this.backPage)
  63. }
  64. backPage = () => {
  65. //const params = this.props.route.params;
  66. if (!this.isHide) {
  67. startPage(PageList.home);
  68. return true;
  69. }
  70. }
  71. getTotalCount() {
  72. apiNotification.getTabDotCount().then(res => {
  73. if (res.data) {
  74. this.setState({
  75. countInfo: res.data
  76. })
  77. AlertUtil.setBadgeCount(res.data)
  78. }
  79. }).catch(err => {
  80. if (res.data) {
  81. this.setState({
  82. countInfo: {}
  83. })
  84. AlertUtil.setBadgeCount()
  85. }
  86. })
  87. }
  88. getBadgeText(index) {
  89. let count = 0;
  90. switch (index) {
  91. /*case 0:
  92. count = this.state.countInfo?.campaignUnreadCount;
  93. break;*/
  94. case 0:
  95. count = this.state.countInfo?.alertUnreadCount;
  96. break
  97. case 2:
  98. count = this.state.countInfo?.newsUnreadCount;
  99. break;
  100. }
  101. if (count > 0) {
  102. return <TextView style={styles.badgeText}>{count}</TextView>
  103. } else {
  104. return <></>
  105. }
  106. }
  107. render() {
  108. const Tab = createMaterialTopTabNavigator();
  109. return (
  110. <Tab.Navigator
  111. style={styles.container}
  112. screenOptions={{
  113. lazy: false,
  114. lazyPreloadDistance: 1,
  115. ...this.tabBarStyle
  116. }}
  117. backBehavior={() => this.backPage()}>
  118. { this.pageAdapter.map((item, index) =>
  119. <Tab.Screen
  120. key={index}
  121. name={item.name}
  122. component={item.component}
  123. options={{
  124. title: item.title,
  125. tabBarAllowFontScaling: false,
  126. tabBarBadge: () => this.getBadgeText(index)
  127. }}
  128. />
  129. )}
  130. </Tab.Navigator>
  131. );
  132. }
  133. }
  134. const styles = StyleSheet.create({
  135. container: {
  136. flex: 1,
  137. backgroundColor: colorLight
  138. },
  139. tabStyle: {
  140. backgroundColor: app.isWhitelabel ? colorLight : colorPrimary
  141. },
  142. indicator: {
  143. backgroundColor: app.isWhitelabel ? colorPrimary : colorLight
  144. },
  145. badgeText: {
  146. width: 20,
  147. height: 20,
  148. color: textLight,
  149. fontSize: 10,
  150. marginTop: 4,
  151. marginRight: 8,
  152. borderRadius: 30,
  153. fontWeight: 'bold',
  154. alignItems: 'center',
  155. flexDirection: 'row',
  156. justifyContent: 'center',
  157. backgroundColor: "#FF3B30"
  158. }
  159. })