announ_vm.dart 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import 'package:domain/entity/notice_board_announ_entity.dart';
  2. import 'package:plugin_platform/http/http_result.dart';
  3. import 'package:riverpod_annotation/riverpod_annotation.dart';
  4. import 'package:shared/utils/log_utils.dart';
  5. import 'package:plugin_platform/engine/toast/toast_engine.dart';
  6. import 'package:widgets/load_state_layout.dart';
  7. import 'package:widgets/widget_export.dart';
  8. import '../page/announ_state.dart';
  9. import '../repository/announ_repository.dart';
  10. part 'announ_vm.g.dart';
  11. @riverpod
  12. class AnnounVm extends _$AnnounVm {
  13. late AnnounRepository announRepository;
  14. var page = 1;
  15. var limit = 10;
  16. bool _needShowPlaceholder = false; //是否展示LoadingView
  17. // Refresh 控制器
  18. final EasyRefreshController refreshController = EasyRefreshController(
  19. controlFinishRefresh: true, //允许刷新
  20. controlFinishLoad: true, //允许加载
  21. );
  22. AnnounState initState() {
  23. return AnnounState(
  24. list: [],
  25. );
  26. }
  27. @override
  28. AnnounState build() {
  29. // 引入数据仓库
  30. announRepository = ref.read(announRepositoryProvider);
  31. // 初始化状态
  32. AnnounState state = initState();
  33. // 初始化列表数据
  34. return state;
  35. }
  36. //刷新页面状态
  37. void changeLoadingState(LoadState loadState, String? errorMsg) {
  38. state = state.copyWith(loadingState: loadState, errorMessage: errorMsg);
  39. }
  40. // 初始化页面数据&0
  41. initPageData() {
  42. Log.d("----property_news_vm-----initPageData");
  43. onRefresh();
  44. }
  45. // 上拉加载 更多
  46. Future loadMore() async {
  47. Log.d("----property_news_vm-----loadMore");
  48. // await Future.delayed(const Duration(seconds: 2));
  49. // if(state.list.length >= state.count){
  50. // return;
  51. // }else {
  52. // int page = page + 1;
  53. // state = state.copyWith(page: page,);
  54. // getListData();
  55. // }
  56. // 检查 page 是否为 null,并初始化为 1
  57. page++;
  58. getListData();
  59. }
  60. // 下拉刷新
  61. Future onRefresh() async {
  62. Log.d("----property_news_vm-----onRefresh ");
  63. // await Future.delayed(const Duration(seconds: 2));
  64. page = 1;
  65. getListData();
  66. }
  67. // 重试请求
  68. Future retryRequest() async {
  69. page = 1;
  70. _needShowPlaceholder = true;
  71. getListData();
  72. }
  73. // 获取list 列表数据
  74. Future getListData<T>() async {
  75. // if (_needShowPlaceholder) {
  76. // changeLoadingState(LoadState.State_Loading, null);
  77. // }
  78. Log.d("加载listData数据---------------start--${page}---");
  79. try {
  80. //请求网络
  81. Map<String, dynamic> params = {
  82. "page": page,
  83. "limit": limit,
  84. };
  85. Log.d("请求参数------$params");
  86. final result = await announRepository.fetchList(params);
  87. //校验成功失败
  88. if (result.isSuccess) {
  89. handlerResultList((result.data as NoticeBoardAnnounEntity).list);
  90. } else {
  91. String errorMessage = result.errorMsg!;
  92. changeLoadingState(LoadState.State_Error, errorMessage);
  93. ToastEngine.show(result.errorMsg ?? "Network Load Error");
  94. }
  95. } catch (e) {
  96. ToastEngine.show("Error: $e");
  97. }
  98. }
  99. void handlerResultList(List<NoticeBoardAnnounList>? list) {
  100. if (list != null && list.isNotEmpty) {
  101. //有数据,判断是刷新还是加载更多的数据
  102. if (page == 1) {
  103. //刷新的方式
  104. state.list!.clear();
  105. state.list!.addAll(list.map((item) => item.toJson()).toList());
  106. refreshController.finishRefresh();
  107. //更新展示的状态
  108. changeLoadingState(LoadState.State_Success, null);
  109. } else {
  110. //加载更多
  111. final allList = state.list;
  112. state = state.copyWith(list: allList);
  113. refreshController.finishLoad();
  114. // update();
  115. }
  116. } else {
  117. if (page == 1) {
  118. //展示无数据的布局
  119. state.list!.clear();
  120. changeLoadingState(LoadState.State_Empty, null);
  121. refreshController.finishRefresh();
  122. } else {
  123. //展示加载完成,没有更多数据了
  124. if (state.list!.length == 0) {
  125. changeLoadingState(LoadState.State_Empty, null);
  126. } else {
  127. changeLoadingState(LoadState.State_Success, null);
  128. }
  129. refreshController.finishLoad(IndicatorResult.noMore);
  130. }
  131. }
  132. }
  133. }