Skip to content

Commit 5985446

Browse files
authored
Merge pull request #558 from tidepool-org/cameron/LOOP-4596-scenario-organization
[LOOP-4596] Scenario Organization
2 parents d65a48a + 8f8f093 commit 5985446

File tree

7 files changed

+35
-69
lines changed

7 files changed

+35
-69
lines changed

Loop/Managers/Alerts/AlertManager.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,7 @@ extension AlertManager {
782782
let alert = UIAlertController(title: "New Study Product Detected", message: "We've detected a new study product is selected. In order to show use this study product, Tidepool Loop will need to restart.", preferredStyle: .alert)
783783
alert.addAction(UIAlertAction(title: "Confirm", style: .default, handler: { _ in
784784
confirmAction()
785-
exit(0)
785+
fatalError("DEBUG: Resetting Loop")
786786
}))
787787
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel))
788788

Loop/Managers/LocalTestingScenariosManager.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ final class LocalTestingScenariosManager: TestingScenariosManagerRequirements, D
3030
delegate?.testingScenariosManager(self, didUpdateScenarioURLs: scenarioURLs)
3131
}
3232
}
33+
34+
var pluginManager: PluginManager {
35+
deviceManager.pluginManager
36+
}
3337

3438
init(deviceManager: DeviceDataManager) {
3539
guard FeatureFlags.scenariosEnabled else {
@@ -63,7 +67,6 @@ final class LocalTestingScenariosManager: TestingScenariosManagerRequirements, D
6367
do {
6468
let scenarioURLs = try fileManager.contentsOfDirectory(at: scenariosSource, includingPropertiesForKeys: nil)
6569
.filter { $0.pathExtension == "json" }
66-
.sorted(by: { $0.lastPathComponent < $1.lastPathComponent })
6770
self.scenarioURLs = scenarioURLs
6871
delegate?.testingScenariosManager(self, didUpdateScenarioURLs: scenarioURLs)
6972
log.debug("Reloaded scenario URLs")

Loop/Managers/LoopAppManager.swift

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -370,45 +370,14 @@ class LoopAppManager: NSObject {
370370
return false
371371
}
372372

373-
private func resetLoop() {
374-
deviceDataManager.pumpManager?.prepareForDeactivation({ _ in })
375-
376-
resetLoopUserDefaults()
377-
resetLoopDocuments()
378-
}
379-
380-
private func resetLoopUserDefaults() {
381-
// Store values to persist
382-
let allowDebugFeatures = UserDefaults.appGroup?.allowDebugFeatures
383-
let studyProductSelection = UserDefaults.appGroup?.studyProductSelection
384-
385-
// Wipe away whole domain
386-
UserDefaults.appGroup?.removePersistentDomain(forName: Bundle.main.appGroupSuiteName)
387-
388-
// Restore values to persist
389-
UserDefaults.appGroup?.allowDebugFeatures = allowDebugFeatures ?? false
390-
UserDefaults.appGroup?.studyProductSelection = studyProductSelection
391-
}
392-
393-
private func resetLoopDocuments() {
394-
let appGroup = Bundle.main.appGroupSuiteName
395-
guard let directoryURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroup) else {
396-
preconditionFailure("Could not get a container directory URL. Please ensure App Groups are set up correctly in entitlements.")
397-
}
398-
399-
let documents: URL = directoryURL.appendingPathComponent("com.loopkit.LoopKit", isDirectory: true)
400-
try? FileManager.default.removeItem(at: documents)
401-
402-
guard let localDocuments = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true) else {
403-
preconditionFailure("Could not get a documents directory URL.")
404-
}
405-
try? FileManager.default.removeItem(at: localDocuments)
406-
}
407-
408373
func askUserToConfirmCrashIfNecessary() {
409-
if UserDefaults.appGroup?.resetLoop == true {
410-
alertManager.presentConfirmCrashAlert() { [weak self] in
411-
self?.resetLoop()
374+
deviceDataManager.pluginManager.availableSupports.forEach { supportUI in
375+
if supportUI.loopNeedsReset {
376+
supportUI.loopNeedsReset = false
377+
alertManager.presentConfirmCrashAlert() { [weak self] in
378+
self?.deviceDataManager.pumpManager?.prepareForDeactivation({ _ in })
379+
supportUI.resetLoop()
380+
}
412381
}
413382
}
414383
}

Loop/Managers/OnboardingManager.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,12 @@ extension OnboardingManager: TherapySettingsProvider {
435435

436436
extension OnboardingManager: OnboardingProvider {
437437
var allowDebugFeatures: Bool { FeatureFlags.allowDebugFeatures } // NOTE: DEBUG FEATURES - DEBUG AND TEST ONLY
438-
var studyProductSelection: StudyProduct { StudyProduct(rawValue: UserDefaults.appGroup?.studyProductSelection ?? "none") ?? .none }
438+
}
439+
440+
// MARK: - SupportProvider
441+
442+
extension OnboardingManager: SupportProvider {
443+
var availableSupports: [SupportUI] { deviceDataManager.pluginManager.availableSupports }
439444
}
440445

441446
// MARK: - OnboardingUI

Loop/Managers/TestingScenariosManager.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ protocol TestingScenariosManager: AnyObject {
1818
var delegate: TestingScenariosManagerDelegate? { get set }
1919
var activeScenarioURL: URL? { get }
2020
var scenarioURLs: [URL] { get }
21+
var pluginManager: PluginManager { get }
2122
func loadScenario(from url: URL, completion: @escaping (Error?) -> Void)
2223
func loadScenario(from url: URL, advancedByLoopIterations iterations: Int, completion: @escaping (Error?) -> Void)
2324
func loadScenario(from url: URL, rewoundByLoopIterations iterations: Int, completion: @escaping (Error?) -> Void)

Loop/View Controllers/TestingScenariosTableViewController.swift

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ final class TestingScenariosTableViewController: RadioSelectionTableViewControll
1414

1515
private let scenariosManager: TestingScenariosManager
1616

17-
private var scenarioURLs: [URL] = [] {
17+
private var scenarios: [LoopScenario] = [] {
1818
didSet {
19-
options = scenarioURLs.map { $0.deletingPathExtension().lastPathComponent }
19+
options = scenarios.map(\.name)
20+
2021
if isViewLoaded {
2122
DispatchQueue.main.async {
2223
self.updateLoadButtonEnabled()
@@ -58,14 +59,14 @@ final class TestingScenariosTableViewController: RadioSelectionTableViewControll
5859
contextHelp = "The scenarios directory location is available in the debug output of the Xcode console."
5960

6061
if let activeScenarioURL = scenariosManager.activeScenarioURL {
61-
selectedIndex = scenarioURLs.firstIndex(of: activeScenarioURL)
62+
selectedIndex = scenarios.firstIndex(where: { $0.url == activeScenarioURL })
6263
}
6364

6465
updateLoadButtonEnabled()
6566
}
6667

6768
override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
68-
let url = scenarioURLs[indexPath.row]
69+
let url = scenarios[indexPath.row].url
6970

7071
let rewindScenario = contextualAction(
7172
rowTitle: "⏮ Rewind",
@@ -79,7 +80,7 @@ final class TestingScenariosTableViewController: RadioSelectionTableViewControll
7980
}
8081

8182
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
82-
let url = scenarioURLs[indexPath.row]
83+
let url = scenarios[indexPath.row].url
8384

8485
let advanceScenario = contextualAction(
8586
rowTitle: "Advance ⏭",
@@ -130,7 +131,7 @@ final class TestingScenariosTableViewController: RadioSelectionTableViewControll
130131
}
131132

132133
private func updateLoadButtonEnabled() {
133-
loadButtonItem.isEnabled = !scenarioURLs.isEmpty && selectedIndex != nil
134+
loadButtonItem.isEnabled = !scenarios.isEmpty && selectedIndex != nil
134135
}
135136

136137
@objc private func loadSelectedScenario() {
@@ -139,7 +140,7 @@ final class TestingScenariosTableViewController: RadioSelectionTableViewControll
139140
return
140141
}
141142

142-
let url = scenarioURLs[selectedIndex]
143+
let url = scenarios[selectedIndex].url
143144

144145
loadButtonItem.isEnabled = false
145146
loadButtonItem.title = "Loading..."
@@ -163,6 +164,13 @@ final class TestingScenariosTableViewController: RadioSelectionTableViewControll
163164

164165
extension TestingScenariosTableViewController: TestingScenariosManagerDelegate {
165166
func testingScenariosManager(_ manager: TestingScenariosManager, didUpdateScenarioURLs scenarioURLs: [URL]) {
166-
self.scenarioURLs = scenarioURLs
167+
var filteredScenarios = Set<LoopScenario>()
168+
manager.pluginManager.availableSupports.forEach { supportUI in
169+
supportUI.getScenarios(from: scenarioURLs).forEach { scenario in
170+
filteredScenarios.insert(scenario)
171+
}
172+
}
173+
174+
self.scenarios = Array(filteredScenarios).sorted(by: { $0.name < $1.name })
167175
}
168176
}

LoopCore/NSUserDefaults.swift

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ extension UserDefaults {
2020
case lastProfileExpirationAlertDate = "com.loopkit.Loop.lastProfileExpirationAlertDate"
2121
case allowDebugFeatures = "com.loopkit.Loop.allowDebugFeatures"
2222
case allowSimulators = "com.loopkit.Loop.allowSimulators"
23-
case studyProductSelection = "com.loopkit.Loop.studyProductSelection"
24-
case resetLoop = "com.loopkit.Loop.resetLoop"
2523
}
2624

2725
public static let appGroup = UserDefaults(suiteName: Bundle.main.appGroupSuiteName)
@@ -128,24 +126,6 @@ extension UserDefaults {
128126
public var allowSimulators: Bool {
129127
return bool(forKey: Key.allowSimulators.rawValue)
130128
}
131-
132-
public var studyProductSelection: String? {
133-
get {
134-
string(forKey: Key.studyProductSelection.rawValue)
135-
}
136-
set {
137-
set(newValue, forKey: Key.studyProductSelection.rawValue)
138-
}
139-
}
140-
141-
public var resetLoop: Bool {
142-
get {
143-
bool(forKey: Key.resetLoop.rawValue)
144-
}
145-
set {
146-
setValue(newValue, forKey: Key.resetLoop.rawValue)
147-
}
148-
}
149129

150130
public func removeLegacyLoopSettings() {
151131
removeObject(forKey: "com.loudnate.Naterade.BasalRateSchedule")

0 commit comments

Comments
 (0)