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