import 'package:appflowy/core/config/kv.dart'; import 'package:appflowy/core/network_monitor.dart'; import 'package:appflowy/env/cloud_env.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/ai_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/shared/appflowy_cache_manager.dart'; import 'package:appflowy/shared/custom_image_cache_manager.dart'; import 'package:appflowy/startup/startup.dart'; import 'package:appflowy/startup/tasks/appflowy_cloud_task.dart'; import 'package:appflowy/user/application/ai_service.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/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/action_navigation/action_navigation_bloc.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/recent/cached_recent_service.dart'; import 'package:appflowy/workspace/application/settings/appearance/base_appearance.dart'; import 'package:appflowy/workspace/application/settings/appearance/desktop_appearance.dart'; import 'package:appflowy/workspace/application/settings/appearance/mobile_appearance.dart'; import 'package:appflowy/workspace/application/settings/prelude.dart'; import 'package:appflowy/workspace/application/sidebar/rename_view/rename_view_bloc.dart'; import 'package:appflowy/workspace/application/subscription_success_listenable/subscription_success_listenable.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/log.dart'; import 'package:appflowy_backend/protobuf/flowy-folder/view.pb.dart'; import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart'; import 'package:appflowy_editor/appflowy_editor.dart' hide Log; import 'package:appflowy_popover/appflowy_popover.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 { // getIt.registerFactory(() => RustKeyValue()); getIt.registerFactory(() => DartKeyValue()); await _resolveCloudDeps(getIt); _resolveUserDeps(getIt, mode); _resolveHomeDeps(getIt); _resolveFolderDeps(getIt); _resolveCommonService(getIt, mode); } } Future _resolveCloudDeps(GetIt getIt) async { final env = await AppFlowyCloudSharedEnv.fromEnv(); Log.info("cloud setting: $env"); getIt.registerFactory(() => env); if (isAppFlowyCloudEnabled) { getIt.registerSingleton( AppFlowyCloudDeepLink(), dispose: (obj) async { await obj.dispose(); }, ); } } void _resolveCommonService( GetIt getIt, IntegrationMode mode, ) async { getIt.registerFactory(() => FilePicker()); getIt.registerFactory( () => mode.isTest ? MockApplicationDataStorage() : ApplicationDataStorage(), ); getIt.registerFactoryAsync( () async { final result = await UserBackendService.getCurrentUserProfile(); return result.fold( (s) { return AppFlowyAIService(); }, (e) { throw Exception('Failed to get user profile: ${e.msg}'); }, ); }, ); getIt.registerFactoryAsync( () async { final result = await UserBackendService.getCurrentUserProfile(); return result.fold( (s) { return HttpStabilityAIRepository( client: http.Client(), apiKey: s.stabilityAiKey, ); }, (e) { throw Exception('Failed to get user profile: ${e.msg}'); }, ); }, ); getIt.registerFactory( () => ClipboardService(), ); // theme getIt.registerFactory( () => PlatformExtension.isMobile ? MobileAppearance() : DesktopAppearance(), ); getIt.registerFactory( () => FlowyCacheManager() ..registerCache(TemporaryDirectoryCache()) ..registerCache(CustomImageCacheManager()) ..registerCache(FeatureFlagCache()), ); } void _resolveUserDeps(GetIt getIt, IntegrationMode mode) { switch (currentCloudType()) { case AuthenticatorType.local: getIt.registerFactory( () => BackendAuthService( AuthenticatorPB.Local, ), ); break; case AuthenticatorType.supabase: getIt.registerFactory(() => SupabaseAuthService()); break; case AuthenticatorType.appflowyCloud: case AuthenticatorType.appflowyCloudSelfHost: case AuthenticatorType.appflowyCloudDevelop: getIt.registerFactory(() => AppFlowyCloudAuthService()); break; } getIt.registerFactory(() => AuthRouter()); getIt.registerFactory( () => SignInBloc(getIt()), ); getIt.registerFactory( () => SignUpBloc(getIt()), ); getIt.registerFactory(() => SplashRouter()); getIt.registerFactory(() => EditPanelBloc()); getIt.registerFactory(() => SplashBloc()); getIt.registerLazySingleton(() => NetworkListener()); getIt.registerLazySingleton(() => CachedRecentService()); getIt.registerLazySingleton( () => SubscriptionSuccessListenable(), ); } void _resolveHomeDeps(GetIt getIt) { getIt.registerSingleton(FToast()); getIt.registerSingleton(MenuSharedState()); getIt.registerFactoryParam( (user, _) => UserListener(userProfile: user), ); // share getIt.registerFactoryParam( (view, _) => ShareBloc(view: view), ); getIt.registerSingleton(ActionNavigationBloc()); getIt.registerLazySingleton(() => TabsBloc()); getIt.registerSingleton(ReminderBloc()); getIt.registerSingleton(RenameViewBloc(PopoverController())); } 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(), ); // Favorite getIt.registerFactory(() => FavoriteBloc()); }