@@ -81,6 +81,7 @@ class LoopAppManager: NSObject {
81
81
private var settingsManager : SettingsManager !
82
82
private var loggingServicesManager = LoggingServicesManager ( )
83
83
private var analyticsServicesManager = AnalyticsServicesManager ( )
84
+ private var resetLoopManager : ResetLoopManager !
84
85
85
86
private var overrideHistory = UserDefaults . appGroup? . overrideHistory ?? TemporaryScheduleOverrideHistory . init ( )
86
87
@@ -142,6 +143,8 @@ class LoopAppManager: NSObject {
142
143
if state == . launchHomeScreen {
143
144
launchHomeScreen ( )
144
145
}
146
+
147
+ askUserToConfirmLoopReset ( )
145
148
}
146
149
147
150
private func checkProtectedDataAvailable( ) {
@@ -164,6 +167,8 @@ class LoopAppManager: NSObject {
164
167
OrientationLock . deviceOrientationController = self
165
168
UNUserNotificationCenter . current ( ) . delegate = self
166
169
170
+ resetLoopManager = ResetLoopManager ( delegate: self )
171
+
167
172
let localCacheDuration = Bundle . main. localCacheDuration
168
173
let cacheStore = PersistenceController . controllerInAppGroupDirectory ( )
169
174
@@ -289,8 +294,6 @@ class LoopAppManager: NSObject {
289
294
self . state = state. next
290
295
291
296
alertManager. playbackAlertsFromPersistence ( )
292
-
293
- askUserToConfirmLoopReset ( )
294
297
}
295
298
296
299
// MARK: - Life Cycle
@@ -399,70 +402,6 @@ class LoopAppManager: NSObject {
399
402
}
400
403
return false
401
404
}
402
-
403
- func askUserToConfirmLoopReset( ) {
404
- if UserDefaults . appGroup? . userRequestedLoopReset == true {
405
- alertManager. presentLoopResetConfirmationAlert (
406
- confirmAction: { [ weak self] completion in
407
- guard let pumpManager = self ? . deviceDataManager. pumpManager else {
408
- self ? . resetLoop ( )
409
- completion ( )
410
- return
411
- }
412
-
413
- pumpManager. prepareForDeactivation ( ) { [ weak self] error in
414
- guard let error = error else {
415
- self ? . resetLoop ( )
416
- completion ( )
417
- return
418
- }
419
- self ? . alertManager. presentCouldNotResetLoopAlert ( error: error)
420
- }
421
- } ,
422
- cancelAction: {
423
- UserDefaults . appGroup? . userRequestedLoopReset = false
424
- }
425
- )
426
- }
427
- }
428
-
429
- private func resetLoop( ) {
430
- deviceDataManager. pluginManager. availableSupports. forEach { supportUI in
431
- supportUI. loopWillReset ( )
432
- }
433
-
434
- resetLoopDocuments ( )
435
- resetLoopUserDefaults ( )
436
-
437
- deviceDataManager. pluginManager. availableSupports. forEach { supportUI in
438
- supportUI. loopDidReset ( )
439
- }
440
- }
441
-
442
- private func resetLoopUserDefaults( ) {
443
- // Store values to persist
444
- let allowDebugFeatures = UserDefaults . appGroup? . allowDebugFeatures
445
-
446
- // Wipe away whole domain
447
- UserDefaults . appGroup? . removePersistentDomain ( forName: Bundle . main. appGroupSuiteName)
448
-
449
- // Restore values to persist
450
- UserDefaults . appGroup? . allowDebugFeatures = allowDebugFeatures ?? false
451
- }
452
-
453
- private func resetLoopDocuments( ) {
454
- guard let directoryURL = FileManager . default. containerURL ( forSecurityApplicationGroupIdentifier: Bundle . main. appGroupSuiteName) else {
455
- preconditionFailure ( " Could not get a container directory URL. Please ensure App Groups are set up correctly in entitlements. " )
456
- }
457
-
458
- let documents : URL = directoryURL. appendingPathComponent ( " com.loopkit.LoopKit " , isDirectory: true )
459
- try ? FileManager . default. removeItem ( at: documents)
460
-
461
- guard let localDocuments = try ? FileManager . default. url ( for: . documentDirectory, in: . userDomainMask, appropriateFor: nil , create: true ) else {
462
- preconditionFailure ( " Could not get a documents directory URL. " )
463
- }
464
- try ? FileManager . default. removeItem ( at: localDocuments)
465
- }
466
405
467
406
private var rootViewController : UIViewController ? {
468
407
get { windowProvider? . window? . rootViewController }
@@ -474,7 +413,9 @@ class LoopAppManager: NSObject {
474
413
475
414
extension LoopAppManager : AlertPresenter {
476
415
func present( _ viewControllerToPresent: UIViewController , animated: Bool , completion: ( ( ) -> Void ) ? ) {
477
- rootViewController? . topmostViewController. present ( viewControllerToPresent, animated: animated, completion: completion)
416
+ DispatchQueue . main. async {
417
+ self . rootViewController? . topmostViewController. present ( viewControllerToPresent, animated: animated, completion: completion)
418
+ }
478
419
}
479
420
480
421
func dismissTopMost( animated: Bool , completion: ( ( ) -> Void ) ? ) {
@@ -625,3 +566,33 @@ extension LoopAppManager: TemporaryScheduleOverrideHistoryDelegate {
625
566
}
626
567
}
627
568
569
+ extension LoopAppManager : ResetLoopManagerDelegate {
570
+ func askUserToConfirmLoopReset( ) {
571
+ resetLoopManager. askUserToConfirmLoopReset ( )
572
+ }
573
+
574
+ func presentConfirmationAlert( confirmAction: @escaping ( PumpManager ? , @escaping ( ) -> Void ) -> Void , cancelAction: @escaping ( ) -> Void ) {
575
+ alertManager. presentLoopResetConfirmationAlert (
576
+ confirmAction: { [ weak self] completion in
577
+ confirmAction ( self ? . deviceDataManager. pumpManager, completion)
578
+ } ,
579
+ cancelAction: cancelAction
580
+ )
581
+ }
582
+
583
+ func loopWillReset( ) {
584
+ deviceDataManager. pluginManager. availableSupports. forEach { supportUI in
585
+ supportUI. loopWillReset ( )
586
+ }
587
+ }
588
+
589
+ func loopDidReset( ) {
590
+ deviceDataManager. pluginManager. availableSupports. forEach { supportUI in
591
+ supportUI. loopDidReset ( )
592
+ }
593
+ }
594
+
595
+ func presentCouldNotResetLoopAlert( error: Error ) {
596
+ alertManager. presentCouldNotResetLoopAlert ( error: error)
597
+ }
598
+ }
0 commit comments