更新 shenyan-app: HealthKit+WeatherKit+冰箱贴+pare+三区布局
- HealthKitBridge.m: 自定义原生模块,心率/步数/睡眠采集 - WeatherKitBridge.swift/m: 天气模块,当前+每日+每小时预报 - pare.py: 身体数据陡度监控,阈值告警→inbox+冰箱贴 - cyberboss: 5-20分钟随机唤醒,pare→decide→push - 冰箱贴: 纸质感卡片,黑字多级透明度,独立输入框 - 首页三区无视觉布局: 左门/中记录/右冰箱 - 端口3003→3004(VS Code占用) - 聊天屏锚定底部滚动 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
88
shenyan-app/ios/ShenYanApp/HealthKitBridge.m
Normal file
88
shenyan-app/ios/ShenYanApp/HealthKitBridge.m
Normal file
@@ -0,0 +1,88 @@
|
||||
#import <React/RCTBridgeModule.h>
|
||||
#import <HealthKit/HealthKit.h>
|
||||
|
||||
@interface HealthKitBridge : NSObject <RCTBridgeModule>
|
||||
@end
|
||||
|
||||
@implementation HealthKitBridge
|
||||
{
|
||||
HKHealthStore *_store;
|
||||
}
|
||||
|
||||
RCT_EXPORT_MODULE();
|
||||
|
||||
- (instancetype)init {
|
||||
self = [super init];
|
||||
if (self) {
|
||||
_store = [[HKHealthStore alloc] init];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
+ (BOOL)requiresMainQueueSetup { return NO; }
|
||||
|
||||
RCT_EXPORT_METHOD(requestAuthorization:(RCTPromiseResolveBlock)resolve
|
||||
rejecter:(RCTPromiseRejectBlock)reject)
|
||||
{
|
||||
NSSet *readTypes = [NSSet setWithArray:@[
|
||||
[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate],
|
||||
[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount],
|
||||
[HKObjectType categoryTypeForIdentifier:HKCategoryTypeIdentifierSleepAnalysis],
|
||||
]];
|
||||
[_store requestAuthorizationToShareTypes:nil readTypes:readTypes completion:^(BOOL success, NSError *error) {
|
||||
if (success) resolve(@YES);
|
||||
else reject(@"healthkit", error.localizedDescription, error);
|
||||
}];
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(getLatestHeartRate:(RCTPromiseResolveBlock)resolve
|
||||
rejecter:(RCTPromiseRejectBlock)reject)
|
||||
{
|
||||
HKQuantityType *type = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate];
|
||||
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:HKSampleSortIdentifierEndDate ascending:NO];
|
||||
HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:type predicate:nil limit:1 sortDescriptors:@[sort]
|
||||
resultsHandler:^(HKSampleQuery *q, NSArray *results, NSError *error) {
|
||||
if (error) { reject(@"healthkit", error.localizedDescription, error); return; }
|
||||
if (results.count == 0) { resolve(@{}); return; }
|
||||
HKQuantitySample *sample = results[0];
|
||||
double bpm = [sample.quantity doubleValueForUnit:[HKUnit unitFromString:@"count/min"]];
|
||||
resolve(@{@"bpm": @(bpm), @"time": @([sample.endDate timeIntervalSince1970] * 1000)});
|
||||
}];
|
||||
[_store executeQuery:query];
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(getTodaySteps:(RCTPromiseResolveBlock)resolve
|
||||
rejecter:(RCTPromiseRejectBlock)reject)
|
||||
{
|
||||
HKQuantityType *type = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
|
||||
NSCalendar *cal = [NSCalendar currentCalendar];
|
||||
NSDate *start = [cal startOfDayForDate:[NSDate date]];
|
||||
NSDate *end = [NSDate date];
|
||||
NSPredicate *pred = [HKQuery predicateForSamplesWithStartDate:start endDate:end options:HKQueryOptionStrictStartDate];
|
||||
HKStatisticsQuery *query = [[HKStatisticsQuery alloc] initWithQuantityType:type
|
||||
quantitySamplePredicate:pred options:HKStatisticsOptionCumulativeSum
|
||||
completionHandler:^(HKStatisticsQuery *q, HKStatistics *result, NSError *error) {
|
||||
if (error) { reject(@"healthkit", error.localizedDescription, error); return; }
|
||||
double steps = [result.sumQuantity doubleValueForUnit:[HKUnit countUnit]];
|
||||
resolve(@{@"steps": @(steps)});
|
||||
}];
|
||||
[_store executeQuery:query];
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(getLastSleep:(RCTPromiseResolveBlock)resolve
|
||||
rejecter:(RCTPromiseRejectBlock)reject)
|
||||
{
|
||||
HKCategoryType *type = [HKObjectType categoryTypeForIdentifier:HKCategoryTypeIdentifierSleepAnalysis];
|
||||
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:HKSampleSortIdentifierEndDate ascending:NO];
|
||||
HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:type predicate:nil limit:1 sortDescriptors:@[sort]
|
||||
resultsHandler:^(HKSampleQuery *q, NSArray *results, NSError *error) {
|
||||
if (error) { reject(@"healthkit", error.localizedDescription, error); return; }
|
||||
if (results.count == 0) { resolve(@{}); return; }
|
||||
HKCategorySample *sample = results[0];
|
||||
double hours = [sample.endDate timeIntervalSinceDate:sample.startDate] / 3600.0;
|
||||
resolve(@{@"hours": @(hours), @"start": @([sample.startDate timeIntervalSince1970] * 1000), @"end": @([sample.endDate timeIntervalSince1970] * 1000)});
|
||||
}];
|
||||
[_store executeQuery:query];
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user