deps_resolver.dart 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. import 'package:appflowy/core/config/kv.dart';
  2. import 'package:appflowy/core/network_monitor.dart';
  3. import 'package:appflowy/env/env.dart';
  4. import 'package:appflowy/plugins/database_view/application/field/field_action_sheet_bloc.dart';
  5. import 'package:appflowy/plugins/database_view/application/field/field_controller.dart';
  6. import 'package:appflowy/plugins/database_view/application/field/field_service.dart';
  7. import 'package:appflowy/plugins/database_view/application/setting/property_bloc.dart';
  8. import 'package:appflowy/plugins/database_view/grid/application/grid_header_bloc.dart';
  9. import 'package:appflowy/plugins/document/application/prelude.dart';
  10. import 'package:appflowy/plugins/document/presentation/editor_plugins/copy_and_paste/clipboard_service.dart';
  11. import 'package:appflowy/plugins/document/presentation/editor_plugins/openai/service/openai_client.dart';
  12. import 'package:appflowy/plugins/document/presentation/editor_plugins/stability_ai/stability_ai_client.dart';
  13. import 'package:appflowy/plugins/trash/application/prelude.dart';
  14. import 'package:appflowy/startup/startup.dart';
  15. import 'package:appflowy/user/application/auth/af_cloud_auth_service.dart';
  16. import 'package:appflowy/user/application/auth/auth_service.dart';
  17. import 'package:appflowy/user/application/auth/supabase_auth_service.dart';
  18. import 'package:appflowy/user/application/auth/supabase_mock_auth_service.dart';
  19. import 'package:appflowy/user/application/prelude.dart';
  20. import 'package:appflowy/user/application/reminder/reminder_bloc.dart';
  21. import 'package:appflowy/user/application/user_listener.dart';
  22. import 'package:appflowy/user/application/user_service.dart';
  23. import 'package:appflowy/user/presentation/router.dart';
  24. import 'package:appflowy/workspace/application/edit_panel/edit_panel_bloc.dart';
  25. import 'package:appflowy/workspace/application/favorite/favorite_bloc.dart';
  26. import 'package:appflowy/workspace/application/local_notifications/notification_action_bloc.dart';
  27. import 'package:appflowy/workspace/application/settings/notifications/notification_settings_cubit.dart';
  28. import 'package:appflowy/workspace/application/settings/prelude.dart';
  29. import 'package:appflowy/workspace/application/tabs/tabs_bloc.dart';
  30. import 'package:appflowy/workspace/application/user/prelude.dart';
  31. import 'package:appflowy/workspace/application/view/prelude.dart';
  32. import 'package:appflowy/workspace/application/workspace/prelude.dart';
  33. import 'package:appflowy/workspace/presentation/home/menu/menu_shared_state.dart';
  34. import 'package:appflowy_backend/protobuf/flowy-folder2/view.pb.dart';
  35. import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart';
  36. import 'package:flowy_infra/file_picker/file_picker_impl.dart';
  37. import 'package:flowy_infra/file_picker/file_picker_service.dart';
  38. import 'package:fluttertoast/fluttertoast.dart';
  39. import 'package:get_it/get_it.dart';
  40. import 'package:http/http.dart' as http;
  41. class DependencyResolver {
  42. static Future<void> resolve(
  43. GetIt getIt,
  44. IntegrationMode mode,
  45. ) async {
  46. _resolveUserDeps(getIt, mode);
  47. _resolveHomeDeps(getIt);
  48. _resolveFolderDeps(getIt);
  49. _resolveDocDeps(getIt);
  50. _resolveGridDeps(getIt);
  51. _resolveCommonService(getIt, mode);
  52. }
  53. }
  54. void _resolveCommonService(
  55. GetIt getIt,
  56. IntegrationMode mode,
  57. ) async {
  58. // getIt.registerFactory<KeyValueStorage>(() => RustKeyValue());
  59. getIt.registerFactory<KeyValueStorage>(() => DartKeyValue());
  60. getIt.registerFactory<FilePickerService>(() => FilePicker());
  61. if (mode.isTest) {
  62. getIt.registerFactory<ApplicationDataStorage>(
  63. () => MockApplicationDataStorage(),
  64. );
  65. } else {
  66. getIt.registerFactory<ApplicationDataStorage>(
  67. () => ApplicationDataStorage(),
  68. );
  69. }
  70. getIt.registerFactoryAsync<OpenAIRepository>(
  71. () async {
  72. final result = await UserBackendService.getCurrentUserProfile();
  73. return result.fold(
  74. (l) {
  75. throw Exception('Failed to get user profile: ${l.msg}');
  76. },
  77. (r) {
  78. return HttpOpenAIRepository(
  79. client: http.Client(),
  80. apiKey: r.openaiKey,
  81. );
  82. },
  83. );
  84. },
  85. );
  86. getIt.registerFactoryAsync<StabilityAIRepository>(
  87. () async {
  88. final result = await UserBackendService.getCurrentUserProfile();
  89. return result.fold(
  90. (l) {
  91. throw Exception('Failed to get user profile: ${l.msg}');
  92. },
  93. (r) {
  94. return HttpStabilityAIRepository(
  95. client: http.Client(),
  96. apiKey: r.stabilityAiKey,
  97. );
  98. },
  99. );
  100. },
  101. );
  102. getIt.registerFactory<ClipboardService>(
  103. () => ClipboardService(),
  104. );
  105. }
  106. void _resolveUserDeps(GetIt getIt, IntegrationMode mode) {
  107. switch (currentCloudType()) {
  108. case CloudType.unknown:
  109. getIt.registerFactory<AuthService>(
  110. () => BackendAuthService(
  111. AuthTypePB.Local,
  112. ),
  113. );
  114. break;
  115. case CloudType.supabase:
  116. if (mode.isIntegrationTest) {
  117. getIt.registerFactory<AuthService>(() => MockAuthService());
  118. } else {
  119. getIt.registerFactory<AuthService>(() => SupabaseAuthService());
  120. }
  121. break;
  122. case CloudType.appflowyCloud:
  123. getIt.registerFactory<AuthService>(() => AFCloudAuthService());
  124. break;
  125. }
  126. getIt.registerFactory<AuthRouter>(() => AuthRouter());
  127. getIt.registerFactory<SignInBloc>(
  128. () => SignInBloc(getIt<AuthService>()),
  129. );
  130. getIt.registerFactory<SignUpBloc>(
  131. () => SignUpBloc(getIt<AuthService>()),
  132. );
  133. getIt.registerFactory<SplashRouter>(() => SplashRouter());
  134. getIt.registerFactory<EditPanelBloc>(() => EditPanelBloc());
  135. getIt.registerFactory<SplashBloc>(() => SplashBloc());
  136. getIt.registerLazySingleton<NetworkListener>(() => NetworkListener());
  137. }
  138. void _resolveHomeDeps(GetIt getIt) {
  139. getIt.registerSingleton(FToast());
  140. getIt.registerSingleton(MenuSharedState());
  141. getIt.registerFactoryParam<UserListener, UserProfilePB, void>(
  142. (user, _) => UserListener(userProfile: user),
  143. );
  144. getIt.registerFactoryParam<WorkspaceBloc, UserProfilePB, void>(
  145. (user, _) => WorkspaceBloc(
  146. userService: UserBackendService(userId: user.id),
  147. ),
  148. );
  149. // share
  150. getIt.registerFactoryParam<DocShareBloc, ViewPB, void>(
  151. (view, _) => DocShareBloc(view: view),
  152. );
  153. getIt.registerSingleton<NotificationActionBloc>(NotificationActionBloc());
  154. getIt.registerLazySingleton<TabsBloc>(() => TabsBloc());
  155. getIt.registerSingleton<NotificationSettingsCubit>(
  156. NotificationSettingsCubit(),
  157. );
  158. getIt.registerSingleton<ReminderBloc>(
  159. ReminderBloc(notificationSettings: getIt<NotificationSettingsCubit>()),
  160. );
  161. }
  162. void _resolveFolderDeps(GetIt getIt) {
  163. //workspace
  164. getIt.registerFactoryParam<WorkspaceListener, UserProfilePB, String>(
  165. (user, workspaceId) => WorkspaceListener(
  166. user: user,
  167. workspaceId: workspaceId,
  168. ),
  169. );
  170. getIt.registerFactoryParam<ViewBloc, ViewPB, void>(
  171. (view, _) => ViewBloc(
  172. view: view,
  173. ),
  174. );
  175. //Settings
  176. getIt.registerFactoryParam<SettingsDialogBloc, UserProfilePB, void>(
  177. (user, _) => SettingsDialogBloc(user),
  178. );
  179. //User
  180. getIt.registerFactoryParam<SettingsUserViewBloc, UserProfilePB, void>(
  181. (user, _) => SettingsUserViewBloc(user),
  182. );
  183. // trash
  184. getIt.registerLazySingleton<TrashService>(() => TrashService());
  185. getIt.registerLazySingleton<TrashListener>(() => TrashListener());
  186. getIt.registerFactory<TrashBloc>(
  187. () => TrashBloc(),
  188. );
  189. getIt.registerFactory<FavoriteBloc>(() => FavoriteBloc());
  190. }
  191. void _resolveDocDeps(GetIt getIt) {
  192. // Doc
  193. getIt.registerFactoryParam<DocumentBloc, ViewPB, void>(
  194. (view, _) => DocumentBloc(view: view),
  195. );
  196. }
  197. void _resolveGridDeps(GetIt getIt) {
  198. getIt.registerFactoryParam<GridHeaderBloc, String, FieldController>(
  199. (viewId, fieldController) => GridHeaderBloc(
  200. viewId: viewId,
  201. fieldController: fieldController,
  202. ),
  203. );
  204. getIt.registerFactoryParam<FieldActionSheetBloc, FieldContext, void>(
  205. (data, _) => FieldActionSheetBloc(fieldCellContext: data),
  206. );
  207. getIt.registerFactoryParam<DatabasePropertyBloc, String, FieldController>(
  208. (viewId, cache) =>
  209. DatabasePropertyBloc(viewId: viewId, fieldController: cache),
  210. );
  211. }