ListNews.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. import VbeSkeleton from '../../components/VbeSkeleton';
  13. export default class ListNews extends Component {
  14. constructor(props) {
  15. super(props);
  16. this.state = {
  17. dataList: [],
  18. loading: true,
  19. refreshing: false,
  20. loadingList: ["", "", "", ""]
  21. };
  22. }
  23. componentDidMount() {
  24. //this.getMessageList();
  25. this.props.navigation.addListener('focus', () => {
  26. this.getMessageList();
  27. });
  28. setTimeout(() => {
  29. this.getMessageList();
  30. }, 500);
  31. }
  32. toDetail(item={}) {
  33. if (item.articleId) {
  34. startPage(PageList.viewArticle, {id: item.articleId});
  35. }
  36. }
  37. onRefresh() {
  38. this.setState({
  39. refreshing: true
  40. })
  41. this.getMessageList();
  42. }
  43. getMessageList() {
  44. apiArticle.getArticleList().then(res => {
  45. if (res.data) {
  46. this.setState({
  47. dataList: res.data
  48. })
  49. }
  50. }).catch(err => {
  51. toastShort(err)
  52. }).finally(() => {
  53. this.setState({
  54. loading: false,
  55. refreshing: false
  56. })
  57. })
  58. }
  59. getMessageListPage() {
  60. const count = AlertUtil.getNewsCount();
  61. if (this.state.dataList.length > 0 && this.state.dataList.length < count) {
  62. const last = this.state.dataList[this.state.dataList.length-1]
  63. if (last.articleId) {
  64. apiArticle.getArticleList(last.articleId).then(res => {
  65. if (res.data) {
  66. const list = [
  67. ...this.state.dataList,
  68. ]
  69. list.push(...res.data)
  70. this.setState({
  71. dataList: list
  72. })
  73. }
  74. }).catch(err => {
  75. toastShort(err)
  76. })
  77. }
  78. }
  79. }
  80. listItem = (props) => {
  81. return (
  82. <ItemArticle
  83. {...props}
  84. onPress={() => this.toDetail(props.item)}
  85. />
  86. )
  87. }
  88. divideView = (props) => {
  89. return (<View style={{height: 1.5/PixelRatio.get()}}></View>)
  90. }
  91. render() {
  92. if (this.state.loading) {
  93. return (
  94. <View style={styles.listView}>
  95. { this.state.loadingList.map((item, index) =>
  96. <View style={styles.loadingView} key={index}>
  97. <VbeSkeleton
  98. style={styles.iconImage}
  99. layout={[
  100. {width: 85, height: 85, borderRadius: 6},
  101. {width: 35, height: 12, marginTop: 4, marginLeft: 25},
  102. ]}
  103. animationDirection={"horizontalRight"}
  104. />
  105. <VbeSkeleton
  106. style={ui.flex1}
  107. layout={[
  108. {width: '100%', height: 20},
  109. {width: '80%', height: 12, marginTop: 6},
  110. {width: '100%', height: 15, marginTop: 12},
  111. {width: '60%', height: 15, marginTop: 6}
  112. ]}
  113. animationDirection={"horizontalRight"}
  114. />
  115. </View>
  116. )}
  117. </View>
  118. )
  119. }
  120. return (
  121. <FlatList
  122. style={styles.listView}
  123. data={this.state.dataList}
  124. renderItem={this.listItem}
  125. ItemSeparatorComponent={this.divideView}
  126. keyExtractor={item => item.articleId}
  127. onEndReached={() => this.getMessageListPage()}
  128. onEndReachedThreshold={0.3}
  129. refreshControl={
  130. <RefreshControl
  131. {...MyRefreshProps()}
  132. refreshing={this.state.refreshing}
  133. onRefresh={() => this.onRefresh()}
  134. />
  135. }
  136. ListEmptyComponent={<Text style={styles.noData}>{$t('notification.empty')}</Text>}
  137. />
  138. );
  139. }
  140. }
  141. const styles = StyleSheet.create({
  142. listView: {
  143. flex: 1
  144. },
  145. noData: {
  146. color: textPlacehoder,
  147. fontSize: 14,
  148. padding: 20,
  149. textAlign: 'center'
  150. },
  151. loadingView: {
  152. padding: 16,
  153. flexDirection: 'row'
  154. },
  155. iconImage: {
  156. width: 85,
  157. height: 85,
  158. borderRadius: 6,
  159. marginRight: 16,
  160. marginBottom: 8
  161. }
  162. })