Skip to content

Commit 1c5dc45

Browse files
authored
Merge pull request #73 from loudnate/workout-target-duration
Adding a duration selection action sheet for workout targets
2 parents 755b4d0 + 193074b commit 1c5dc45

File tree

4 files changed

+57
-5
lines changed

4 files changed

+57
-5
lines changed

Loop.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@
127127
43EDDBF31C361C75007D89B5 /* xDripG5.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 43EDDBF11C361C75007D89B5 /* xDripG5.framework */; };
128128
43F41C331D3A17AA00C11ED6 /* ChartAxisValueDoubleUnit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43F41C321D3A17AA00C11ED6 /* ChartAxisValueDoubleUnit.swift */; };
129129
43F41C351D3B623800C11ED6 /* ChartPointsTouchHighlightLayerViewCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43F41C341D3B623800C11ED6 /* ChartPointsTouchHighlightLayerViewCache.swift */; };
130+
43F41C371D3BF32400C11ED6 /* UIAlertController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43F41C361D3BF32400C11ED6 /* UIAlertController.swift */; };
130131
43F4EF1D1BA2A57600526CE1 /* DiagnosticLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43F4EF1C1BA2A57600526CE1 /* DiagnosticLogger.swift */; };
131132
43F5C2C91B929C09003EB13D /* HealthKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 43F5C2C81B929C09003EB13D /* HealthKit.framework */; };
132133
43F5C2DB1B92A5E1003EB13D /* SettingsTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43F5C2DA1B92A5E1003EB13D /* SettingsTableViewController.swift */; };
@@ -340,6 +341,7 @@
340341
43EDEE6B1CF2E12A00393BE3 /* Loop.entitlements */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = Loop.entitlements; sourceTree = "<group>"; };
341342
43F41C321D3A17AA00C11ED6 /* ChartAxisValueDoubleUnit.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ChartAxisValueDoubleUnit.swift; sourceTree = "<group>"; };
342343
43F41C341D3B623800C11ED6 /* ChartPointsTouchHighlightLayerViewCache.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ChartPointsTouchHighlightLayerViewCache.swift; sourceTree = "<group>"; };
344+
43F41C361D3BF32400C11ED6 /* UIAlertController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIAlertController.swift; sourceTree = "<group>"; };
343345
43F4EF1C1BA2A57600526CE1 /* DiagnosticLogger.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DiagnosticLogger.swift; sourceTree = "<group>"; };
344346
43F5C2C81B929C09003EB13D /* HealthKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = HealthKit.framework; path = System/Library/Frameworks/HealthKit.framework; sourceTree = SDKROOT; };
345347
43F5C2D41B92A4A6003EB13D /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
@@ -586,6 +588,7 @@
586588
4398973A1CD2FC2000223065 /* NSDateFormatter.swift */,
587589
439897341CD2F7DE00223065 /* NSTimeInterval.swift */,
588590
43E344A31B9E1B1C00C85C07 /* NSUserDefaults.swift */,
591+
43F41C361D3BF32400C11ED6 /* UIAlertController.swift */,
589592
43DE92501C541832001FFDE1 /* UIColor.swift */,
590593
437CEEE31CDE5C0A003C8C80 /* UIImage.swift */,
591594
434FF1ED1CF27EEF000DB779 /* UITableViewCell.swift */,
@@ -995,6 +998,7 @@
995998
438849EC1D29EC34003B3F23 /* AmplitudeService.swift in Sources */,
996999
43DBF0571C93F6EB00B3C386 /* ReservoirTableViewController.swift in Sources */,
9971000
435400341C9F878D00D5819C /* SetBolusUserInfo.swift in Sources */,
1001+
43F41C371D3BF32400C11ED6 /* UIAlertController.swift in Sources */,
9981002
437CEEBC1CD6DE6A003C8C80 /* HUDView.swift in Sources */,
9991003
434F545F1D288345002A9274 /* ShareService.swift in Sources */,
10001004
4354003A1C9FB81100D5819C /* UIColor.swift in Sources */,
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//
2+
// UIAlertController.swift
3+
// Loop
4+
//
5+
// Created by Nate Racklyeft on 7/17/16.
6+
// Copyright © 2016 Nathan Racklyeft. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
12+
extension UIAlertController {
13+
/**
14+
Initializes an ActionSheet-styled controller for selecting a workout duration
15+
16+
- parameter handler: A closure to execute when the sheet is dismissed after selection. The closure has a single argument:
17+
- endDate: The date at which the user selected the workout to end
18+
*/
19+
convenience init(workoutDurationSelectionHandler handler: (endDate: NSDate) -> Void) {
20+
self.init(
21+
title: NSLocalizedString("Use Workout Glucose Targets", comment: "The title of the alert controller used to select a duration for workout targets"),
22+
message: nil,
23+
preferredStyle: .ActionSheet
24+
)
25+
26+
let formatter = NSDateComponentsFormatter()
27+
formatter.allowsFractionalUnits = false
28+
formatter.unitsStyle = .Full
29+
30+
for interval in [1, 2].map({ NSTimeInterval(hours: $0) }) {
31+
let duration = NSLocalizedString("For %1$@", comment: "The format string used to describe a finite workout targets duration")
32+
33+
addAction(UIAlertAction(title: String(format: duration, formatter.stringFromTimeInterval(interval)!), style: .Default) { _ in
34+
handler(endDate: NSDate(timeIntervalSinceNow: interval))
35+
})
36+
}
37+
38+
let distantFuture = NSLocalizedString("Indefinitely", comment: "The title of a target alert action specifying an indefinitely long workout targets duration")
39+
addAction(UIAlertAction(title: distantFuture, style: .Default) { _ in
40+
handler(endDate: NSDate.distantFuture())
41+
})
42+
43+
let cancel = NSLocalizedString("Cancel", comment: "The title of the cancel action in an action sheet")
44+
addAction(UIAlertAction(title: cancel, style: .Cancel, handler: nil))
45+
}
46+
}

Loop/Managers/DeviceDataManager.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -713,14 +713,13 @@ class DeviceDataManager: CarbStoreDelegate, TransmitterDelegate {
713713
return override.endDate.timeIntervalSinceNow > 0
714714
}
715715

716-
/// Attempts to enable workout glucose targets for the given duration, and returns true if successful.
716+
/// Attempts to enable workout glucose targets until the given date, and returns true if successful.
717717
/// TODO: This can live on the schedule itself once its a value type, since didSet would invoke when mutated.
718-
func enableWorkoutMode(duration duration: NSTimeInterval) -> Bool {
718+
func enableWorkoutMode(until endDate: NSDate) -> Bool {
719719
guard let glucoseTargetRangeSchedule = glucoseTargetRangeSchedule else {
720720
return false
721721
}
722722

723-
let endDate = NSDate(timeIntervalSinceNow: duration)
724723
glucoseTargetRangeSchedule.setWorkoutOverrideUntilDate(endDate)
725724

726725
NSNotificationCenter.defaultCenter().postNotificationName(self.dynamicType.LoopSettingsUpdatedNotification, object: self)

Loop/View Controllers/StatusTableViewController.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -744,11 +744,14 @@ class StatusTableViewController: UITableViewController, UIGestureRecognizerDeleg
744744
}
745745

746746
@IBAction func toggleWorkoutMode(sender: UIBarButtonItem) {
747-
// TODO: Display an action sheet to select a duration
748747
if let workoutModeEnabled = workoutMode where workoutModeEnabled {
749748
dataManager.disableWorkoutMode()
750749
} else {
751-
dataManager.enableWorkoutMode(duration: NSTimeInterval(hours: 1))
750+
let vc = UIAlertController(workoutDurationSelectionHandler: { (endDate) in
751+
self.dataManager.enableWorkoutMode(until: endDate)
752+
})
753+
754+
presentViewController(vc, animated: true, completion: nil)
752755
}
753756
}
754757

0 commit comments

Comments
 (0)