ListNews.js 4.3 KB

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