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:
13
ios/AppGroupUserDefaultsManager.h
Normal file
13
ios/AppGroupUserDefaultsManager.h
Normal file
@@ -0,0 +1,13 @@
|
||||
//
|
||||
// AppGroupUserDefaultsManager.h
|
||||
// OutLive
|
||||
//
|
||||
// Created by AI Assistant on 2025/11/13.
|
||||
//
|
||||
|
||||
#import <React/RCTBridgeModule.h>
|
||||
#import <React/RCTEventEmitter.h>
|
||||
|
||||
@interface AppGroupUserDefaultsManager : RCTEventEmitter <RCTBridgeModule>
|
||||
|
||||
@end
|
||||
163
ios/AppGroupUserDefaultsManager.m
Normal file
163
ios/AppGroupUserDefaultsManager.m
Normal file
@@ -0,0 +1,163 @@
|
||||
//
|
||||
// AppGroupUserDefaultsManager.m
|
||||
// OutLive
|
||||
//
|
||||
// Created by AI Assistant on 2025/11/13.
|
||||
//
|
||||
|
||||
#import "AppGroupUserDefaultsManager.h"
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
// App Group标识符
|
||||
static NSString * const APP_GROUP_ID = @"group.com.anonymous.digitalpilates";
|
||||
|
||||
@implementation AppGroupUserDefaultsManager
|
||||
|
||||
RCT_EXPORT_MODULE();
|
||||
|
||||
- (NSArray<NSString *> *)supportedEvents {
|
||||
return @[];
|
||||
}
|
||||
|
||||
+ (BOOL)requiresMainQueueSetup {
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (NSUserDefaults *)getAppGroupUserDefaults {
|
||||
return [[NSUserDefaults alloc] initWithSuiteName:APP_GROUP_ID];
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(setString:(NSString *)groupId
|
||||
key:(NSString *)key
|
||||
value:(NSString *)value
|
||||
resolver:(RCTPromiseResolveBlock)resolve
|
||||
rejecter:(RCTPromiseRejectBlock)reject) {
|
||||
|
||||
@try {
|
||||
NSUserDefaults *defaults = [self getAppGroupUserDefaults];
|
||||
if (defaults) {
|
||||
[defaults setObject:value forKey:key];
|
||||
[defaults synchronize];
|
||||
resolve(nil);
|
||||
} else {
|
||||
reject(@"APP_GROUP_ERROR", @"Failed to access App Group UserDefaults", nil);
|
||||
}
|
||||
} @catch (NSException *exception) {
|
||||
reject(@"SET_STRING_ERROR", exception.reason, nil);
|
||||
}
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(getString:(NSString *)groupId
|
||||
key:(NSString *)key
|
||||
resolver:(RCTPromiseResolveBlock)resolve
|
||||
rejecter:(RCTPromiseRejectBlock)reject) {
|
||||
|
||||
@try {
|
||||
NSUserDefaults *defaults = [self getAppGroupUserDefaults];
|
||||
if (defaults) {
|
||||
NSString *value = [defaults stringForKey:key];
|
||||
resolve(value ?: [NSNull null]);
|
||||
} else {
|
||||
reject(@"APP_GROUP_ERROR", @"Failed to access App Group UserDefaults", nil);
|
||||
}
|
||||
} @catch (NSException *exception) {
|
||||
reject(@"GET_STRING_ERROR", exception.reason, nil);
|
||||
}
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(setNumber:(NSString *)groupId
|
||||
key:(NSString *)key
|
||||
value:(nonnull NSNumber *)value
|
||||
resolver:(RCTPromiseResolveBlock)resolve
|
||||
rejecter:(RCTPromiseRejectBlock)reject) {
|
||||
|
||||
@try {
|
||||
NSUserDefaults *defaults = [self getAppGroupUserDefaults];
|
||||
if (defaults) {
|
||||
[defaults setObject:value forKey:key];
|
||||
[defaults synchronize];
|
||||
resolve(nil);
|
||||
} else {
|
||||
reject(@"APP_GROUP_ERROR", @"Failed to access App Group UserDefaults", nil);
|
||||
}
|
||||
} @catch (NSException *exception) {
|
||||
reject(@"SET_NUMBER_ERROR", exception.reason, nil);
|
||||
}
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(getNumber:(NSString *)groupId
|
||||
key:(NSString *)key
|
||||
resolver:(RCTPromiseResolveBlock)resolve
|
||||
rejecter:(RCTPromiseRejectBlock)reject) {
|
||||
|
||||
@try {
|
||||
NSUserDefaults *defaults = [self getAppGroupUserDefaults];
|
||||
if (defaults) {
|
||||
NSNumber *value = [defaults objectForKey:key];
|
||||
resolve(value ?: @0);
|
||||
} else {
|
||||
reject(@"APP_GROUP_ERROR", @"Failed to access App Group UserDefaults", nil);
|
||||
}
|
||||
} @catch (NSException *exception) {
|
||||
reject(@"GET_NUMBER_ERROR", exception.reason, nil);
|
||||
}
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(removeKey:(NSString *)groupId
|
||||
key:(NSString *)key
|
||||
resolver:(RCTPromiseResolveBlock)resolve
|
||||
rejecter:(RCTPromiseRejectBlock)reject) {
|
||||
|
||||
@try {
|
||||
NSUserDefaults *defaults = [self getAppGroupUserDefaults];
|
||||
if (defaults) {
|
||||
[defaults removeObjectForKey:key];
|
||||
[defaults synchronize];
|
||||
resolve(nil);
|
||||
} else {
|
||||
reject(@"APP_GROUP_ERROR", @"Failed to access App Group UserDefaults", nil);
|
||||
}
|
||||
} @catch (NSException *exception) {
|
||||
reject(@"REMOVE_KEY_ERROR", exception.reason, nil);
|
||||
}
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(setArray:(NSString *)groupId
|
||||
key:(NSString *)key
|
||||
value:(NSArray *)value
|
||||
resolver:(RCTPromiseResolveBlock)resolve
|
||||
rejecter:(RCTPromiseRejectBlock)reject) {
|
||||
|
||||
@try {
|
||||
NSUserDefaults *defaults = [self getAppGroupUserDefaults];
|
||||
if (defaults) {
|
||||
[defaults setObject:value forKey:key];
|
||||
[defaults synchronize];
|
||||
resolve(nil);
|
||||
} else {
|
||||
reject(@"APP_GROUP_ERROR", @"Failed to access App Group UserDefaults", nil);
|
||||
}
|
||||
} @catch (NSException *exception) {
|
||||
reject(@"SET_ARRAY_ERROR", exception.reason, nil);
|
||||
}
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(getArray:(NSString *)groupId
|
||||
key:(NSString *)key
|
||||
resolver:(RCTPromiseResolveBlock)resolve
|
||||
rejecter:(RCTPromiseRejectBlock)reject) {
|
||||
|
||||
@try {
|
||||
NSUserDefaults *defaults = [self getAppGroupUserDefaults];
|
||||
if (defaults) {
|
||||
NSArray *value = [defaults arrayForKey:key];
|
||||
resolve(value ?: [NSNull null]);
|
||||
} else {
|
||||
reject(@"APP_GROUP_ERROR", @"Failed to access App Group UserDefaults", nil);
|
||||
}
|
||||
} @catch (NSException *exception) {
|
||||
reject(@"GET_ARRAY_ERROR", exception.reason, nil);
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -3,7 +3,7 @@
|
||||
archiveVersion = 1;
|
||||
classes = {
|
||||
};
|
||||
objectVersion = 54;
|
||||
objectVersion = 70;
|
||||
objects = {
|
||||
|
||||
/* Begin PBXBuildFile section */
|
||||
@@ -16,6 +16,12 @@
|
||||
79B2CB702E7B954600B51753 /* OutLive-Bridging-Header.h in Sources */ = {isa = PBXBuildFile; fileRef = F11748442D0722820044C1D9 /* OutLive-Bridging-Header.h */; };
|
||||
79B2CB732E7B954F00B51753 /* HealthKitManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 79B2CB712E7B954F00B51753 /* HealthKitManager.m */; };
|
||||
79B2CB742E7B954F00B51753 /* HealthKitManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79B2CB722E7B954F00B51753 /* HealthKitManager.swift */; };
|
||||
79E80BA42EC5D92A004425BE /* WidgetKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 79E80BA32EC5D92A004425BE /* WidgetKit.framework */; };
|
||||
79E80BA62EC5D92A004425BE /* SwiftUI.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 79E80BA52EC5D92A004425BE /* SwiftUI.framework */; };
|
||||
79E80BB72EC5D92B004425BE /* medicineExtension.appex in Embed Foundation Extensions */ = {isa = PBXBuildFile; fileRef = 79E80BA22EC5D92A004425BE /* medicineExtension.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; };
|
||||
79E80BFF2EC5E127004425BE /* AppGroupUserDefaultsManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 79E80BFC2EC5E127004425BE /* AppGroupUserDefaultsManager.m */; };
|
||||
79E80C002EC5E127004425BE /* WidgetManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 79E80BFE2EC5E127004425BE /* WidgetManager.m */; };
|
||||
79E80C522EC5E500004425BE /* WidgetCenterHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79E80C512EC5E500004425BE /* WidgetCenterHelper.swift */; };
|
||||
91B7BA17B50D328546B5B4B8 /* PrivacyInfo.xcprivacy in Resources */ = {isa = PBXBuildFile; fileRef = B7F23062EE59F61E6260DBA8 /* PrivacyInfo.xcprivacy */; };
|
||||
AE00ECEC9D078460F642F131 /* libPods-OutLive.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 6F6136AA7113B3D210693D88 /* libPods-OutLive.a */; };
|
||||
B6B9273B2FD4F4A800C6391C /* BackgroundTaskBridge.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6B9273A2FD4F4A800C6391C /* BackgroundTaskBridge.swift */; };
|
||||
@@ -24,6 +30,30 @@
|
||||
F11748422D0307B40044C1D9 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = F11748412D0307B40044C1D9 /* AppDelegate.swift */; };
|
||||
/* End PBXBuildFile section */
|
||||
|
||||
/* Begin PBXContainerItemProxy section */
|
||||
79E80BB52EC5D92B004425BE /* PBXContainerItemProxy */ = {
|
||||
isa = PBXContainerItemProxy;
|
||||
containerPortal = 83CBB9F71A601CBA00E9B192 /* Project object */;
|
||||
proxyType = 1;
|
||||
remoteGlobalIDString = 79E80BA12EC5D92A004425BE;
|
||||
remoteInfo = medicineExtension;
|
||||
};
|
||||
/* End PBXContainerItemProxy section */
|
||||
|
||||
/* Begin PBXCopyFilesBuildPhase section */
|
||||
79E80BB82EC5D92B004425BE /* Embed Foundation Extensions */ = {
|
||||
isa = PBXCopyFilesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
dstPath = "";
|
||||
dstSubfolderSpec = 13;
|
||||
files = (
|
||||
79E80BB72EC5D92B004425BE /* medicineExtension.appex in Embed Foundation Extensions */,
|
||||
);
|
||||
name = "Embed Foundation Extensions";
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
/* End PBXCopyFilesBuildPhase section */
|
||||
|
||||
/* Begin PBXFileReference section */
|
||||
08BACF4D920A957DC2FE4350 /* Pods-OutLive.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-OutLive.debug.xcconfig"; path = "Target Support Files/Pods-OutLive/Pods-OutLive.debug.xcconfig"; sourceTree = "<group>"; };
|
||||
13B07F961A680F5B00A75B9A /* OutLive.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = OutLive.app; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
@@ -36,6 +66,15 @@
|
||||
792C52612EB05B8F002F3F09 /* NativeToastManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = NativeToastManager.swift; path = OutLive/NativeToastManager.swift; sourceTree = "<group>"; };
|
||||
79B2CB712E7B954F00B51753 /* HealthKitManager.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = HealthKitManager.m; path = OutLive/HealthKitManager.m; sourceTree = "<group>"; };
|
||||
79B2CB722E7B954F00B51753 /* HealthKitManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = HealthKitManager.swift; path = OutLive/HealthKitManager.swift; sourceTree = "<group>"; };
|
||||
79E80BA22EC5D92A004425BE /* medicineExtension.appex */ = {isa = PBXFileReference; explicitFileType = "wrapper.app-extension"; includeInIndex = 0; path = medicineExtension.appex; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
79E80BA32EC5D92A004425BE /* WidgetKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = WidgetKit.framework; path = System/Library/Frameworks/WidgetKit.framework; sourceTree = SDKROOT; };
|
||||
79E80BA52EC5D92A004425BE /* SwiftUI.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SwiftUI.framework; path = System/Library/Frameworks/SwiftUI.framework; sourceTree = SDKROOT; };
|
||||
79E80BBE2EC5DC50004425BE /* medicineExtensionRelease.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = medicineExtensionRelease.entitlements; sourceTree = "<group>"; };
|
||||
79E80BFB2EC5E127004425BE /* AppGroupUserDefaultsManager.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppGroupUserDefaultsManager.h; sourceTree = "<group>"; };
|
||||
79E80BFC2EC5E127004425BE /* AppGroupUserDefaultsManager.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppGroupUserDefaultsManager.m; sourceTree = "<group>"; };
|
||||
79E80BFD2EC5E127004425BE /* WidgetManager.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WidgetManager.h; sourceTree = "<group>"; };
|
||||
79E80BFE2EC5E127004425BE /* WidgetManager.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = WidgetManager.m; sourceTree = "<group>"; };
|
||||
79E80C512EC5E500004425BE /* WidgetCenterHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = WidgetCenterHelper.swift; path = OutLive/WidgetCenterHelper.swift; sourceTree = "<group>"; };
|
||||
9B6A6CEBED2FC0931F7B7236 /* Pods-OutLive.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-OutLive.release.xcconfig"; path = "Target Support Files/Pods-OutLive/Pods-OutLive.release.xcconfig"; sourceTree = "<group>"; };
|
||||
AA286B85B6C04FC6940260E9 /* SplashScreen.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; name = SplashScreen.storyboard; path = OutLive/SplashScreen.storyboard; sourceTree = "<group>"; };
|
||||
B6B9273A2FD4F4A800C6391C /* BackgroundTaskBridge.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = BackgroundTaskBridge.swift; path = OutLive/BackgroundTaskBridge.swift; sourceTree = "<group>"; };
|
||||
@@ -47,6 +86,20 @@
|
||||
F11748442D0722820044C1D9 /* OutLive-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "OutLive-Bridging-Header.h"; path = "OutLive/OutLive-Bridging-Header.h"; sourceTree = "<group>"; };
|
||||
/* End PBXFileReference section */
|
||||
|
||||
/* Begin PBXFileSystemSynchronizedBuildFileExceptionSet section */
|
||||
79E80BBB2EC5D92B004425BE /* PBXFileSystemSynchronizedBuildFileExceptionSet */ = {
|
||||
isa = PBXFileSystemSynchronizedBuildFileExceptionSet;
|
||||
membershipExceptions = (
|
||||
Info.plist,
|
||||
);
|
||||
target = 79E80BA12EC5D92A004425BE /* medicineExtension */;
|
||||
};
|
||||
/* End PBXFileSystemSynchronizedBuildFileExceptionSet section */
|
||||
|
||||
/* Begin PBXFileSystemSynchronizedRootGroup section */
|
||||
79E80BA72EC5D92A004425BE /* medicine */ = {isa = PBXFileSystemSynchronizedRootGroup; exceptions = (79E80BBB2EC5D92B004425BE /* PBXFileSystemSynchronizedBuildFileExceptionSet */, ); explicitFileTypes = {}; explicitFolders = (); path = medicine; sourceTree = "<group>"; };
|
||||
/* End PBXFileSystemSynchronizedRootGroup section */
|
||||
|
||||
/* Begin PBXFrameworksBuildPhase section */
|
||||
13B07F8C1A680F5B00A75B9A /* Frameworks */ = {
|
||||
isa = PBXFrameworksBuildPhase;
|
||||
@@ -57,6 +110,15 @@
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
79E80B9F2EC5D92A004425BE /* Frameworks */ = {
|
||||
isa = PBXFrameworksBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
79E80BA62EC5D92A004425BE /* SwiftUI.framework in Frameworks */,
|
||||
79E80BA42EC5D92A004425BE /* WidgetKit.framework in Frameworks */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
/* End PBXFrameworksBuildPhase section */
|
||||
|
||||
/* Begin PBXGroup section */
|
||||
@@ -80,6 +142,8 @@
|
||||
792C52582EA880A7002F3F09 /* StoreKit.framework */,
|
||||
ED297162215061F000B7C4FE /* JavaScriptCore.framework */,
|
||||
6F6136AA7113B3D210693D88 /* libPods-OutLive.a */,
|
||||
79E80BA32EC5D92A004425BE /* WidgetKit.framework */,
|
||||
79E80BA52EC5D92A004425BE /* SwiftUI.framework */,
|
||||
);
|
||||
name = Frameworks;
|
||||
sourceTree = "<group>";
|
||||
@@ -102,6 +166,12 @@
|
||||
83CBB9F61A601CBA00E9B192 = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
79E80BFB2EC5E127004425BE /* AppGroupUserDefaultsManager.h */,
|
||||
79E80BFC2EC5E127004425BE /* AppGroupUserDefaultsManager.m */,
|
||||
79E80BFD2EC5E127004425BE /* WidgetManager.h */,
|
||||
79E80BFE2EC5E127004425BE /* WidgetManager.m */,
|
||||
79E80C512EC5E500004425BE /* WidgetCenterHelper.swift */,
|
||||
79E80BBE2EC5DC50004425BE /* medicineExtensionRelease.entitlements */,
|
||||
792C52602EB05B8F002F3F09 /* NativeToastManager.m */,
|
||||
792C52612EB05B8F002F3F09 /* NativeToastManager.swift */,
|
||||
79B2CB712E7B954F00B51753 /* HealthKitManager.m */,
|
||||
@@ -110,6 +180,7 @@
|
||||
B6B9273A2FD4F4A800C6391C /* BackgroundTaskBridge.swift */,
|
||||
13B07FAE1A68108700A75B9A /* OutLive */,
|
||||
832341AE1AAA6A7D00B99B32 /* Libraries */,
|
||||
79E80BA72EC5D92A004425BE /* medicine */,
|
||||
83CBBA001A601CBA00E9B192 /* Products */,
|
||||
2D16E6871FA4F8E400B85C8A /* Frameworks */,
|
||||
80E2A1E8ECA8777F7264D855 /* ExpoModulesProviders */,
|
||||
@@ -124,6 +195,7 @@
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
13B07F961A680F5B00A75B9A /* OutLive.app */,
|
||||
79E80BA22EC5D92A004425BE /* medicineExtension.appex */,
|
||||
);
|
||||
name = Products;
|
||||
sourceTree = "<group>";
|
||||
@@ -169,27 +241,53 @@
|
||||
00DD1BFF1BD5951E006B06BC /* Bundle React Native code and images */,
|
||||
8F598744CAFFA386101BCC07 /* [CP] Embed Pods Frameworks */,
|
||||
54EB3ED8CF242B308A7FE01E /* [CP] Copy Pods Resources */,
|
||||
79E80BB82EC5D92B004425BE /* Embed Foundation Extensions */,
|
||||
);
|
||||
buildRules = (
|
||||
);
|
||||
dependencies = (
|
||||
79E80BB62EC5D92B004425BE /* PBXTargetDependency */,
|
||||
);
|
||||
name = OutLive;
|
||||
productName = OutLive;
|
||||
productReference = 13B07F961A680F5B00A75B9A /* OutLive.app */;
|
||||
productType = "com.apple.product-type.application";
|
||||
};
|
||||
79E80BA12EC5D92A004425BE /* medicineExtension */ = {
|
||||
isa = PBXNativeTarget;
|
||||
buildConfigurationList = 79E80BBC2EC5D92B004425BE /* Build configuration list for PBXNativeTarget "medicineExtension" */;
|
||||
buildPhases = (
|
||||
79E80B9E2EC5D92A004425BE /* Sources */,
|
||||
79E80B9F2EC5D92A004425BE /* Frameworks */,
|
||||
79E80BA02EC5D92A004425BE /* Resources */,
|
||||
);
|
||||
buildRules = (
|
||||
);
|
||||
dependencies = (
|
||||
);
|
||||
fileSystemSynchronizedGroups = (
|
||||
79E80BA72EC5D92A004425BE /* medicine */,
|
||||
);
|
||||
name = medicineExtension;
|
||||
productName = medicineExtension;
|
||||
productReference = 79E80BA22EC5D92A004425BE /* medicineExtension.appex */;
|
||||
productType = "com.apple.product-type.app-extension";
|
||||
};
|
||||
/* End PBXNativeTarget section */
|
||||
|
||||
/* Begin PBXProject section */
|
||||
83CBB9F71A601CBA00E9B192 /* Project object */ = {
|
||||
isa = PBXProject;
|
||||
attributes = {
|
||||
LastSwiftUpdateCheck = 2610;
|
||||
LastUpgradeCheck = 1130;
|
||||
TargetAttributes = {
|
||||
13B07F861A680F5B00A75B9A = {
|
||||
LastSwiftMigration = 1250;
|
||||
};
|
||||
79E80BA12EC5D92A004425BE = {
|
||||
CreatedOnToolsVersion = 26.1;
|
||||
};
|
||||
};
|
||||
};
|
||||
buildConfigurationList = 83CBB9FA1A601CBA00E9B192 /* Build configuration list for PBXProject "OutLive" */;
|
||||
@@ -206,6 +304,7 @@
|
||||
projectRoot = "";
|
||||
targets = (
|
||||
13B07F861A680F5B00A75B9A /* OutLive */,
|
||||
79E80BA12EC5D92A004425BE /* medicineExtension */,
|
||||
);
|
||||
};
|
||||
/* End PBXProject section */
|
||||
@@ -222,6 +321,13 @@
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
79E80BA02EC5D92A004425BE /* Resources */ = {
|
||||
isa = PBXResourcesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
/* End PBXResourcesBuildPhase section */
|
||||
|
||||
/* Begin PBXShellScriptBuildPhase section */
|
||||
@@ -374,6 +480,9 @@
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
792C52622EB05B8F002F3F09 /* NativeToastManager.m in Sources */,
|
||||
79E80BFF2EC5E127004425BE /* AppGroupUserDefaultsManager.m in Sources */,
|
||||
79E80C002EC5E127004425BE /* WidgetManager.m in Sources */,
|
||||
79E80C522EC5E500004425BE /* WidgetCenterHelper.swift in Sources */,
|
||||
792C52632EB05B8F002F3F09 /* NativeToastManager.swift in Sources */,
|
||||
79B2CB732E7B954F00B51753 /* HealthKitManager.m in Sources */,
|
||||
79B2CB742E7B954F00B51753 /* HealthKitManager.swift in Sources */,
|
||||
@@ -385,8 +494,23 @@
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
79E80B9E2EC5D92A004425BE /* Sources */ = {
|
||||
isa = PBXSourcesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
/* End PBXSourcesBuildPhase section */
|
||||
|
||||
/* Begin PBXTargetDependency section */
|
||||
79E80BB62EC5D92B004425BE /* PBXTargetDependency */ = {
|
||||
isa = PBXTargetDependency;
|
||||
target = 79E80BA12EC5D92A004425BE /* medicineExtension */;
|
||||
targetProxy = 79E80BB52EC5D92B004425BE /* PBXContainerItemProxy */;
|
||||
};
|
||||
/* End PBXTargetDependency section */
|
||||
|
||||
/* Begin XCBuildConfiguration section */
|
||||
13B07F941A680F5B00A75B9A /* Debug */ = {
|
||||
isa = XCBuildConfiguration;
|
||||
@@ -456,6 +580,102 @@
|
||||
};
|
||||
name = Release;
|
||||
};
|
||||
79E80BB92EC5D92B004425BE /* Debug */ = {
|
||||
isa = XCBuildConfiguration;
|
||||
buildSettings = {
|
||||
ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES;
|
||||
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
|
||||
ASSETCATALOG_COMPILER_WIDGET_BACKGROUND_COLOR_NAME = WidgetBackground;
|
||||
CLANG_ANALYZER_NONNULL = YES;
|
||||
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
|
||||
CLANG_CXX_LANGUAGE_STANDARD = "gnu++20";
|
||||
CLANG_ENABLE_OBJC_WEAK = YES;
|
||||
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
|
||||
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
|
||||
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
|
||||
CODE_SIGN_STYLE = Automatic;
|
||||
CURRENT_PROJECT_VERSION = 1;
|
||||
DEBUG_INFORMATION_FORMAT = dwarf;
|
||||
DEVELOPMENT_TEAM = 756WVXJ6MT;
|
||||
ENABLE_USER_SCRIPT_SANDBOXING = YES;
|
||||
GCC_C_LANGUAGE_STANDARD = gnu17;
|
||||
GENERATE_INFOPLIST_FILE = YES;
|
||||
INFOPLIST_FILE = medicine/Info.plist;
|
||||
INFOPLIST_KEY_CFBundleDisplayName = medicine;
|
||||
INFOPLIST_KEY_NSHumanReadableCopyright = "";
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 26.1;
|
||||
LD_RUNPATH_SEARCH_PATHS = (
|
||||
"$(inherited)",
|
||||
"@executable_path/Frameworks",
|
||||
"@executable_path/../../Frameworks",
|
||||
);
|
||||
LOCALIZATION_PREFERS_STRING_CATALOGS = YES;
|
||||
MARKETING_VERSION = 1.0;
|
||||
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
|
||||
MTL_FAST_MATH = YES;
|
||||
OTHER_SWIFT_FLAGS = "$(inherited) -D EXPO_CONFIGURATION_DEBUG";
|
||||
PRODUCT_BUNDLE_IDENTIFIER = com.anonymous.digitalpilates.medicine;
|
||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||
SKIP_INSTALL = YES;
|
||||
STRING_CATALOG_GENERATE_SYMBOLS = YES;
|
||||
SWIFT_ACTIVE_COMPILATION_CONDITIONS = "DEBUG $(inherited)";
|
||||
SWIFT_APPROACHABLE_CONCURRENCY = YES;
|
||||
SWIFT_EMIT_LOC_STRINGS = YES;
|
||||
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
|
||||
SWIFT_UPCOMING_FEATURE_MEMBER_IMPORT_VISIBILITY = YES;
|
||||
SWIFT_VERSION = 5.0;
|
||||
TARGETED_DEVICE_FAMILY = "1,2";
|
||||
};
|
||||
name = Debug;
|
||||
};
|
||||
79E80BBA2EC5D92B004425BE /* Release */ = {
|
||||
isa = XCBuildConfiguration;
|
||||
buildSettings = {
|
||||
ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES;
|
||||
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
|
||||
ASSETCATALOG_COMPILER_WIDGET_BACKGROUND_COLOR_NAME = WidgetBackground;
|
||||
CLANG_ANALYZER_NONNULL = YES;
|
||||
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
|
||||
CLANG_CXX_LANGUAGE_STANDARD = "gnu++20";
|
||||
CLANG_ENABLE_OBJC_WEAK = YES;
|
||||
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
|
||||
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
|
||||
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
|
||||
CODE_SIGN_ENTITLEMENTS = medicineExtensionRelease.entitlements;
|
||||
CODE_SIGN_STYLE = Automatic;
|
||||
COPY_PHASE_STRIP = NO;
|
||||
CURRENT_PROJECT_VERSION = 1;
|
||||
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
|
||||
DEVELOPMENT_TEAM = 756WVXJ6MT;
|
||||
ENABLE_USER_SCRIPT_SANDBOXING = YES;
|
||||
GCC_C_LANGUAGE_STANDARD = gnu17;
|
||||
GENERATE_INFOPLIST_FILE = YES;
|
||||
INFOPLIST_FILE = medicine/Info.plist;
|
||||
INFOPLIST_KEY_CFBundleDisplayName = medicine;
|
||||
INFOPLIST_KEY_NSHumanReadableCopyright = "";
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 26.1;
|
||||
LD_RUNPATH_SEARCH_PATHS = (
|
||||
"$(inherited)",
|
||||
"@executable_path/Frameworks",
|
||||
"@executable_path/../../Frameworks",
|
||||
);
|
||||
LOCALIZATION_PREFERS_STRING_CATALOGS = YES;
|
||||
MARKETING_VERSION = 1.0;
|
||||
MTL_FAST_MATH = YES;
|
||||
OTHER_SWIFT_FLAGS = "$(inherited) -D EXPO_CONFIGURATION_RELEASE";
|
||||
PRODUCT_BUNDLE_IDENTIFIER = com.anonymous.digitalpilates.medicine;
|
||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||
SKIP_INSTALL = YES;
|
||||
STRING_CATALOG_GENERATE_SYMBOLS = YES;
|
||||
SWIFT_APPROACHABLE_CONCURRENCY = YES;
|
||||
SWIFT_COMPILATION_MODE = wholemodule;
|
||||
SWIFT_EMIT_LOC_STRINGS = YES;
|
||||
SWIFT_UPCOMING_FEATURE_MEMBER_IMPORT_VISIBILITY = YES;
|
||||
SWIFT_VERSION = 5.0;
|
||||
TARGETED_DEVICE_FAMILY = "1,2";
|
||||
};
|
||||
name = Release;
|
||||
};
|
||||
83CBBA201A601CBA00E9B192 /* Debug */ = {
|
||||
isa = XCBuildConfiguration;
|
||||
buildSettings = {
|
||||
@@ -586,6 +806,15 @@
|
||||
defaultConfigurationIsVisible = 0;
|
||||
defaultConfigurationName = Release;
|
||||
};
|
||||
79E80BBC2EC5D92B004425BE /* Build configuration list for PBXNativeTarget "medicineExtension" */ = {
|
||||
isa = XCConfigurationList;
|
||||
buildConfigurations = (
|
||||
79E80BB92EC5D92B004425BE /* Debug */,
|
||||
79E80BBA2EC5D92B004425BE /* Release */,
|
||||
);
|
||||
defaultConfigurationIsVisible = 0;
|
||||
defaultConfigurationName = Release;
|
||||
};
|
||||
83CBB9FA1A601CBA00E9B192 /* Build configuration list for PBXProject "OutLive" */ = {
|
||||
isa = XCConfigurationList;
|
||||
buildConfigurations = (
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>1.0.25</string>
|
||||
<string>1.0.24</string>
|
||||
<key>CFBundleSignature</key>
|
||||
<string>????</string>
|
||||
<key>CFBundleURLTypes</key>
|
||||
|
||||
@@ -3,3 +3,5 @@
|
||||
//
|
||||
#import <React/RCTBridgeModule.h>
|
||||
#import <React/RCTViewManager.h>
|
||||
#import "../AppGroupUserDefaultsManager.h"
|
||||
#import "../WidgetManager.h"
|
||||
|
||||
@@ -16,5 +16,9 @@
|
||||
</array>
|
||||
<key>com.apple.developer.healthkit.background-delivery</key>
|
||||
<true/>
|
||||
<key>com.apple.security.application-groups</key>
|
||||
<array>
|
||||
<string>group.com.anonymous.digitalpilates</string>
|
||||
</array>
|
||||
</dict>
|
||||
</plist>
|
||||
|
||||
17
ios/OutLive/WidgetCenterHelper.swift
Normal file
17
ios/OutLive/WidgetCenterHelper.swift
Normal file
@@ -0,0 +1,17 @@
|
||||
import Foundation
|
||||
import WidgetKit
|
||||
|
||||
@objc class WidgetCenterHelper: NSObject {
|
||||
@objc static func reloadAllTimelinesIfAvailable() {
|
||||
if #available(iOS 14.0, *) {
|
||||
WidgetCenter.shared.reloadAllTimelines()
|
||||
}
|
||||
}
|
||||
|
||||
@objc(reloadTimelinesIfAvailableOfKind:)
|
||||
static func reloadTimelinesIfAvailable(ofKind kind: String) {
|
||||
if #available(iOS 14.0, *) {
|
||||
WidgetCenter.shared.reloadTimelines(ofKind: kind)
|
||||
}
|
||||
}
|
||||
}
|
||||
12
ios/WidgetManager.h
Normal file
12
ios/WidgetManager.h
Normal file
@@ -0,0 +1,12 @@
|
||||
//
|
||||
// WidgetManager.h
|
||||
// OutLive
|
||||
//
|
||||
// Created by AI Assistant on 2025/11/13.
|
||||
//
|
||||
|
||||
#import <React/RCTBridgeModule.h>
|
||||
|
||||
@interface WidgetManager : NSObject <RCTBridgeModule>
|
||||
|
||||
@end
|
||||
55
ios/WidgetManager.m
Normal file
55
ios/WidgetManager.m
Normal file
@@ -0,0 +1,55 @@
|
||||
//
|
||||
// WidgetManager.m
|
||||
// OutLive
|
||||
//
|
||||
// Created by AI Assistant on 2025/11/13.
|
||||
//
|
||||
|
||||
#import "WidgetManager.h"
|
||||
#import "OutLive-Swift.h"
|
||||
|
||||
@implementation WidgetManager
|
||||
|
||||
RCT_EXPORT_MODULE();
|
||||
|
||||
+ (BOOL)requiresMainQueueSetup {
|
||||
return NO;
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(reloadTimelines:(RCTPromiseResolveBlock)resolve
|
||||
rejecter:(RCTPromiseRejectBlock)reject) {
|
||||
|
||||
@try {
|
||||
if (@available(iOS 14.0, *)) {
|
||||
// 刷新所有小组件
|
||||
[WidgetCenterHelper reloadAllTimelinesIfAvailable];
|
||||
|
||||
// 也可以只刷新特定的medicine小组件
|
||||
[WidgetCenterHelper reloadTimelinesIfAvailableOfKind:@"medicine"];
|
||||
|
||||
resolve(@"Widget timelines reloaded successfully");
|
||||
} else {
|
||||
reject(@"IOS_VERSION_ERROR", @"WidgetKit is not available on this iOS version", nil);
|
||||
}
|
||||
} @catch (NSException *exception) {
|
||||
reject(@"RELOAD_TIMELINES_ERROR", exception.reason, nil);
|
||||
}
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(reloadTimeline:(NSString *)widgetKind
|
||||
resolver:(RCTPromiseResolveBlock)resolve
|
||||
rejecter:(RCTPromiseRejectBlock)reject) {
|
||||
|
||||
@try {
|
||||
if (@available(iOS 14.0, *)) {
|
||||
[WidgetCenterHelper reloadTimelinesIfAvailableOfKind:widgetKind];
|
||||
resolve([NSString stringWithFormat:@"Widget timeline reloaded for kind: %@", widgetKind]);
|
||||
} else {
|
||||
reject(@"IOS_VERSION_ERROR", @"WidgetKit is not available on this iOS version", nil);
|
||||
}
|
||||
} @catch (NSException *exception) {
|
||||
reject(@"RELOAD_TIMELINE_ERROR", exception.reason, nil);
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
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
|
||||
}
|
||||
10
ios/medicineExtensionRelease.entitlements
Normal file
10
ios/medicineExtensionRelease.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>
|
||||
Reference in New Issue
Block a user