From 6bc893f670ce5219fa109d66047a219d7ad90a44 Mon Sep 17 00:00:00 2001 From: Grzegorz Adam Hankiewicz Date: Tue, 2 Jun 2015 22:33:22 +0200 Subject: [PATCH] Adds watcher support. Refs #37. --- src/GPS.h | 1 - src/GPS.m | 7 ------- src/SGPS.swift | 35 +++++++++++++++++++++++++++++++---- 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/GPS.h b/src/GPS.h index db713e8..fdb7d44 100644 --- a/src/GPS.h +++ b/src/GPS.h @@ -47,7 +47,6 @@ typedef enum ACCURACY_ENUM ACCURACY; + (GPS*)get; + (NSString*)degrees_to_dms:(CLLocationDegrees)value latitude:(BOOL)latitude; -+ (NSString*)key_path; - (id)init; - (bool)start; - (void)stop; diff --git a/src/GPS.m b/src/GPS.m index c40018c..cdbc966 100644 --- a/src/GPS.m +++ b/src/GPS.m @@ -177,13 +177,6 @@ - (void)stop [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. */ diff --git a/src/SGPS.swift b/src/SGPS.swift index 210fc79..d98fa50 100644 --- a/src/SGPS.swift +++ b/src/SGPS.swift @@ -10,6 +10,7 @@ enum Accuracy @objc class SGPS : NSObject, CLLocationManagerDelegate { private let _GPS_IS_ON_KEY = "gps_is_on" + private let _KEY_PATH = "last_pos" private let _KEY_SAVE_SINGLE_POSITION = "save_single_positions" static private let cInstance: SGPS = SGPS() @@ -70,14 +71,14 @@ enum Accuracy } } - func stop() + func pingWatchdog() { - println("Stopping, not implemented!") + println("Pinging watchdog, not implemented!"); } - func pingWatchdog() + func stopWatchdog() { - println("Pinging watchdog, not implemented!"); + println("Stopping watchdog, not implemented!"); } /** Converts a coordinate from degrees to decimal minute second format. @@ -132,4 +133,30 @@ enum Accuracy return false } } + /** Stops the GPS tracking. + * You can call this function anytime, doesn't really fail. + */ + func stop() { + if gpsIsOn && !mNoLog { + DB.get().log("Stopping location updates"); + } + stopWatchdog() + gpsIsOn = false + mManager.stopUpdatingLocation() + } + + /** Registers an observer for changes to last_pos. + * Observers will monitor the key_path value. + */ + func addWatcher(watcher: NSObject) { + addObserver(watcher, forKeyPath:_KEY_PATH, + options: .New, context: nil) + } + + /** Removes an observer for changes to last_pos. + */ + func removeWatcher(watcher: NSObject) { + removeObserver(watcher, forKeyPath: _KEY_PATH) + } + }