Notification.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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, View } 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. pageAdapter: []
  24. };
  25. this.pageAdapterAll = [{
  26. title: $t('notification.tabCampaign'),
  27. name: "Campaigns",
  28. component: ListCampaign
  29. }, {
  30. title: $t('notification.tabAlerts'),
  31. name: "Alerts",
  32. component: ListAlerts
  33. }, {
  34. title: AlertUtil.isPromotion ? $t('notification.tabPromotions') : $t("notification.tabArticles"),
  35. name: "News",
  36. component: ListNews
  37. }]
  38. this.pageAdapter = [{
  39. title: $t('notification.tabAlerts'),
  40. name: "Alerts",
  41. component: ListAlerts
  42. }, {
  43. title: AlertUtil.isPromotion ? $t('notification.tabPromotions') : $t("notification.tabArticles"),
  44. name: "News",
  45. component: ListNews
  46. }]
  47. this.tabBarStyle = {
  48. tabBarStyle: styles.tabStyle,
  49. tabBarPressColor: rippleColor,
  50. tabBarScrollEnabled: false,
  51. tabBarIndicatorStyle: styles.indicator,
  52. tabBarActiveTintColor: app.isWhitelabel ? textPrimary : colorLight,
  53. tabBarInactiveTintColor: app.isWhitelabel ? textSecondary : "#E0E0E0"
  54. }
  55. this.isHide = false;
  56. }
  57. componentDidMount() {
  58. this.setState({
  59. pageAdapter: app.v3.showCampaigns ? this.pageAdapterAll : this.pageAdapter
  60. }, () => {
  61. setTimeout(() => {
  62. this.init();
  63. }, 300);
  64. });
  65. this.props.navigation.addListener('focus', () => {
  66. if (this.isHide) {
  67. this.isHide = false;
  68. this.getTotalCount();
  69. }
  70. });
  71. this.props.navigation.addListener('blur', () => {
  72. this.isHide = true;
  73. });
  74. }
  75. componentWillUnmount() {
  76. AlertUtil.release();
  77. //BackHandler.removeEventListener("hardwareBackPress", this.backPage)
  78. }
  79. init() {
  80. utils.setBackClick([this.props?.route?.name, "Campaigns", "Alerts", "Promotions"], this.backPage)
  81. this.getTotalCount();
  82. //BackHandler.addEventListener('hardwareBackPress', this.backPage)
  83. AlertUtil.setOnRefreshListener(() => {
  84. this.getTotalCount();
  85. })
  86. }
  87. backPage = () => {
  88. //const params = this.props.route.params;
  89. if (!this.isHide) {
  90. startPage(PageList.home);
  91. return true;
  92. }
  93. }
  94. getTotalCount() {
  95. apiNotification.getTabDotCount().then(res => {
  96. if (res.data) {
  97. this.setState({
  98. countInfo: res.data
  99. })
  100. AlertUtil.setBadgeCount(res.data)
  101. }
  102. }).catch(err => {
  103. if (res.data) {
  104. this.setState({
  105. countInfo: {}
  106. })
  107. AlertUtil.setBadgeCount()
  108. }
  109. })
  110. }
  111. getBadgeText(index) {
  112. let count = 0;
  113. switch (index) {
  114. case 0:
  115. if (app.v3.showCampaigns) {
  116. count = this.state.countInfo?.campaignUnreadCount;
  117. } else {
  118. count = this.state.countInfo?.alertUnreadCount;
  119. }
  120. break;
  121. case 1:
  122. if (app.v3.showCampaigns) {
  123. count = this.state.countInfo?.alertUnreadCount;
  124. } else {
  125. count = this.state.countInfo?.newsUnreadCount;
  126. }
  127. break
  128. case 2:
  129. count = this.state.countInfo?.newsUnreadCount;
  130. break;
  131. }
  132. if (count > 0) {
  133. return <TextView style={styles.badgeText}>{count}</TextView>
  134. } else {
  135. return <></>
  136. }
  137. }
  138. render() {
  139. const Tab = createMaterialTopTabNavigator();
  140. if (this.state.pageAdapter.length > 0) {
  141. return (
  142. <Tab.Navigator
  143. style={styles.container}
  144. screenOptions={{
  145. lazy: false,
  146. lazyPreloadDistance: 1,
  147. ...this.tabBarStyle
  148. }}
  149. backBehavior={() => this.backPage()}>
  150. { this.state.pageAdapter.map((item, index) =>
  151. <Tab.Screen
  152. key={index}
  153. name={item.name}
  154. component={item.component}
  155. options={{
  156. title: item.title,
  157. tabBarAllowFontScaling: false,
  158. tabBarBadge: () => this.getBadgeText(index)
  159. }}
  160. />
  161. )}
  162. </Tab.Navigator>
  163. );
  164. } else {
  165. return <View></View>
  166. }
  167. }
  168. }
  169. const styles = StyleSheet.create({
  170. container: {
  171. flex: 1,
  172. backgroundColor: colorLight
  173. },
  174. tabStyle: {
  175. backgroundColor: app.isWhitelabel ? colorLight : colorPrimary
  176. },
  177. indicator: {
  178. backgroundColor: app.isWhitelabel ? colorPrimary : colorLight
  179. },
  180. badgeText: {
  181. width: 20,
  182. height: 20,
  183. color: textLight,
  184. fontSize: 10,
  185. marginTop: 4,
  186. marginRight: 8,
  187. borderRadius: 30,
  188. fontWeight: 'bold',
  189. alignItems: 'center',
  190. flexDirection: 'row',
  191. justifyContent: 'center',
  192. backgroundColor: "#FF3B30"
  193. }
  194. })