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

166 lines
5.5 KiB
Dart
Raw Normal View History

import 'dart:io';
2024-02-16 14:33:03 +03:30
import 'package:hiddify/core/haptic/haptic_service.dart';
2023-12-01 12:56:24 +03:30
import 'package:hiddify/core/preferences/general_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-12-14 14:50:10 +03:30
import 'package:hiddify/features/profile/model/profile_entity.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';
2024-01-29 22:33:56 +01:00
import 'package:sentry_flutter/sentry_flutter.dart';
2024-03-17 14:45:15 +01:00
import 'package:in_app_review/in_app_review.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
Stream<ConnectionStatus> build() async* {
if (Platform.isIOS) {
await _connectionRepo.setup().mapLeft((l) {
loggy.error("error setting up connection repository", l);
}).run();
}
2024-02-18 18:24:42 +03:30
ref.listenSelf(
(previous, next) async {
if (previous == next) return;
if (previous case AsyncData(:final value) when !value.isConnected) {
if (next case AsyncData(value: final Connected _)) {
await ref.read(hapticServiceProvider.notifier).heavyImpact();
2024-03-17 14:45:15 +01:00
if (Platform.isAndroid &&
!ref.read(Preferences.storeReviewedByUser)) {
if (await InAppReview.instance.isAvailable()) {
InAppReview.instance.requestReview();
ref.read(Preferences.storeReviewedByUser.notifier).update(true);
}
}
2024-02-18 18:24:42 +03:30
}
}
},
);
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-12-14 14:50:10 +03:30
await reconnect(next);
2023-08-19 22:27:23 +03:30
}
},
2023-07-06 17:18:41 +03:30
);
yield* _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) {
2024-03-02 22:53:14 +03:30
ref.read(Preferences.startedByUser.notifier).update(false);
2023-11-02 12:16:13 +03:30
}
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 {
2024-02-16 14:33:03 +03:30
final haptic = ref.read(hapticServiceProvider.notifier);
2023-08-19 22:27:23 +03:30
if (state case AsyncError()) {
2024-02-16 14:33:03 +03:30
await haptic.lightImpact();
2023-08-19 22:27:23 +03:30
await _connect();
} else if (state case AsyncData(:final value)) {
switch (value) {
case Disconnected():
2024-02-16 14:33:03 +03:30
await haptic.lightImpact();
2024-03-02 22:53:14 +03:30
await ref.read(Preferences.startedByUser.notifier).update(true);
2023-08-19 22:27:23 +03:30
await _connect();
case Connected():
2024-02-16 14:33:03 +03:30
await haptic.mediumImpact();
2024-03-02 22:53:14 +03:30
await ref.read(Preferences.startedByUser.notifier).update(false);
2023-08-19 22:27:23 +03:30
await _disconnect();
default:
loggy.warning("switching status, debounce");
}
}
}
2023-12-14 14:50:10 +03:30
Future<void> reconnect(ProfileEntity? profile) async {
2023-09-10 20:25:04 +03:30
if (state case AsyncData(:final value) when value == const Connected()) {
2023-12-14 14:50:10 +03:30
if (profile == 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");
2024-03-02 22:53:14 +03:30
await ref.read(Preferences.startedByUser.notifier).update(true);
2023-12-01 12:56:24 +03:30
await _connectionRepo
2023-12-14 14:50:10 +03:30
.reconnect(
profile.id,
profile.name,
2024-03-02 22:53:14 +03:30
ref.read(Preferences.disableMemoryLimit),
2023-12-14 14:50:10 +03:30
)
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);
if (activeProfile == null) {
loggy.info("no active profile, not connecting");
return;
}
2023-12-01 12:56:24 +03:30
await _connectionRepo
2023-12-14 14:50:10 +03:30
.connect(
activeProfile.id,
2023-12-14 14:50:10 +03:30
activeProfile.name,
2024-03-02 22:53:14 +03:30
ref.read(Preferences.disableMemoryLimit),
2023-12-14 14:50:10 +03:30
)
2023-11-12 21:55:17 +03:30
.mapLeft((err) async {
loggy.warning("error connecting", err);
//Go err is not normal object to see the go errors are string and need to be dumped
loggy.warning(err);
2024-02-16 14:33:03 +03:30
if (err.toString().contains("panic")) {
2024-01-29 22:33:56 +01:00
await Sentry.captureException(Exception(err.toString()));
}
2024-03-02 22:53:14 +03:30
await ref.read(Preferences.startedByUser.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);