feat: complete widget support
This commit is contained in:
@@ -1,81 +1,202 @@
|
||||
import WidgetKit
|
||||
import SwiftUI
|
||||
|
||||
struct Provider: AppIntentTimelineProvider {
|
||||
func placeholder(in context: Context) -> SimpleEntry {
|
||||
SimpleEntry(date: Date(), configuration: ConfigurationAppIntent())
|
||||
}
|
||||
// MARK: - Timeline Entry
|
||||
|
||||
func snapshot(for configuration: ConfigurationAppIntent, in context: Context) async -> SimpleEntry {
|
||||
SimpleEntry(date: Date(), configuration: configuration)
|
||||
}
|
||||
|
||||
func timeline(for configuration: ConfigurationAppIntent, in context: Context) async -> Timeline<SimpleEntry> {
|
||||
var entries: [SimpleEntry] = []
|
||||
|
||||
// Generate a timeline consisting of five entries an hour apart, starting from the current date.
|
||||
let currentDate = Date()
|
||||
for hourOffset in 0 ..< 5 {
|
||||
let entryDate = Calendar.current.date(byAdding: .hour, value: hourOffset, to: currentDate)!
|
||||
let entry = SimpleEntry(date: entryDate, configuration: configuration)
|
||||
entries.append(entry)
|
||||
}
|
||||
|
||||
return Timeline(entries: entries, policy: .atEnd)
|
||||
}
|
||||
|
||||
// func relevances() async -> WidgetRelevances<ConfigurationAppIntent> {
|
||||
// // Generate a list containing the contexts this widget is relevant in.
|
||||
// }
|
||||
}
|
||||
|
||||
struct SimpleEntry: TimelineEntry {
|
||||
struct DeviceEntry: TimelineEntry {
|
||||
let date: Date
|
||||
let configuration: ConfigurationAppIntent
|
||||
let device: DeviceInfo?
|
||||
}
|
||||
|
||||
struct widgetEntryView : View {
|
||||
// MARK: - Timeline Provider
|
||||
|
||||
struct Provider: AppIntentTimelineProvider {
|
||||
func placeholder(in context: Context) -> DeviceEntry {
|
||||
DeviceEntry(
|
||||
date: Date(),
|
||||
configuration: ConfigurationAppIntent(),
|
||||
device: DeviceInfo(id: "placeholder", name: "My Computer", mac: "AA:BB:CC:DD:EE:FF", ip: "192.168.1.100", status: "unknown")
|
||||
)
|
||||
}
|
||||
|
||||
func snapshot(for configuration: ConfigurationAppIntent, in context: Context) async -> DeviceEntry {
|
||||
let device = configuration.device.flatMap { SharedDeviceData.getDevice(id: $0.id) }
|
||||
return DeviceEntry(date: Date(), configuration: configuration, device: device)
|
||||
}
|
||||
|
||||
func timeline(for configuration: ConfigurationAppIntent, in context: Context) async -> Timeline<DeviceEntry> {
|
||||
let device = configuration.device.flatMap { SharedDeviceData.getDevice(id: $0.id) }
|
||||
let entry = DeviceEntry(date: Date(), configuration: configuration, device: device)
|
||||
|
||||
// Widgets don't update frequently enough for status, so we just provide a static entry
|
||||
// Refresh every 30 minutes to pick up device list changes
|
||||
let nextUpdate = Calendar.current.date(byAdding: .minute, value: 30, to: Date())!
|
||||
return Timeline(entries: [entry], policy: .after(nextUpdate))
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Deep Link Helper
|
||||
|
||||
enum DeviceAction: String {
|
||||
case wake
|
||||
case sleep
|
||||
case restart
|
||||
case shutdown
|
||||
|
||||
func url(for deviceId: String) -> URL {
|
||||
URL(string: "remotewol-upsnap://action/\(self.rawValue)/\(deviceId)")!
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Widget View
|
||||
|
||||
struct DeviceWidgetEntryView: View {
|
||||
var entry: Provider.Entry
|
||||
|
||||
@Environment(\.widgetFamily) var widgetFamily
|
||||
|
||||
var body: some View {
|
||||
VStack {
|
||||
Text("Time:")
|
||||
Text(entry.date, style: .time)
|
||||
|
||||
Text("Favorite Emoji:")
|
||||
Text(entry.configuration.favoriteEmoji)
|
||||
if let device = entry.device {
|
||||
VStack(alignment: .leading, spacing: 8) {
|
||||
// Device Info Header
|
||||
VStack(alignment: .leading, spacing: 2) {
|
||||
Text(device.name)
|
||||
.font(.system(size: 15, weight: .semibold))
|
||||
.lineLimit(1)
|
||||
Text(device.mac)
|
||||
.font(.system(size: 11))
|
||||
.foregroundColor(.secondary)
|
||||
.lineLimit(1)
|
||||
}
|
||||
|
||||
Spacer(minLength: 4)
|
||||
|
||||
// Action Buttons
|
||||
if widgetFamily == .systemSmall {
|
||||
// Small widget: 2x2 grid
|
||||
VStack(spacing: 6) {
|
||||
HStack(spacing: 6) {
|
||||
ActionButton(action: .wake, deviceId: device.id)
|
||||
ActionButton(action: .sleep, deviceId: device.id)
|
||||
}
|
||||
HStack(spacing: 6) {
|
||||
ActionButton(action: .restart, deviceId: device.id)
|
||||
ActionButton(action: .shutdown, deviceId: device.id)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Medium/Large widget: horizontal layout
|
||||
HStack(spacing: 8) {
|
||||
ActionButton(action: .wake, deviceId: device.id)
|
||||
ActionButton(action: .sleep, deviceId: device.id)
|
||||
ActionButton(action: .restart, deviceId: device.id)
|
||||
ActionButton(action: .shutdown, deviceId: device.id)
|
||||
}
|
||||
}
|
||||
}
|
||||
.padding(.vertical, 4)
|
||||
} else {
|
||||
// No device selected
|
||||
VStack(spacing: 8) {
|
||||
Image(systemName: "desktopcomputer")
|
||||
.font(.system(size: 32))
|
||||
.foregroundColor(.secondary)
|
||||
Text("Select a Device")
|
||||
.font(.system(size: 14, weight: .medium))
|
||||
.foregroundColor(.secondary)
|
||||
Text("Long press to configure")
|
||||
.font(.system(size: 11))
|
||||
.foregroundColor(.secondary.opacity(0.7))
|
||||
}
|
||||
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct widget: Widget {
|
||||
// MARK: - Action Button
|
||||
|
||||
struct ActionButton: View {
|
||||
let action: DeviceAction
|
||||
let deviceId: String
|
||||
|
||||
var icon: String {
|
||||
switch action {
|
||||
case .wake: return "bolt.fill"
|
||||
case .sleep: return "moon.fill"
|
||||
case .restart: return "arrow.clockwise"
|
||||
case .shutdown: return "power"
|
||||
}
|
||||
}
|
||||
|
||||
var color: Color {
|
||||
switch action {
|
||||
case .wake: return .green
|
||||
case .sleep: return .orange
|
||||
case .restart: return .blue
|
||||
case .shutdown: return .red
|
||||
}
|
||||
}
|
||||
|
||||
var label: String {
|
||||
switch action {
|
||||
case .wake: return "Wake"
|
||||
case .sleep: return "Sleep"
|
||||
case .restart: return "Restart"
|
||||
case .shutdown: return "Shut Down"
|
||||
}
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
Link(destination: action.url(for: deviceId)) {
|
||||
VStack(spacing: 2) {
|
||||
Image(systemName: icon)
|
||||
.font(.system(size: 16, weight: .medium))
|
||||
.foregroundColor(color)
|
||||
}
|
||||
.frame(maxWidth: .infinity, minHeight: 36)
|
||||
.background(color.opacity(0.15))
|
||||
.cornerRadius(8)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Widget Definition
|
||||
|
||||
struct DeviceControlWidget: Widget {
|
||||
let kind: String = "widget"
|
||||
|
||||
var body: some WidgetConfiguration {
|
||||
AppIntentConfiguration(kind: kind, intent: ConfigurationAppIntent.self, provider: Provider()) { entry in
|
||||
widgetEntryView(entry: entry)
|
||||
DeviceWidgetEntryView(entry: entry)
|
||||
.containerBackground(.fill.tertiary, for: .widget)
|
||||
}
|
||||
.configurationDisplayName("Device Control")
|
||||
.description("Control your devices with wake, sleep, restart, and shutdown actions.")
|
||||
.supportedFamilies([.systemSmall, .systemMedium])
|
||||
}
|
||||
}
|
||||
|
||||
extension ConfigurationAppIntent {
|
||||
fileprivate static var smiley: ConfigurationAppIntent {
|
||||
let intent = ConfigurationAppIntent()
|
||||
intent.favoriteEmoji = "😀"
|
||||
return intent
|
||||
}
|
||||
|
||||
fileprivate static var starEyes: ConfigurationAppIntent {
|
||||
let intent = ConfigurationAppIntent()
|
||||
intent.favoriteEmoji = "🤩"
|
||||
return intent
|
||||
}
|
||||
}
|
||||
// Keep legacy name for backwards compatibility with existing widget installations
|
||||
typealias widget = DeviceControlWidget
|
||||
|
||||
// MARK: - Preview
|
||||
|
||||
#Preview(as: .systemSmall) {
|
||||
widget()
|
||||
DeviceControlWidget()
|
||||
} timeline: {
|
||||
SimpleEntry(date: .now, configuration: .smiley)
|
||||
SimpleEntry(date: .now, configuration: .starEyes)
|
||||
DeviceEntry(
|
||||
date: .now,
|
||||
configuration: ConfigurationAppIntent(),
|
||||
device: DeviceInfo(id: "preview", name: "Gaming PC", mac: "AA:BB:CC:DD:EE:FF", ip: "192.168.1.100", status: "online")
|
||||
)
|
||||
}
|
||||
|
||||
#Preview(as: .systemMedium) {
|
||||
DeviceControlWidget()
|
||||
} timeline: {
|
||||
DeviceEntry(
|
||||
date: .now,
|
||||
configuration: ConfigurationAppIntent(),
|
||||
device: DeviceInfo(id: "preview", name: "Home Server", mac: "11:22:33:44:55:66", ip: "192.168.1.50", status: "offline")
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user