Files
umbrix/lib/features/auto_start/notifier/auto_start_notifier.dart

38 lines
1.1 KiB
Dart
Raw Normal View History

2023-09-09 16:47:11 +03:30
import 'dart:io';
2023-12-01 12:56:24 +03:30
import 'package:hiddify/core/app_info/app_info_provider.dart';
2023-09-09 16:47:11 +03:30
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_notifier.g.dart';
2023-09-09 16:47:11 +03:30
@Riverpod(keepAlive: true)
class AutoStartNotifier extends _$AutoStartNotifier with InfraLogger {
2023-09-09 16:47:11 +03:30
@override
Future<bool> build() async {
if (!PlatformUtils.isDesktop) return false;
2023-12-01 12:56:24 +03:30
final appInfo = ref.watch(appInfoProvider).requireValue;
2023-09-09 16:47:11 +03:30
launchAtStartup.setup(
2023-09-12 15:22:58 +03:30
appName: appInfo.name,
2023-09-09 16:47:11 +03:30
appPath: Platform.resolvedExecutable,
);
final isEnabled = await launchAtStartup.isEnabled();
loggy.info("auto start is [${isEnabled ? "Enabled" : "Disabled"}]");
return isEnabled;
}
Future<void> enable() async {
loggy.debug("enabling auto start");
await launchAtStartup.enable();
state = const AsyncValue.data(true);
}
Future<void> disable() async {
loggy.debug("disabling auto start");
await launchAtStartup.disable();
state = const AsyncValue.data(false);
}
}