Notification.js 5.9 KB

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