Notification.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. AlertUtil.setOnRefreshListener(() => {
  61. this.getTotalCount();
  62. })
  63. }
  64. componentWillUnmount() {
  65. AlertUtil.release();
  66. //BackHandler.removeEventListener("hardwareBackPress", this.backPage)
  67. }
  68. backPage = () => {
  69. //const params = this.props.route.params;
  70. if (!this.isHide) {
  71. startPage(PageList.home);
  72. return true;
  73. }
  74. }
  75. getTotalCount() {
  76. apiNotification.getTabDotCount().then(res => {
  77. if (res.data) {
  78. this.setState({
  79. countInfo: res.data
  80. })
  81. AlertUtil.setBadgeCount(res.data)
  82. }
  83. }).catch(err => {
  84. if (res.data) {
  85. this.setState({
  86. countInfo: {}
  87. })
  88. AlertUtil.setBadgeCount()
  89. }
  90. })
  91. }
  92. getBadgeText(index) {
  93. let count = 0;
  94. switch (index) {
  95. /*case 0:
  96. count = this.state.countInfo?.campaignUnreadCount;
  97. break;*/
  98. case 0:
  99. count = this.state.countInfo?.alertUnreadCount;
  100. break
  101. case 2:
  102. count = this.state.countInfo?.newsUnreadCount;
  103. break;
  104. }
  105. if (count > 0) {
  106. return <TextView style={styles.badgeText}>{count}</TextView>
  107. } else {
  108. return <></>
  109. }
  110. }
  111. render() {
  112. const Tab = createMaterialTopTabNavigator();
  113. return (
  114. <Tab.Navigator
  115. style={styles.container}
  116. screenOptions={{
  117. lazy: false,
  118. lazyPreloadDistance: 1,
  119. ...this.tabBarStyle
  120. }}
  121. backBehavior={() => this.backPage()}>
  122. { this.pageAdapter.map((item, index) =>
  123. <Tab.Screen
  124. key={index}
  125. name={item.name}
  126. component={item.component}
  127. options={{
  128. title: item.title,
  129. tabBarAllowFontScaling: false,
  130. tabBarBadge: () => this.getBadgeText(index)
  131. }}
  132. />
  133. )}
  134. </Tab.Navigator>
  135. );
  136. }
  137. }
  138. const styles = StyleSheet.create({
  139. container: {
  140. flex: 1,
  141. backgroundColor: colorLight
  142. },
  143. tabStyle: {
  144. backgroundColor: app.isWhitelabel ? colorLight : colorPrimary
  145. },
  146. indicator: {
  147. backgroundColor: app.isWhitelabel ? colorPrimary : colorLight
  148. },
  149. badgeText: {
  150. width: 20,
  151. height: 20,
  152. color: textLight,
  153. fontSize: 10,
  154. marginTop: 4,
  155. marginRight: 8,
  156. borderRadius: 30,
  157. fontWeight: 'bold',
  158. alignItems: 'center',
  159. flexDirection: 'row',
  160. justifyContent: 'center',
  161. backgroundColor: "#FF3B30"
  162. }
  163. })