import 'package:appflowy/core/config/kv.dart'; import 'package:appflowy/core/network_monitor.dart'; import 'package:appflowy/env/env.dart'; import 'package:appflowy/plugins/database_view/application/field/field_action_sheet_bloc.dart'; import 'package:appflowy/plugins/database_view/application/field/field_controller.dart'; import 'package:appflowy/plugins/database_view/application/field/field_service.dart'; import 'package:appflowy/plugins/database_view/application/setting/property_bloc.dart'; import 'package:appflowy/plugins/database_view/grid/application/grid_header_bloc.dart'; import 'package:appflowy/plugins/document/application/prelude.dart'; import 'package:appflowy/plugins/document/presentation/editor_plugins/copy_and_paste/clipboard_service.dart'; import 'package:appflowy/plugins/document/presentation/editor_plugins/openai/service/openai_client.dart'; import 'package:appflowy/plugins/document/presentation/editor_plugins/stability_ai/stability_ai_client.dart'; import 'package:appflowy/plugins/trash/application/prelude.dart'; import 'package:appflowy/startup/startup.dart'; import 'package:appflowy/user/application/auth/af_cloud_auth_service.dart'; import 'package:appflowy/user/application/auth/auth_service.dart'; import 'package:appflowy/user/application/auth/supabase_auth_service.dart'; import 'package:appflowy/user/application/auth/supabase_mock_auth_service.dart'; import 'package:appflowy/user/application/prelude.dart'; import 'package:appflowy/user/application/reminder/reminder_bloc.dart'; import 'package:appflowy/user/application/user_listener.dart'; import 'package:appflowy/user/application/user_service.dart'; import 'package:appflowy/user/presentation/router.dart'; import 'package:appflowy/workspace/application/edit_panel/edit_panel_bloc.dart'; import 'package:appflowy/workspace/application/favorite/favorite_bloc.dart'; import 'package:appflowy/workspace/application/local_notifications/notification_action_bloc.dart'; import 'package:appflowy/workspace/application/settings/notifications/notification_settings_cubit.dart'; import 'package:appflowy/workspace/application/settings/prelude.dart'; import 'package:appflowy/workspace/application/tabs/tabs_bloc.dart'; import 'package:appflowy/workspace/application/user/prelude.dart'; import 'package:appflowy/workspace/application/view/prelude.dart'; import 'package:appflowy/workspace/application/workspace/prelude.dart'; import 'package:appflowy/workspace/presentation/home/menu/menu_shared_state.dart'; import 'package:appflowy_backend/protobuf/flowy-folder2/view.pb.dart'; import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart'; import 'package:flowy_infra/file_picker/file_picker_impl.dart'; import 'package:flowy_infra/file_picker/file_picker_service.dart'; import 'package:fluttertoast/fluttertoast.dart'; import 'package:get_it/get_it.dart'; import 'package:http/http.dart' as http; class DependencyResolver { static Future resolve( GetIt getIt, IntegrationMode mode, ) async { _resolveUserDeps(getIt, mode); _resolveHomeDeps(getIt); _resolveFolderDeps(getIt); _resolveDocDeps(getIt); _resolveGridDeps(getIt); _resolveCommonService(getIt, mode); } } void _resolveCommonService( GetIt getIt, IntegrationMode mode, ) async { // getIt.registerFactory(() => RustKeyValue()); getIt.registerFactory(() => DartKeyValue()); getIt.registerFactory(() => FilePicker()); if (mode.isTest) { getIt.registerFactory( () => MockApplicationDataStorage(), ); } else { getIt.registerFactory( () => ApplicationDataStorage(), ); } getIt.registerFactoryAsync( () async { final result = await UserBackendService.getCurrentUserProfile(); return result.fold( (l) { throw Exception('Failed to get user profile: ${l.msg}'); }, (r) { return HttpOpenAIRepository( client: http.Client(), apiKey: r.openaiKey, ); }, ); }, ); getIt.registerFactoryAsync( () async { final result = await UserBackendService.getCurrentUserProfile(); return result.fold( (l) { throw Exception('Failed to get user profile: ${l.msg}'); }, (r) { return HttpStabilityAIRepository( client: http.Client(), apiKey: r.stabilityAiKey, ); }, ); }, ); getIt.registerFactory( () => ClipboardService(), ); } void _resolveUserDeps(GetIt getIt, IntegrationMode mode) { switch (currentCloudType()) { case CloudType.unknown: getIt.registerFactory( () => BackendAuthService( AuthTypePB.Local, ), ); break; case CloudType.supabase: if (mode.isIntegrationTest) { getIt.registerFactory(() => MockAuthService()); } else { getIt.registerFactory(() => SupabaseAuthService()); } break; case CloudType.appflowyCloud: getIt.registerFactory(() => AFCloudAuthService()); break; } getIt.registerFactory(() => AuthRouter()); getIt.registerFactory( () => SignInBloc(getIt()), ); getIt.registerFactory( () => SignUpBloc(getIt()), ); getIt.registerFactory(() => SplashRouter()); getIt.registerFactory(() => EditPanelBloc()); getIt.registerFactory(() => SplashBloc()); getIt.registerLazySingleton(() => NetworkListener()); } void _resolveHomeDeps(GetIt getIt) { getIt.registerSingleton(FToast()); getIt.registerSingleton(MenuSharedState()); getIt.registerFactoryParam( (user, _) => UserListener(userProfile: user), ); getIt.registerFactoryParam( (user, _) => WorkspaceBloc( userService: UserBackendService(userId: user.id), ), ); // share getIt.registerFactoryParam( (view, _) => DocShareBloc(view: view), ); getIt.registerSingleton(NotificationActionBloc()); getIt.registerLazySingleton(() => TabsBloc()); getIt.registerSingleton( NotificationSettingsCubit(), ); getIt.registerSingleton( ReminderBloc(notificationSettings: getIt()), ); } void _resolveFolderDeps(GetIt getIt) { //workspace getIt.registerFactoryParam( (user, workspaceId) => WorkspaceListener( user: user, workspaceId: workspaceId, ), ); getIt.registerFactoryParam( (view, _) => ViewBloc( view: view, ), ); //Settings getIt.registerFactoryParam( (user, _) => SettingsDialogBloc(user), ); //User getIt.registerFactoryParam( (user, _) => SettingsUserViewBloc(user), ); // trash getIt.registerLazySingleton(() => TrashService()); getIt.registerLazySingleton(() => TrashListener()); getIt.registerFactory( () => TrashBloc(), ); getIt.registerFactory(() => FavoriteBloc()); } void _resolveDocDeps(GetIt getIt) { // Doc getIt.registerFactoryParam( (view, _) => DocumentBloc(view: view), ); } void _resolveGridDeps(GetIt getIt) { getIt.registerFactoryParam( (viewId, fieldController) => GridHeaderBloc( viewId: viewId, fieldController: fieldController, ), ); getIt.registerFactoryParam( (data, _) => FieldActionSheetBloc(fieldCellContext: data), ); getIt.registerFactoryParam( (viewId, cache) => DatabasePropertyBloc(viewId: viewId, fieldController: cache), ); }