event_vm.dart 4.3 KB

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