Notification.js 5.8 KB

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