Files
umbrix/lib/features/connection/notifier/connection_notifier.dart

115 lines
3.8 KiB
Dart
Raw Normal View History

2023-12-01 12:56:24 +03:30
import 'package:hiddify/core/preferences/general_preferences.dart';
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';
2023-11-26 21:20:58 +03:30
import 'package:hiddify/features/profile/notifier/active_profile_notifier.dart';
2023-07-06 17:18:41 +03:30
import 'package:hiddify/utils/utils.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
2023-10-12 00:27:23 +03:30
import 'package:rxdart/rxdart.dart';
2023-07-06 17:18:41 +03:30
2023-12-01 12:56:24 +03:30
part 'connection_notifier.g.dart';
2023-07-06 17:18:41 +03:30
@Riverpod(keepAlive: true)
2023-12-01 12:56:24 +03:30
class ConnectionNotifier extends _$ConnectionNotifier with AppLogger {
2023-07-06 17:18:41 +03:30
@override
2023-08-19 22:27:23 +03:30
Stream<ConnectionStatus> build() {
2023-07-06 17:18:41 +03:30
ref.listen(
2023-08-19 22:27:23 +03:30
activeProfileProvider.select((value) => value.asData?.value),
(previous, next) async {
if (previous == null) return;
final shouldReconnect = next == null || previous.id != next.id;
2023-08-19 22:27:23 +03:30
if (shouldReconnect) {
2023-09-10 20:25:04 +03:30
await reconnect(next?.id);
2023-08-19 22:27:23 +03:30
}
},
2023-07-06 17:18:41 +03:30
);
2023-12-01 12:56:24 +03:30
return _connectionRepo.watchConnectionStatus().doOnData((event) {
2023-11-26 21:20:58 +03:30
if (event case Disconnected(connectionFailure: final _?)
2023-11-02 12:16:13 +03:30
when PlatformUtils.isDesktop) {
ref.read(startedByUserProvider.notifier).update(false);
}
2023-10-12 00:27:23 +03:30
loggy.info("connection status: ${event.format()}");
});
2023-07-06 17:18:41 +03:30
}
2023-12-01 12:56:24 +03:30
ConnectionRepository get _connectionRepo =>
ref.read(connectionRepositoryProvider);
2023-07-06 17:18:41 +03:30
2023-11-02 12:16:13 +03:30
Future<void> mayConnect() async {
if (state case AsyncData(:final value)) {
if (value case Disconnected()) return _connect();
}
}
2023-07-06 17:18:41 +03:30
Future<void> toggleConnection() async {
2023-08-19 22:27:23 +03:30
if (state case AsyncError()) {
await _connect();
} else if (state case AsyncData(:final value)) {
switch (value) {
case Disconnected():
2023-11-02 12:16:13 +03:30
await ref.read(startedByUserProvider.notifier).update(true);
2023-08-19 22:27:23 +03:30
await _connect();
case Connected():
2023-11-02 12:16:13 +03:30
await ref.read(startedByUserProvider.notifier).update(false);
2023-08-19 22:27:23 +03:30
await _disconnect();
default:
loggy.warning("switching status, debounce");
}
}
}
2023-09-10 20:25:04 +03:30
Future<void> reconnect(String? profileId) async {
if (state case AsyncData(:final value) when value == const Connected()) {
if (profileId == null) {
2023-10-26 20:37:58 +03:30
loggy.info("no active profile, disconnecting");
2023-09-10 20:25:04 +03:30
return _disconnect();
2023-08-19 22:27:23 +03:30
}
2023-10-26 20:37:58 +03:30
loggy.info("active profile changed, reconnecting");
2023-11-02 12:16:13 +03:30
await ref.read(startedByUserProvider.notifier).update(true);
2023-12-01 12:56:24 +03:30
await _connectionRepo
.reconnect(profileId, ref.read(disableMemoryLimitProvider))
2023-10-26 15:16:25 +03:30
.mapLeft((err) {
2023-10-03 21:12:14 +03:30
loggy.warning("error reconnecting", err);
state = AsyncError(err, StackTrace.current);
2023-09-10 20:25:04 +03:30
}).run();
2023-07-06 17:18:41 +03:30
}
}
2023-08-19 22:27:23 +03:30
Future<void> abortConnection() async {
if (state case AsyncData(:final value)) {
switch (value) {
case Connected() || Connecting():
loggy.debug("aborting connection");
await _disconnect();
default:
}
}
}
Future<void> _connect() async {
final activeProfile = await ref.read(activeProfileProvider.future);
2023-12-01 12:56:24 +03:30
await _connectionRepo
.connect(activeProfile!.id, ref.read(disableMemoryLimitProvider))
2023-11-12 21:55:17 +03:30
.mapLeft((err) async {
loggy.warning("error connecting", err);
await ref.read(startedByUserProvider.notifier).update(false);
2023-10-03 21:12:14 +03:30
state = AsyncError(err, StackTrace.current);
2023-08-19 22:27:23 +03:30
}).run();
}
Future<void> _disconnect() async {
2023-12-01 12:56:24 +03:30
await _connectionRepo.disconnect().mapLeft((err) {
2023-10-03 21:12:14 +03:30
loggy.warning("error disconnecting", err);
state = AsyncError(err, StackTrace.current);
2023-08-19 22:27:23 +03:30
}).run();
}
2023-07-06 17:18:41 +03:30
}
2023-08-19 22:27:23 +03:30
@Riverpod(keepAlive: true)
Future<bool> serviceRunning(ServiceRunningRef ref) => ref
.watch(
2023-12-01 12:56:24 +03:30
connectionNotifierProvider.selectAsync((data) => data.isConnected),
2023-08-19 22:27:23 +03:30
)
.onError((error, stackTrace) => false);