Add support for openURL event
This commit is contained in:
@@ -4,7 +4,9 @@ import SwiftUI
|
|||||||
|
|
||||||
public struct ExtensionStatusView: View {
|
public struct ExtensionStatusView: View {
|
||||||
@Environment(\.scenePhase) private var scenePhase
|
@Environment(\.scenePhase) private var scenePhase
|
||||||
|
@Environment(\.openURL) private var openURL
|
||||||
@StateObject private var commandClient = CommandClient(.status)
|
@StateObject private var commandClient = CommandClient(.status)
|
||||||
|
|
||||||
@State private var columnCount: Int = 4
|
@State private var columnCount: Int = 4
|
||||||
@State private var alert: Alert?
|
@State private var alert: Alert?
|
||||||
|
|
||||||
@@ -89,7 +91,9 @@ public struct ExtensionStatusView: View {
|
|||||||
.padding([.top, .leading, .trailing])
|
.padding([.top, .leading, .trailing])
|
||||||
}
|
}
|
||||||
.onAppear {
|
.onAppear {
|
||||||
commandClient.connect()
|
commandClient.connect { urlString in
|
||||||
|
openURL(URL(string: urlString)!)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
.onDisappear {
|
.onDisappear {
|
||||||
commandClient.disconnect()
|
commandClient.disconnect()
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ public class CommandClient: ObservableObject {
|
|||||||
private let logMaxLines: Int
|
private let logMaxLines: Int
|
||||||
private var commandClient: LibboxCommandClient?
|
private var commandClient: LibboxCommandClient?
|
||||||
private var connectTask: Task<Void, Error>?
|
private var connectTask: Task<Void, Error>?
|
||||||
|
private var openURLFunc: ((String) -> Void)?
|
||||||
|
|
||||||
@Published public var isConnected: Bool
|
@Published public var isConnected: Bool
|
||||||
@Published public var status: LibboxStatusMessage?
|
@Published public var status: LibboxStatusMessage?
|
||||||
@@ -30,13 +31,15 @@ public class CommandClient: ObservableObject {
|
|||||||
public init(_ connectionType: ConnectionType, logMaxLines: Int = 300) {
|
public init(_ connectionType: ConnectionType, logMaxLines: Int = 300) {
|
||||||
self.connectionType = connectionType
|
self.connectionType = connectionType
|
||||||
self.logMaxLines = logMaxLines
|
self.logMaxLines = logMaxLines
|
||||||
|
openURLFunc = nil
|
||||||
logList = []
|
logList = []
|
||||||
clashModeList = []
|
clashModeList = []
|
||||||
clashMode = ""
|
clashMode = ""
|
||||||
isConnected = false
|
isConnected = false
|
||||||
}
|
}
|
||||||
|
|
||||||
public func connect() {
|
public func connect(_ openURLFunc: ((String) -> Void)? = nil) {
|
||||||
|
self.openURLFunc = openURLFunc
|
||||||
if isConnected {
|
if isConnected {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -49,6 +52,7 @@ public class CommandClient: ObservableObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public func disconnect() {
|
public func disconnect() {
|
||||||
|
openURLFunc = nil
|
||||||
if let connectTask {
|
if let connectTask {
|
||||||
connectTask.cancel()
|
connectTask.cancel()
|
||||||
self.connectTask = nil
|
self.connectTask = nil
|
||||||
@@ -223,6 +227,13 @@ public class CommandClient: ObservableObject {
|
|||||||
commandClient.connections = connections
|
commandClient.connections = connections
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func openURL(_ url: String?) {
|
||||||
|
guard let url else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
commandClient.openURLFunc?(url)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user