From 08e0e7f72a99818020132d8821b89c9d508767b2 Mon Sep 17 00:00:00 2001 From: problematicconsumer Date: Sat, 9 Sep 2023 16:47:11 +0330 Subject: [PATCH] Add desktop autostart --- assets/translations/strings.i18n.json | 1 + assets/translations/strings_fa.i18n.json | 1 + lib/bootstrap.dart | 2 + .../widgets/general_setting_tiles.dart | 12 ++++++ lib/services/auto_start_service.dart | 37 +++++++++++++++++++ lib/services/runtime_details_service.dart | 8 ++-- pubspec.lock | 8 ++++ pubspec.yaml | 1 + 8 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 lib/services/auto_start_service.dart diff --git a/assets/translations/strings.i18n.json b/assets/translations/strings.i18n.json index c4570d6d..2f9494af 100644 --- a/assets/translations/strings.i18n.json +++ b/assets/translations/strings.i18n.json @@ -105,6 +105,7 @@ "light": "Light mode" }, "trueBlack": "True Black", + "autoStart": "Start on Boot", "silentStart": "Silent Start", "openWorkingDir": "Open Working Directory", "ignoreBatteryOptimizations": "Ignore Battery Optimization", diff --git a/assets/translations/strings_fa.i18n.json b/assets/translations/strings_fa.i18n.json index 840225a5..15c1400b 100644 --- a/assets/translations/strings_fa.i18n.json +++ b/assets/translations/strings_fa.i18n.json @@ -105,6 +105,7 @@ "light": "تم روشن" }, "trueBlack": "کاملا سیاه", + "autoStart": "اجرا با روشن شدن سیستم", "silentStart": "اجرای ساکت", "openWorkingDir": "باز کردن دایرکتوری کاری", "ignoreBatteryOptimizations": "نادیده‌گرفتن بهینه‌سازی باتری", diff --git a/lib/bootstrap.dart b/lib/bootstrap.dart index cd9da059..42343e08 100644 --- a/lib/bootstrap.dart +++ b/lib/bootstrap.dart @@ -11,6 +11,7 @@ import 'package:hiddify/features/common/active_profile/active_profile_notifier.d import 'package:hiddify/features/common/common.dart'; import 'package:hiddify/features/common/window/window_controller.dart'; import 'package:hiddify/features/system_tray/system_tray.dart'; +import 'package:hiddify/services/auto_start_service.dart'; import 'package:hiddify/services/deep_link_service.dart'; import 'package:hiddify/services/service_providers.dart'; import 'package:hiddify/utils/utils.dart'; @@ -47,6 +48,7 @@ Future lazyBootstrap(WidgetsBinding widgetsBinding) async { FlutterNativeSplash.remove(); } if (PlatformUtils.isDesktop) { + await container.read(autoStartServiceProvider.future); await container.read(windowControllerProvider.future); } diff --git a/lib/features/settings/widgets/general_setting_tiles.dart b/lib/features/settings/widgets/general_setting_tiles.dart index 226de814..a2baf48c 100644 --- a/lib/features/settings/widgets/general_setting_tiles.dart +++ b/lib/features/settings/widgets/general_setting_tiles.dart @@ -4,6 +4,7 @@ import 'package:go_router/go_router.dart'; import 'package:hiddify/core/core_providers.dart'; import 'package:hiddify/core/prefs/prefs.dart'; import 'package:hiddify/features/settings/widgets/theme_mode_switch_button.dart'; +import 'package:hiddify/services/auto_start_service.dart'; import 'package:hiddify/utils/utils.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart'; @@ -85,6 +86,17 @@ class GeneralSettingTiles extends HookConsumerWidget { onChanged: ref.read(trueBlackThemeNotifierProvider.notifier).update, ), if (PlatformUtils.isDesktop) ...[ + SwitchListTile( + title: Text(t.settings.general.autoStart), + value: ref.watch(autoStartServiceProvider).asData!.value, + onChanged: (value) async { + if (value) { + await ref.read(autoStartServiceProvider.notifier).enable(); + } else { + await ref.read(autoStartServiceProvider.notifier).disable(); + } + }, + ), SwitchListTile( title: Text(t.settings.general.silentStart), value: ref.watch(silentStartNotifierProvider), diff --git a/lib/services/auto_start_service.dart b/lib/services/auto_start_service.dart new file mode 100644 index 00000000..a03aeb2c --- /dev/null +++ b/lib/services/auto_start_service.dart @@ -0,0 +1,37 @@ +import 'dart:io'; + +import 'package:hiddify/services/service_providers.dart'; +import 'package:hiddify/utils/utils.dart'; +import 'package:launch_at_startup/launch_at_startup.dart'; +import 'package:riverpod_annotation/riverpod_annotation.dart'; + +part 'auto_start_service.g.dart'; + +@Riverpod(keepAlive: true) +class AutoStartService extends _$AutoStartService with InfraLogger { + @override + Future build() async { + loggy.debug("initializing"); + if (!PlatformUtils.isDesktop) return false; + final packageInfo = ref.watch(runtimeDetailsServiceProvider).packageInfo; + launchAtStartup.setup( + appName: packageInfo.appName, + appPath: Platform.resolvedExecutable, + ); + final isEnabled = await launchAtStartup.isEnabled(); + loggy.info("auto start is [${isEnabled ? "Enabled" : "Disabled"}]"); + return isEnabled; + } + + Future enable() async { + loggy.debug("enabling auto start"); + await launchAtStartup.enable(); + state = const AsyncValue.data(true); + } + + Future disable() async { + loggy.debug("disabling auto start"); + await launchAtStartup.disable(); + state = const AsyncValue.data(false); + } +} diff --git a/lib/services/runtime_details_service.dart b/lib/services/runtime_details_service.dart index dc70fc6e..ff51bd08 100644 --- a/lib/services/runtime_details_service.dart +++ b/lib/services/runtime_details_service.dart @@ -4,17 +4,17 @@ import 'package:hiddify/utils/utils.dart'; import 'package:package_info_plus/package_info_plus.dart'; class RuntimeDetailsService with InfraLogger { - late final PackageInfo _packageInfo; + late final PackageInfo packageInfo; - String get appVersion => _packageInfo.version; - String get buildNumber => _packageInfo.buildNumber; + String get appVersion => packageInfo.version; + String get buildNumber => packageInfo.buildNumber; late final String operatingSystem = Platform.operatingSystem; late final String userAgent; Future init() async { loggy.debug("initializing"); - _packageInfo = await PackageInfo.fromPlatform(); + packageInfo = await PackageInfo.fromPlatform(); userAgent = "HiddifyNext/$appVersion ($operatingSystem)"; loggy.info( diff --git a/pubspec.lock b/pubspec.lock index 040789b2..a2ee14c0 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -709,6 +709,14 @@ packages: url: "https://pub.dev" source: hosted version: "6.7.1" + launch_at_startup: + dependency: "direct main" + description: + name: launch_at_startup + sha256: "93fc5638e088290004fae358bae691486673d469957d461d9dae5b12248593eb" + url: "https://pub.dev" + source: hosted + version: "0.2.2" lint: dependency: "direct dev" description: diff --git a/pubspec.yaml b/pubspec.yaml index d4bd9144..5429d5f9 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -53,6 +53,7 @@ dependencies: package_info_plus: ^4.1.0 url_launcher: ^6.1.14 vclibs: ^0.1.0 + launch_at_startup: ^0.2.2 # utils combine: ^0.5.6