rewards_home_vm.dart 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import 'package:cs_resources/generated/assets.dart';
  2. import 'package:domain/entity/rewards_home_entity.dart';
  3. import 'package:domain/entity/rewards_home_tx_entity.dart';
  4. import 'package:domain/entity/rewards_list_entity.dart';
  5. import 'package:domain/entity/text_kon_entity.dart';
  6. import 'package:flutter/material.dart';
  7. import 'package:plugin_platform/http/http_result.dart';
  8. import 'package:riverpod_annotation/riverpod_annotation.dart';
  9. import 'package:shared/utils/log_utils.dart';
  10. import 'package:plugin_platform/engine/toast/toast_engine.dart';
  11. import 'package:widgets/load_state_layout.dart';
  12. import 'package:widgets/picker/option_pick_util.dart';
  13. import 'package:widgets/widget_export.dart';
  14. import './rewards_home_state.dart';
  15. import './rewards_home_repository.dart';
  16. part 'rewards_home_vm.g.dart';
  17. @riverpod
  18. class RewardsHomeVm extends _$RewardsHomeVm {
  19. late RewardsHomeRepository rewardsHomeRepository;
  20. bool _needShowPlaceholder = false; //是否展示LoadingView
  21. // Refresh 控制器
  22. final EasyRefreshController refreshController = EasyRefreshController(
  23. controlFinishRefresh: true, //允许刷新
  24. controlFinishLoad: true, //允许加载
  25. );
  26. RewardsHomeState initState() {
  27. return RewardsHomeState(
  28. list: [],
  29. lists: [
  30. {'title': 'YY', 'icon': Assets.rewardsRewardsIndex1},
  31. {'title': 'F&B', 'icon': Assets.rewardsRewardsIndex2},
  32. {'title': 'F&B', 'icon': Assets.rewardsRewardsIndex3},
  33. {'title': 'Beauty', 'icon': Assets.rewardsRewardsIndex4},
  34. {'title': 'Activites', 'icon': Assets.rewardsRewardsIndex5},
  35. {'title': 'Travel', 'icon': Assets.rewardsRewardsIndex6},
  36. {'title': 'Electronics', 'icon': Assets.rewardsRewardsIndex7},
  37. {'title': 'Gifts', 'icon': Assets.rewardsRewardsIndex8},
  38. {'title': 'Services', 'icon': Assets.rewardsRewardsIndex9},
  39. ],
  40. );
  41. }
  42. @override
  43. RewardsHomeState build() {
  44. // 引入数据仓库
  45. rewardsHomeRepository = ref.read(rewardsHomeRepositoryProvider);
  46. // 初始化状态
  47. RewardsHomeState state = initState();
  48. // 初始化列表数据
  49. return state;
  50. }
  51. // 初始化页面数据
  52. initPageData() {
  53. Log.d("----property_news_vm-----initPageData");
  54. getListData();
  55. }
  56. // 上拉加载
  57. Future onLoadData() async {
  58. Log.d("----property_news_vm-----initListData");
  59. getListData();
  60. }
  61. // 去新闻详情页
  62. void goNewsDetail(String item) {
  63. Log.d(item);
  64. // PropertyPage.startInstance(context: context, item: item);
  65. }
  66. // 重试请求
  67. Future retryRequest() async {
  68. _needShowPlaceholder = true;
  69. getListData();
  70. }
  71. //刷新页面状态
  72. void changeLoadingState(LoadState loadState, String? errorMsg) {
  73. state = state.copyWith(loadingState: loadState, errorMessage: errorMsg);
  74. }
  75. void changeList(List<Map<String, dynamic>> lis) {
  76. state = state.copyWith(list: lis);
  77. }
  78. // 获取list 列表数据
  79. void getListData<T>() async {
  80. Log.d("加载listData数据---------------start-----");
  81. try {
  82. //请求网络
  83. Map<String, dynamic> params = {};
  84. Log.d("请求参数------$params");
  85. final result = await rewardsHomeRepository.fetchPropertyNewsList(params);
  86. Log.d("请求完成结果------${result.list}");
  87. //校验成功失败
  88. if (result.isSuccess) {
  89. handlerResultList((result.list as List<RewardsListEntity>));
  90. // state.list.addAll(result.list?.toList());
  91. // final lis = result.getListJson();
  92. // state =
  93. // state.copyWith(list: result.list);
  94. ToastEngine.show("获取数据成功");
  95. } else {
  96. String errorMessage = result.errorMsg!;
  97. changeLoadingState(LoadState.State_Error, errorMessage);
  98. ToastEngine.show(result.errorMsg ?? "Network Load Error");
  99. }
  100. } catch (e) {
  101. ToastEngine.show("Error: $e");
  102. }
  103. }
  104. void handlerResultList(List<RewardsListEntity>? list) {
  105. if (list != null) {
  106. //有数据,判断是刷新还是加载更多的数据
  107. //刷新的方式
  108. state.list.clear();
  109. state.list.addAll(list.map((item) => item.toJson()).toList());
  110. Log.d("请state.list------${state.list}");
  111. changeLoadingState(LoadState.State_Success, null);
  112. }
  113. }
  114. // // 下拉刷新
  115. // Future refreshListData() async {
  116. // Log.d("----property_news_vm-----refreshListData ");
  117. // // await Future.delayed(const Duration(seconds: 2));
  118. // state = state.copyWith(curPage: 1, pageSize: 10);
  119. // // ref.invalidateSelf();
  120. // // ref.invalidate(propertyNewsVmProvider);
  121. // getListData();
  122. // }
  123. }