rewards_search_vm.dart 5.2 KB

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