Files
friflex_flutter_starter/lib/di/di_container.dart

64 lines
2.5 KiB
Dart
Raw Normal View History

2025-01-21 14:24:31 +03:00
import 'package:friflex_starter/app/app_config/app_config.dart';
import 'package:friflex_starter/app/app_env.dart';
import 'package:friflex_starter/app/http/app_http_client.dart';
import 'package:friflex_starter/app/http/i_http_client.dart';
import 'package:friflex_starter/di/di_repositories.dart';
import 'package:friflex_starter/di/di_services.dart';
2025-01-21 14:24:31 +03:00
import 'package:friflex_starter/di/di_typedefs.dart';
import 'package:friflex_starter/features/debug/i_debug_service.dart';
/// {@template dependencies_container}
/// Контейнер для зависимостей
/// [env] - окружение приложения
/// [debugService] - сервис для отладки
2025-01-21 14:24:31 +03:00
/// {@endtemplate}
final class DiContainer {
/// {@macro dependencies_container}
DiContainer({required this.env, required IDebugService dService})
: debugService = dService;
2025-01-21 14:24:31 +03:00
final AppEnv env;
/// Сервис для отладки, получаем из конструктора
late final IDebugService debugService;
/// Конфигурация приложения
late final IAppConfig appConfig;
/// Сервис для работы с HTTP запросами
late final IHttpClient Function(IDebugService, IAppConfig) httpClientFactory;
2025-01-21 14:24:31 +03:00
/// Репозитории приложения
2025-01-21 14:24:31 +03:00
late final DiRepositories repositories;
/// Сервисы приложения
late final DiServices services;
2025-01-21 14:24:31 +03:00
/// Метод для инициализации зависимостей
Future<void> init({
required OnProgress onProgress,
required OnComplete onComplete,
required OnError onError,
}) async {
// Инициализация конфигурации приложения
2025-01-21 14:24:31 +03:00
appConfig = switch (env) {
.dev => AppConfigDev(),
.prod => AppConfigProd(),
.stage => AppConfigStage(),
2025-01-21 14:24:31 +03:00
};
// Инициализация HTTP клиента
httpClientFactory = (debugService, appConfig) =>
AppHttpClient(debugService: debugService, appConfig: appConfig);
2025-01-21 14:24:31 +03:00
// Инициализация сервисов
services = DiServices()
..init(onProgress: onProgress, onError: onError, diContainer: this);
// throw Exception('Тестовая - ошибка инициализации зависимостей');
// Инициализация репозиториев
repositories = DiRepositories()
..init(onProgress: onProgress, onError: onError, diContainer: this);
onComplete('Инициализация зависимостей завершена!');
2025-01-21 14:24:31 +03:00
}
}