Skip to content

Commit

Permalink
request location until something good comes before remove transition …
Browse files Browse the repository at this point in the history
…view
  • Loading branch information
jimmymlu committed Apr 6, 2017
1 parent 8dbed9a commit 0aa5692
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 37 deletions.
3 changes: 2 additions & 1 deletion SleepModel/HEMLocationService.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ typedef void(^HEMLocationHandler)(HEMLocation* _Nullable mostRecentLocation, NSE
- (HEMLocationAuthStatus)authorizationStatus;
- (BOOL)requiresPermission;
- (BOOL)hasDeniedPermission;
- (NSError*)quickLocation:(HEMLocationHandler)completion;
- (BOOL)isEnabled;
- (nullable NSError*)quickLocation:(HEMLocationHandler)completion;
- (void)requestPermission:(HEMLocationAuthorizationHandler)authHandler;
- (nullable HEMLocationActivity*)startLocationActivity:(HEMLocationHandler)update
error:(NSError**)error;
Expand Down
4 changes: 4 additions & 0 deletions SleepModel/HEMLocationService.m
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ - (HEMLocationAuthStatus)authorizationStatus {
return HEMLocationAuthStatusAuthorized;
}
}

- (BOOL)isEnabled {
return [CLLocationManager locationServicesEnabled];
}

- (BOOL)hasDeniedPermission {
return [self authorizationStatus] == HEMLocationAuthStatusDenied;
Expand Down
92 changes: 59 additions & 33 deletions SleepModel/NightModeSettingsPresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class NightModeSettingsPresenter: HEMListPresenter {
fileprivate var footer: UIView?
fileprivate var waitingOnPermission: Bool
fileprivate weak var transitionView: UIView?
fileprivate var locationActivity: HEMLocationActivity?

init(nightModeService: NightModeService, locationService: HEMLocationService) {
let optionsTitle = NSLocalizedString("settings.night-mode.options.title", comment: "table options title")
Expand Down Expand Up @@ -74,9 +75,12 @@ class NightModeSettingsPresenter: HEMListPresenter {
textView.isScrollEnabled = false
textView.backgroundColor = self.tableView!.backgroundColor

let enabled = self.locationService.isEnabled()
let denied = self.locationService.hasDeniedPermission()

let footer = UIView()
footer.addSubview(textView)
footer.isHidden = self.locationService.hasDeniedPermission() == false
footer.isHidden = denied == false && enabled == true

return footer
}
Expand Down Expand Up @@ -134,7 +138,9 @@ class NightModeSettingsPresenter: HEMListPresenter {
cell.descriptionLabel?.text = self.detail(forItem: item)

if option == .sunsetToSunrise {
self.footer?.isHidden = self.locationService.hasDeniedPermission() == false
let enabled = self.locationService.isEnabled()
let denied = self.locationService.hasDeniedPermission()
self.footer?.isHidden = denied == false && enabled == true
cell.enable(self.footer?.isHidden == true)
} else {
cell.enable(true)
Expand Down Expand Up @@ -183,24 +189,24 @@ class NightModeSettingsPresenter: HEMListPresenter {
self.locationService.requestPermission({ (status: HEMLocationAuthStatus) in
self.waitingOnPermission = false
switch status {
case .notEnabled:
fallthrough
case .denied:
self.revertSelection(error: nil)
default:
self.scheduleNightModeFromLocation()
case .notEnabled:
fallthrough
case .denied:
self.revertSelection(withError: false)
default:
self.scheduleNightModeFromLocation()
}
})
}

fileprivate func revertSelection(error: Error?) {
fileprivate func revertSelection(withError: Bool) {
let savedOption = self.nightModeService.savedOption()
self.selectedItemNames = [savedOption.localizedDescription()]
self.tableView?.reloadData() // to disable the schedule cell

self.removeTransitionView(animate: true)

if error != nil {
if withError == true {
// show error
let title = NSLocalizedString("settings.night-mode", comment: "title, same as screen title")
let message = NSLocalizedString("settings.night-mode.error.no-location", comment: "no location error")
Expand Down Expand Up @@ -228,31 +234,51 @@ class NightModeSettingsPresenter: HEMListPresenter {
}

fileprivate func scheduleNightModeFromLocation() {
var scheduled = false
let service = self.locationService
let error = service?.quickLocation({[weak self] (loc: HEMLocation?, err: Error?) in
// to ensure only 1 location is used and to not call it too many times
guard scheduled == false else {
return
}

scheduled = true

if loc != nil {
SENAnalytics.trackNightModeChange(withSetting: kHEMAnalyticsPropNightModeValueAuto)
self?.nightModeService.scheduleForSunset(latitude: Double(loc!.lat), longitude: Double(loc!.lon))
self?.removeTransitionView(animate: true)
} else if err != nil {
self?.revertSelection(error: err)
} else {
self?.revertSelection(error: nil)
}

})
guard let service = self.locationService else {
self.revertSelection(withError: false)
return
}

if error != nil {
self.revertSelection(error: error)
var done = false
do {
self.locationActivity = try service.startLocationActivity({ [weak self] (loc: HEMLocation?, err: Error?) in
// to ensure only 1 location is used and to not call it too many times
guard done == false else {
return
}

if loc != nil {
done = true
SENAnalytics.trackNightModeChange(withSetting: kHEMAnalyticsPropNightModeValueAuto)
self?.nightModeService.scheduleForSunset(latitude: Double(loc!.lat), longitude: Double(loc!.lon))
self?.removeTransitionView(animate: true)
} else if err != nil {
done = true
self?.revertSelection(withError: true)
}

if done == true {
if let activity = self?.locationActivity, let locService = self?.locationService {
locService.stop(activity)
}
}

})
} catch _ {
self.revertSelection(withError: true)
}
}

deinit {
guard let service = self.locationService else {
return
}

guard let activity = self.locationActivity else {
return
}

service.stop(activity)
}

}
3 changes: 0 additions & 3 deletions SleepModel/Theme.swift
Original file line number Diff line number Diff line change
Expand Up @@ -302,15 +302,13 @@ import UIKit
if let navVC = controller as? UINavigationController {
viewControllers = navVC.viewControllers
viewControllers?.forEach({ (controllerInStack: UIViewController) in
print("calling viewcontrollers of nav,", NSStringFromClass(type(of: navVC)))
self.apply(viewController: controllerInStack, auto: auto)
})
}

if let tabVC = controller as? UITabBarController {
viewControllers = tabVC.viewControllers
viewControllers?.forEach({ (tabController: UIViewController) in
print("calling viewcontrollers of tab,", NSStringFromClass(type(of: tabVC)))
self.apply(viewController: tabController, auto: auto)
})
}
Expand All @@ -319,7 +317,6 @@ import UIKit
return
}

print("notifying controller,", NSStringFromClass(type(of: controller)))
themedVC.didChange(theme: self, auto: auto)
controller.childViewControllers.forEach { (child: UIViewController) in
if viewControllers == nil || viewControllers!.contains(child) == false {
Expand Down

0 comments on commit 0aa5692

Please sign in to comment.