announ_vm.dart 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import 'package:plugin_platform/http/http_result.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 '../page/announ_state.dart';
  6. import '../repository/announ_repository.dart';
  7. part 'announ_vm.g.dart';
  8. @riverpod
  9. class AnnounVm extends _$AnnounVm {
  10. late AnnounRepository announRepository;
  11. AnnounState initState() {
  12. return AnnounState(
  13. curPage: 1,
  14. pageSize: 10,
  15. list: [
  16. {
  17. "id": 1,
  18. "title": "Standard Operating procedure For Replacement V...",
  19. "price": "Monday 14 Oct 2024, 10:19 AM",
  20. },
  21. {
  22. "id": 2,
  23. "title": "Removal Of Items In Dry Risers",
  24. "price": "Monday 14 Oct 2024, 10:19 AM",
  25. },
  26. {
  27. "id": 3,
  28. "title": "Fire Safety Awareness And Guidelines",
  29. "price": "Monday 14 Oct 2024, 10:19 AM",
  30. },
  31. {
  32. "id": 4,
  33. "title": "Bicycle Tagging Exercise",
  34. "price": "Monday 14 Oct 2024, 10:19 AM",
  35. },
  36. ],
  37. filterCount: 2,
  38. );
  39. }
  40. @override
  41. AnnounState build() {
  42. // 引入数据仓库
  43. announRepository = ref.read(announRepositoryProvider);
  44. // 初始化状态
  45. AnnounState state = initState();
  46. // 初始化列表数据
  47. return state;
  48. }
  49. // 初始化页面数据
  50. initPageData() {
  51. Log.d("----property_news_vm-----initPageData");
  52. refreshListData();
  53. }
  54. // 上拉加载
  55. Future onLoadData() async {
  56. Log.d("----property_news_vm-----initListData");
  57. // await Future.delayed(const Duration(seconds: 2));
  58. // if(state.list.length >= state.filterCount){
  59. // return;
  60. // }else {
  61. // int curPage = state.curPage + 1;
  62. // state = state.copyWith(curPage: curPage,);
  63. // getListData();
  64. // }
  65. getListData();
  66. }
  67. // 去新闻详情页
  68. void goNewsDetail(String item) {
  69. Log.d(item);
  70. // PropertyPage.startInstance(context: context, item: item);
  71. }
  72. // 获取list 列表数据
  73. void getListData<T>() async {
  74. Log.d("加载listData数据---------------start-----");
  75. try {
  76. //请求网络
  77. Map<String, dynamic> params = {
  78. "curPage": state.curPage,
  79. "pageSize": state.pageSize,
  80. };
  81. Log.d("请求参数------$params");
  82. final result = await announRepository.fetchPropertyNewsList(params);
  83. Log.d("请求完成结果------${result.data}");
  84. //校验成功失败
  85. if (result.isSuccess) {
  86. // state = state.copyWith(serverTime: result.data);
  87. state = state;
  88. ToastEngine.show("获取数据成功");
  89. } else {
  90. ToastEngine.show(result.errorMsg ?? "Network Load Error");
  91. }
  92. } catch (e) {
  93. ToastEngine.show("Error: $e");
  94. }
  95. }
  96. // 下拉刷新
  97. Future refreshListData() async {
  98. Log.d("----property_news_vm-----refreshListData ");
  99. // await Future.delayed(const Duration(seconds: 2));
  100. state = state.copyWith(curPage: 1, pageSize: 10);
  101. // ref.invalidateSelf();
  102. // ref.invalidate(propertyNewsVmProvider);
  103. getListData();
  104. }
  105. }