event_vm.dart 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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/event_state.dart';
  6. import '../repository/event_repository.dart';
  7. part 'event_vm.g.dart';
  8. @riverpod
  9. class EventVm extends _$EventVm {
  10. late EventRepository eventRepository;
  11. EventState initState() {
  12. return EventState(
  13. curPage: 1,
  14. pageSize: 10,
  15. list: [
  16. {
  17. "id": 1,
  18. "title":
  19. "The community will hold the activity of making Zongzi on the Loong Boat ……",
  20. "price": "Monday 14 0ct 2024, 15:00 PM~18:00PM",
  21. },
  22. {
  23. "id": 2,
  24. "title": "Community basketball competition activities",
  25. "price": "Monday 14 Oct 2024, 10:19 AM",
  26. },
  27. ],
  28. filterCount: 2,
  29. );
  30. }
  31. @override
  32. EventState build() {
  33. // 引入数据仓库
  34. eventRepository = ref.read(eventRepositoryProvider);
  35. // 初始化状态
  36. EventState state = initState();
  37. // 初始化列表数据
  38. return state;
  39. }
  40. // 初始化页面数据
  41. initPageData() {
  42. Log.d("----property_news_vm-----initPageData");
  43. refreshListData();
  44. }
  45. // 上拉加载
  46. Future onLoadData() async {
  47. Log.d("----property_news_vm-----initListData");
  48. // await Future.delayed(const Duration(seconds: 2));
  49. // if(state.list.length >= state.filterCount){
  50. // return;
  51. // }else {
  52. // int curPage = state.curPage + 1;
  53. // state = state.copyWith(curPage: curPage,);
  54. // getListData();
  55. // }
  56. getListData();
  57. }
  58. // 去新闻详情页
  59. void goNewsDetail(String item) {
  60. Log.d(item);
  61. // PropertyPage.startInstance(context: context, item: item);
  62. }
  63. // 获取list 列表数据
  64. void getListData<T>() async {
  65. Log.d("加载listData数据---------------start-----");
  66. try {
  67. //请求网络
  68. Map<String, dynamic> params = {
  69. "curPage": state.curPage,
  70. "pageSize": state.pageSize,
  71. };
  72. Log.d("请求参数------$params");
  73. final result = await eventRepository.fetchPropertyNewsList(params);
  74. Log.d("请求完成结果------${result.data}");
  75. //校验成功失败
  76. if (result.isSuccess) {
  77. // state = state.copyWith(serverTime: result.data);
  78. state = state;
  79. ToastEngine.show("获取数据成功");
  80. } else {
  81. ToastEngine.show(result.errorMsg ?? "Network Load Error");
  82. }
  83. } catch (e) {
  84. ToastEngine.show("Error: $e");
  85. }
  86. }
  87. // 下拉刷新
  88. Future refreshListData() async {
  89. Log.d("----property_news_vm-----refreshListData ");
  90. // await Future.delayed(const Duration(seconds: 2));
  91. state = state.copyWith(curPage: 1, pageSize: 10);
  92. // ref.invalidateSelf();
  93. // ref.invalidate(propertyNewsVmProvider);
  94. getListData();
  95. }
  96. }