From 509ed018d86c03d20d1376b787ea42d463d51f5c Mon Sep 17 00:00:00 2001 From: mrFq1 <1xxbx0il0@mozmail.com> Date: Fri, 4 Oct 2024 17:58:19 +0800 Subject: [PATCH] fix: menu item traffic --- ClashX/Dashboard/Views/ClashApiDatasStorage.swift | 6 ++++-- ClashX/General/ApiRequest.swift | 6 ++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/ClashX/Dashboard/Views/ClashApiDatasStorage.swift b/ClashX/Dashboard/Views/ClashApiDatasStorage.swift index fbf04b662..18cc03c0f 100644 --- a/ClashX/Dashboard/Views/ClashApiDatasStorage.swift +++ b/ClashX/Dashboard/Views/ClashApiDatasStorage.swift @@ -16,8 +16,10 @@ class ClashApiDatasStorage: NSObject, ObservableObject { @Published var connsStorage = ClashConnsStorage() func resetStreamApi() { - ApiRequest.shared.delegate = self - ApiRequest.shared.resetStreamApis() + ApiRequest.shared.dashboardDelegate = self + if ApiRequest.shared.delegate == nil { + ApiRequest.shared.resetStreamApis() + } } } diff --git a/ClashX/General/ApiRequest.swift b/ClashX/General/ApiRequest.swift index c7f1bb5f9..dc573091a 100644 --- a/ClashX/General/ApiRequest.swift +++ b/ClashX/General/ApiRequest.swift @@ -81,6 +81,7 @@ class ApiRequest { } weak var delegate: ApiRequestStreamDelegate? + weak var dashboardDelegate: ApiRequestStreamDelegate? private var trafficWebSocket: WebSocket? private var loggingWebSocket: WebSocket? @@ -640,6 +641,7 @@ extension ApiRequest: WebSocketDelegate { ConfigManager.shared.isRunning = true delegate?.streamStatusChanged() + dashboardDelegate?.streamStatusChanged() case loggingWebSocket: loggingWebSocketRetryDelay = 1 Logger.log("loggingWebSocket did Connect", level: .debug) @@ -656,6 +658,7 @@ extension ApiRequest: WebSocketDelegate { if (socket as? WebSocket) == trafficWebSocket { ConfigManager.shared.isRunning = false delegate?.streamStatusChanged() + dashboardDelegate?.streamStatusChanged() } guard let err = error else { @@ -711,10 +714,13 @@ extension ApiRequest: WebSocketDelegate { switch webSocket { case trafficWebSocket: delegate?.didUpdateTraffic(up: json["up"].intValue, down: json["down"].intValue) + dashboardDelegate?.didUpdateTraffic(up: json["up"].intValue, down: json["down"].intValue) case loggingWebSocket: delegate?.didGetLog(log: json["payload"].stringValue, level: json["type"].string ?? "info") + dashboardDelegate?.didGetLog(log: json["payload"].stringValue, level: json["type"].string ?? "info") case memoryWebSocket: delegate?.didUpdateMemory(memory: json["inuse"].int64Value) + dashboardDelegate?.didUpdateMemory(memory: json["inuse"].int64Value) default: return }