documents_vm.dart 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import 'package:domain/entity/notice_board_documents_entity.dart';
  2. import 'package:riverpod_annotation/riverpod_annotation.dart';
  3. import 'package:shared/utils/log_utils.dart';
  4. import 'package:plugin_platform/engine/toast/toast_engine.dart';
  5. import 'package:url_launcher/url_launcher.dart';
  6. import 'package:widgets/load_state_layout.dart';
  7. import 'package:widgets/widget_export.dart';
  8. import '../page/documents_state.dart';
  9. import '../repository/documents_repository.dart';
  10. part 'documents_vm.g.dart';
  11. @riverpod
  12. class DocumentsVm extends _$DocumentsVm {
  13. late DocumentsRepository documentsRepository;
  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. DocumentsState initState() {
  23. // 初始化页面数据&0
  24. return DocumentsState(
  25. list: [],
  26. );
  27. }
  28. @override
  29. DocumentsState build() {
  30. // 引入数据仓库
  31. documentsRepository = ref.read(documentsRepositoryProvider);
  32. // 初始化状态
  33. DocumentsState 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. "parent_id": 0
  86. };
  87. Log.d("请求参数------$params");
  88. final result = await documentsRepository.fetchList(params);
  89. //校验成功失败
  90. if (result.isSuccess) {
  91. handlerResultList((result.data as NoticeBoardDocumentsEntity).list);
  92. } else {
  93. String errorMessage = result.errorMsg!;
  94. changeLoadingState(LoadState.State_Error, errorMessage);
  95. ToastEngine.show(result.errorMsg ?? "Network Load Error");
  96. }
  97. } catch (e) {
  98. ToastEngine.show("Error: $e");
  99. }
  100. }
  101. void handlerResultList(List<NoticeBoardDocumentsList>? list) {
  102. if (list != null && list.isNotEmpty) {
  103. //有数据,判断是刷新还是加载更多的数据
  104. if (page == 1) {
  105. //刷新的方式
  106. state.list!.clear();
  107. state.list!.addAll(list.map((item) => item.toJson()).toList());
  108. refreshController.finishRefresh();
  109. //更新展示的状态
  110. changeLoadingState(LoadState.State_Success, null);
  111. } else {
  112. //加载更多
  113. final allList = state.list;
  114. state = state.copyWith(list: allList);
  115. refreshController.finishLoad();
  116. // update();
  117. }
  118. } else {
  119. if (page == 1) {
  120. //展示无数据的布局
  121. state.list!.clear();
  122. changeLoadingState(LoadState.State_Empty, null);
  123. refreshController.finishRefresh();
  124. } else {
  125. //展示加载完成,没有更多数据了
  126. if (state.list!.length == 0) {
  127. changeLoadingState(LoadState.State_Empty, null);
  128. } else {
  129. changeLoadingState(LoadState.State_Success, null);
  130. }
  131. refreshController.finishLoad(IndicatorResult.noMore);
  132. }
  133. }
  134. }
  135. // 重试请求
  136. void launchURL(url) async {
  137. // const url = 'https://flutterchina.club/';
  138. if (await canLaunch(url)) {
  139. await launch(
  140. url,
  141. // Add optional `forceWebView` flag for Android to use a WebView
  142. forceWebView: true,
  143. // Add optional `enableBarColors` flag for iOS to change the color of the bars
  144. enableJavaScript: true,
  145. // Add optional `chromeCustomTabs` parameter for Android to customize Chrome Custom Tabs
  146. enableDomStorage: true,
  147. universalLinksOnly: true,
  148. forceSafariVC: true,
  149. );
  150. } else {
  151. throw 'Could not launch $url';
  152. }
  153. }
  154. }