feat(ios): 添加用药计划Widget小组件支持
- 创建medicineExtension小组件,支持iOS桌面显示用药计划 - 实现App Group数据共享机制,支持主应用与小组件数据同步 - 添加AppGroupUserDefaultsManager原生模块,提供跨应用数据访问能力 - 添加WidgetManager和WidgetCenterHelper,实现小组件刷新控制 - 在medications页面和Redux store中集成小组件数据同步逻辑 - 支持实时同步今日用药状态(待服用/已服用/已错过)到小组件 - 配置App Group entitlements (group.com.anonymous.digitalpilates) - 更新Xcode项目配置,添加WidgetKit和SwiftUI框架支持
This commit is contained in:
18
ios/medicine/AppIntent.swift
Normal file
18
ios/medicine/AppIntent.swift
Normal file
@@ -0,0 +1,18 @@
|
||||
//
|
||||
// AppIntent.swift
|
||||
// medicine
|
||||
//
|
||||
// Created by richard on 2025/11/13.
|
||||
//
|
||||
|
||||
import WidgetKit
|
||||
import AppIntents
|
||||
|
||||
struct ConfigurationAppIntent: WidgetConfigurationIntent {
|
||||
static var title: LocalizedStringResource { "Configuration" }
|
||||
static var description: IntentDescription { "This is an example widget." }
|
||||
|
||||
// An example configurable parameter.
|
||||
@Parameter(title: "Favorite Emoji", default: "😃")
|
||||
var favoriteEmoji: String
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"colors" : [
|
||||
{
|
||||
"idiom" : "universal"
|
||||
}
|
||||
],
|
||||
"info" : {
|
||||
"author" : "xcode",
|
||||
"version" : 1
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
{
|
||||
"images" : [
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"platform" : "ios",
|
||||
"size" : "1024x1024"
|
||||
},
|
||||
{
|
||||
"appearances" : [
|
||||
{
|
||||
"appearance" : "luminosity",
|
||||
"value" : "dark"
|
||||
}
|
||||
],
|
||||
"idiom" : "universal",
|
||||
"platform" : "ios",
|
||||
"size" : "1024x1024"
|
||||
},
|
||||
{
|
||||
"appearances" : [
|
||||
{
|
||||
"appearance" : "luminosity",
|
||||
"value" : "tinted"
|
||||
}
|
||||
],
|
||||
"idiom" : "universal",
|
||||
"platform" : "ios",
|
||||
"size" : "1024x1024"
|
||||
}
|
||||
],
|
||||
"info" : {
|
||||
"author" : "xcode",
|
||||
"version" : 1
|
||||
}
|
||||
}
|
||||
6
ios/medicine/Assets.xcassets/Contents.json
Normal file
6
ios/medicine/Assets.xcassets/Contents.json
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"info" : {
|
||||
"author" : "xcode",
|
||||
"version" : 1
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"colors" : [
|
||||
{
|
||||
"idiom" : "universal"
|
||||
}
|
||||
],
|
||||
"info" : {
|
||||
"author" : "xcode",
|
||||
"version" : 1
|
||||
}
|
||||
}
|
||||
15
ios/medicine/Info.plist
Normal file
15
ios/medicine/Info.plist
Normal file
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>CFBundleDisplayName</key>
|
||||
<string>用药计划</string>
|
||||
<key>NSExtension</key>
|
||||
<dict>
|
||||
<key>NSExtensionPointIdentifier</key>
|
||||
<string>com.apple.widgetkit-extension</string>
|
||||
</dict>
|
||||
<key>RCTNewArchEnabled</key>
|
||||
<true/>
|
||||
</dict>
|
||||
</plist>
|
||||
10
ios/medicine/medicine.entitlements
Normal file
10
ios/medicine/medicine.entitlements
Normal file
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>com.apple.security.application-groups</key>
|
||||
<array>
|
||||
<string>group.com.anonymous.digitalpilates</string>
|
||||
</array>
|
||||
</dict>
|
||||
</plist>
|
||||
295
ios/medicine/medicine.swift
Normal file
295
ios/medicine/medicine.swift
Normal file
@@ -0,0 +1,295 @@
|
||||
//
|
||||
// medicine.swift
|
||||
// medicine
|
||||
//
|
||||
// Created by richard on 2025/11/13.
|
||||
//
|
||||
|
||||
import WidgetKit
|
||||
import SwiftUI
|
||||
|
||||
// MARK: - 用药数据模型
|
||||
struct MedicationItem: Codable, Identifiable {
|
||||
let id: String
|
||||
let name: String
|
||||
let dosage: String
|
||||
let scheduledTime: String
|
||||
let status: String
|
||||
let medicationId: String
|
||||
let recordId: String?
|
||||
let image: String?
|
||||
}
|
||||
|
||||
struct MedicationWidgetData: Codable {
|
||||
let medications: [MedicationItem]
|
||||
let lastSyncTime: String
|
||||
let date: String
|
||||
}
|
||||
|
||||
// MARK: - Timeline Provider
|
||||
struct Provider: AppIntentTimelineProvider {
|
||||
func placeholder(in context: Context) -> MedicationEntry {
|
||||
MedicationEntry(
|
||||
date: Date(),
|
||||
medications: [],
|
||||
displayDate: formatDate(Date()),
|
||||
lastSyncTime: Date().timeIntervalSince1970
|
||||
)
|
||||
}
|
||||
|
||||
func snapshot(for configuration: ConfigurationAppIntent, in context: Context) async -> MedicationEntry {
|
||||
let medicationData = loadMedicationDataFromAppGroup()
|
||||
let lastSyncTimeInterval = parseTimeInterval(from: medicationData.lastSyncTime)
|
||||
return MedicationEntry(
|
||||
date: Date(),
|
||||
medications: medicationData.medications,
|
||||
displayDate: medicationData.date,
|
||||
lastSyncTime: lastSyncTimeInterval
|
||||
)
|
||||
}
|
||||
|
||||
func timeline(for configuration: ConfigurationAppIntent, in context: Context) async -> Timeline<MedicationEntry> {
|
||||
let currentDate = Date()
|
||||
let medicationData = loadMedicationDataFromAppGroup()
|
||||
let lastSyncTimeInterval = parseTimeInterval(from: medicationData.lastSyncTime)
|
||||
let entry = MedicationEntry(
|
||||
date: currentDate,
|
||||
medications: medicationData.medications,
|
||||
displayDate: medicationData.date,
|
||||
lastSyncTime: lastSyncTimeInterval
|
||||
)
|
||||
|
||||
// 下次更新时间 - 每15分钟更新一次
|
||||
let nextUpdate = Calendar.current.date(byAdding: .minute, value: 15, to: currentDate)!
|
||||
return Timeline(entries: [entry], policy: .after(nextUpdate))
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Timeline Entry
|
||||
struct MedicationEntry: TimelineEntry {
|
||||
let date: Date
|
||||
let medications: [MedicationItem]
|
||||
let displayDate: String
|
||||
let lastSyncTime: TimeInterval
|
||||
}
|
||||
|
||||
// MARK: - Widget View
|
||||
struct medicineEntryView: View {
|
||||
var entry: Provider.Entry
|
||||
|
||||
var body: some View {
|
||||
VStack(spacing: 8) {
|
||||
// 日期标题
|
||||
Text(entry.displayDate)
|
||||
.font(.caption)
|
||||
.foregroundColor(.secondary)
|
||||
|
||||
// 用药列表
|
||||
if entry.medications.isEmpty {
|
||||
Text("今日无用药计划")
|
||||
.font(.body)
|
||||
.foregroundColor(.secondary)
|
||||
} else {
|
||||
LazyVStack(spacing: 6) {
|
||||
ForEach(entry.medications, id: \.id) { medication in
|
||||
MedicationRowView(medication: medication)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Spacer()
|
||||
|
||||
// 底部信息
|
||||
Text("最后更新: \(formatTime(from: entry.lastSyncTime))")
|
||||
.font(.caption2)
|
||||
.foregroundColor(Color.secondary)
|
||||
}
|
||||
.padding()
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - 用药行视图
|
||||
struct MedicationRowView: View {
|
||||
let medication: MedicationItem
|
||||
|
||||
var body: some View {
|
||||
HStack {
|
||||
// 状态指示器
|
||||
Circle()
|
||||
.fill(statusColor)
|
||||
.frame(width: 8, height: 8)
|
||||
|
||||
VStack(alignment: .leading, spacing: 2) {
|
||||
Text(medication.name)
|
||||
.font(.caption)
|
||||
.fontWeight(.medium)
|
||||
.lineLimit(1)
|
||||
|
||||
Text("\(medication.scheduledTime) • \(medication.dosage)")
|
||||
.font(.caption2)
|
||||
.foregroundColor(.secondary)
|
||||
}
|
||||
|
||||
Spacer()
|
||||
|
||||
// 状态标签
|
||||
Text(statusText)
|
||||
.font(.caption2)
|
||||
.padding(.horizontal, 8)
|
||||
.padding(.vertical, 2)
|
||||
.background(statusColor.opacity(0.2))
|
||||
.foregroundColor(statusColor)
|
||||
.cornerRadius(4)
|
||||
}
|
||||
.padding(.vertical, 2)
|
||||
}
|
||||
|
||||
private var statusColor: Color {
|
||||
switch medication.status {
|
||||
case "taken":
|
||||
return .green
|
||||
case "missed":
|
||||
return .red
|
||||
default:
|
||||
return .blue
|
||||
}
|
||||
}
|
||||
|
||||
private var statusText: String {
|
||||
switch medication.status {
|
||||
case "taken":
|
||||
return "已服用"
|
||||
case "missed":
|
||||
return "已错过"
|
||||
default:
|
||||
return "待服用"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Widget Configuration
|
||||
struct medicine: Widget {
|
||||
let kind: String = "medicine"
|
||||
|
||||
var body: some WidgetConfiguration {
|
||||
AppIntentConfiguration(kind: kind, intent: ConfigurationAppIntent.self, provider: Provider()) { entry in
|
||||
medicineEntryView(entry: entry)
|
||||
.containerBackground(.fill.tertiary, for: .widget)
|
||||
}
|
||||
.configurationDisplayName("用药计划")
|
||||
.description("显示今日用药计划和服药状态")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Helper Functions
|
||||
private func loadMedicationDataFromAppGroup() -> MedicationWidgetData {
|
||||
guard let userDefaults = UserDefaults(suiteName: "group.com.anonymous.digitalpilates") else {
|
||||
print("❌ Failed to initialize UserDefaults with App Group")
|
||||
return MedicationWidgetData(
|
||||
medications: [],
|
||||
lastSyncTime: Date().toISOString(),
|
||||
date: formatDate(Date())
|
||||
)
|
||||
}
|
||||
|
||||
guard let dataJson = userDefaults.string(forKey: "widget_medication_data") else {
|
||||
print("⚠️ No medication data found in App Group UserDefaults")
|
||||
return MedicationWidgetData(
|
||||
medications: [],
|
||||
lastSyncTime: Date().toISOString(),
|
||||
date: formatDate(Date())
|
||||
)
|
||||
}
|
||||
|
||||
print("✅ Found medication data JSON: \(dataJson.prefix(100))...")
|
||||
|
||||
guard let data = dataJson.data(using: .utf8) else {
|
||||
print("❌ Failed to convert JSON string to Data")
|
||||
return MedicationWidgetData(
|
||||
medications: [],
|
||||
lastSyncTime: Date().toISOString(),
|
||||
date: formatDate(Date())
|
||||
)
|
||||
}
|
||||
|
||||
do {
|
||||
let medicationData = try JSONDecoder().decode(MedicationWidgetData.self, from: data)
|
||||
print("✅ Successfully decoded medication data: \(medicationData.medications.count) medications")
|
||||
return medicationData
|
||||
} catch {
|
||||
print("❌ Failed to decode medication data: \(error)")
|
||||
return MedicationWidgetData(
|
||||
medications: [],
|
||||
lastSyncTime: Date().toISOString(),
|
||||
date: formatDate(Date())
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private func formatDate(_ date: Date) -> String {
|
||||
let formatter = DateFormatter()
|
||||
formatter.dateFormat = "M月d日"
|
||||
return formatter.string(from: date)
|
||||
}
|
||||
|
||||
private func formatTime(from timeInterval: TimeInterval) -> String {
|
||||
let date = Date(timeIntervalSince1970: timeInterval)
|
||||
let formatter = DateFormatter()
|
||||
formatter.timeStyle = .short
|
||||
formatter.locale = Locale(identifier: "zh_CN")
|
||||
return formatter.string(from: date)
|
||||
}
|
||||
|
||||
/// 解析时间间隔 - 支持ISO字符串和时间戳
|
||||
private func parseTimeInterval(from string: String) -> TimeInterval {
|
||||
// 尝试直接转换为数字(时间戳)
|
||||
if let timestamp = Double(string) {
|
||||
return timestamp
|
||||
}
|
||||
|
||||
// 尝试解析ISO 8601格式
|
||||
let isoFormatter = ISO8601DateFormatter()
|
||||
isoFormatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
|
||||
if let date = isoFormatter.date(from: string) {
|
||||
return date.timeIntervalSince1970
|
||||
}
|
||||
|
||||
// 尝试不带毫秒的ISO格式
|
||||
isoFormatter.formatOptions = [.withInternetDateTime]
|
||||
if let date = isoFormatter.date(from: string) {
|
||||
return date.timeIntervalSince1970
|
||||
}
|
||||
|
||||
// 如果都失败,返回当前时间
|
||||
return Date().timeIntervalSince1970
|
||||
}
|
||||
|
||||
/// 将Date转换为ISO字符串(辅助函数)
|
||||
extension Date {
|
||||
func toISOString() -> String {
|
||||
let formatter = ISO8601DateFormatter()
|
||||
formatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
|
||||
return formatter.string(from: self)
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
#Preview(as: .systemSmall) {
|
||||
medicine()
|
||||
} timeline: {
|
||||
MedicationEntry(date: .now, medications: [], displayDate: formatDate(Date()), lastSyncTime: Date().timeIntervalSince1970)
|
||||
MedicationEntry(date: .now, medications: [], displayDate: formatDate(Date()), lastSyncTime: Date().timeIntervalSince1970)
|
||||
}
|
||||
18
ios/medicine/medicineBundle.swift
Normal file
18
ios/medicine/medicineBundle.swift
Normal file
@@ -0,0 +1,18 @@
|
||||
//
|
||||
// medicineBundle.swift
|
||||
// medicine
|
||||
//
|
||||
// Created by richard on 2025/11/13.
|
||||
//
|
||||
|
||||
import WidgetKit
|
||||
import SwiftUI
|
||||
|
||||
@main
|
||||
struct medicineBundle: WidgetBundle {
|
||||
var body: some Widget {
|
||||
medicine()
|
||||
medicineControl()
|
||||
medicineLiveActivity()
|
||||
}
|
||||
}
|
||||
77
ios/medicine/medicineControl.swift
Normal file
77
ios/medicine/medicineControl.swift
Normal file
@@ -0,0 +1,77 @@
|
||||
//
|
||||
// medicineControl.swift
|
||||
// medicine
|
||||
//
|
||||
// Created by richard on 2025/11/13.
|
||||
//
|
||||
|
||||
import AppIntents
|
||||
import SwiftUI
|
||||
import WidgetKit
|
||||
|
||||
struct medicineControl: ControlWidget {
|
||||
static let kind: String = "com.anonymous.digitalpilates.medicine"
|
||||
|
||||
var body: some ControlWidgetConfiguration {
|
||||
AppIntentControlConfiguration(
|
||||
kind: Self.kind,
|
||||
provider: Provider()
|
||||
) { value in
|
||||
ControlWidgetToggle(
|
||||
"Start Timer",
|
||||
isOn: value.isRunning,
|
||||
action: StartTimerIntent(value.name)
|
||||
) { isRunning in
|
||||
Label(isRunning ? "On" : "Off", systemImage: "timer")
|
||||
}
|
||||
}
|
||||
.displayName("Timer")
|
||||
.description("A an example control that runs a timer.")
|
||||
}
|
||||
}
|
||||
|
||||
extension medicineControl {
|
||||
struct Value {
|
||||
var isRunning: Bool
|
||||
var name: String
|
||||
}
|
||||
|
||||
struct Provider: AppIntentControlValueProvider {
|
||||
func previewValue(configuration: TimerConfiguration) -> Value {
|
||||
medicineControl.Value(isRunning: false, name: configuration.timerName)
|
||||
}
|
||||
|
||||
func currentValue(configuration: TimerConfiguration) async throws -> Value {
|
||||
let isRunning = true // Check if the timer is running
|
||||
return medicineControl.Value(isRunning: isRunning, name: configuration.timerName)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct TimerConfiguration: ControlConfigurationIntent {
|
||||
static let title: LocalizedStringResource = "Timer Name Configuration"
|
||||
|
||||
@Parameter(title: "Timer Name", default: "Timer")
|
||||
var timerName: String
|
||||
}
|
||||
|
||||
struct StartTimerIntent: SetValueIntent {
|
||||
static let title: LocalizedStringResource = "Start a timer"
|
||||
|
||||
@Parameter(title: "Timer Name")
|
||||
var name: String
|
||||
|
||||
@Parameter(title: "Timer is running")
|
||||
var value: Bool
|
||||
|
||||
init() {}
|
||||
|
||||
init(_ name: String) {
|
||||
self.name = name
|
||||
}
|
||||
|
||||
func perform() async throws -> some IntentResult {
|
||||
// Start the timer…
|
||||
return .result()
|
||||
}
|
||||
}
|
||||
80
ios/medicine/medicineLiveActivity.swift
Normal file
80
ios/medicine/medicineLiveActivity.swift
Normal file
@@ -0,0 +1,80 @@
|
||||
//
|
||||
// medicineLiveActivity.swift
|
||||
// medicine
|
||||
//
|
||||
// Created by richard on 2025/11/13.
|
||||
//
|
||||
|
||||
import ActivityKit
|
||||
import WidgetKit
|
||||
import SwiftUI
|
||||
|
||||
struct medicineAttributes: ActivityAttributes {
|
||||
public struct ContentState: Codable, Hashable {
|
||||
// Dynamic stateful properties about your activity go here!
|
||||
var emoji: String
|
||||
}
|
||||
|
||||
// Fixed non-changing properties about your activity go here!
|
||||
var name: String
|
||||
}
|
||||
|
||||
struct medicineLiveActivity: Widget {
|
||||
var body: some WidgetConfiguration {
|
||||
ActivityConfiguration(for: medicineAttributes.self) { context in
|
||||
// Lock screen/banner UI goes here
|
||||
VStack {
|
||||
Text("Hello \(context.state.emoji)")
|
||||
}
|
||||
.activityBackgroundTint(Color.cyan)
|
||||
.activitySystemActionForegroundColor(Color.black)
|
||||
|
||||
} dynamicIsland: { context in
|
||||
DynamicIsland {
|
||||
// Expanded UI goes here. Compose the expanded UI through
|
||||
// various regions, like leading/trailing/center/bottom
|
||||
DynamicIslandExpandedRegion(.leading) {
|
||||
Text("Leading")
|
||||
}
|
||||
DynamicIslandExpandedRegion(.trailing) {
|
||||
Text("Trailing")
|
||||
}
|
||||
DynamicIslandExpandedRegion(.bottom) {
|
||||
Text("Bottom \(context.state.emoji)")
|
||||
// more content
|
||||
}
|
||||
} compactLeading: {
|
||||
Text("L")
|
||||
} compactTrailing: {
|
||||
Text("T \(context.state.emoji)")
|
||||
} minimal: {
|
||||
Text(context.state.emoji)
|
||||
}
|
||||
.widgetURL(URL(string: "http://www.apple.com"))
|
||||
.keylineTint(Color.red)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension medicineAttributes {
|
||||
fileprivate static var preview: medicineAttributes {
|
||||
medicineAttributes(name: "World")
|
||||
}
|
||||
}
|
||||
|
||||
extension medicineAttributes.ContentState {
|
||||
fileprivate static var smiley: medicineAttributes.ContentState {
|
||||
medicineAttributes.ContentState(emoji: "😀")
|
||||
}
|
||||
|
||||
fileprivate static var starEyes: medicineAttributes.ContentState {
|
||||
medicineAttributes.ContentState(emoji: "🤩")
|
||||
}
|
||||
}
|
||||
|
||||
#Preview("Notification", as: .content, using: medicineAttributes.preview) {
|
||||
medicineLiveActivity()
|
||||
} contentStates: {
|
||||
medicineAttributes.ContentState.smiley
|
||||
medicineAttributes.ContentState.starEyes
|
||||
}
|
||||
Reference in New Issue
Block a user