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-09-16 15:16:20 +03:30
|
|
|
import 'package:firebase_core/firebase_core.dart';
|
|
|
|
|
import 'package:firebase_crashlytics/firebase_crashlytics.dart';
|
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';
|
|
|
|
|
import 'package:hiddify/core/app/app.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-06 17:18:41 +03:30
|
|
|
import 'package:hiddify/features/common/active_profile/active_profile_notifier.dart';
|
2023-07-15 18:00:44 +03:30
|
|
|
import 'package:hiddify/features/common/window/window_controller.dart';
|
2023-07-06 17:18:41 +03:30
|
|
|
import 'package:hiddify/features/system_tray/system_tray.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';
|
|
|
|
|
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
|
|
|
|
|
|
|
|
final _loggy = Loggy('bootstrap');
|
|
|
|
|
final _stopWatch = Stopwatch();
|
|
|
|
|
|
2023-09-12 15:22:58 +03:30
|
|
|
Future<void> lazyBootstrap(
|
|
|
|
|
WidgetsBinding widgetsBinding,
|
|
|
|
|
Environment env,
|
|
|
|
|
) async {
|
2023-07-06 17:18:41 +03:30
|
|
|
_stopWatch.start();
|
|
|
|
|
|
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-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-16 15:16:20 +03:30
|
|
|
if (container.read(autoCrashReportProvider) && !kDebugMode) {
|
|
|
|
|
_loggy.debug("initializing crashlytics");
|
|
|
|
|
await initCrashlytics();
|
|
|
|
|
}
|
|
|
|
|
|
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-07-06 17:18:41 +03:30
|
|
|
|
2023-08-24 16:18:05 +03:30
|
|
|
initLoggers(container.read, debug);
|
2023-09-12 15:22:58 +03:30
|
|
|
_loggy.info(
|
|
|
|
|
"os: [${Platform.operatingSystem}](${Platform.operatingSystemVersion}), processor count [${Platform.numberOfProcessors}]",
|
|
|
|
|
);
|
2023-08-24 14:49:23 +03:30
|
|
|
_loggy.info("basic setup took [${_stopWatch.elapsedMilliseconds}]ms");
|
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();
|
|
|
|
|
}
|
|
|
|
|
if (PlatformUtils.isDesktop) {
|
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-07-06 17:18:41 +03:30
|
|
|
await initAppServices(container.read);
|
|
|
|
|
await initControllers(container.read);
|
|
|
|
|
|
|
|
|
|
runApp(
|
|
|
|
|
ProviderScope(
|
|
|
|
|
parent: container,
|
|
|
|
|
child: const AppView(),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
|
2023-07-15 18:00:44 +03:30
|
|
|
if (!silentStart) FlutterNativeSplash.remove();
|
2023-07-06 17:18:41 +03:30
|
|
|
_stopWatch.stop();
|
2023-08-24 14:49:23 +03:30
|
|
|
_loggy.info("bootstrapping took [${_stopWatch.elapsedMilliseconds}]ms");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-08-24 14:49:23 +03:30
|
|
|
Loggy.initLoggy(
|
|
|
|
|
logPrinter: MultiLogPrinter(
|
|
|
|
|
const PrettyPrinter(),
|
|
|
|
|
logToFile
|
|
|
|
|
? FileLogPrinter(read(filesEditorServiceProvider).appLogsPath)
|
|
|
|
|
: null,
|
|
|
|
|
),
|
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
|
|
|
}
|
|
|
|
|
|
2023-09-16 15:16:20 +03:30
|
|
|
Future<void> initCrashlytics() async {
|
|
|
|
|
switch (Platform.operatingSystem) {
|
|
|
|
|
case "android" || "ios" || "macos":
|
|
|
|
|
await Firebase.initializeApp();
|
|
|
|
|
FlutterError.onError =
|
|
|
|
|
FirebaseCrashlytics.instance.recordFlutterFatalError;
|
|
|
|
|
default:
|
|
|
|
|
_loggy.debug("platform is not supported for crashlytics");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-06 17:18:41 +03:30
|
|
|
Future<void> initAppServices(
|
|
|
|
|
Result Function<Result>(ProviderListenable<Result>) read,
|
|
|
|
|
) async {
|
2023-08-23 00:06:51 +03:30
|
|
|
_loggy.debug("initializing app services");
|
2023-07-06 17:18:41 +03:30
|
|
|
await Future.wait(
|
|
|
|
|
[
|
2023-09-10 20:25:04 +03:30
|
|
|
read(singboxServiceProvider).init(),
|
2023-07-06 17:18:41 +03:30
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
_loggy.debug('initialized app services');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> initControllers(
|
|
|
|
|
Result Function<Result>(ProviderListenable<Result>) read,
|
|
|
|
|
) async {
|
2023-08-19 22:27:23 +03:30
|
|
|
_loggy.debug("initializing controllers");
|
2023-07-06 17:18:41 +03:30
|
|
|
await Future.wait(
|
|
|
|
|
[
|
|
|
|
|
read(activeProfileProvider.future),
|
|
|
|
|
read(deepLinkServiceProvider.future),
|
|
|
|
|
if (PlatformUtils.isDesktop) read(systemTrayControllerProvider.future),
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
_loggy.debug("initialized base controllers");
|
|
|
|
|
}
|