Add android dynamic notification

This commit is contained in:
problematicconsumer
2023-12-14 14:50:10 +03:30
parent b9f1e83473
commit af64efec00
19 changed files with 251 additions and 95 deletions

View File

@@ -184,3 +184,20 @@ class MarkNewProfileActive extends _$MarkNewProfileActive {
return _pref.update(value);
}
}
@riverpod
class DynamicNotification extends _$DynamicNotification {
late final _pref = Pref(
ref.watch(sharedPreferencesProvider).requireValue,
"dynamic_notification",
true,
);
@override
bool build() => _pref.getValue();
Future<void> update(bool value) {
state = value;
return _pref.update(value);
}
}

View File

@@ -19,11 +19,13 @@ abstract interface class ConnectionRepository {
Stream<ConnectionStatus> watchConnectionStatus();
TaskEither<ConnectionFailure, Unit> connect(
String fileName,
String profileName,
bool disableMemoryLimit,
);
TaskEither<ConnectionFailure, Unit> disconnect();
TaskEither<ConnectionFailure, Unit> reconnect(
String fileName,
String profileName,
bool disableMemoryLimit,
);
}
@@ -144,6 +146,7 @@ class ConnectionRepositoryImpl
@override
TaskEither<ConnectionFailure, Unit> connect(
String fileName,
String profileName,
bool disableMemoryLimit,
) {
return TaskEither<ConnectionFailure, Unit>.Do(
@@ -173,6 +176,7 @@ class ConnectionRepositoryImpl
singbox
.start(
profilePathResolver.file(fileName).path,
profileName,
disableMemoryLimit,
)
.mapLeft(UnexpectedConnectionFailure.new),
@@ -192,6 +196,7 @@ class ConnectionRepositoryImpl
@override
TaskEither<ConnectionFailure, Unit> reconnect(
String fileName,
String profileName,
bool disableMemoryLimit,
) {
return exceptionHandler(
@@ -202,6 +207,7 @@ class ConnectionRepositoryImpl
() => singbox
.restart(
profilePathResolver.file(fileName).path,
profileName,
disableMemoryLimit,
)
.mapLeft(UnexpectedConnectionFailure.new),

View File

@@ -3,6 +3,7 @@ import 'package:hiddify/core/preferences/service_preferences.dart';
import 'package:hiddify/features/connection/data/connection_data_providers.dart';
import 'package:hiddify/features/connection/data/connection_repository.dart';
import 'package:hiddify/features/connection/model/connection_status.dart';
import 'package:hiddify/features/profile/model/profile_entity.dart';
import 'package:hiddify/features/profile/notifier/active_profile_notifier.dart';
import 'package:hiddify/utils/utils.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
@@ -20,7 +21,7 @@ class ConnectionNotifier extends _$ConnectionNotifier with AppLogger {
if (previous == null) return;
final shouldReconnect = next == null || previous.id != next.id;
if (shouldReconnect) {
await reconnect(next?.id);
await reconnect(next);
}
},
);
@@ -59,16 +60,20 @@ class ConnectionNotifier extends _$ConnectionNotifier with AppLogger {
}
}
Future<void> reconnect(String? profileId) async {
Future<void> reconnect(ProfileEntity? profile) async {
if (state case AsyncData(:final value) when value == const Connected()) {
if (profileId == null) {
if (profile == null) {
loggy.info("no active profile, disconnecting");
return _disconnect();
}
loggy.info("active profile changed, reconnecting");
await ref.read(startedByUserProvider.notifier).update(true);
await _connectionRepo
.reconnect(profileId, ref.read(disableMemoryLimitProvider))
.reconnect(
profile.id,
profile.name,
ref.read(disableMemoryLimitProvider),
)
.mapLeft((err) {
loggy.warning("error reconnecting", err);
state = AsyncError(err, StackTrace.current);
@@ -90,7 +95,11 @@ class ConnectionNotifier extends _$ConnectionNotifier with AppLogger {
Future<void> _connect() async {
final activeProfile = await ref.read(activeProfileProvider.future);
await _connectionRepo
.connect(activeProfile!.id, ref.read(disableMemoryLimitProvider))
.connect(
activeProfile!.id,
activeProfile.name,
ref.read(disableMemoryLimitProvider),
)
.mapLeft((err) async {
loggy.warning("error connecting", err);
await ref.read(startedByUserProvider.notifier).update(false);

View File

@@ -128,7 +128,7 @@ class UpdateProfile extends _$UpdateProfile with AppLogger {
if (active != null && active.id == profile.id) {
await ref
.read(connectionNotifierProvider.notifier)
.reconnect(profile.id);
.reconnect(profile);
}
});
return unit;

View File

@@ -1,3 +1,5 @@
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:hiddify/core/localization/translations.dart';
@@ -73,6 +75,17 @@ class GeneralSettingTiles extends HookConsumerWidget {
}
},
),
if (Platform.isAndroid)
SwitchListTile(
title: Text(t.settings.general.dynamicNotification),
secondary: const Icon(Icons.speed),
value: ref.watch(dynamicNotificationProvider),
onChanged: (value) async {
await ref
.read(dynamicNotificationProvider.notifier)
.update(value);
},
),
if (PlatformUtils.isDesktop) ...[
SwitchListTile(
title: Text(t.settings.general.autoStart),

View File

@@ -158,7 +158,11 @@ class FFISingboxService with InfraLogger implements SingboxService {
}
@override
TaskEither<String, Unit> start(String configPath, bool disableMemoryLimit) {
TaskEither<String, Unit> start(
String configPath,
String name,
bool disableMemoryLimit,
) {
loggy.debug("starting, memory limit: [${!disableMemoryLimit}]");
return TaskEither(
() => CombineWorker().execute(
@@ -195,7 +199,11 @@ class FFISingboxService with InfraLogger implements SingboxService {
}
@override
TaskEither<String, Unit> restart(String configPath, bool disableMemoryLimit) {
TaskEither<String, Unit> restart(
String configPath,
String name,
bool disableMemoryLimit,
) {
loggy.debug("restarting, memory limit: [${!disableMemoryLimit}]");
return TaskEither(
() => CombineWorker().execute(

View File

@@ -89,13 +89,17 @@ class PlatformSingboxService with InfraLogger implements SingboxService {
}
@override
TaskEither<String, Unit> start(String path, bool disableMemoryLimit) {
TaskEither<String, Unit> start(
String path,
String name,
bool disableMemoryLimit,
) {
return TaskEither(
() async {
loggy.debug("starting");
await _methodChannel.invokeMethod(
"start",
{"path": path},
{"path": path, "name": name},
);
return right(unit);
},
@@ -114,13 +118,17 @@ class PlatformSingboxService with InfraLogger implements SingboxService {
}
@override
TaskEither<String, Unit> restart(String path, bool disableMemoryLimit) {
TaskEither<String, Unit> restart(
String path,
String name,
bool disableMemoryLimit,
) {
return TaskEither(
() async {
loggy.debug("restarting");
await _methodChannel.invokeMethod(
"restart",
{"path": path},
{"path": path, "name": name},
);
return right(unit);
},

View File

@@ -38,11 +38,19 @@ abstract interface class SingboxService {
String path,
);
TaskEither<String, Unit> start(String path, bool disableMemoryLimit);
TaskEither<String, Unit> start(
String path,
String name,
bool disableMemoryLimit,
);
TaskEither<String, Unit> stop();
TaskEither<String, Unit> restart(String path, bool disableMemoryLimit);
TaskEither<String, Unit> restart(
String path,
String name,
bool disableMemoryLimit,
);
Stream<List<SingboxOutboundGroup>> watchOutbounds();