Skip to content

Commit

Permalink
Setup Significant Location Updates when app launches instead of deep …
Browse files Browse the repository at this point in the history
…in HAAPI
  • Loading branch information
robbiet480 committed Apr 21, 2017
1 parent a38e210 commit d340714
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
31 changes: 30 additions & 1 deletion HomeAssistant/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import RealmSwift
import UserNotifications
import AlamofireNetworkActivityIndicator
import KeychainAccess
import SwiftLocation

let realmConfig = Realm.Configuration(schemaVersion: 3, migrationBlock: nil)

Expand Down Expand Up @@ -61,6 +62,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
prefs.synchronize()
}

registerForSignificantLocationUpdates()

window = UIWindow.init(frame: UIScreen.main.bounds)
window?.backgroundColor = .white

Expand Down Expand Up @@ -164,7 +167,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

func application(_ application: UIApplication,
performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
print("Background fetch activated!")
let timestamp = DateFormatter.localizedString(from: Date(), dateStyle: .medium, timeStyle: .short)
print("Background fetch activated at \(timestamp)!")
if HomeAssistantAPI.sharedInstance.Configured == false {
if let baseURL = keychain["baseURL"], let apiPass = keychain["apiPassword"] {
HomeAssistantAPI.sharedInstance.Setup(baseURL: baseURL, password: apiPass,
Expand Down Expand Up @@ -245,6 +249,31 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
return true
}

func registerForSignificantLocationUpdates() {
if HomeAssistantAPI.sharedInstance.locationEnabled {
Location.getLocation(accuracy: .neighborhood, frequency: .significant, timeout: nil,
success: { (_, location) -> (Void) in

if HomeAssistantAPI.sharedInstance.Configured == false {
if let baseURL = keychain["baseURL"], let apiPass = keychain["apiPassword"] {
HomeAssistantAPI.sharedInstance.Setup(baseURL: baseURL, password: apiPass,
deviceID: keychain["deviceID"])
}
}

HomeAssistantAPI.sharedInstance.submitLocation(updateType: .SignificantLocationUpdate,
coordinates: location.coordinate,
accuracy: location.horizontalAccuracy,
zone: nil)
}) { (_, _, error) -> (Void) in
// something went wrong. request will be cancelled automatically
NSLog("Something went wrong when trying to get significant location updates! Error was: @%",
error.localizedDescription)
Crashlytics.sharedInstance().recordError(error)
}
}
}

}

@available(iOS 10, *)
Expand Down
18 changes: 2 additions & 16 deletions HomeAssistant/HAAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public class HomeAssistantAPI {

_ = self.GetStates().then(execute: { _ -> Void in
if self.locationEnabled {
self.trackLocation()
self.setupZones()
}

if self.loadedComponents.contains("ios") {
Expand Down Expand Up @@ -224,21 +224,7 @@ public class HomeAssistantAPI {

}

func trackLocation() {
_ = Location.getLocation(accuracy: .neighborhood,
frequency: .significant,
timeout: 50,
success: { (_, location) -> (Void) in
self.submitLocation(updateType: .Manual,
coordinates: location.coordinate,
accuracy: location.horizontalAccuracy,
zone: nil)
}) { (_, _, error) -> (Void) in
// something went wrong. request will be cancelled automatically
print("Something went wrong when trying to get significant location updates! Error was:", error)
Crashlytics.sharedInstance().recordError(error)
}

func setupZones() {
if let cachedEntities = HomeAssistantAPI.sharedInstance.cachedEntities {
if let zoneEntities: [Zone] = cachedEntities.filter({ (entity) -> Bool in
return entity.Domain == "zone"
Expand Down
3 changes: 2 additions & 1 deletion HomeAssistant/Views/SettingsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,8 @@ class SettingsViewController: FormViewController {
if finished {
print("Location Permissions finished!", finished, results)
if results[0].status == .authorized {
HomeAssistantAPI.sharedInstance.trackLocation()
HomeAssistantAPI.sharedInstance.setupZones()
_ = HomeAssistantAPI.sharedInstance.sendOneshotLocation()
}
row.hidden = true
row.updateCell()
Expand Down

0 comments on commit d340714

Please sign in to comment.