ListNews.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. * 新闻信息列表
  3. * @邠心vbe on 2023/08/17
  4. */
  5. import React, { Component } from 'react';
  6. import { Text, FlatList, StyleSheet, RefreshControl, PixelRatio, View } from 'react-native';
  7. import apiArticle from '../../api/apiArticle';
  8. import { MyRefreshProps } from '../../components/ThemesConfig';
  9. import { PageList } from '../Router';
  10. import AlertUtil from './AlertUtil';
  11. import ItemArticle from './ItemArticle';
  12. export default class ListNews extends Component {
  13. constructor(props) {
  14. super(props);
  15. this.state = {
  16. dataList: [],
  17. refreshing: false
  18. };
  19. }
  20. componentDidMount() {
  21. //this.getMessageList();
  22. this.props.navigation.addListener('focus', () => {
  23. this.getMessageList();
  24. });
  25. setTimeout(() => {
  26. this.getMessageList();
  27. }, 500);
  28. }
  29. toDetail(item={}) {
  30. if (item.articleId) {
  31. startPage(PageList.viewArticle, {id: item.articleId});
  32. }
  33. }
  34. onRefresh() {
  35. this.setState({
  36. refreshing: true
  37. })
  38. this.getMessageList();
  39. }
  40. getMessageList() {
  41. apiArticle.getArticleList().then(res => {
  42. if (res.data) {
  43. this.setState({
  44. dataList: res.data
  45. })
  46. }
  47. }).catch(err => {
  48. toastShort(err)
  49. }).finally(() => {
  50. this.setState({
  51. refreshing: false
  52. })
  53. })
  54. }
  55. getMessageListPage() {
  56. const count = AlertUtil.getNewsCount();
  57. if (this.state.dataList.length > 0 && this.state.dataList.length < count) {
  58. const last = this.state.dataList[this.state.dataList.length-1]
  59. if (last.articleId) {
  60. apiArticle.getArticleList(last.articleId).then(res => {
  61. if (res.data) {
  62. const list = [
  63. ...this.state.dataList,
  64. ]
  65. list.push(...res.data)
  66. this.setState({
  67. dataList: list
  68. })
  69. }
  70. }).catch(err => {
  71. toastShort(err)
  72. })
  73. }
  74. }
  75. }
  76. listItem = (props) => {
  77. return (
  78. <ItemArticle
  79. {...props}
  80. onPress={() => this.toDetail(props.item)}
  81. />
  82. )
  83. }
  84. divideView = (props) => {
  85. return (<View style={{height: 1.5/PixelRatio.get()}}></View>)
  86. }
  87. render() {
  88. return (
  89. <FlatList
  90. style={styles.listView}
  91. data={this.state.dataList}
  92. renderItem={this.listItem}
  93. ItemSeparatorComponent={this.divideView}
  94. keyExtractor={item => item.articleId}
  95. onEndReached={() => this.getMessageListPage()}
  96. onEndReachedThreshold={0.3}
  97. refreshControl={
  98. <RefreshControl
  99. {...MyRefreshProps()}
  100. refreshing={this.state.refreshing}
  101. onRefresh={() => this.onRefresh()}
  102. />
  103. }
  104. ListEmptyComponent={<Text style={styles.noData}>{$t('notification.empty')}</Text>}
  105. />
  106. );
  107. }
  108. }
  109. const styles = StyleSheet.create({
  110. listView: {
  111. flex: 1
  112. },
  113. noData: {
  114. color: textPlacehoder,
  115. fontSize: 14,
  116. padding: 20,
  117. textAlign: 'center'
  118. }
  119. })