property_rent_vm.dart 2.9 KB

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