Skip to content

Commit

Permalink
Add location update via background fetch notification
Browse files Browse the repository at this point in the history
  • Loading branch information
robbiet480 committed Apr 21, 2017
1 parent 9e0e446 commit 0abb792
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 5 deletions.
2 changes: 1 addition & 1 deletion HomeAssistant/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
}
}
if HomeAssistantAPI.sharedInstance.locationEnabled {
HomeAssistantAPI.sharedInstance.sendOneshotLocation().then { success -> Void in
HomeAssistantAPI.sharedInstance.getAndSendLocation(trigger: .BackgroundFetch).then { success -> Void in
if success == true {
completionHandler(UIBackgroundFetchResult.newData)
} else {
Expand Down
32 changes: 31 additions & 1 deletion HomeAssistant/HAAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ public class HomeAssistantAPI {
notificationBody = "Significant location change detected"
notificationIdentifer = "sig_change"
shouldNotify = prefs.bool(forKey: "significantLocationChangeNotifications")
case .BackgroundFetch:
notificationBody = "Current location delivery triggered via background fetch"
notificationIdentifer = "background_fetch"
shouldNotify = prefs.bool(forKey: "backgroundFetchLocationChangeNotifications")
default:
notificationBody = ""
}
Expand Down Expand Up @@ -284,7 +288,32 @@ public class HomeAssistantAPI {
return Promise { fulfill, reject in
Location.getLocation(accuracy: .neighborhood, frequency: .oneShot, timeout: 25, success: { (_, location) in
print("A new update of location is available: \(location)")
self.submitLocation(updateType: .SignificantLocationUpdate,
self.submitLocation(updateType: .Manual,
coordinates: location.coordinate,
accuracy: location.horizontalAccuracy,
zone: nil)
fulfill(true)
}) { (_, _, error) in
if error == LocationError.timeout {
fulfill(false)
} else {
print("Error when trying to get a oneshot location!", error)
Crashlytics.sharedInstance().recordError(error)
reject(error)
}
}
}
}

func getAndSendLocation(trigger: LocationUpdateTypes?) -> Promise<Bool> {
var updateTrigger: LocationUpdateTypes = .Manual
if let trigger = trigger {
updateTrigger = trigger
}
return Promise { fulfill, reject in
Location.getLocation(accuracy: .neighborhood, frequency: .oneShot, timeout: 25, success: { (_, location) in
print("A new update of location is available: \(location) via \(updateTrigger) trigger")
self.submitLocation(updateType: updateTrigger,
coordinates: location.coordinate,
accuracy: location.horizontalAccuracy,
zone: nil)
Expand Down Expand Up @@ -1215,4 +1244,5 @@ enum LocationUpdateTypes {
case BeaconRegionExit
case Manual
case SignificantLocationUpdate
case BackgroundFetch
}
7 changes: 6 additions & 1 deletion HomeAssistant/Resources/SwiftGen/Strings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,11 @@ enum L10n {
/// Notifications
static let header = L10n.tr("settings_details.location.notifications.header")

enum BackgroundFetch {
/// Background Fetch Notifications
static let title = L10n.tr("settings_details.location.notifications.background_fetch.title")
}

enum BeaconEnter {
/// Enter Zone via iBeacon Notifications
static let title = L10n.tr("settings_details.location.notifications.beacon_enter.title")
Expand All @@ -325,7 +330,7 @@ enum L10n {
}

enum LocationChange {
/// Location Change Zone Notifications
/// Significant Location Change Notifications
static let title = L10n.tr("settings_details.location.notifications.location_change.title")
}
}
Expand Down
3 changes: 2 additions & 1 deletion HomeAssistant/Resources/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@
"settings_details.location.notifications.exit.title" = "Exit Zone Notifications";
"settings_details.location.notifications.beacon_enter.title" = "Enter Zone via iBeacon Notifications";
"settings_details.location.notifications.beacon_exit.title" = "Exit Zone via iBeacon Notifications";
"settings_details.location.notifications.location_change.title" = "Location Change Zone Notifications";
"settings_details.location.notifications.location_change.title" = "Significant Location Change Notifications";
"settings_details.location.notifications.background_fetch.title" = "Background Fetch Notifications";
"settings_details.location.zones.enter_exit_tracked.title" = "Enter/exit tracked";
"settings_details.location.zones.location.title" = "Location";
"settings_details.location.zones.radius.title" = "Radius";
Expand Down
8 changes: 8 additions & 0 deletions HomeAssistant/Views/SettingsDetailViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ class SettingsDetailViewController: FormViewController {
prefs.set(val, forKey: "significantLocationChangeNotifications")
}
})
<<< SwitchRow {
$0.title = L10n.SettingsDetails.Location.Notifications.BackgroundFetch.title
$0.value = prefs.bool(forKey: "backgroundFetchLocationChangeNotifications")
}.onChange({ (row) in
if let val = row.value {
prefs.set(val, forKey: "backgroundFetchLocationChangeNotifications")
}
})
if let cachedEntities = HomeAssistantAPI.sharedInstance.cachedEntities {
if let zoneEntities: [Zone] = cachedEntities.filter({ (entity) -> Bool in
return entity.Domain == "zone"
Expand Down
2 changes: 1 addition & 1 deletion HomeAssistant/Views/WebViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ class WebViewController: UIViewController, WKNavigationDelegate, WKUIDelegate {
}

func sendCurrentLocation(_ sender: UIButton) {
HomeAssistantAPI.sharedInstance.sendOneshotLocation().then { _ -> Void in
HomeAssistantAPI.sharedInstance.getAndSendLocation(trigger: .Manual).then { _ -> Void in
let alert = UIAlertController(title: L10n.ManualLocationUpdateNotification.title,
message: L10n.ManualLocationUpdateNotification.message,
preferredStyle: UIAlertControllerStyle.alert)
Expand Down

0 comments on commit 0abb792

Please sign in to comment.