main_view_model.dart 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import 'package:plugin_basic/constants/app_constant.dart';
  2. import 'package:plugin_basic/provider/user_config/user_config_service.dart';
  3. import 'package:plugin_platform/http/dio/dio_cancelable_mixin.dart';
  4. import 'package:riverpod_annotation/riverpod_annotation.dart';
  5. import 'package:shared/utils/event_bus.dart';
  6. import 'package:shared/utils/log_utils.dart';
  7. import 'main_state.dart';
  8. part 'main_view_model.g.dart';
  9. @riverpod
  10. class MainViewModel extends _$MainViewModel with DioCancelableMixin {
  11. @override
  12. MainState build() {
  13. final state = MainState();
  14. registerEventBus();
  15. registerCancellation(callback: () {
  16. unregisterEventBus();
  17. });
  18. return state;
  19. }
  20. /// 获取用户信息
  21. void fetchUserInfo() {
  22. UserConfigService.getInstance().refreshUserInfo();
  23. }
  24. // EventBus 的事件接收
  25. Subscription? subscribe;
  26. void registerEventBus() {
  27. subscribe = bus.on(AppConstant.eventProfileRefresh, (arg) {
  28. Log.d("收到消息刷新用户信息");
  29. fetchUserInfo();
  30. });
  31. }
  32. void unregisterEventBus() {
  33. bus.off(AppConstant.eventProfileRefresh, subscribe);
  34. }
  35. }