From a0c5739c1cb5032f315837377554a49fca82648c Mon Sep 17 00:00:00 2001 From: Grzegorz Adam Hankiewicz Date: Sat, 6 Jun 2015 01:34:38 +0200 Subject: [PATCH] Replaces GPS with EHGPS in swift, finds circular dependency. Refs #37. --- Record_my_position.xcodeproj/project.pbxproj | 26 +- src/App_delegate.m | 45 +-- src/{SGPS.swift => EHGPS.swift} | 38 +- src/GPS.h | 60 ---- src/GPS.m | 348 ------------------- src/controllers/Capture_tab.m | 24 +- src/db/DB.m | 19 +- src/db/DB_log.h | 8 +- src/db/DB_log.m | 4 +- src/db/Rows_to_attachment.m | 6 +- 10 files changed, 68 insertions(+), 510 deletions(-) rename src/{SGPS.swift => EHGPS.swift} (88%) delete mode 100644 src/GPS.h delete mode 100644 src/GPS.m diff --git a/Record_my_position.xcodeproj/project.pbxproj b/Record_my_position.xcodeproj/project.pbxproj index e510466..e02705b 100755 --- a/Record_my_position.xcodeproj/project.pbxproj +++ b/Record_my_position.xcodeproj/project.pbxproj @@ -105,7 +105,6 @@ D59AAEF9134BC5BC00E81139 /* EGODatabaseRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = D5BEDA7A126F8C6A0056DB78 /* EGODatabaseRequest.m */; }; D59AAEFA134BC5BC00E81139 /* EGODatabaseResult.m in Sources */ = {isa = PBXBuildFile; fileRef = D5BEDA7C126F8C6A0056DB78 /* EGODatabaseResult.m */; }; D59AAEFB134BC5BC00E81139 /* EGODatabaseRow.m in Sources */ = {isa = PBXBuildFile; fileRef = D5BEDA7E126F8C6A0056DB78 /* EGODatabaseRow.m */; }; - D59AAEFD134BC5BC00E81139 /* GPS.m in Sources */ = {isa = PBXBuildFile; fileRef = D5C47781127177D500CB008F /* GPS.m */; }; D59AAEFE134BC5BC00E81139 /* hardware.m in Sources */ = {isa = PBXBuildFile; fileRef = D51EB90C127A297C0016E191 /* hardware.m */; }; D59AAEFF134BC5BC00E81139 /* DB.m in Sources */ = {isa = PBXBuildFile; fileRef = D544A086127F5FAA00835938 /* DB.m */; }; D59AAF00134BC5BC00E81139 /* DB_log.m in Sources */ = {isa = PBXBuildFile; fileRef = D544A088127F5FAA00835938 /* DB_log.m */; }; @@ -144,7 +143,6 @@ D5B08A63133D30E700A60B34 /* EGODatabaseRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = D5BEDA7A126F8C6A0056DB78 /* EGODatabaseRequest.m */; }; D5B08A64133D30E700A60B34 /* EGODatabaseResult.m in Sources */ = {isa = PBXBuildFile; fileRef = D5BEDA7C126F8C6A0056DB78 /* EGODatabaseResult.m */; }; D5B08A65133D30E700A60B34 /* EGODatabaseRow.m in Sources */ = {isa = PBXBuildFile; fileRef = D5BEDA7E126F8C6A0056DB78 /* EGODatabaseRow.m */; }; - D5B08A67133D30E700A60B34 /* GPS.m in Sources */ = {isa = PBXBuildFile; fileRef = D5C47781127177D500CB008F /* GPS.m */; }; D5B08A68133D30E700A60B34 /* hardware.m in Sources */ = {isa = PBXBuildFile; fileRef = D51EB90C127A297C0016E191 /* hardware.m */; }; D5B08A69133D30E700A60B34 /* DB.m in Sources */ = {isa = PBXBuildFile; fileRef = D544A086127F5FAA00835938 /* DB.m */; }; D5B08A6A133D30E700A60B34 /* DB_log.m in Sources */ = {isa = PBXBuildFile; fileRef = D544A088127F5FAA00835938 /* DB_log.m */; }; @@ -169,14 +167,13 @@ D5BEDA98126F8C6A0056DB78 /* EGODatabaseResult.m in Sources */ = {isa = PBXBuildFile; fileRef = D5BEDA7C126F8C6A0056DB78 /* EGODatabaseResult.m */; }; D5BEDA99126F8C6A0056DB78 /* EGODatabaseRow.m in Sources */ = {isa = PBXBuildFile; fileRef = D5BEDA7E126F8C6A0056DB78 /* EGODatabaseRow.m */; }; D5BEDAA2126F8D130056DB78 /* libsqlite3.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = D5BEDAA1126F8D130056DB78 /* libsqlite3.dylib */; }; - D5C47782127177D500CB008F /* GPS.m in Sources */ = {isa = PBXBuildFile; fileRef = D5C47781127177D500CB008F /* GPS.m */; }; D5C477B212717BEA00CB008F /* CoreLocation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D5C477B112717BEA00CB008F /* CoreLocation.framework */; }; D5C4AFCD12897956005BE697 /* SystemConfiguration.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D5C4AFCC12897956005BE697 /* SystemConfiguration.framework */; }; D5C86D1F1299CF8D00253C7F /* share_icon.png in Resources */ = {isa = PBXBuildFile; fileRef = D5C86D1D1299CF8D00253C7F /* share_icon.png */; }; D5C86D201299CF8D00253C7F /* share_icon@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = D5C86D1E1299CF8D00253C7F /* share_icon@2x.png */; }; - D5D216571B1A312800AB04AB /* SGPS.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5D216561B1A312800AB04AB /* SGPS.swift */; }; - D5D216581B1A312800AB04AB /* SGPS.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5D216561B1A312800AB04AB /* SGPS.swift */; }; - D5D216591B1A312800AB04AB /* SGPS.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5D216561B1A312800AB04AB /* SGPS.swift */; }; + D5D216571B1A312800AB04AB /* EHGPS.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5D216561B1A312800AB04AB /* EHGPS.swift */; }; + D5D216581B1A312800AB04AB /* EHGPS.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5D216561B1A312800AB04AB /* EHGPS.swift */; }; + D5D216591B1A312800AB04AB /* EHGPS.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5D216561B1A312800AB04AB /* EHGPS.swift */; }; D5D7CDD4128A9D1300B83104 /* Capture_tab.m in Sources */ = {isa = PBXBuildFile; fileRef = D5D7CDCD128A9D1300B83104 /* Capture_tab.m */; }; D5D7CDD5128A9D1300B83104 /* Log_tab.m in Sources */ = {isa = PBXBuildFile; fileRef = D5D7CDCF128A9D1300B83104 /* Log_tab.m */; }; D5D7CDD6128A9D1300B83104 /* Share_tab.m in Sources */ = {isa = PBXBuildFile; fileRef = D5D7CDD1128A9D1300B83104 /* Share_tab.m */; }; @@ -269,8 +266,6 @@ D5BEDA7D126F8C6A0056DB78 /* EGODatabaseRow.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EGODatabaseRow.h; sourceTree = ""; }; D5BEDA7E126F8C6A0056DB78 /* EGODatabaseRow.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = EGODatabaseRow.m; sourceTree = ""; }; D5BEDAA1126F8D130056DB78 /* libsqlite3.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libsqlite3.dylib; path = usr/lib/libsqlite3.dylib; sourceTree = SDKROOT; }; - D5C47780127177D500CB008F /* GPS.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GPS.h; sourceTree = ""; }; - D5C47781127177D500CB008F /* GPS.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPS.m; sourceTree = ""; }; D5C477B112717BEA00CB008F /* CoreLocation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreLocation.framework; path = System/Library/Frameworks/CoreLocation.framework; sourceTree = SDKROOT; }; D5C4AFCC12897956005BE697 /* SystemConfiguration.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SystemConfiguration.framework; path = System/Library/Frameworks/SystemConfiguration.framework; sourceTree = SDKROOT; }; D5C86D1D1299CF8D00253C7F /* share_icon.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = share_icon.png; sourceTree = ""; }; @@ -278,7 +273,7 @@ D5D216531B1A312800AB04AB /* Record_my_position-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Record_my_position-Bridging-Header.h"; sourceTree = ""; }; D5D216541B1A312800AB04AB /* Er recorder adhoc-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Er recorder adhoc-Bridging-Header.h"; sourceTree = ""; }; D5D216551B1A312800AB04AB /* Record_my_position_appstore-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Record_my_position_appstore-Bridging-Header.h"; sourceTree = ""; }; - D5D216561B1A312800AB04AB /* SGPS.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SGPS.swift; sourceTree = ""; }; + D5D216561B1A312800AB04AB /* EHGPS.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EHGPS.swift; sourceTree = ""; }; D5D7CDCC128A9D1300B83104 /* Capture_tab.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Capture_tab.h; sourceTree = ""; }; D5D7CDCD128A9D1300B83104 /* Capture_tab.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Capture_tab.m; sourceTree = ""; }; D5D7CDCE128A9D1300B83104 /* Log_tab.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Log_tab.h; sourceTree = ""; }; @@ -397,11 +392,9 @@ D544A083127F5FAA00835938 /* db */, D59D034F126E372400F4D543 /* App_delegate.h */, D59D0350126E372400F4D543 /* App_delegate.m */, - D5C47780127177D500CB008F /* GPS.h */, - D5C47781127177D500CB008F /* GPS.m */, D5BEDA3D126F8C100056DB78 /* macro.h */, 29B97316FDCFA39411CA2CEA /* main.m */, - D5D216561B1A312800AB04AB /* SGPS.swift */, + D5D216561B1A312800AB04AB /* EHGPS.swift */, D5D216531B1A312800AB04AB /* Record_my_position-Bridging-Header.h */, D5D216541B1A312800AB04AB /* Er recorder adhoc-Bridging-Header.h */, D5D216551B1A312800AB04AB /* Record_my_position_appstore-Bridging-Header.h */, @@ -738,7 +731,6 @@ D5BEDA97126F8C6A0056DB78 /* EGODatabaseRequest.m in Sources */, D5BEDA98126F8C6A0056DB78 /* EGODatabaseResult.m in Sources */, D5BEDA99126F8C6A0056DB78 /* EGODatabaseRow.m in Sources */, - D5C47782127177D500CB008F /* GPS.m in Sources */, D51EB90E127A297C0016E191 /* hardware.m in Sources */, D544A08C127F5FAA00835938 /* DB.m in Sources */, D544A08D127F5FAA00835938 /* DB_log.m in Sources */, @@ -752,7 +744,7 @@ D52550B013C9C0950050DD86 /* Base64+ELHASO.m in Sources */, D52550B113C9C0950050DD86 /* CLLocation+ELHASO.m in Sources */, D52550B213C9C0950050DD86 /* ELHASO.m in Sources */, - D5D216571B1A312800AB04AB /* SGPS.swift in Sources */, + D5D216571B1A312800AB04AB /* EHGPS.swift in Sources */, D52550B313C9C0950050DD86 /* NSArray+ELHASO.m in Sources */, D52550B413C9C0950050DD86 /* NSDictionary+ELHASO.m in Sources */, D52550B513C9C0950050DD86 /* NSString+ELHASO.m in Sources */, @@ -776,7 +768,6 @@ D59AAEF9134BC5BC00E81139 /* EGODatabaseRequest.m in Sources */, D59AAEFA134BC5BC00E81139 /* EGODatabaseResult.m in Sources */, D59AAEFB134BC5BC00E81139 /* EGODatabaseRow.m in Sources */, - D59AAEFD134BC5BC00E81139 /* GPS.m in Sources */, D59AAEFE134BC5BC00E81139 /* hardware.m in Sources */, D59AAEFF134BC5BC00E81139 /* DB.m in Sources */, D59AAF00134BC5BC00E81139 /* DB_log.m in Sources */, @@ -788,7 +779,7 @@ D59AAF07134BC5BC00E81139 /* Note_taking_controller.m in Sources */, D59AAF08134BC5BC00E81139 /* View_controller.m in Sources */, D52550C413C9C0950050DD86 /* Base64+ELHASO.m in Sources */, - D5D216591B1A312800AB04AB /* SGPS.swift in Sources */, + D5D216591B1A312800AB04AB /* EHGPS.swift in Sources */, D52550C513C9C0950050DD86 /* CLLocation+ELHASO.m in Sources */, D52550C613C9C0950050DD86 /* ELHASO.m in Sources */, D52550C713C9C0950050DD86 /* NSArray+ELHASO.m in Sources */, @@ -814,7 +805,6 @@ D5B08A63133D30E700A60B34 /* EGODatabaseRequest.m in Sources */, D5B08A64133D30E700A60B34 /* EGODatabaseResult.m in Sources */, D5B08A65133D30E700A60B34 /* EGODatabaseRow.m in Sources */, - D5B08A67133D30E700A60B34 /* GPS.m in Sources */, D5B08A68133D30E700A60B34 /* hardware.m in Sources */, D5B08A69133D30E700A60B34 /* DB.m in Sources */, D5B08A6A133D30E700A60B34 /* DB_log.m in Sources */, @@ -826,7 +816,7 @@ D5B08A71133D30E700A60B34 /* Note_taking_controller.m in Sources */, D5B08A72133D30E700A60B34 /* View_controller.m in Sources */, D52550BA13C9C0950050DD86 /* Base64+ELHASO.m in Sources */, - D5D216581B1A312800AB04AB /* SGPS.swift in Sources */, + D5D216581B1A312800AB04AB /* EHGPS.swift in Sources */, D52550BB13C9C0950050DD86 /* CLLocation+ELHASO.m in Sources */, D52550BC13C9C0950050DD86 /* ELHASO.m in Sources */, D52550BD13C9C0950050DD86 /* NSArray+ELHASO.m in Sources */, diff --git a/src/App_delegate.m b/src/App_delegate.m index 18f9194..cb4ddcf 100644 --- a/src/App_delegate.m +++ b/src/App_delegate.m @@ -1,6 +1,5 @@ #import "App_delegate.h" -#import "GPS.h" #import "controllers/Tab_controller.h" #import "db/DB.h" #import "macro.h" @@ -75,15 +74,6 @@ - (BOOL)application:(UIApplication *)application return NO; } - SGPS* test = [SGPS get]; - [test start]; - test.gpsIsOn = YES; - test.saveAllPositions = YES; -#ifdef DEBUG - [self test_swift_migration]; -#endif - - // For the moment we don't know what to do with this... if (launch_options) [db_ log:[NSString stringWithFormat:@"Launch options? %@", @@ -102,7 +92,7 @@ - (BOOL)application:(UIApplication *)application */ - (void)applicationWillResignActive:(UIApplication *)application { - [[GPS get] set_accuracy:MEDIUM_ACCURACY reason:@"Lost focus."]; + [[EHGPS get] setAccuracy:AccuracyMedium reason:@"Lost focus."]; [db_ flush]; } @@ -111,7 +101,7 @@ - (void)applicationWillResignActive:(UIApplication *)application */ - (void)applicationWillEnterForeground:(UIApplication *)application { - [[GPS get] set_accuracy:HIGH_ACCURACY reason:@"Gained focus."]; + [[EHGPS get] setAccuracy:AccuracyHigh reason:@"Gained focus."]; } /** The user quit the app, and we are supporting background operation. @@ -124,7 +114,7 @@ - (void)applicationWillEnterForeground:(UIApplication *)application - (void)applicationDidEnterBackground:(UIApplication *)application { db_.in_background = YES; - [[GPS get] set_accuracy:LOW_ACCURACY reason:@"Entering background mode."]; + [[EHGPS get] setAccuracy:AccuracyLow reason:@"Entering background mode."]; [db_ flush]; } @@ -134,7 +124,7 @@ - (void)applicationDidEnterBackground:(UIApplication *)application - (void)applicationDidBecomeActive:(UIApplication *)application { db_.in_background = NO; - [[GPS get] set_accuracy:HIGH_ACCURACY reason:@"Raising from background."]; + [[EHGPS get] setAccuracy:AccuracyHigh reason:@"Raising from background."]; } /** Application shutdown. Save cache and stuff... @@ -146,7 +136,7 @@ - (void)applicationDidBecomeActive:(UIApplication *)application */ - (void)applicationWillTerminate:(UIApplication *)application { - if ([GPS get].gps_is_on) + if ([EHGPS get].gpsIsOn) [db_ log:@"Terminating app while GPS was on..."]; [db_ flush]; @@ -202,8 +192,8 @@ - (void)purge_database DLOG(@"Purging database."); [db_ flush]; [db_ close]; - GPS *gps = [GPS get]; - const BOOL activate = gps.gps_is_on; + EHGPS *gps = [EHGPS get]; + const BOOL activate = gps.gpsIsOn; [gps stop]; [DB purge]; @@ -213,27 +203,6 @@ - (void)purge_database [gps start]; } -/** Runs some asserts on values returned by the swift implementation. - * These values are checked against the objc version for sanity. - */ -- (void)test_swift_migration -{ - CLLocationDegrees d = 33.333; - NSString *objc, *swift; - -#define TEST(X) do { \ - d = X; \ - objc = [GPS degrees_to_dms:d latitude:NO]; \ - swift = [SGPS degreesToDms:d latitude:NO]; \ - DLOG(@"Testing %@ vs %@", objc, swift); \ - LASSERT([objc isEqualToString:swift], @"Bad check"); \ -} while(0) - TEST(33.33); - TEST(133.33); - TEST(-1133.33); - TEST(0.02); -} - #pragma mark UIAlertViewDelegate protocol - (void)alertView:(UIAlertView *)alertView diff --git a/src/SGPS.swift b/src/EHGPS.swift similarity index 88% rename from src/SGPS.swift rename to src/EHGPS.swift index e4adb99..f31eeef 100644 --- a/src/SGPS.swift +++ b/src/EHGPS.swift @@ -1,37 +1,39 @@ import Foundation import CoreLocation +import UIKit -enum Accuracy +@objc internal enum Accuracy: Int { case High, Medium, Low } -@objc class SGPS : NSObject, CLLocationManagerDelegate +@objc class EHGPS : NSObject, CLLocationManagerDelegate { private let _GPS_IS_ON_KEY = "gps_is_on" - static internal let KEY_PATH = "lastPos" private let _KEY_SAVE_SINGLE_POSITION = "save_single_positions" private let _WATCHDOG_SECONDS = 60 * 60.0 + static internal let KEY_PATH = "lastPos" + static internal var mDB: DB? - static private let cInstance: SGPS = SGPS() + static private let cInstance: EHGPS = EHGPS() private var mSaveAllPositions = false private var mGpsIsOn = false private var mManager: CLLocationManager - private var mAccuracy: Accuracy private var mNoLog = false private var mZasca: NSTimer? + internal var mAccuracy: Accuracy internal var lastPos: CLLocation? - static func get() -> SGPS + static func get() -> EHGPS { return cInstance } override init() { - println("Initializing SGPS") + println("Initializing EHGPS") mManager = CLLocationManager() //assert(nil !== mManager) TODO: Why does the check fail? mAccuracy = .High @@ -112,8 +114,9 @@ enum Accuracy func start() -> Bool { if CLLocationManager.locationServicesEnabled() { + assert(nil != EHGPS.mDB) if (!gpsIsOn && !mNoLog) { - DB.get().log("Starting to update location") + EHGPS.mDB!.log("Starting to update location") } pingWatchdog() @@ -131,7 +134,8 @@ enum Accuracy */ func stop() { if gpsIsOn && !mNoLog { - DB.get().log("Stopping location updates"); + assert(nil != EHGPS.mDB) + EHGPS.mDB!.log("Stopping location updates"); } stopWatchdog() gpsIsOn = false @@ -142,14 +146,14 @@ enum Accuracy * Observers will monitor the key_path value. */ func addWatcher(watcher: NSObject) { - addObserver(watcher, forKeyPath:SGPS.KEY_PATH, + addObserver(watcher, forKeyPath:EHGPS.KEY_PATH, options: .New, context: nil) } /** Removes an observer for changes to last_pos. */ func removeWatcher(watcher: NSObject) { - removeObserver(watcher, forKeyPath: SGPS.KEY_PATH) + removeObserver(watcher, forKeyPath: EHGPS.KEY_PATH) } /** Changes the desired accuracy of the GPS readings. @@ -180,10 +184,11 @@ enum Accuracy } if gpsIsOn { + assert(nil != EHGPS.mDB) if let reason = reason { - DB.get().log(String(format: "%@ Reason: %@", message, reason)) + EHGPS.mDB!.log(String(format: "%@ Reason: %@", message, reason)) } else { - DB.get().log(message) + EHGPS.mDB!.log(message) } mNoLog = true @@ -203,7 +208,8 @@ enum Accuracy return } - DB.get().log("location error: " + error.localizedDescription) + assert(nil != EHGPS.mDB) + EHGPS.mDB!.log("location error: " + error.localizedDescription) } /** Receives a location update. @@ -220,7 +226,6 @@ enum Accuracy return } - // TODO: More compact way to write this if? if let pos = lastPos where pos.timestamp.isEqualToDate(newLocation.timestamp) { @@ -274,7 +279,8 @@ enum Accuracy */ func zasca() { - DB.get().log("Watchdog timer kicking in due to inactivity.") + assert(nil != EHGPS.mDB) + EHGPS.mDB!.log("Watchdog timer kicking in due to inactivity.") mNoLog = true stop() start() diff --git a/src/GPS.h b/src/GPS.h deleted file mode 100644 index db713e8..0000000 --- a/src/GPS.h +++ /dev/null @@ -1,60 +0,0 @@ -#import - -/// Possible values for the accuracy setting of the GPS. -enum ACCURACY_ENUM -{ - HIGH_ACCURACY, ///< Best the device can provide. Default. - MEDIUM_ACCURACY, ///< About 50m. - LOW_ACCURACY, ///< 150m or more. -}; - -/// Required alias for enum. -typedef enum ACCURACY_ENUM ACCURACY; - - -/** Wraps and controlls the GPS collection of data. - * - * Holds the pointer to the real sqlite object and provides additional - * wrapper helper functions to handle the database. - */ -@interface GPS : NSObject -{ - /// Pointer to the manager activating/desactivating Core Location. - CLLocationManager *manager_; - - /// Last received position. - CLLocation *last_pos_; - - BOOL gps_is_on_; - - /// Watchdog timer to detect GPS entering into zombie state. - NSTimer *zasca_; - - /// Set this to YES if you want to avoid logging by the GPS class. - BOOL nolog_; - - /// Current accuracy setting. - ACCURACY accuracy_; - - /// Remembers if we are saving all the positions. - BOOL save_all_positions_; -} - -@property (nonatomic, retain, readonly) CLLocation *last_pos; -@property (nonatomic, readonly, assign) BOOL gps_is_on; -@property (nonatomic, readonly, assign) ACCURACY accuracy; -@property (nonatomic, assign) BOOL save_all_positions; - -+ (GPS*)get; -+ (NSString*)degrees_to_dms:(CLLocationDegrees)value latitude:(BOOL)latitude; -+ (NSString*)key_path; -- (id)init; -- (bool)start; -- (void)stop; -- (void)add_watcher:(id)watcher; -- (void)remove_watcher:(id)watcher; -- (void)set_accuracy:(ACCURACY)accuracy reason:(NSString*)reason; - -@end - -// vim:tabstop=4 shiftwidth=4 syntax=objc diff --git a/src/GPS.m b/src/GPS.m deleted file mode 100644 index 8c68a51..0000000 --- a/src/GPS.m +++ /dev/null @@ -1,348 +0,0 @@ -#import "GPS.h" - -#import "db/DB.h" -#import "macro.h" - - -#define _KEY_PATH @"last_pos" -#define _WATCHDOG_SECONDS (60 * 60) -#define _GPS_IS_ON_KEY @"gps_is_on" -#define _KEY_SAVE_SINGLE_POSITION @"save_single_positions" - - -@interface GPS () -- (void)ping_watchdog; -- (void)stop_watchdog; -- (void)zasca; -@end - -@implementation GPS - -static GPS *g_; - -@synthesize last_pos = last_pos_; -@synthesize gps_is_on = gps_is_on_; -@synthesize accuracy = accuracy_; - -#pragma mark - -#pragma mark Life - -/** Returns the pointer to the singleton GPS class. - * The class will be constructed if necessary. - */ -+ (GPS*)get -{ - if (!g_) { - g_ = [GPS new]; - - NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; - const BOOL gps_is_on = [defaults boolForKey:_GPS_IS_ON_KEY]; - if (gps_is_on) - [g_ start]; - - g_->save_all_positions_ = - ![defaults boolForKey:_KEY_SAVE_SINGLE_POSITION]; - } - - return g_; -} - -/** Initialises the GPS class. - */ -- (id)init -{ - if (!(self = [super init])) - return nil; - - manager_ = [[CLLocationManager alloc] init]; - if (!manager_) { - LOG(@"Couldn't instantiate CLLocationManager!"); - return nil; - } - - // Set no filter and try to get the best accuracy possible. - accuracy_ = HIGH_ACCURACY; - manager_.distanceFilter = kCLDistanceFilterNone; - manager_.desiredAccuracy = kCLLocationAccuracyBest; - manager_.delegate = self; - - return self; -} - -- (void)dealloc -{ - [self stop]; - [manager_ release]; - [super dealloc]; -} - -#pragma mark - -#pragma mark Properties - -/** Write property to serialize value for later usage. - * Setting the variable also saves it to the user's defaults, - * allowing the value to be restored in case of crash or device without - * background tracking support. - */ -- (void)setGps_is_on:(BOOL)value -{ - gps_is_on_ = value; - NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; - [defaults setBool:value forKey:_GPS_IS_ON_KEY]; - [defaults synchronize]; -} - -/// Sets the setting for saving all positions. -- (void)setSave_all_positions:(BOOL)value -{ - save_all_positions_ = value; - NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; - [defaults setBool:!value forKey:_KEY_SAVE_SINGLE_POSITION]; - - // Force saving preferences here, otherwise if we get killed they are lost. - [defaults synchronize]; -} - -/// Reads the setting for reading all positions. -- (BOOL)save_all_positions -{ - return save_all_positions_; -} - -#pragma mark - -#pragma mark Methods - -/** Converts a coordinate from degrees to decimal minute second format. - * Specify with the latitude bool if you are converting the latitude - * part of the coordinates, which has a different letter. - * - * \return Returns the string with the formated value as - * Ddeg Mmin Ssec X, where X is a letter. - */ -+ (NSString*)degrees_to_dms:(CLLocationDegrees)value latitude:(BOOL)latitude -{ - const int degrees = fabsl(value); - const double min_rest = (fabs(value) - degrees) * 60.0; - const int minutes = min_rest; - const double seconds = (min_rest - minutes) * 60.0; - char letter = 0; - if (latitude) { - if (value > 0) - letter = 'N'; - else if (value < 0) - letter = 'S'; - } else { - if (value > 0) - letter = 'E'; - else if (value < 0) - letter = 'W'; - } - if (letter) - return [NSString stringWithFormat:@"%ddeg %dmin %0.2fsec %c", - degrees, minutes, seconds, letter]; - else - return [NSString stringWithFormat:@"%ddeg %dmin %0.2fsec", - degrees, minutes, seconds]; -} - -/** Starts the GPS tracking. - * Returns false if the location services are not available. - */ -- (bool)start -{ - if (manager_.locationServicesEnabled) { - if (!self.gps_is_on && !nolog_) - [[DB get] log:@"Starting to update location"]; - - [self ping_watchdog]; - - [manager_ startUpdatingLocation]; - self.gps_is_on = YES; - return true; - } else { - self.gps_is_on = NO; - return false; - } -} - -/** Stops the GPS tracking. - * You can call this function anytime, doesn't really fail. - */ -- (void)stop -{ - if (self.gps_is_on && !nolog_) - [[DB get] log:@"Stopping to update location"]; - [self stop_watchdog]; - self.gps_is_on = NO; - [manager_ stopUpdatingLocation]; -} - -/** Returns the string used by add_watcher: and removeObserver:. - */ -+ (NSString*)key_path -{ - return _KEY_PATH; -} - -/** Registers an observer for changes to last_pos. - * Observers will monitor the key_path value. - */ -- (void)add_watcher:(id)watcher -{ - [self addObserver:watcher forKeyPath:_KEY_PATH - options:NSKeyValueObservingOptionNew context:nil]; -} - -/** Removes an observer for changes to last_pos. - */ -- (void)remove_watcher:(id)watcher -{ - [self removeObserver:watcher forKeyPath:_KEY_PATH]; -} - -/** Changes the desired accuracy of the GPS readings. - * If the GPS is on, it will be reset just in case. Provide a reason - * for the change or nil if you don't want to log the change. - */ -- (void)set_accuracy:(ACCURACY)accuracy reason:(NSString*)reason -{ - if (accuracy_ == accuracy) - return; - - accuracy_ = accuracy; - NSString *message = nil; -#define _MSG(X) \ - message = @"Setting accuracy to " # X "."; - - switch (accuracy) { - case HIGH_ACCURACY: - _MSG(HIGH_ACCURACY); - manager_.distanceFilter = kCLDistanceFilterNone; - manager_.desiredAccuracy = kCLLocationAccuracyBest; - break; - - case MEDIUM_ACCURACY: - _MSG(MEDIUM_ACCURACY); - manager_.distanceFilter = 50; - manager_.desiredAccuracy = kCLLocationAccuracyNearestTenMeters; - break; - - case LOW_ACCURACY: - _MSG(LOW_ACCURACY); - manager_.distanceFilter = 150; - manager_.desiredAccuracy = kCLLocationAccuracyHundredMeters; - break; - - default: - NSAssert(0, @"Unexpected accuracy value"); - return; - } -#undef _MSG - - if (self.gps_is_on) { - if (reason.length > 0) - [[DB get] log:[NSString stringWithFormat:@"%@ Reason: %@", - message, reason]]; - else - [[DB get] log:message]; - - nolog_ = YES; - [self stop]; - [self start]; - nolog_ = NO; - } -} - -#pragma mark CLLocationManagerDelegate - -/** Something bad happened retrieving the location. What? - * We ignore location errors only. Rest are logged. - */ -- (void)locationManager:(CLLocationManager *)manager - didFailWithError:(NSError *)error -{ - if ([error code] == kCLErrorLocationUnknown) - return; - - [[DB get] log:[NSString stringWithFormat:@"location error: %@", error]]; -} - -/** Receives a location update. - * This generates the correct KVO messages to notify observers. - * Also resets the watchdog. Repeated locations based on timestamp - * will be discarded. - */ -- (void)locationManager:(CLLocationManager*)manager - didUpdateToLocation:(CLLocation*)new_location - fromLocation:(CLLocation*)old_location -{ - if (new_location.horizontalAccuracy < 0) { - DLOG(@"Bad returned accuracy, ignoring update."); - return; - } - - if (self.last_pos && - [self.last_pos.timestamp isEqualToDate:new_location.timestamp]) { - DLOG(@"Discarding repeated location %@", [new_location description]); - return; - } - - DLOG(@"Updating to %@", [new_location description]); - - // Keep the new location for map showing. - [self willChangeValueForKey:_KEY_PATH]; - [new_location retain]; - [last_pos_ release]; - last_pos_ = new_location; - [self didChangeValueForKey:_KEY_PATH]; - - [self ping_watchdog]; -} - -#pragma mark Watchdog - -/** Starts or refreshes the timer used for the GPS watchdog. - * Sometimes if you loose network signal the GPS will stop updating - * values even though the hardware may well be getting them. The - * watchdog will set a time and force a stop/start if there were no - * recent updates received. - * - * You have to call this function every time you start to watch - * updates and every time you receive one, so the watchdog timer is - * reset. - * - * Miss Merge: I'm walking on sunshine, woo hoo! - */ -- (void)ping_watchdog -{ - if (zasca_) - [zasca_ invalidate]; - - zasca_ = [NSTimer scheduledTimerWithTimeInterval:_WATCHDOG_SECONDS - target:self selector:@selector(zasca) userInfo:nil repeats:NO]; -} - -/** Stops the watchdog, if it is on. Otherwise does nothing. - */ -- (void)stop_watchdog -{ - if (zasca_) - [zasca_ invalidate]; - zasca_ = nil; -} - -/** Handles the stop/start of the GPS. - * Note that the stop/start will automatically reschedule the watchdog. - */ -- (void)zasca -{ - [[DB get] log:@"Watchdog timer kicking in due to inactivity."]; - nolog_ = YES; - [self stop]; - [self start]; - nolog_ = NO; -} - -@end - -// vim:tabstop=4 shiftwidth=4 syntax=objc diff --git a/src/controllers/Capture_tab.m b/src/controllers/Capture_tab.m index ab2987f..ccb0de6 100644 --- a/src/controllers/Capture_tab.m +++ b/src/controllers/Capture_tab.m @@ -1,9 +1,9 @@ #import "controllers/Capture_tab.h" #import "App_delegate.h" -#import "GPS.h" #import "controllers/Note_taking_controller.h" #import "macro.h" +#import "Record_my_position-swift.h" @interface Capture_tab () @@ -46,7 +46,7 @@ - (void)dealloc [timer_ invalidate]; if (watching_) - [[GPS get] remove_watcher:self]; + [[EHGPS get] remove_watcher:self]; [start_switch_ removeTarget:self action:@selector(gps_switch_changed) forControlEvents:UIControlEventValueChanged]; [record_type_switch_ removeTarget:self @@ -110,7 +110,7 @@ - (void)loadView action:@selector(record_type_switch_changed) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:record_type_switch_]; - record_type_switch_.on = [GPS get].save_all_positions; + record_type_switch_.on = [EHGPS get].saveAllPositions; explanation_label_ = [[UILabel alloc] initWithFrame:CGRectMake(10, 210, 300, 25)]; @@ -173,7 +173,7 @@ - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; - start_switch_.on = [GPS get].gps_is_on; + start_switch_.on = [EHGPS get].gpsIsOn; [self update_gui]; [self start_timer]; @@ -195,7 +195,7 @@ - (void)viewWillDisappear:(BOOL)animated /// User toggled on/off GUI switch. - (void)gps_switch_changed { - GPS *gps = [GPS get]; + EHGPS *gps = [EHGPS get]; if ([start_switch_ isOn]) { if (![gps start]) { @@ -218,7 +218,7 @@ - (void)gps_switch_changed /// User toggled the record all/single switch. - (void)record_type_switch_changed { - [GPS get].save_all_positions = record_type_switch_.on; + [EHGPS get].saveAllPositions = record_type_switch_.on; [self update_gui]; } @@ -259,7 +259,7 @@ - (void)update_gui } // Last location. - CLLocation *location = [GPS get].last_pos; + CLLocation *location = [EHGPS get].lastPos; if (!location) { longitude_.text = @"No last position"; latitude_.text = @""; @@ -271,10 +271,10 @@ - (void)update_gui } longitude_.text = [NSString stringWithFormat:@"Longitude: %@", - [GPS degrees_to_dms:location.coordinate.longitude latitude:NO]]; + [EHGPS degrees_to_dms:location.coordinate.longitude latitude:NO]]; latitude_.text = [NSString stringWithFormat:@"Latitude: %@", - [GPS degrees_to_dms:location.coordinate.latitude latitude:YES]]; + [EHGPS degrees_to_dms:location.coordinate.latitude latitude:YES]]; const CLLocationAccuracy v = (location.horizontalAccuracy + location.horizontalAccuracy) / 2.0; @@ -342,13 +342,13 @@ - (void)did_enter_background */ - (void)add_note { - if (![GPS get].gps_is_on) { + if (![EHGPS get].gpsIsOn) { [self warn:@"Please turn GPS on to take a note with position." title:@"GPS capture off"]; return; } - CLLocation *location = [GPS get].last_pos; + CLLocation *location = [EHGPS get].lastPos; if (!location) { [self warn:@"Wait until the GPS receives at least one position." title:@"No GPS data"]; @@ -369,7 +369,7 @@ - (void)add_note - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { - if ([keyPath isEqual:[GPS key_path]]) + if ([keyPath isEqual:[EHGPS key_path]]) [self update_gui]; else [super observeValueForKeyPath:keyPath ofObject:object change:change diff --git a/src/db/DB.m b/src/db/DB.m index 36287e9..0db91e4 100644 --- a/src/db/DB.m +++ b/src/db/DB.m @@ -1,11 +1,11 @@ #import "db/DB.h" #import "App_delegate.h" -#import "GPS.h" #import "db/DB_log.h" #import "db/Rows_to_attachment.h" #import "db/internal.h" #import "macro.h" +#import "Record_my_position-Swift.h" #import @@ -120,7 +120,8 @@ + (DB*)open_database [defaults setInteger:_DB_MODEL_VERSION forKey:_DB_MODEL_KEY]; DLOG(@"Disk db open at %@", path); - [[GPS get] add_watcher:db]; + [EHGPS setMDB:db]; + [[EHGPS get] addWatcher:db]; return db; } @@ -161,7 +162,7 @@ + (DB*)get */ - (void)close { - [[GPS get] remove_watcher:self]; + [[EHGPS get] removeWatcher:self]; [super close]; } @@ -175,7 +176,7 @@ - (void)log:(id)text_or_location if (!buffer_) buffer_ = [[NSMutableArray alloc] initWithCapacity:_BUFFER]; - const ACCURACY accuracy = [[GPS get] accuracy]; + const Accuracy accuracy = [EHGPS get].mAccuracy; DB_log *db_log = nil; if ([text_or_location respondsToSelector:@selector(coordinate)]) db_log = [[DB_log alloc] init_with_location:text_or_location @@ -320,7 +321,7 @@ - (int)log_note:(CLLocation*)location; [self flush]; DB_log *db_log = [[DB_log alloc] init_with_location:location - in_background:in_background_ accuracy:[[GPS get] accuracy]]; + in_background:in_background_ accuracy:[EHGPS get].mAccuracy]; RASSERT(db_log, @"Couldn't create DB_log", return -1); const BOOL ret = [self executeUpdateWithParameters:@"INSERT into Positions " @@ -397,10 +398,10 @@ - (void)delete_note:(int)num - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { - if ([keyPath isEqual:[GPS key_path]]) { - GPS *gps = [GPS get]; - if (gps.save_all_positions) - [self log:gps.last_pos]; + if ([keyPath isEqual:EHGPS.KEY_PATH]) { + EHGPS *gps = [EHGPS get]; + if (gps.saveAllPositions) + [self log:gps.lastPos]; } else { [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; diff --git a/src/db/DB_log.h b/src/db/DB_log.h index 4be75b1..0faf6d8 100644 --- a/src/db/DB_log.h +++ b/src/db/DB_log.h @@ -1,6 +1,6 @@ // vim:tabstop=4 shiftwidth=4 encoding=utf-8 syntax=objc -#import "GPS.h" +#import "Record_my_position-Swift.h" @class CLLocation; @@ -30,7 +30,7 @@ float battery_level_; /// Accuracy setting at the time of logging. - ACCURACY accuracy_; + Accuracy accuracy_; /// Tells if the device is connected to an external power source. BOOL external_power_; @@ -50,10 +50,10 @@ @property (nonatomic, retain) CLLocation *location; - (id)init_with_string:(NSString*)text in_background:(BOOL)in_background - accuracy:(ACCURACY)accuracy; + accuracy:(Accuracy)accuracy; - (id)init_with_location:(CLLocation*)location - in_background:(BOOL)in_background accuracy:(ACCURACY)accuracy; + in_background:(BOOL)in_background accuracy:(Accuracy)accuracy; - (NSString*)description; diff --git a/src/db/DB_log.m b/src/db/DB_log.m index 1abe88f..2066aa0 100644 --- a/src/db/DB_log.m +++ b/src/db/DB_log.m @@ -19,7 +19,7 @@ @implementation DB_log /** Constructs a text oriented log entry. */ - (id)init_with_string:(NSString*)text in_background:(BOOL)in_background - accuracy:(ACCURACY)accuracy + accuracy:(Accuracy)accuracy { if (!(self = [super init])) return nil; @@ -41,7 +41,7 @@ - (id)init_with_string:(NSString*)text in_background:(BOOL)in_background /** Constructs a location oriented log entry. */ - (id)init_with_location:(CLLocation*)location - in_background:(BOOL)in_background accuracy:(ACCURACY)accuracy + in_background:(BOOL)in_background accuracy:(Accuracy)accuracy { if (!(self = [super init])) return nil; diff --git a/src/db/Rows_to_attachment.m b/src/db/Rows_to_attachment.m index 29d4064..2fb7548 100644 --- a/src/db/Rows_to_attachment.m +++ b/src/db/Rows_to_attachment.m @@ -1,9 +1,9 @@ #import "db/Rows_to_attachment.h" -#import "GPS.h" #import "db/DB.h" #import "db/internal.h" #import "macro.h" +#import "Record_my_position-Swift.h" #include @@ -127,8 +127,8 @@ - (NSArray*)get_attachments:(BOOL)make_gpx [csv_strings addObject:[NSString stringWithFormat:@"%d,%@," @"%0.8f,%0.8f,%@,%@,%0.1f,%0.1f,%0.1f,%d," @"%d,%d,%0.2f,%0.2f,%0.2f,%d,%d", type, text, - longitude, latitude, [GPS degrees_to_dms:longitude latitude:NO], - [GPS degrees_to_dms:latitude latitude:YES], + longitude, latitude, [EHGPS degreesToDms:longitude latitude:NO], + [EHGPS degreesToDms:latitude latitude:YES], h_accuracy, v_accuracy, altitude, timestamp, in_background, requested_accuracy, speed, direction, battery_level, external_power, reachability]];