import 'package:get_it/get_it.dart'; import 'package:dio/dio.dart'; import 'package:shared_preferences/shared_preferences.dart'; import '../../core/network/api_client.dart'; import '../../features/auth/data/datasources/auth_local_datasource.dart'; import '../../features/auth/data/datasources/auth_local_datasource_impl.dart'; import '../../features/auth/data/datasources/auth_remote_datasource.dart'; import '../../features/auth/data/datasources/auth_remote_datasource_impl.dart'; import '../../features/auth/data/repositories/auth_repository_impl.dart'; import '../../features/auth/domain/repositories/auth_repository.dart'; import '../../features/auth/presentation/bloc/auth_bloc.dart'; import '../../core/database/database_service.dart'; import '../../core/network/signalr_service.dart'; import '../../features/chat/data/datasources/chat_local_datasource.dart'; import '../../features/chat/data/datasources/chat_local_datasource_impl.dart'; import '../../features/chat/data/datasources/chat_remote_datasource.dart'; import '../../features/chat/data/datasources/chat_remote_datasource_impl.dart'; import '../../features/chat/data/repositories/chat_repository_impl.dart'; import '../../features/chat/domain/repositories/chat_repository.dart'; import '../../features/chat/presentation/bloc/chat_bloc.dart'; import '../../features/settings/data/datasources/settings_local_datasource.dart'; import '../../features/settings/data/datasources/settings_remote_datasource.dart'; import '../../features/settings/data/repositories/settings_repository_impl.dart'; import '../../features/settings/domain/repositories/settings_repository.dart'; import '../../features/settings/presentation/bloc/settings_bloc.dart'; final sl = GetIt.instance; Future initDependencies() async { // Core - SharedPreferences final prefs = await SharedPreferences.getInstance(); sl.registerLazySingleton(() => prefs); // Core - Settings Local DataSource sl.registerLazySingleton( () => SettingsLocalDataSourceImpl(sl()), ); // Core - Dio (with dynamic base URL from settings) sl.registerLazySingleton(() { final prefs = sl(); var apiUrl = prefs.getString('api_url') ?? 'https://api.messenger.app'; // Нормализуем URL: убираем /api в конце если есть apiUrl = apiUrl.replaceAll(RegExp(r'/+$'), ''); if (apiUrl.endsWith('/api')) { apiUrl = apiUrl.substring(0, apiUrl.length - 4).replaceAll(RegExp(r'/+$'), ''); } final dio = Dio(BaseOptions( baseUrl: apiUrl, connectTimeout: const Duration(seconds: 30), receiveTimeout: const Duration(seconds: 30), )); dio.interceptors.add(InterceptorsWrapper( onRequest: (options, handler) { final token = prefs.getString('access_token'); if (token != null) { options.headers['Authorization'] = 'Bearer $token'; } return handler.next(options); }, )); return dio; }); // Core - API Client sl.registerLazySingleton(() => ApiClient(sl())); // Core - Database & SignalR final dbService = DatabaseService(); await dbService.init(); sl.registerSingleton(dbService); sl.registerLazySingleton(() => SignalRService(sl())); // Settings sl.registerLazySingleton( () => SettingsRemoteDataSourceImpl(sl()), ); sl.registerLazySingleton( () => SettingsRepositoryImpl(sl(), sl()), ); sl.registerFactory(() => SettingsBloc(sl())); // Auth sl.registerLazySingleton( () => AuthLocalDataSourceImpl(sl()), ); sl.registerLazySingleton( () => AuthRemoteDataSourceImpl(sl()), ); sl.registerLazySingleton( () => AuthRepositoryImpl( remoteDataSource: sl(), localDataSource: sl(), ), ); sl.registerFactory(() => AuthBloc(authRepository: sl())); // Chat sl.registerLazySingleton( () => ChatLocalDataSourceImpl(sl().isar), ); sl.registerLazySingleton( () => ChatRemoteDataSourceImpl(sl()), ); sl.registerLazySingleton( () => ChatRepositoryImpl( remoteDataSource: sl(), localDataSource: sl(), signalRService: sl(), ), ); sl.registerFactory(() => ChatBloc(chatRepository: sl())); }