ListNews.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. }
  26. toDetail(item={}) {
  27. if (item.articleId) {
  28. startPage(PageList.viewArticle, {id: item.articleId});
  29. }
  30. }
  31. onRefresh() {
  32. this.setState({
  33. refreshing: true
  34. })
  35. this.getMessageList();
  36. }
  37. getMessageList() {
  38. apiArticle.getArticleList().then(res => {
  39. if (res.data) {
  40. this.setState({
  41. dataList: res.data
  42. })
  43. }
  44. }).catch(err => {
  45. toastShort(err)
  46. }).finally(() => {
  47. this.setState({
  48. refreshing: false
  49. })
  50. })
  51. }
  52. getMessageListPage() {
  53. const count = AlertUtil.getNewsCount();
  54. if (this.state.dataList.length > 0 && this.state.dataList.length < count) {
  55. const last = this.state.dataList[this.state.dataList.length-1]
  56. if (last.articleId) {
  57. apiArticle.getArticleList(last.articleId).then(res => {
  58. if (res.data) {
  59. const list = [
  60. ...this.state.dataList,
  61. ]
  62. list.push(...res.data)
  63. this.setState({
  64. dataList: list
  65. })
  66. }
  67. }).catch(err => {
  68. toastShort(err)
  69. })
  70. }
  71. }
  72. }
  73. listItem = (props) => {
  74. return (
  75. <ItemArticle
  76. {...props}
  77. onPress={() => this.toDetail(props.item)}
  78. />
  79. )
  80. }
  81. divideView = (props) => {
  82. return (<View style={{height: 1.5/PixelRatio.get()}}></View>)
  83. }
  84. render() {
  85. return (
  86. <FlatList
  87. style={styles.listView}
  88. data={this.state.dataList}
  89. renderItem={this.listItem}
  90. ItemSeparatorComponent={this.divideView}
  91. keyExtractor={item => item.articleId}
  92. onEndReached={() => this.getMessageListPage()}
  93. refreshControl={
  94. <RefreshControl
  95. {...MyRefreshProps()}
  96. refreshing={this.state.refreshing}
  97. onRefresh={() => this.onRefresh()}
  98. />
  99. }
  100. ListEmptyComponent={<Text style={styles.noData}>{$t('notification.empty')}</Text>}
  101. />
  102. );
  103. }
  104. }
  105. const styles = StyleSheet.create({
  106. listView: {
  107. flex: 1
  108. },
  109. noData: {
  110. color: textPlacehoder,
  111. fontSize: 14,
  112. padding: 20,
  113. textAlign: 'center'
  114. }
  115. })