home_view_model.dart 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import 'package:cpt_main/modules/home/property_news/home_property_news_page.dart';
  2. import 'package:cpt_main/modules/notification/notification_page.dart';
  3. import 'package:domain/repository/main_repository.dart';
  4. import 'package:plugin_platform/engine/toast/toast_engine.dart';
  5. import 'package:plugin_platform/http/dio/dio_cancelable_mixin.dart';
  6. import 'package:riverpod_annotation/riverpod_annotation.dart';
  7. import 'package:router/componentRouter/component_service_manager.dart';
  8. import 'package:widgets/widget_export.dart';
  9. import 'home_state.dart';
  10. import 'latest_news/latest_news_page.dart';
  11. import 'management_guides/management_guides_page.dart';
  12. part 'home_view_model.g.dart';
  13. @riverpod
  14. class HomeViewModel extends _$HomeViewModel with DioCancelableMixin {
  15. late final MainRepository _mainRepository;
  16. var _needShowLoading = true; //是否展示LoadingView
  17. // Refresh 控制器
  18. final EasyRefreshController refreshController = EasyRefreshController(
  19. controlFinishRefresh: true, //允许刷新
  20. controlFinishLoad: false, //允许加载
  21. );
  22. @override
  23. HomeState build() {
  24. _mainRepository = ref.read(mainRepositoryProvider);
  25. final state = HomeState();
  26. registerCancellation();
  27. return state;
  28. }
  29. // Refresh 刷新事件
  30. Future onRefresh() async {
  31. fetchHomeIndex();
  32. }
  33. /// 获取首页数据
  34. Future fetchHomeIndex() async {
  35. // 获取列表
  36. var result = await _mainRepository.fetchHomeIndex(
  37. needLoading: _needShowLoading,
  38. cancelToken: cancelToken,
  39. );
  40. // 处理数据
  41. if (result.isSuccess) {
  42. state = state.copyWith(homeIndex: result.data);
  43. } else {
  44. ToastEngine.show(result.errorMsg ?? "Network Load Error");
  45. }
  46. // 最后赋值
  47. _needShowLoading = false;
  48. refreshController.finishRefresh();
  49. }
  50. /// 根据索引触发点击的事件
  51. void switchCategory(int index) {
  52. switch (index) {
  53. case 0:
  54. //设备
  55. ComponentServiceManager().facilityService.startFacilityPage();
  56. break;
  57. case 1:
  58. //表单
  59. ComponentServiceManager().formService.startFormPage();
  60. break;
  61. case 2:
  62. //公告板
  63. ComponentServiceManager().noticeBoardService.startNoticeBoardPage();
  64. break;
  65. case 3:
  66. //房产
  67. ComponentServiceManager().propertyService.startPropertyPage();
  68. break;
  69. case 4:
  70. //广告服务
  71. ComponentServiceManager().servicesService.startServicePage();
  72. break;
  73. case 5:
  74. //社区
  75. ComponentServiceManager().communityService.startCommunityPage();
  76. break;
  77. }
  78. }
  79. //去消息通知页面
  80. void gotoNotificationPage() {
  81. NotificationPage.startInstance();
  82. }
  83. void gotoPaymentPage() {
  84. ComponentServiceManager().paymentService.startPaymentPage();
  85. }
  86. void gotoRewardsPage() {
  87. ComponentServiceManager().rewardsService.startRewardsPage();
  88. }
  89. void gotoLastNewsPage() {
  90. LatestNewsPage.startInstance();
  91. }
  92. void gotoPropertyNewsPage() {
  93. HomePropertyNewsPage.startInstance();
  94. }
  95. void gotoManageGuidePage() {
  96. ManagementGuidesPage.startInstance();
  97. }
  98. }