property_sale_vm.dart 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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_sale_state.dart';
  6. import '../repository/property_sale_repository.dart';
  7. part 'property_sale_vm.g.dart';
  8. @riverpod
  9. class PropertySaleVm extends _$PropertySaleVm {
  10. late PropertySaleRepository propertySaleRepository;
  11. PropertySaleState initState() {
  12. return PropertySaleState(
  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. PropertySaleState build() {
  32. // 引入数据仓库
  33. propertySaleRepository = ref.read(propertySaleRepositoryProvider);
  34. // 初始化状态
  35. PropertySaleState 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. // 获取list 列表数据
  58. void getListData<T>() async {
  59. Log.d("加载listData数据---------------start-----");
  60. try {
  61. //请求网络
  62. Map<String, dynamic> params = {
  63. "curPage": state.curPage,
  64. "pageSize": state.pageSize,
  65. };
  66. Log.d("请求参数------$params");
  67. final result = await propertySaleRepository.fetchPropertyNewsList(params);
  68. Log.d("请求完成结果------${result.data}");
  69. //校验成功失败
  70. if (result.isSuccess) {
  71. // state = state.copyWith(serverTime: result.data);
  72. state = state;
  73. ToastEngine.show("获取数据成功");
  74. } else {
  75. ToastEngine.show(result.errorMsg ?? "Network Load Error");
  76. }
  77. } catch (e) {
  78. ToastEngine.show("Error: $e");
  79. }
  80. }
  81. // 下拉刷新
  82. Future refreshListData() async {
  83. Log.d("----property_news_vm-----refreshListData ");
  84. // await Future.delayed(const Duration(seconds: 2));
  85. state = state.copyWith(curPage: 1, pageSize: 10);
  86. // ref.invalidateSelf();
  87. // ref.invalidate(propertyNewsVmProvider);
  88. getListData();
  89. }
  90. }