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

@@ -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),