2024-01-15 01:31:20 +03:30
|
|
|
import 'dart:io';
|
|
|
|
|
|
2026-01-17 13:09:20 +03:00
|
|
|
import 'package:umbrix/core/haptic/haptic_service.dart';
|
|
|
|
|
import 'package:umbrix/core/preferences/general_preferences.dart';
|
|
|
|
|
import 'package:umbrix/features/connection/data/connection_data_providers.dart';
|
|
|
|
|
import 'package:umbrix/features/connection/data/connection_repository.dart';
|
|
|
|
|
import 'package:umbrix/features/connection/model/connection_status.dart';
|
|
|
|
|
import 'package:umbrix/features/profile/model/profile_entity.dart';
|
|
|
|
|
import 'package:umbrix/features/profile/notifier/active_profile_notifier.dart';
|
|
|
|
|
import 'package:umbrix/utils/utils.dart';
|
2024-07-04 21:04:44 +02:00
|
|
|
import 'package:in_app_review/in_app_review.dart';
|
2023-07-06 17:18:41 +03:30
|
|
|
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';
|
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
|
2024-01-15 01:31:20 +03:30
|
|
|
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
|
|
|
|
2024-07-04 21:04:44 +02:00
|
|
|
if (Platform.isAndroid && !ref.read(Preferences.storeReviewedByUser)) {
|
2024-03-17 14:45:15 +01:00
|
|
|
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;
|
2023-10-27 19:12:46 +03:30
|
|
|
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
|
|
|
);
|
2024-01-15 01:31:20 +03:30
|
|
|
yield* _connectionRepo.watchConnectionStatus().doOnData((event) {
|
2024-07-04 21:04:44 +02:00
|
|
|
if (event case Disconnected(connectionFailure: final _?) 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
|
|
|
}
|
|
|
|
|
|
2024-07-04 21:04:44 +02:00
|
|
|
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),
|
2024-07-29 13:11:51 +02:00
|
|
|
profile.testUrl,
|
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);
|
2024-02-22 10:04:21 +01:00
|
|
|
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(
|
2024-02-22 10:04:21 +01:00
|
|
|
activeProfile.id,
|
2023-12-14 14:50:10 +03:30
|
|
|
activeProfile.name,
|
2024-03-02 22:53:14 +03:30
|
|
|
ref.read(Preferences.disableMemoryLimit),
|
2024-07-29 13:11:51 +02:00
|
|
|
activeProfile.testUrl,
|
2023-12-14 14:50:10 +03:30
|
|
|
)
|
2023-11-12 21:55:17 +03:30
|
|
|
.mapLeft((err) async {
|
|
|
|
|
loggy.warning("error connecting", err);
|
2024-02-20 09:30:34 +01:00
|
|
|
//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);
|