component_service_manager.dart 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import 'package:flutter_riverpod/flutter_riverpod.dart';
  2. import 'package:router/componentRouter/auth_service.dart';
  3. import 'package:router/componentRouter/profile_service.dart';
  4. import 'package:router/componentRouter/rewards_service.dart';
  5. import '../ext/router_provider_container.dart';
  6. import 'app_service.dart';
  7. import 'community_service.dart';
  8. import 'facility_service.dart';
  9. import 'form_service.dart';
  10. import 'main_service.dart';
  11. import 'notice_board_service.dart';
  12. import 'payment_service.dart';
  13. import 'property_service.dart';
  14. import 'service_service.dart';
  15. class ComponentServiceManager {
  16. // 私有构造函数
  17. ComponentServiceManager._internal();
  18. // 单例实例
  19. static final ComponentServiceManager _instance = ComponentServiceManager._internal();
  20. factory ComponentServiceManager() {
  21. return _instance;
  22. }
  23. // 存储 `ServiceProvider` 的 Map
  24. final Map<Type, Provider> _serviceProviders = {};
  25. // 添加 `ServiceProvider` 的方法
  26. void addServiceProvider<T>(Provider<T> provider) {
  27. _serviceProviders[T] = provider;
  28. }
  29. // 获取 `MainService` 的方法
  30. MainService get mainService {
  31. final provider = _serviceProviders[MainService];
  32. if (provider == null) {
  33. throw Exception('Provider for MainService not found.');
  34. }
  35. return routerContainer.read(provider as Provider<MainService>);
  36. }
  37. AppService get appService {
  38. final provider = _serviceProviders[AppService];
  39. if (provider == null) {
  40. throw Exception('Provider for AppService not found.');
  41. }
  42. return routerContainer.read(provider as Provider<AppService>);
  43. }
  44. ProfileService get profileService {
  45. final provider = _serviceProviders[ProfileService];
  46. if (provider == null) {
  47. throw Exception('Provider for ProfileService not found.');
  48. }
  49. return routerContainer.read(provider as Provider<ProfileService>);
  50. }
  51. AuthService get authService {
  52. final provider = _serviceProviders[AuthService];
  53. if (provider == null) {
  54. throw Exception('Provider for AuthService not found.');
  55. }
  56. return routerContainer.read(provider as Provider<AuthService>);
  57. }
  58. CommunityService get communityService {
  59. final provider = _serviceProviders[CommunityService];
  60. if (provider == null) {
  61. throw Exception('Provider for CommunityService not found.');
  62. }
  63. return routerContainer.read(provider as Provider<CommunityService>);
  64. }
  65. FacilityService get facilityService {
  66. final provider = _serviceProviders[FacilityService];
  67. if (provider == null) {
  68. throw Exception('Provider for FacilityService not found.');
  69. }
  70. return routerContainer.read(provider as Provider<FacilityService>);
  71. }
  72. FormService get formService {
  73. final provider = _serviceProviders[FormService];
  74. if (provider == null) {
  75. throw Exception('Provider for FormService not found.');
  76. }
  77. return routerContainer.read(provider as Provider<FormService>);
  78. }
  79. NoticeBoardService get noticeBoardService {
  80. final provider = _serviceProviders[NoticeBoardService];
  81. if (provider == null) {
  82. throw Exception('Provider for NoticeBoardService not found.');
  83. }
  84. return routerContainer.read(provider as Provider<NoticeBoardService>);
  85. }
  86. PaymentService get paymentService {
  87. final provider = _serviceProviders[PaymentService];
  88. if (provider == null) {
  89. throw Exception('Provider for PaymentService not found.');
  90. }
  91. return routerContainer.read(provider as Provider<PaymentService>);
  92. }
  93. PropertyService get propertyService {
  94. final provider = _serviceProviders[PropertyService];
  95. if (provider == null) {
  96. throw Exception('Provider for PropertyService not found.');
  97. }
  98. return routerContainer.read(provider as Provider<PropertyService>);
  99. }
  100. RewardsService get rewardsService {
  101. final provider = _serviceProviders[RewardsService];
  102. if (provider == null) {
  103. throw Exception('Provider for RewardsService not found.');
  104. }
  105. return routerContainer.read(provider as Provider<RewardsService>);
  106. }
  107. ServiceService get servicesService {
  108. final provider = _serviceProviders[ServiceService];
  109. if (provider == null) {
  110. throw Exception('Provider for ServiceService not found.');
  111. }
  112. return routerContainer.read(provider as Provider<ServiceService>);
  113. }
  114. }