Decouple WidgetExtension from Library

This commit is contained in:
世界
2026-02-05 15:19:14 +08:00
parent 20f2482376
commit f765bf1e3b
3 changed files with 74 additions and 30 deletions
+4 -15
View File
@@ -1,12 +1,11 @@
import AppIntents
import Library
import SwiftUI
import WidgetKit
struct ServiceToggleControl: ControlWidget {
var body: some ControlWidgetConfiguration {
StaticControlConfiguration(
kind: ExtensionProfile.controlKind,
kind: WidgetAppConfiguration.widgetControlKind,
provider: Provider()
) { value in
ControlWidgetToggle(
@@ -15,7 +14,7 @@ struct ServiceToggleControl: ControlWidget {
action: ToggleServiceControlIntent()
) { isOn in
Label(isOn ? "Running" : "Stopped", systemImage: "shippingbox.fill")
// .controlWidgetActionHint(isOn ? "Stop" : "Start")
.controlWidgetActionHint(isOn ? "Stop" : "Start")
}
.tint(.init(red: CGFloat(Double(69) / 255), green: CGFloat(Double(90) / 255), blue: CGFloat(Double(100) / 255)))
}
@@ -31,10 +30,7 @@ extension ServiceToggleControl {
}
func currentValue() async throws -> Bool {
guard let extensionProfile = try await (ExtensionProfile.load()) else {
return false
}
return await extensionProfile.status.isStarted
try await WidgetTunnelControl.currentIsStarted()
}
}
}
@@ -46,14 +42,7 @@ struct ToggleServiceControlIntent: SetValueIntent {
var value: Bool
func perform() async throws -> some IntentResult {
guard let extensionProfile = try await (ExtensionProfile.load()) else {
return .result()
}
if value {
try await extensionProfile.start()
} else {
try await extensionProfile.stop()
}
try await WidgetTunnelControl.setStarted(value)
return .result()
}
}