From 204c47605c40e0e83f80396a7921972ccf177849 Mon Sep 17 00:00:00 2001 From: problematicconsumer Date: Mon, 15 Jan 2024 01:31:20 +0330 Subject: [PATCH 1/2] Fix ios connection status on app restart --- .../connection/data/connection_repository.dart | 3 ++- .../connection/notifier/connection_notifier.dart | 12 ++++++++++-- lib/singbox/service/platform_singbox_service.dart | 15 +++++++++++++-- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/lib/features/connection/data/connection_repository.dart b/lib/features/connection/data/connection_repository.dart index fe544dd2..71c5ee69 100644 --- a/lib/features/connection/data/connection_repository.dart +++ b/lib/features/connection/data/connection_repository.dart @@ -16,6 +16,7 @@ import 'package:hiddify/utils/utils.dart'; import 'package:meta/meta.dart'; abstract interface class ConnectionRepository { + TaskEither setup(); Stream watchConnectionStatus(); TaskEither connect( String fileName, @@ -120,7 +121,7 @@ class ConnectionRepositoryImpl ); } - @visibleForTesting + @override TaskEither setup() { if (_initialized) return TaskEither.of(unit); return exceptionHandler( diff --git a/lib/features/connection/notifier/connection_notifier.dart b/lib/features/connection/notifier/connection_notifier.dart index c1d3f1ec..18d18e35 100644 --- a/lib/features/connection/notifier/connection_notifier.dart +++ b/lib/features/connection/notifier/connection_notifier.dart @@ -1,3 +1,5 @@ +import 'dart:io'; + 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'; @@ -14,7 +16,13 @@ part 'connection_notifier.g.dart'; @Riverpod(keepAlive: true) class ConnectionNotifier extends _$ConnectionNotifier with AppLogger { @override - Stream build() { + Stream build() async* { + if (Platform.isIOS) { + await _connectionRepo.setup().mapLeft((l) { + loggy.error("error setting up connection repository", l); + }).run(); + } + ref.listen( activeProfileProvider.select((value) => value.asData?.value), (previous, next) async { @@ -25,7 +33,7 @@ class ConnectionNotifier extends _$ConnectionNotifier with AppLogger { } }, ); - return _connectionRepo.watchConnectionStatus().doOnData((event) { + yield* _connectionRepo.watchConnectionStatus().doOnData((event) { if (event case Disconnected(connectionFailure: final _?) when PlatformUtils.isDesktop) { ref.read(startedByUserProvider.notifier).update(false); diff --git a/lib/singbox/service/platform_singbox_service.dart b/lib/singbox/service/platform_singbox_service.dart index 4a5f7089..4dbc0fe4 100644 --- a/lib/singbox/service/platform_singbox_service.dart +++ b/lib/singbox/service/platform_singbox_service.dart @@ -1,4 +1,5 @@ import 'dart:convert'; +import 'dart:io'; import 'package:flutter/services.dart'; import 'package:fpdart/fpdart.dart'; @@ -37,8 +38,18 @@ class PlatformSingboxService with InfraLogger implements SingboxService { TaskEither setup( Directories directories, bool debug, - ) => - TaskEither.of(unit); + ) { + return TaskEither( + () async { + if (!Platform.isIOS) { + return right(unit); + } + + await _methodChannel.invokeMethod("setup"); + return right(unit); + }, + ); + } @override TaskEither validateConfigByPath( From 1d5b50c8f565053fdfca2dd1843fd92ccd7f49de Mon Sep 17 00:00:00 2001 From: problematicconsumer Date: Mon, 15 Jan 2024 01:39:05 +0330 Subject: [PATCH 2/2] Add config export on ios --- ios/Runner/Handlers/MethodHandler.swift | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/ios/Runner/Handlers/MethodHandler.swift b/ios/Runner/Handlers/MethodHandler.swift index 61337dd7..10b76798 100644 --- a/ios/Runner/Handlers/MethodHandler.swift +++ b/ios/Runner/Handlers/MethodHandler.swift @@ -154,6 +154,21 @@ public class MethodHandler: NSObject, FlutterPlugin { return } result(true) + case "generate_config": + guard + let args = call.arguments as? [String:Any?], + let path = args["path"] as? String + else { + result(FlutterError(code: "INVALID_ARGS", message: nil, details: nil)) + return + } + var error: NSError? + let config = MobileBuildConfig(path, VPNConfig.shared.configOptions, &error) + if let error { + result(FlutterError(code: "BUILD_CONFIG", message: error.localizedDescription, details: nil)) + return + } + result(config) default: result(FlutterMethodNotImplemented) }