Port navigate and weather

This commit is contained in:
James Babcock committed 2026-09-18 23:46:27 -07:00
1 parent 2b3e068a23
commit 9e18745e53
33 files changed
+630 -60

No files matched your search

+1
View File
@@ -3,4 +3,5 @@
@interface FaceclawGraphics : NSObject
+ (UIImage *)preview:(NSData *)gray width:(NSInteger)width height:(NSInteger)height green:(BOOL)green;
+ (NSData *)renderSVG:(NSString *)svg size:(NSInteger)size;
+ (NSData *)decodeImage:(NSData *)data width:(NSInteger)width height:(NSInteger)height;
@end
+27
View File
@@ -1,7 +1,34 @@
#import "FaceclawGraphics.h"
#import <SVGKit/SVGKit.h>
#import <ImageIO/ImageIO.h>
@implementation FaceclawGraphics
+ (NSData *)decodeImage:(NSData *)data width:(NSInteger)width height:(NSInteger)height {
if (!data.length || width <= 0 || height <= 0 || width > 2048 || height > 2048) return nil;
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
if (!source) return nil;
NSDictionary *options = @{(id)kCGImageSourceCreateThumbnailFromImageAlways: @YES,
(id)kCGImageSourceCreateThumbnailWithTransform: @YES,
(id)kCGImageSourceThumbnailMaxPixelSize: @(MAX(width, height))};
CGImageRef image = CGImageSourceCreateThumbnailAtIndex(source, 0, (__bridge CFDictionaryRef)options);
CFRelease(source);
if (!image) return nil;
NSMutableData *rgba = [NSMutableData dataWithLength:width * height * 4];
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(rgba.mutableBytes, width, height, 8, width * 4, space,
kCGBitmapByteOrder32Big | (CGBitmapInfo)kCGImageAlphaPremultipliedLast);
CGColorSpaceRelease(space);
if (!context) { CGImageRelease(image); return nil; }
// ImageIO applies image orientation; the bitmap's row order is top-to-bottom.
CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);
NSMutableData *gray = [NSMutableData dataWithLength:width * height];
const uint8_t *src = rgba.bytes;
uint8_t *dst = gray.mutableBytes;
for (NSInteger i = 0; i < width * height; i++)
dst[i] = (uint8_t)round(0.2126 * src[i * 4] + 0.7152 * src[i * 4 + 1] + 0.0722 * src[i * 4 + 2]);
CGContextRelease(context); CGImageRelease(image);
return gray;
}
+ (UIImage *)preview:(NSData *)gray width:(NSInteger)width height:(NSInteger)height green:(BOOL)green {
if (width <= 0 || height <= 0 || gray.length != width * height) return nil;
NSMutableData *rgba = [NSMutableData dataWithLength:width * height * 4];
+9
View File
@@ -5,5 +5,14 @@
+ (instancetype)shared;
- (BOOL)hasPermission;
- (void)requestPermission:(void (^)(BOOL granted))completion;
- (void)requestPrecisePermission:(void (^)(BOOL granted))completion;
- (void)requestDeclination:(void (^)(double degrees, double latitude, double longitude, NSString *error))completion;
@end
/** Each lookup/stream owns a separate manager, so stopping one never stops another. Main thread only. */
@interface FaceclawLocationUpdates : NSObject
@property(nonatomic, copy) void (^eventHandler)(NSString *json);
- (void)startOnce;
- (void)startTracking:(NSInteger)intervalMs;
- (void)stop;
@end
+96
View File
@@ -47,6 +47,17 @@
for (void (^callback)(BOOL) in callbacks) callback([self hasPermission]);
if (![self hasPermission]) [self finishWithError:@"Location permission is required for true north"];
}
- (void)requestPrecisePermission:(void (^)(BOOL))completion {
[self requestPermission:^(BOOL granted) {
if (!granted || self.manager.accuracyAuthorization == CLAccuracyAuthorizationFullAccuracy) {
completion(granted);
return;
}
[self.manager requestTemporaryFullAccuracyAuthorizationWithPurposeKey:@"Navigation" completion:^(NSError *error) {
completion([self hasPermission] && self.manager.accuracyAuthorization == CLAccuracyAuthorizationFullAccuracy);
}];
}];
}
- (void)requestDeclination:(void (^)(double, double, double, NSString *))completion {
if (![self hasPermission] || ![CLLocationManager headingAvailable]) {
completion(0, 0, 0, @"Location or heading unavailable for true north");
@@ -99,3 +110,88 @@
callback(degrees, coordinate.latitude, coordinate.longitude, error);
}
@end
@interface FaceclawLocationUpdates () <CLLocationManagerDelegate>
@property(nonatomic, strong) CLLocationManager *manager;
@property(nonatomic, strong) NSTimer *timeout;
@property(nonatomic) BOOL running;
@property(nonatomic) BOOL once;
@property(nonatomic) NSTimeInterval interval;
@property(nonatomic) NSTimeInterval lastTimestamp;
@end
@implementation FaceclawLocationUpdates
- (void)startOnce { [self start:YES interval:0]; }
- (void)startTracking:(NSInteger)intervalMs { [self start:NO interval:MAX(500, intervalMs) / 1000.0]; }
- (void)start:(BOOL)once interval:(NSTimeInterval)interval {
if (self.running) return;
self.once = once; self.interval = interval; self.lastTimestamp = 0;
self.manager = [CLLocationManager new];
self.manager.delegate = self;
CLAuthorizationStatus status = self.manager.authorizationStatus;
if (status != kCLAuthorizationStatusAuthorizedAlways && status != kCLAuthorizationStatusAuthorizedWhenInUse) {
[self fail:@"Location permission is required. Allow location in the phone's Settings, then retry."]; return;
}
if (!once && self.manager.accuracyAuthorization != CLAccuracyAuthorizationFullAccuracy) {
[self fail:@"Precise location is required for navigation. Enable Precise Location in the phone's Settings."]; return;
}
self.running = YES;
self.manager.desiredAccuracy = once ? kCLLocationAccuracyKilometer : kCLLocationAccuracyBestForNavigation;
self.manager.distanceFilter = kCLDistanceFilterNone;
if (!once) {
self.manager.activityType = CLActivityTypeOtherNavigation;
self.manager.pausesLocationUpdatesAutomatically = NO;
self.manager.allowsBackgroundLocationUpdates = YES;
self.manager.showsBackgroundLocationIndicator = YES;
}
[self.manager startUpdatingLocation];
if (once) self.timeout = [NSTimer scheduledTimerWithTimeInterval:20 repeats:NO block:^(NSTimer *timer) {
[self fail:@"Couldn't get a current location. Check Location Services and retry."];
}];
}
- (void)stop {
self.running = NO;
[self.timeout invalidate]; self.timeout = nil;
[self.manager stopUpdatingLocation];
self.manager.delegate = nil; self.manager = nil;
self.eventHandler = nil;
}
- (void)emit:(NSDictionary *)event {
NSData *data = [NSJSONSerialization dataWithJSONObject:event options:0 error:nil];
if (data && self.eventHandler) self.eventHandler([[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}
- (void)fail:(NSString *)message {
void (^handler)(NSString *) = self.eventHandler;
[self stop];
NSData *data = [NSJSONSerialization dataWithJSONObject:@{@"error": message} options:0 error:nil];
if (handler) handler([[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}
- (void)locationManagerDidChangeAuthorization:(CLLocationManager *)manager {
if (!self.running) return;
CLAuthorizationStatus status = manager.authorizationStatus;
if (status == kCLAuthorizationStatusDenied || status == kCLAuthorizationStatusRestricted ||
(!self.once && manager.accuracyAuthorization != CLAccuracyAuthorizationFullAccuracy))
[self fail:@"Location permission changed. Allow precise location in the phone's Settings, then retry."];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
if (!self.running) return;
CLLocation *fix = locations.lastObject;
if (!fix || fix.horizontalAccuracy < 0 || fabs(fix.timestamp.timeIntervalSinceNow) > 60) return;
NSTimeInterval timestamp = fix.timestamp.timeIntervalSince1970;
if (self.lastTimestamp && timestamp - self.lastTimestamp < self.interval) return;
self.lastTimestamp = timestamp;
NSDictionary *event = @{@"latitude": @(fix.coordinate.latitude), @"longitude": @(fix.coordinate.longitude),
@"accuracyMeters": @(fix.horizontalAccuracy), @"timestampMs": @(timestamp * 1000),
@"bearingDeg": fix.course >= 0 ? @(fix.course) : NSNull.null,
@"speedMps": fix.speed >= 0 ? @(fix.speed) : NSNull.null};
if (self.once) {
void (^handler)(NSString *) = self.eventHandler;
[self stop];
NSData *data = [NSJSONSerialization dataWithJSONObject:event options:0 error:nil];
if (handler) handler([[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
} else [self emit:event];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
if (self.running && error.code != kCLErrorLocationUnknown) [self fail:error.localizedDescription];
}
@end