property_news_vm.dart 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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/property_news_state.dart';
  6. import '../repository/property_news_repository.dart';
  7. part 'property_news_vm.g.dart';
  8. @riverpod
  9. class PropertyNewsVm extends _$PropertyNewsVm {
  10. late PropertyNewsRepository propertyNewsRepository;
  11. PropertyNewsState initState() {
  12. return PropertyNewsState(
  13. curPage: 1,
  14. pageSize: 10,
  15. list: [
  16. {
  17. "id": 1,
  18. "title": "fkladsfk fldask fldsakfllfkaslsd",
  19. "description": "fsklfdsk罚款乱收费上课了发送卡",
  20. "time": "2024-02-15 12:00:00",
  21. "isCollection": true,
  22. "pic": ""
  23. },
  24. {
  25. "id": 2,
  26. "title": "JHKFDSAJKjfkdsfjkasjkjklfajfkajifwoqirujweiqofjndsaikfniasdhfiasdhfiadshfifjadslfjkdlsafjlkadsj",
  27. "description": "oifosjf fjdskafj hjiwehfriohjfiash",
  28. "time": "2024-10-16 12:00:00",
  29. "isCollection": false,
  30. "pic": ""
  31. },
  32. ],
  33. filterCount: 2,
  34. );
  35. }
  36. @override
  37. PropertyNewsState build() {
  38. // 引入数据仓库
  39. propertyNewsRepository = ref.read(propertyNewsRepositoryProvider);
  40. // 初始化状态
  41. PropertyNewsState state = initState();
  42. // 初始化列表数据
  43. return state;
  44. }
  45. // 初始化页面数据
  46. initPageData() {
  47. Log.d("----property_news_vm-----initPageData");
  48. refreshListData();
  49. }
  50. // 上拉加载
  51. Future onLoadData() async {
  52. Log.d("----property_news_vm-----initListData");
  53. // await Future.delayed(const Duration(seconds: 2));
  54. // if(state.list.length >= state.filterCount){
  55. // return;
  56. // }else {
  57. // int curPage = state.curPage + 1;
  58. // state = state.copyWith(curPage: curPage,);
  59. // getListData();
  60. // }
  61. getListData();
  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 propertyNewsRepository.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. // 去新闻详情页
  97. void goNewsDetail(String item) {
  98. Log.d("goNewsDetail");
  99. // PropertyPage.startInstance(context: context, item: item);
  100. }
  101. // 收藏/取消收藏
  102. void handlerCollection(curItem, bool isCollection){
  103. List<Map<String, dynamic>> newList = state.list.map((item) {
  104. if(item['id'] == curItem['id']){
  105. return {
  106. ...item,
  107. 'isCollection': !isCollection
  108. };
  109. }
  110. return item;
  111. }).toList();
  112. // Log.d("handlerCollection $newList");
  113. state = state.copyWith(list: newList);
  114. if(isCollection){
  115. ToastEngine.show("取消收藏");
  116. }else {
  117. ToastEngine.show("收藏成功");
  118. }
  119. }
  120. }