- 在useWaterData中统一处理数据变更后的Widget同步逻辑 - 新增数组类型数据存取方法支持更复杂数据结构 - 重构Widget UI为圆形进度条设计,提升视觉体验 - 修复数据同步时可能存在的竞态条件问题 - 优化错误处理,确保Widget同步失败不影响主功能
184 lines
7.3 KiB
Swift
184 lines
7.3 KiB
Swift
//
|
|
// WaterWidget.swift
|
|
// WaterWidget
|
|
//
|
|
// Created by richard on 2025/9/9.
|
|
//
|
|
|
|
import WidgetKit
|
|
import SwiftUI
|
|
|
|
// Data model for water intake
|
|
struct WaterData {
|
|
let currentIntake: Int
|
|
let targetIntake: Int
|
|
let quickAddAmount: Int
|
|
let progressPercentage: Double
|
|
|
|
init(currentIntake: Int = 0, targetIntake: Int = 2000, quickAddAmount: Int = 150) {
|
|
self.currentIntake = currentIntake
|
|
self.targetIntake = targetIntake
|
|
self.quickAddAmount = quickAddAmount
|
|
self.progressPercentage = min(Double(currentIntake) / Double(targetIntake), 1.0)
|
|
}
|
|
}
|
|
|
|
struct Provider: AppIntentTimelineProvider {
|
|
func placeholder(in context: Context) -> SimpleEntry {
|
|
SimpleEntry(date: Date(), configuration: ConfigurationAppIntent(), waterData: WaterData())
|
|
}
|
|
|
|
func snapshot(for configuration: ConfigurationAppIntent, in context: Context) async -> SimpleEntry {
|
|
// In a real app, you would fetch the actual water data here
|
|
let waterData = await fetchWaterData()
|
|
return SimpleEntry(date: Date(), configuration: configuration, waterData: waterData)
|
|
}
|
|
|
|
func timeline(for configuration: ConfigurationAppIntent, in context: Context) async -> Timeline<SimpleEntry> {
|
|
var entries: [SimpleEntry] = []
|
|
let currentDate = Date()
|
|
|
|
// Fetch current water data
|
|
let waterData = await fetchWaterData()
|
|
|
|
// Generate timeline entries for every hour for the next 5 hours
|
|
for hourOffset in 0 ..< 5 {
|
|
let entryDate = Calendar.current.date(byAdding: .hour, value: hourOffset, to: currentDate)!
|
|
let entry = SimpleEntry(date: entryDate, configuration: configuration, waterData: waterData)
|
|
entries.append(entry)
|
|
}
|
|
|
|
// Refresh every 15 minutes to keep data current
|
|
return Timeline(entries: entries, policy: .after(Calendar.current.date(byAdding: .minute, value: 15, to: currentDate)!))
|
|
}
|
|
|
|
// Fetch water data from shared App Group storage
|
|
private func fetchWaterData() async -> WaterData {
|
|
guard let sharedDefaults = UserDefaults(suiteName: "group.com.anonymous.digitalpilates") else {
|
|
print("Failed to access App Group UserDefaults")
|
|
return WaterData() // Return default data
|
|
}
|
|
|
|
// Read data using the same keys as defined in widgetDataSync.ts
|
|
let currentIntake = sharedDefaults.object(forKey: "widget_current_water_intake") as? Int ?? 0
|
|
let targetIntake = sharedDefaults.object(forKey: "widget_daily_water_goal") as? Int ?? 2000
|
|
let quickAddAmount = sharedDefaults.object(forKey: "widget_quick_add_amount") as? Int ?? 150
|
|
|
|
print("Widget data loaded - Current: \(currentIntake)ml, Target: \(targetIntake)ml, Quick: \(quickAddAmount)ml")
|
|
|
|
return WaterData(
|
|
currentIntake: currentIntake,
|
|
targetIntake: targetIntake,
|
|
quickAddAmount: quickAddAmount
|
|
)
|
|
}
|
|
}
|
|
|
|
struct SimpleEntry: TimelineEntry {
|
|
let date: Date
|
|
let configuration: ConfigurationAppIntent
|
|
let waterData: WaterData
|
|
}
|
|
|
|
struct WaterWidgetEntryView : View {
|
|
var entry: Provider.Entry
|
|
|
|
var body: some View {
|
|
VStack(spacing: 8) {
|
|
// Header with title and add button
|
|
HStack() {
|
|
Text("饮水")
|
|
.font(.system(size: 13, weight: .semibold))
|
|
.foregroundColor(Color(red: 0.2, green: 0.2, blue: 0.2))
|
|
|
|
|
|
// Quick add water button
|
|
Button(intent: AddWaterIntent(amount: entry.waterData.quickAddAmount)) {
|
|
Text("+\(entry.waterData.quickAddAmount)")
|
|
.font(.system(size: 10, weight: .bold))
|
|
.foregroundColor(.white)
|
|
.padding(.horizontal, 8)
|
|
.padding(.vertical, 5)
|
|
.background(
|
|
LinearGradient(
|
|
gradient: Gradient(colors: [
|
|
Color(red: 0.3, green: 0.7, blue: 1.0),
|
|
Color(red: 0.2, green: 0.6, blue: 0.9)
|
|
]),
|
|
startPoint: .topLeading,
|
|
endPoint: .bottomTrailing
|
|
)
|
|
)
|
|
.cornerRadius(10)
|
|
}
|
|
.buttonStyle(PlainButtonStyle())
|
|
}
|
|
|
|
// Main content - left right layout
|
|
HStack(alignment: .center, spacing: 12) {
|
|
// Left side - Progress circle
|
|
ZStack {
|
|
// Background circle
|
|
Circle()
|
|
.stroke(Color(red: 0.95, green: 0.95, blue: 0.95), lineWidth: 4)
|
|
.frame(width: 45, height: 45)
|
|
|
|
// Progress circle
|
|
Circle()
|
|
.trim(from: 0, to: entry.waterData.progressPercentage)
|
|
.stroke(
|
|
LinearGradient(
|
|
gradient: Gradient(colors: [
|
|
Color(red: 0.3, green: 0.8, blue: 1.0),
|
|
Color(red: 0.1, green: 0.6, blue: 0.9)
|
|
]),
|
|
startPoint: .topLeading,
|
|
endPoint: .bottomTrailing
|
|
),
|
|
style: StrokeStyle(lineWidth: 4, lineCap: .round)
|
|
)
|
|
.frame(width: 45, height: 45)
|
|
.rotationEffect(.degrees(-90))
|
|
|
|
// Progress percentage in center
|
|
Text("\(Int(entry.waterData.progressPercentage * 100))%")
|
|
.font(.system(size: 10, weight: .bold))
|
|
.foregroundColor(Color(red: 0.3, green: 0.7, blue: 1.0))
|
|
}
|
|
|
|
}
|
|
}
|
|
.padding(12)
|
|
.background(Color.white)
|
|
.cornerRadius(16)
|
|
.containerBackground(Color.clear, for: .widget)
|
|
}
|
|
}
|
|
|
|
struct WaterWidget: Widget {
|
|
let kind: String = "WaterWidget"
|
|
|
|
var body: some WidgetConfiguration {
|
|
AppIntentConfiguration(kind: kind, intent: ConfigurationAppIntent.self, provider: Provider()) { entry in
|
|
WaterWidgetEntryView(entry: entry)
|
|
}
|
|
.configurationDisplayName("饮水记录")
|
|
.description("追踪你的每日饮水量,快速添加饮水记录")
|
|
.supportedFamilies([.systemSmall])
|
|
}
|
|
}
|
|
|
|
extension ConfigurationAppIntent {
|
|
fileprivate static var defaultConfig: ConfigurationAppIntent {
|
|
return ConfigurationAppIntent()
|
|
}
|
|
}
|
|
|
|
#Preview(as: .systemSmall) {
|
|
WaterWidget()
|
|
} timeline: {
|
|
SimpleEntry(date: .now, configuration: .defaultConfig, waterData: WaterData(currentIntake: 500, targetIntake: 2000, quickAddAmount: 150))
|
|
SimpleEntry(date: .now, configuration: .defaultConfig, waterData: WaterData(currentIntake: 1200, targetIntake: 2000, quickAddAmount: 200))
|
|
SimpleEntry(date: .now, configuration: .defaultConfig, waterData: WaterData(currentIntake: 1800, targetIntake: 2000, quickAddAmount: 150))
|
|
}
|