Files
mindfulness/client/ios/情绪小组件/EmotionWidget.swift

114 lines
4.3 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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])
}
}