Skip to content

Commit

Permalink
Replaces GPS with EHGPS in swift, finds circular dependency. Refs #37.
Browse files Browse the repository at this point in the history
  • Loading branch information
gradha committed Jun 5, 2015
1 parent 6b3e44e commit a0c5739
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 510 deletions.
26 changes: 8 additions & 18 deletions Record_my_position.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -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 */; };
Expand All @@ -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 /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = D5C86D1E1299CF8D00253C7F /* [email protected] */; };
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 */; };
Expand Down Expand Up @@ -269,16 +266,14 @@
D5BEDA7D126F8C6A0056DB78 /* EGODatabaseRow.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EGODatabaseRow.h; sourceTree = "<group>"; };
D5BEDA7E126F8C6A0056DB78 /* EGODatabaseRow.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = EGODatabaseRow.m; sourceTree = "<group>"; };
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 = "<group>"; };
D5C47781127177D500CB008F /* GPS.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPS.m; sourceTree = "<group>"; };
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 = "<group>"; };
D5C86D1E1299CF8D00253C7F /* [email protected] */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "[email protected]"; sourceTree = "<group>"; };
D5D216531B1A312800AB04AB /* Record_my_position-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Record_my_position-Bridging-Header.h"; sourceTree = "<group>"; };
D5D216541B1A312800AB04AB /* Er recorder adhoc-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Er recorder adhoc-Bridging-Header.h"; sourceTree = "<group>"; };
D5D216551B1A312800AB04AB /* Record_my_position_appstore-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Record_my_position_appstore-Bridging-Header.h"; sourceTree = "<group>"; };
D5D216561B1A312800AB04AB /* SGPS.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SGPS.swift; sourceTree = "<group>"; };
D5D216561B1A312800AB04AB /* EHGPS.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EHGPS.swift; sourceTree = "<group>"; };
D5D7CDCC128A9D1300B83104 /* Capture_tab.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Capture_tab.h; sourceTree = "<group>"; };
D5D7CDCD128A9D1300B83104 /* Capture_tab.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Capture_tab.m; sourceTree = "<group>"; };
D5D7CDCE128A9D1300B83104 /* Log_tab.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Log_tab.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -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 */,
Expand All @@ -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 */,
Expand All @@ -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 */,
Expand All @@ -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 */,
Expand All @@ -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 */,
Expand All @@ -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 */,
Expand Down
45 changes: 7 additions & 38 deletions src/App_delegate.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#import "App_delegate.h"

#import "GPS.h"
#import "controllers/Tab_controller.h"
#import "db/DB.h"
#import "macro.h"
Expand Down Expand Up @@ -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? %@",
Expand All @@ -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];
}

Expand All @@ -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.
Expand All @@ -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];
}

Expand All @@ -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...
Expand All @@ -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];
Expand Down Expand Up @@ -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];
Expand All @@ -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
Expand Down
38 changes: 22 additions & 16 deletions src/SGPS.swift → src/EHGPS.swift
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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) {

Expand Down Expand Up @@ -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()
Expand Down
Loading

0 comments on commit a0c5739

Please sign in to comment.