2023-07-06 17:18:41 +03:30
|
|
|
import 'dart:async';
|
2023-08-23 00:06:51 +03:30
|
|
|
import 'dart:io';
|
2023-07-06 17:18:41 +03:30
|
|
|
|
2023-08-24 14:49:23 +03:30
|
|
|
import 'package:flutter/foundation.dart';
|
2023-07-06 17:18:41 +03:30
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import 'package:flutter_native_splash/flutter_native_splash.dart';
|
2023-11-09 15:23:48 +03:30
|
|
|
import 'package:hiddify/core/app/app_view.dart';
|
2023-09-12 15:22:58 +03:30
|
|
|
import 'package:hiddify/core/core_providers.dart';
|
2023-07-15 18:00:44 +03:30
|
|
|
import 'package:hiddify/core/prefs/prefs.dart';
|
2023-07-06 17:18:41 +03:30
|
|
|
import 'package:hiddify/data/data_providers.dart';
|
2023-09-12 15:22:58 +03:30
|
|
|
import 'package:hiddify/data/repository/app_repository_impl.dart';
|
|
|
|
|
import 'package:hiddify/domain/environment.dart';
|
2023-07-15 18:00:44 +03:30
|
|
|
import 'package:hiddify/features/common/window/window_controller.dart';
|
2023-11-25 22:00:40 +03:30
|
|
|
import 'package:hiddify/features/geo_asset/data/geo_asset_data_providers.dart';
|
2023-11-26 21:20:58 +03:30
|
|
|
import 'package:hiddify/features/profile/data/profile_data_providers.dart';
|
|
|
|
|
import 'package:hiddify/features/profile/notifier/active_profile_notifier.dart';
|
2023-11-01 22:02:13 +03:30
|
|
|
import 'package:hiddify/features/system_tray/system_tray_controller.dart';
|
2023-09-09 16:47:11 +03:30
|
|
|
import 'package:hiddify/services/auto_start_service.dart';
|
2023-07-06 17:18:41 +03:30
|
|
|
import 'package:hiddify/services/deep_link_service.dart';
|
|
|
|
|
import 'package:hiddify/services/service_providers.dart';
|
|
|
|
|
import 'package:hiddify/utils/utils.dart';
|
|
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
|
|
|
import 'package:loggy/loggy.dart';
|
2023-09-17 00:23:31 +03:30
|
|
|
import 'package:sentry_flutter/sentry_flutter.dart';
|
2023-07-06 17:18:41 +03:30
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
2023-07-15 18:00:44 +03:30
|
|
|
import 'package:window_manager/window_manager.dart';
|
2023-07-06 17:18:41 +03:30
|
|
|
|
2023-09-18 14:01:44 +03:30
|
|
|
final _logger = Loggy('bootstrap');
|
2023-09-17 00:23:31 +03:30
|
|
|
const _testCrashReport = false;
|
2023-09-18 14:01:44 +03:30
|
|
|
final _loggers = MultiLogPrinter(const PrettyPrinter(), []);
|
2023-07-06 17:18:41 +03:30
|
|
|
|
2023-09-12 15:22:58 +03:30
|
|
|
Future<void> lazyBootstrap(
|
|
|
|
|
WidgetsBinding widgetsBinding,
|
|
|
|
|
Environment env,
|
|
|
|
|
) async {
|
2023-07-15 18:00:44 +03:30
|
|
|
FlutterNativeSplash.preserve(widgetsBinding: widgetsBinding);
|
2023-07-18 14:03:48 +03:30
|
|
|
if (PlatformUtils.isDesktop) await windowManager.ensureInitialized();
|
2023-09-18 22:34:17 +03:30
|
|
|
|
|
|
|
|
final sentryLogger = SentryLoggyIntegration();
|
|
|
|
|
_loggers.addPrinter(sentryLogger);
|
2023-09-18 14:01:44 +03:30
|
|
|
Loggy.initLoggy();
|
2023-07-15 18:00:44 +03:30
|
|
|
|
2023-09-12 15:22:58 +03:30
|
|
|
final appInfo = await AppRepositoryImpl.getAppInfo(env);
|
2023-07-06 17:18:41 +03:30
|
|
|
final sharedPreferences = await SharedPreferences.getInstance();
|
|
|
|
|
final container = ProviderContainer(
|
2023-09-12 15:22:58 +03:30
|
|
|
overrides: [
|
|
|
|
|
appInfoProvider.overrideWithValue(appInfo),
|
|
|
|
|
sharedPreferencesProvider.overrideWithValue(sharedPreferences),
|
|
|
|
|
],
|
2023-07-06 17:18:41 +03:30
|
|
|
);
|
|
|
|
|
|
2023-09-17 00:23:31 +03:30
|
|
|
final enableAnalytics = container.read(enableAnalyticsProvider);
|
|
|
|
|
|
|
|
|
|
await SentryFlutter.init(
|
|
|
|
|
(options) {
|
|
|
|
|
if ((enableAnalytics && !kDebugMode) || _testCrashReport) {
|
|
|
|
|
options.dsn = Environment.sentryDSN;
|
|
|
|
|
} else {
|
|
|
|
|
options.dsn = "";
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-18 14:01:44 +03:30
|
|
|
options.environment = env.name;
|
|
|
|
|
options.dist = appInfo.release.name;
|
2023-09-17 00:23:31 +03:30
|
|
|
options.debug = kDebugMode;
|
2023-09-18 14:01:44 +03:30
|
|
|
options.enableNativeCrashHandling = true;
|
|
|
|
|
options.enableNdkScopeSync = true;
|
|
|
|
|
options.attachThreads = true;
|
|
|
|
|
options.tracesSampleRate = 0.25;
|
2023-09-20 12:42:06 +03:30
|
|
|
options.enableUserInteractionTracing = true;
|
2023-09-18 14:01:44 +03:30
|
|
|
options.addIntegration(sentryLogger);
|
2023-10-03 21:12:14 +03:30
|
|
|
options.beforeSend = sentryBeforeSend;
|
2023-09-22 23:54:00 +03:30
|
|
|
options.logger = (level, message, {exception, logger, stackTrace}) {
|
|
|
|
|
if (level == SentryLevel.fatal) {
|
|
|
|
|
_logger.debug(message);
|
|
|
|
|
}
|
|
|
|
|
};
|
2023-09-17 00:23:31 +03:30
|
|
|
},
|
|
|
|
|
appRunner: () => _lazyBootstrap(widgetsBinding, container, env),
|
|
|
|
|
);
|
|
|
|
|
}
|
2023-09-16 15:16:20 +03:30
|
|
|
|
2023-09-17 00:23:31 +03:30
|
|
|
Future<void> _lazyBootstrap(
|
|
|
|
|
WidgetsBinding widgetsBinding,
|
|
|
|
|
ProviderContainer container,
|
|
|
|
|
Environment env,
|
|
|
|
|
) async {
|
2023-09-07 13:43:46 +03:30
|
|
|
final debug = container.read(debugModeNotifierProvider) || kDebugMode;
|
2023-08-24 16:18:05 +03:30
|
|
|
|
2023-08-19 22:27:23 +03:30
|
|
|
final filesEditor = container.read(filesEditorServiceProvider);
|
|
|
|
|
await filesEditor.init();
|
2023-11-25 22:00:40 +03:30
|
|
|
await container.read(geoAssetRepositoryProvider.future);
|
2023-11-26 21:20:58 +03:30
|
|
|
await container.read(profileRepositoryProvider.future);
|
2023-07-06 17:18:41 +03:30
|
|
|
|
2023-08-24 16:18:05 +03:30
|
|
|
initLoggers(container.read, debug);
|
2023-10-06 23:42:06 +03:30
|
|
|
_logger.info(container.read(appInfoProvider).format());
|
2023-08-23 00:06:51 +03:30
|
|
|
|
2023-09-07 13:43:46 +03:30
|
|
|
final silentStart = container.read(silentStartNotifierProvider);
|
2023-07-15 18:00:44 +03:30
|
|
|
if (silentStart) {
|
|
|
|
|
FlutterNativeSplash.remove();
|
|
|
|
|
}
|
2023-11-12 21:55:17 +03:30
|
|
|
|
2023-07-15 18:00:44 +03:30
|
|
|
if (PlatformUtils.isDesktop) {
|
2023-11-12 21:55:17 +03:30
|
|
|
_logger.debug("initializing [Auto Start Service] and [Window Controller]");
|
2023-09-09 16:47:11 +03:30
|
|
|
await container.read(autoStartServiceProvider.future);
|
2023-07-15 18:00:44 +03:30
|
|
|
await container.read(windowControllerProvider.future);
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-12 21:55:17 +03:30
|
|
|
await container.read(singboxServiceProvider).init();
|
|
|
|
|
_logger.debug("initialized [Singbox Service]");
|
|
|
|
|
|
|
|
|
|
await container.read(activeProfileProvider.future);
|
|
|
|
|
await container.read(deepLinkServiceProvider.future);
|
|
|
|
|
if (PlatformUtils.isDesktop) {
|
|
|
|
|
try {
|
|
|
|
|
await container
|
|
|
|
|
.read(systemTrayControllerProvider.future)
|
|
|
|
|
.timeout(const Duration(seconds: 1));
|
|
|
|
|
_logger.debug("initialized [System Tray Controller]");
|
|
|
|
|
} catch (error, stackTrace) {
|
|
|
|
|
_logger.warning(
|
|
|
|
|
"error initializing [System Tray Controller]",
|
|
|
|
|
error,
|
|
|
|
|
stackTrace,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-07-06 17:18:41 +03:30
|
|
|
|
|
|
|
|
runApp(
|
|
|
|
|
ProviderScope(
|
|
|
|
|
parent: container,
|
2023-09-18 14:01:44 +03:30
|
|
|
child: SentryUserInteractionWidget(
|
|
|
|
|
child: const AppView(),
|
|
|
|
|
),
|
2023-07-06 17:18:41 +03:30
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
|
2023-07-15 18:00:44 +03:30
|
|
|
if (!silentStart) FlutterNativeSplash.remove();
|
2023-08-24 14:49:23 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void initLoggers(
|
|
|
|
|
Result Function<Result>(ProviderListenable<Result>) read,
|
2023-08-24 16:18:05 +03:30
|
|
|
bool debug,
|
2023-08-24 14:49:23 +03:30
|
|
|
) {
|
2023-08-24 16:18:05 +03:30
|
|
|
final logLevel = debug ? LogLevel.all : LogLevel.info;
|
|
|
|
|
final logToFile = debug || (!Platform.isAndroid && !Platform.isIOS);
|
2023-09-18 14:01:44 +03:30
|
|
|
if (logToFile) {
|
|
|
|
|
_loggers.addPrinter(
|
2023-10-26 13:51:20 +03:30
|
|
|
FileLogPrinter(read(filesEditorServiceProvider).appLogsFile.path),
|
2023-09-18 14:01:44 +03:30
|
|
|
);
|
|
|
|
|
}
|
2023-08-24 14:49:23 +03:30
|
|
|
Loggy.initLoggy(
|
2023-09-18 14:01:44 +03:30
|
|
|
logPrinter: _loggers,
|
2023-08-24 16:18:05 +03:30
|
|
|
logOptions: LogOptions(logLevel),
|
2023-08-24 14:49:23 +03:30
|
|
|
);
|
2023-07-06 17:18:41 +03:30
|
|
|
}
|