Skip to content

Commit

Permalink
Makes dependency injection more obvious in postInit(). Refs #37.
Browse files Browse the repository at this point in the history
  • Loading branch information
gradha committed Jun 6, 2015
1 parent 02f29d6 commit d56673f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/App_delegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ - (BOOL)application:(UIApplication *)application
[db_ log:[NSString stringWithFormat:@"Launch options? %@",
launch_options]];

[[EHGPS get] postInit];
[[EHGPS get] postInit:db_];

tab_controller_ = [Tab_controller new];
[window_ addSubview:tab_controller_.view];
Expand Down
30 changes: 15 additions & 15 deletions src/EHGPS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import UIKit
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 var cDB: DB?

static private let cInstance: EHGPS = EHGPS()

Expand All @@ -39,7 +39,6 @@ import UIKit
{
DLOG("Initializing EHGPS")
mManager = CLLocationManager()
//assert(nil !== mManager) TODO: Why does the check fail?
mAccuracy = .High
super.init()

Expand All @@ -50,10 +49,11 @@ import UIKit
}

/** Finishes the initialization.
* Call this when the DB dependency is solved.
* Call this with the DB dependency to inject.
*/
func postInit()
func postInit(db: DB)
{
EHGPS.cDB = db
let defaults = NSUserDefaults.standardUserDefaults()
if defaults.boolForKey(_GPS_IS_ON_KEY) {
start()
Expand Down Expand Up @@ -124,9 +124,9 @@ import UIKit
func start() -> Bool
{
if CLLocationManager.locationServicesEnabled() {
assert(nil != EHGPS.mDB)
assert(nil != EHGPS.cDB)
if (!gpsIsOn && !mNoLog) {
EHGPS.mDB!.log("Starting to update location")
EHGPS.cDB!.log("Starting to update location")
}

pingWatchdog()
Expand All @@ -144,8 +144,8 @@ import UIKit
*/
func stop() {
if gpsIsOn && !mNoLog {
assert(nil != EHGPS.mDB)
EHGPS.mDB!.log("Stopping location updates");
assert(nil != EHGPS.cDB)
EHGPS.cDB!.log("Stopping location updates");
}
stopWatchdog()
gpsIsOn = false
Expand Down Expand Up @@ -194,11 +194,11 @@ import UIKit
}

if gpsIsOn {
assert(nil != EHGPS.mDB)
assert(nil != EHGPS.cDB)
if let reason = reason {
EHGPS.mDB!.log(String(format: "%@ Reason: %@", message, reason))
EHGPS.cDB!.log(String(format: "%@ Reason: %@", message, reason))
} else {
EHGPS.mDB!.log(message)
EHGPS.cDB!.log(message)
}

mNoLog = true
Expand All @@ -218,8 +218,8 @@ import UIKit
return
}

assert(nil != EHGPS.mDB)
EHGPS.mDB!.log("location error: " + error.localizedDescription)
assert(nil != EHGPS.cDB)
EHGPS.cDB!.log("location error: " + error.localizedDescription)
}

/** Receives a location update.
Expand Down Expand Up @@ -289,8 +289,8 @@ import UIKit
*/
func zasca()
{
assert(nil != EHGPS.mDB)
EHGPS.mDB!.log("Watchdog timer kicking in due to inactivity.")
assert(nil != EHGPS.cDB)
EHGPS.cDB!.log("Watchdog timer kicking in due to inactivity.")
mNoLog = true
stop()
start()
Expand Down
1 change: 0 additions & 1 deletion src/db/DB.m
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ + (DB*)open_database

[defaults setInteger:_DB_MODEL_VERSION forKey:_DB_MODEL_KEY];
DLOG(@"Disk db open at %@", path);
[EHGPS setMDB:db];
[[EHGPS get] addWatcher:db];
return db;
}
Expand Down

0 comments on commit d56673f

Please sign in to comment.