rewards_home_vm.dart 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import 'package:cs_resources/generated/assets.dart';
  2. import 'package:domain/entity/rewards_category_entity.dart';
  3. import 'package:domain/entity/rewards_home_entity.dart';
  4. import 'package:domain/entity/rewards_home_tx_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. categoryList: [],
  30. lists: [
  31. {'title': 'YY', 'icon': Assets.rewardsRewardsIndex1},
  32. {'title': 'F&B', 'icon': Assets.rewardsRewardsIndex2},
  33. {'title': 'F&B', 'icon': Assets.rewardsRewardsIndex3},
  34. {'title': 'Beauty', 'icon': Assets.rewardsRewardsIndex4},
  35. {'title': 'Activites', 'icon': Assets.rewardsRewardsIndex5},
  36. {'title': 'Travel', 'icon': Assets.rewardsRewardsIndex6},
  37. {'title': 'Electronics', 'icon': Assets.rewardsRewardsIndex7},
  38. {'title': 'Gifts', 'icon': Assets.rewardsRewardsIndex8},
  39. {'title': 'Services', 'icon': Assets.rewardsRewardsIndex9},
  40. ],
  41. );
  42. }
  43. @override
  44. RewardsHomeState build() {
  45. // 引入数据仓库
  46. rewardsHomeRepository = ref.read(rewardsHomeRepositoryProvider);
  47. // 初始化状态
  48. RewardsHomeState state = initState();
  49. // 初始化列表数据
  50. return state;
  51. }
  52. // 初始化页面数据
  53. initPageData() async {
  54. Log.d("----property_news_vm-----initPageData");
  55. await getCategoryData();
  56. getListData();
  57. }
  58. // 上拉加载
  59. Future onLoadData() async {
  60. Log.d("----property_news_vm-----initListData");
  61. getListData();
  62. }
  63. // 去新闻详情页
  64. void goNewsDetail(String item) {
  65. Log.d(item);
  66. // PropertyPage.startInstance(context: context, item: item);
  67. }
  68. // 重试请求
  69. Future retryRequest() async {
  70. _needShowPlaceholder = true;
  71. getListData();
  72. }
  73. //刷新页面状态
  74. void changeLoadingState(LoadState loadState, String? errorMsg) {
  75. state = state.copyWith(loadingState: loadState, errorMessage: errorMsg);
  76. }
  77. void changeList(List<Map<String, dynamic>> lis) {
  78. state = state.copyWith(list: lis);
  79. }
  80. // 获取list 列表数据
  81. void getListData<T>() async {
  82. Log.d("加载listData数据---------------start-----");
  83. try {
  84. //请求网络
  85. Map<String, dynamic> params = {};
  86. Log.d("请求参数------$params");
  87. final result = await rewardsHomeRepository.fetchPropertyNewsList(params);
  88. Log.d("请求完成结果------${result.list}");
  89. //校验成功失败
  90. if (result.isSuccess) {
  91. handlerResultList((result.list as List<RewardsHomeTxEntity>));
  92. } else {
  93. String errorMessage = result.errorMsg!;
  94. changeLoadingState(LoadState.State_Error, errorMessage);
  95. ToastEngine.show(result.errorMsg ?? "Network Load Error");
  96. }
  97. } catch (e) {
  98. ToastEngine.show("Error: $e");
  99. }
  100. }
  101. void handlerResultList(List<RewardsHomeTxEntity>? list) {
  102. if (list != null) {
  103. //有数据,判断是刷新还是加载更多的数据
  104. //刷新的方式
  105. state.list.clear();
  106. state.list.addAll(list.map((item) => item.toJson()).toList());
  107. Log.d("请state.list------${state.list}");
  108. changeLoadingState(LoadState.State_Success, null);
  109. }
  110. }
  111. // 获取Category数据
  112. Future getCategoryData<T>() async {
  113. try {
  114. //请求网络
  115. Map<String, dynamic> params = {};
  116. Log.d("请求参数------$params");
  117. final result = await rewardsHomeRepository.fetchCategory(params);
  118. //校验成功失败
  119. if (result.isSuccess) {
  120. handlerCategory((result.list as List<RewardsCategoryEntity>));
  121. } else {
  122. ToastEngine.show(result.errorMsg ?? "Network Load Error");
  123. }
  124. } catch (e) {
  125. ToastEngine.show("Error: $e");
  126. }
  127. }
  128. void handlerCategory(List<RewardsCategoryEntity>? list) {
  129. if (list != null) {
  130. //有数据,判断是刷新还是加载更多的数据
  131. //刷新的方式
  132. state.categoryList?.clear();
  133. state.categoryList?.addAll(list.map((item) => item.toJson()).toList());
  134. Log.d("请categoryList------${state.categoryList}");
  135. }
  136. }
  137. }