Fix status mapper

This commit is contained in:
problematicconsumer
2023-12-01 14:22:00 +03:30
parent ed614988a2
commit d1ec932fff
3 changed files with 11 additions and 14 deletions

View File

@@ -6,6 +6,7 @@ import com.hiddify.hiddify.constant.Alert
import com.hiddify.hiddify.constant.Status
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.EventChannel
import io.flutter.plugin.common.JSONMethodCodec
class EventHandler : FlutterPlugin {
@@ -22,8 +23,8 @@ class EventHandler : FlutterPlugin {
private var alertsObserver: Observer<ServiceEvent?>? = null
override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
statusChannel = EventChannel(flutterPluginBinding.binaryMessenger, SERVICE_STATUS)
alertsChannel = EventChannel(flutterPluginBinding.binaryMessenger, SERVICE_ALERTS)
statusChannel = EventChannel(flutterPluginBinding.binaryMessenger, SERVICE_STATUS, JSONMethodCodec.INSTANCE)
alertsChannel = EventChannel(flutterPluginBinding.binaryMessenger, SERVICE_ALERTS, JSONMethodCodec.INSTANCE)
statusChannel!!.setStreamHandler(object : EventChannel.StreamHandler {
override fun onListen(arguments: Any?, events: EventChannel.EventSink?) {

View File

@@ -26,6 +26,8 @@ sealed class SingboxStatus with _$SingboxStatus {
(e) => alertStr?.toLowerCase() == e.name.toLowerCase(),
);
return SingboxStatus.stopped(alert: alert, message: messageStr);
case {"status": "Stopped"}:
return const SingboxStatus.stopped();
case {"status": "Starting"}:
return const SingboxStarting();
case {"status": "Started"}:

View File

@@ -14,9 +14,9 @@ import 'package:rxdart/rxdart.dart';
class PlatformSingboxService with InfraLogger implements SingboxService {
late final _methodChannel = const MethodChannel("com.hiddify.app/method");
late final _statusChannel =
const EventChannel("com.hiddify.app/service.status");
const EventChannel("com.hiddify.app/service.status", JSONMethodCodec());
late final _alertsChannel =
const EventChannel("com.hiddify.app/service.alerts");
const EventChannel("com.hiddify.app/service.alerts", JSONMethodCodec());
late final _logsChannel = const EventChannel("com.hiddify.app/service.logs");
late final ValueStream<SingboxStatus> _status;
@@ -24,16 +24,10 @@ class PlatformSingboxService with InfraLogger implements SingboxService {
@override
Future<void> init() async {
loggy.debug("initializing");
final status = _statusChannel.receiveBroadcastStream().map(
(event) {
return SingboxStatus.fromEvent(event);
},
);
final alerts = _alertsChannel.receiveBroadcastStream().map(
(event) {
return SingboxStatus.fromEvent(event);
},
);
final status =
_statusChannel.receiveBroadcastStream().map(SingboxStatus.fromEvent);
final alerts =
_alertsChannel.receiveBroadcastStream().map(SingboxStatus.fromEvent);
_status = ValueConnectableStream(Rx.merge([status, alerts])).autoConnect();
await _status.first;
}