forked from eatvio/eatvio_app
87 lines
3.1 KiB
Dart
87 lines
3.1 KiB
Dart
import 'dart:async';
|
||
import 'dart:io' as io;
|
||
import 'package:eatvio_app/core/env_config.dart';
|
||
import 'package:eatvio_app/core/error_reporting/error_report_policy.dart';
|
||
import 'package:eatvio_app/core/locator_service.dart' as di;
|
||
import 'package:eatvio_app/features/video_calls/services/video_call_fcm_handler.dart';
|
||
import 'package:eatvio_app/firebase_options.dart';
|
||
import 'package:firebase_core/firebase_core.dart';
|
||
import 'package:flutter/foundation.dart';
|
||
import 'package:flutter_native_splash/flutter_native_splash.dart';
|
||
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
||
import 'package:appmetrica_plugin/appmetrica_plugin.dart';
|
||
import 'package:sentry_flutter/sentry_flutter.dart';
|
||
|
||
/// Класс для инициализации приложения
|
||
class AppInitializer {
|
||
/// Инициализация всех сервисов приложения
|
||
static Future<void> initialize({required AppBuildType buildType}) async {
|
||
await _initializeEnvironment();
|
||
await _initializeDependencyInjection(buildType: buildType);
|
||
await _initializeSentry();
|
||
await _initializeFirebase();
|
||
await _initializeDebugMode();
|
||
await _initializeAnalytics();
|
||
}
|
||
|
||
/// Инициализация Sentry
|
||
static Future<void> _initializeSentry() async {
|
||
final widgetsBinding = SentryWidgetsFlutterBinding.ensureInitialized();
|
||
FlutterNativeSplash.preserve(widgetsBinding: widgetsBinding);
|
||
}
|
||
|
||
/// Загрузка переменных окружения
|
||
static Future<void> _initializeEnvironment() async {
|
||
await dotenv.load(fileName: ".env");
|
||
}
|
||
|
||
/// Инициализация Firebase
|
||
static Future<void> _initializeFirebase() async {
|
||
await Firebase.initializeApp(
|
||
options: DefaultFirebaseOptions.currentPlatform,
|
||
).timeout(
|
||
const Duration(seconds: 10),
|
||
onTimeout: () {
|
||
throw TimeoutException('Firebase initialization timeout');
|
||
},
|
||
);
|
||
VideoCallFcmHandler.registerBackgroundHandler();
|
||
}
|
||
|
||
/// Инициализация системы внедрения зависимостей
|
||
static Future<void> _initializeDependencyInjection({
|
||
required AppBuildType buildType,
|
||
}) async {
|
||
await di.init(buildType: buildType);
|
||
}
|
||
|
||
/// Настройка режима отладки
|
||
static Future<void> _initializeDebugMode() async {
|
||
if (kDebugMode) {
|
||
io.HttpClient.enableTimelineLogging = true;
|
||
}
|
||
}
|
||
|
||
/// Инициализация аналитики
|
||
static Future<void> _initializeAnalytics() async {
|
||
AppMetrica.activate(AppMetricaConfig(EnvConfig.appMetricaApiKey));
|
||
AppMetrica.reportEvent('Application started');
|
||
}
|
||
|
||
/// Запуск приложения с Sentry
|
||
static Future<void> runAppWithSentry(VoidCallback appRunner) async {
|
||
await SentryFlutter.init((options) {
|
||
options.dsn = EnvConfig.sentryDsn;
|
||
options.tracesSampleRate = 1.0;
|
||
options.debug = false;
|
||
options.beforeSend = (event, hint) {
|
||
if (kDebugMode) return null;
|
||
if (!ErrorReportPolicy.allowSentryEvent(event, hint)) return null;
|
||
return event;
|
||
};
|
||
}, appRunner: appRunner);
|
||
}
|
||
}
|
||
|
||
enum AppBuildType { prod, tests, dev }
|