114 lines
4.3 KiB
Swift
114 lines
4.3 KiB
Swift
import WidgetKit
|
||
import SwiftUI
|
||
|
||
// V2:纯色背景 + 随机文案小组件(Small/Medium/Large + 点击跳转 Home)
|
||
|
||
struct EmotionProvider: TimelineProvider {
|
||
private let quotes = [
|
||
"你已经很努力了,今天也值得被温柔对待。",
|
||
"轻轻呼吸,感受当下的每一刻。",
|
||
"所有的压力,都会在深呼吸中慢慢消散。",
|
||
"给生活一点留白,给自己一点温柔。",
|
||
"不要走得太快,等一等落下的灵魂。",
|
||
"世界虽嘈杂,但你可以拥有一颗宁静的心。",
|
||
"每一个瞬间,都是生命最好的安排。",
|
||
"抱抱自己,辛苦了,亲爱的。",
|
||
"慢一点也没关系,只要你在前行。",
|
||
"今天,你对自己微笑了吗?",
|
||
"愿你历经山河,仍觉得人间值得。",
|
||
"心简单,世界就简单;心平顺,生活就平顺。",
|
||
"即使生活偶尔晦暗,你也要成为自己的光。",
|
||
"别让琐事挤走生活的快乐,别让压力消磨奋斗的激情。"
|
||
]
|
||
|
||
func placeholder(in context: Context) -> EmotionEntry {
|
||
EmotionEntry(date: Date(), text: quotes[0])
|
||
}
|
||
|
||
func getSnapshot(in context: Context, completion: @escaping (EmotionEntry) -> ()) {
|
||
let entry = EmotionEntry(date: Date(), text: quotes.randomElement() ?? quotes[0])
|
||
completion(entry)
|
||
}
|
||
|
||
func getTimeline(in context: Context, completion: @escaping (Timeline<EmotionEntry>) -> ()) {
|
||
var entries: [EmotionEntry] = []
|
||
let currentDate = Date()
|
||
|
||
// 生成未来 24 小时的 6 个条目,每 4 小时更换一次随机文案
|
||
for hourOffset in 0..<6 {
|
||
let entryDate = Calendar.current.date(byAdding: .hour, value: hourOffset * 4, to: currentDate)!
|
||
let entry = EmotionEntry(date: entryDate, text: quotes.randomElement() ?? quotes[0])
|
||
entries.append(entry)
|
||
}
|
||
|
||
let timeline = Timeline(entries: entries, policy: .atEnd)
|
||
completion(timeline)
|
||
}
|
||
}
|
||
|
||
struct EmotionEntry: TimelineEntry {
|
||
let date: Date
|
||
let text: String
|
||
}
|
||
|
||
struct EmotionWidgetView: View {
|
||
var entry: EmotionProvider.Entry
|
||
@Environment(\.widgetFamily) var family
|
||
|
||
private let title = "正念"
|
||
private let deepLink = URL(string: "client:///(app)/home")
|
||
|
||
// 背景色 #F7D9BF
|
||
private let backgroundColor = Color(red: 247/255, green: 217/255, blue: 191/255)
|
||
// 文本颜色(深咖色,适合搭配浅橘色背景)
|
||
private let textColor = Color(red: 74/255, green: 52/255, blue: 40/255)
|
||
|
||
var body: some View {
|
||
VStack(alignment: .center, spacing: 0) {
|
||
Spacer(minLength: 0)
|
||
|
||
Text(entry.text)
|
||
.font(.system(size: family == .systemSmall ? 17 : 20, weight: .medium))
|
||
.foregroundColor(textColor)
|
||
.lineSpacing(6)
|
||
.multilineTextAlignment(.center)
|
||
.minimumScaleFactor(0.7)
|
||
.fixedSize(horizontal: false, vertical: true)
|
||
|
||
Spacer(minLength: 0)
|
||
|
||
if family != .systemSmall {
|
||
Text("Hey Mama")
|
||
.font(.system(size: 10, weight: .semibold))
|
||
.foregroundColor(textColor.opacity(0.3))
|
||
.padding(.bottom, 4)
|
||
}
|
||
}
|
||
.padding(family == .systemSmall ? 16 : 24)
|
||
.frame(maxWidth: .infinity, maxHeight: .infinity) // 强制撑开容器
|
||
.background(backgroundColor) // 将背景色直接应用到容器上
|
||
.widgetURL(deepLink)
|
||
}
|
||
}
|
||
|
||
@main
|
||
struct EmotionWidget: Widget {
|
||
let kind: String = "EmotionWidget"
|
||
|
||
var body: some WidgetConfiguration {
|
||
StaticConfiguration(kind: kind, provider: EmotionProvider()) { entry in
|
||
if #available(iOS 17.0, *) {
|
||
EmotionWidgetView(entry: entry)
|
||
.containerBackground(Color(red: 247/255, green: 217/255, blue: 191/255), for: .widget)
|
||
} else {
|
||
EmotionWidgetView(entry: entry)
|
||
.background(Color(red: 247/255, green: 217/255, blue: 191/255))
|
||
}
|
||
}
|
||
.configurationDisplayName("情绪小组件")
|
||
.description("一段温柔提醒,陪你回到当下。")
|
||
.supportedFamilies([.systemSmall, .systemMedium, .systemLarge])
|
||
}
|
||
}
|
||
|