Add iOS 18 control widget
This commit is contained in:
@@ -107,7 +107,12 @@ public class CommandClient: ObservableObject {
|
||||
case .connections:
|
||||
clientOptions.command = LibboxCommandConnections
|
||||
}
|
||||
clientOptions.statusInterval = Int64(2 * NSEC_PER_SEC)
|
||||
switch connectionType {
|
||||
case .log:
|
||||
clientOptions.statusInterval = Int64(500 * NSEC_PER_MSEC)
|
||||
default:
|
||||
clientOptions.statusInterval = Int64(2 * NSEC_PER_SEC)
|
||||
}
|
||||
let client = LibboxNewCommandClient(clientHandler(self), clientOptions)!
|
||||
do {
|
||||
for i in 0 ..< 10 {
|
||||
@@ -152,21 +157,23 @@ public class CommandClient: ObservableObject {
|
||||
}
|
||||
}
|
||||
|
||||
func clearLog() {
|
||||
func clearLogs() {
|
||||
DispatchQueue.main.async { [self] in
|
||||
commandClient.logList.removeAll()
|
||||
}
|
||||
}
|
||||
|
||||
func writeLog(_ message: String?) {
|
||||
guard let message else {
|
||||
func writeLogs(_ messageList: (any LibboxStringIteratorProtocol)?) {
|
||||
guard let messageList else {
|
||||
return
|
||||
}
|
||||
DispatchQueue.main.async { [self] in
|
||||
if commandClient.logList.count > commandClient.logMaxLines {
|
||||
commandClient.logList.removeFirst()
|
||||
if commandClient.logList.count >= commandClient.logMaxLines {
|
||||
commandClient.logList.removeSubrange(0 ..< Int(messageList.len()))
|
||||
}
|
||||
while messageList.hasNext() {
|
||||
commandClient.logList.append(messageList.next())
|
||||
}
|
||||
commandClient.logList.append(message)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,8 @@ import Libbox
|
||||
import NetworkExtension
|
||||
|
||||
public class ExtensionProfile: ObservableObject {
|
||||
public static let controlKind = "io.nekohasekai.sfavt.widget.ServiceToggle"
|
||||
|
||||
private let manager: NEVPNManager
|
||||
private var connection: NEVPNConnection
|
||||
private var observer: Any?
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import Foundation
|
||||
import Libbox
|
||||
import NetworkExtension
|
||||
#if os(iOS)
|
||||
import WidgetKit
|
||||
#endif
|
||||
|
||||
open class ExtensionProvider: NEPacketTunnelProvider {
|
||||
public var username: String? = nil
|
||||
@@ -49,13 +52,16 @@ open class ExtensionProvider: NEPacketTunnelProvider {
|
||||
}
|
||||
writeMessage("(packet-tunnel): Here I stand")
|
||||
await startService()
|
||||
#if os(iOS)
|
||||
if #available(iOS 18.0, *) {
|
||||
ControlCenter.shared.reloadControls(ofKind: ExtensionProfile.controlKind)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
func writeMessage(_ message: String) {
|
||||
if let commandServer {
|
||||
commandServer.writeMessage(message)
|
||||
} else {
|
||||
NSLog(message)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -154,6 +160,11 @@ open class ExtensionProvider: NEPacketTunnelProvider {
|
||||
await SharedPreferences.startedByUser.set(reason == .userInitiated)
|
||||
}
|
||||
#endif
|
||||
#if os(iOS)
|
||||
if #available(iOS 18.0, *) {
|
||||
ControlCenter.shared.reloadControls(ofKind: ExtensionProfile.controlKind)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
override open func handleAppMessage(_ messageData: Data) async -> Data? {
|
||||
|
||||
@@ -4,7 +4,16 @@ import NetworkExtension
|
||||
public extension NEVPNStatus {
|
||||
var isEnabled: Bool {
|
||||
switch self {
|
||||
case .connected, .disconnected, .reasserting:
|
||||
case .connecting, .connected, .disconnected, .reasserting:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
var isStarted: Bool {
|
||||
switch self {
|
||||
case .connecting, .connected, .reasserting:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
|
||||
Reference in New Issue
Block a user