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_service.g.dart';
|
|
|
|
|
|
|
|
|
|
@Riverpod(keepAlive: true)
|
|
|
|
|
class AutoStartService extends _$AutoStartService with InfraLogger {
|
|
|
|
|
@override
|
|
|
|
|
Future<bool> build() async {
|
|
|
|
|
loggy.debug("initializing");
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|