Notification.js 5.5 KB

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