75 lines
2.5 KiB
Swift
75 lines
2.5 KiB
Swift
import SwiftUI
|
|
import WidgetKit
|
|
|
|
/// 语记复杂功能:显示今日速记数,点一下打开手表 App 开始录音。
|
|
/// 放进 Widget Extension target;今日数读的是手表本地 UserDefaults(与 WatchSessionManager 共用 key)。
|
|
struct YuJiEntry: TimelineEntry {
|
|
let date: Date
|
|
let count: Int
|
|
}
|
|
|
|
struct YuJiProvider: TimelineProvider {
|
|
func placeholder(in context: Context) -> YuJiEntry {
|
|
YuJiEntry(date: Date(), count: 3)
|
|
}
|
|
|
|
func getSnapshot(in context: Context, completion: @escaping (YuJiEntry) -> Void) {
|
|
completion(YuJiEntry(date: Date(), count: WatchSessionManager.loadTodayCount()))
|
|
}
|
|
|
|
func getTimeline(in context: Context, completion: @escaping (Timeline<YuJiEntry>) -> Void) {
|
|
let entry = YuJiEntry(date: Date(), count: WatchSessionManager.loadTodayCount())
|
|
let nextMidnight = Calendar.current.startOfDay(for: Date().addingTimeInterval(86400))
|
|
completion(Timeline(entries: [entry], policy: .after(nextMidnight)))
|
|
}
|
|
}
|
|
|
|
struct YuJiComplicationView: View {
|
|
@Environment(\.widgetFamily) private var family
|
|
var entry: YuJiEntry
|
|
|
|
var body: some View {
|
|
switch family {
|
|
case .accessoryCircular:
|
|
ZStack {
|
|
AccessoryWidgetBackground()
|
|
VStack(spacing: 0) {
|
|
Image(systemName: "mic.fill").font(.caption2)
|
|
Text("\(entry.count)").font(.headline)
|
|
}
|
|
}
|
|
case .accessoryRectangular:
|
|
HStack {
|
|
Image(systemName: "mic.fill")
|
|
VStack(alignment: .leading) {
|
|
Text("语记").font(.caption2)
|
|
Text("今日 \(entry.count) 条").font(.caption)
|
|
}
|
|
}
|
|
default:
|
|
HStack(spacing: 2) {
|
|
Image(systemName: "mic.fill").font(.caption2)
|
|
Text("\(entry.count)").font(.caption)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
struct YuJiComplicationWidget: Widget {
|
|
var body: some WidgetConfiguration {
|
|
StaticConfiguration(kind: "YuJiComplication", provider: YuJiProvider()) { entry in
|
|
YuJiComplicationView(entry: entry)
|
|
}
|
|
.configurationDisplayName("语记")
|
|
.description("今日速记数,点一下开始录音")
|
|
.supportedFamilies([.accessoryCircular, .accessoryRectangular, .accessoryInline])
|
|
}
|
|
}
|
|
|
|
@main
|
|
struct YuJiWidgets: WidgetBundle {
|
|
var body: some Widget {
|
|
YuJiComplicationWidget()
|
|
}
|
|
}
|