MockSupport.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //
  2. // MockSupport.swift
  3. // MockKitUI
  4. //
  5. // Created by Rick Pasetto on 10/13/21.
  6. // Copyright © 2021 LoopKit Authors. All rights reserved.
  7. //
  8. import Foundation
  9. import LoopKit
  10. import LoopKitUI
  11. import SwiftUI
  12. public class MockSupport: SupportUI {
  13. public static let pluginIdentifier = "MockSupport"
  14. var versionUpdate: VersionUpdate?
  15. var alertIssuer: AlertIssuer? {
  16. return self.delegate
  17. }
  18. var lastVersionCheckAlertDate: Date?
  19. public init() { }
  20. public required init?(rawState: RawStateValue) {
  21. lastVersionCheckAlertDate = rawState["lastVersionCheckAlertDate"] as? Date
  22. }
  23. public var rawState: RawStateValue {
  24. var rawValue: RawStateValue = [:]
  25. rawValue["lastVersionCheckAlertDate"] = lastVersionCheckAlertDate
  26. return rawValue
  27. }
  28. public func checkVersion(bundleIdentifier: String, currentVersion: String) async -> VersionUpdate? {
  29. maybeIssueAlert(versionUpdate ?? .noUpdateNeeded)
  30. return versionUpdate
  31. }
  32. public weak var delegate: SupportUIDelegate?
  33. public func configurationMenuItems() -> [LoopKitUI.CustomMenuItem] {
  34. return []
  35. }
  36. @ViewBuilder
  37. public func supportMenuItem(supportInfoProvider: SupportInfoProvider, urlHandler: @escaping (URL) -> Void) -> some View {
  38. SupportMenuItem(mockSupport: self)
  39. }
  40. public func softwareUpdateView(bundleIdentifier: String, currentVersion: String, guidanceColors: GuidanceColors, openAppStore: (() -> Void)?) -> AnyView? {
  41. return AnyView(
  42. Button("versionUpdate: \(versionUpdate!.localizedDescription)\n\nbundleIdentifier: \(bundleIdentifier)\n\ncurrentVersion: \(currentVersion)") {
  43. openAppStore?()
  44. }
  45. )
  46. }
  47. public func getScenarios(from scenarioURLs: [URL]) -> [LoopScenario] {
  48. scenarioURLs.map { LoopScenario(name: $0.lastPathComponent, url: $0) }
  49. }
  50. public func loopWillReset() {}
  51. public func loopDidReset() {}
  52. }
  53. extension MockSupport {
  54. var alertCadence: TimeInterval {
  55. return TimeInterval.minutes(1)
  56. }
  57. private var appName: String {
  58. return Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") as! String
  59. }
  60. private func maybeIssueAlert(_ versionUpdate: VersionUpdate) {
  61. guard versionUpdate >= .recommended else {
  62. noAlertNecessary()
  63. return
  64. }
  65. let alertIdentifier = Alert.Identifier(managerIdentifier: MockSupport.pluginIdentifier, alertIdentifier: versionUpdate.rawValue)
  66. let alertContent: LoopKit.Alert.Content
  67. if firstAlert {
  68. alertContent = Alert.Content(title: versionUpdate.localizedDescription,
  69. body: String(format: LocalizedString("""
  70. Your %1$@ app is out of date. It will continue to work, but we recommend updating to the latest version.
  71. Go to %2$@ Settings > Software Update to complete.
  72. """, comment: "Alert content body for first software update alert (1: app name)(2: app name)"), appName, appName),
  73. acknowledgeActionButtonLabel: LocalizedString("OK", comment: "Default acknowledgement"))
  74. } else if let lastVersionCheckAlertDate = lastVersionCheckAlertDate,
  75. abs(lastVersionCheckAlertDate.timeIntervalSinceNow) > alertCadence {
  76. alertContent = Alert.Content(title: LocalizedString("Update Reminder", comment: "Recurring software update alert title"),
  77. body: String(format: LocalizedString("""
  78. A software update is recommended to continue using the %1$@ app.
  79. Go to %2$@ Settings > Software Update to install the latest version.
  80. """, comment: "Alert content body for recurring software update alert"), appName, appName),
  81. acknowledgeActionButtonLabel: LocalizedString("OK", comment: "Default acknowledgement"))
  82. } else {
  83. return
  84. }
  85. let interruptionLevel: LoopKit.Alert.InterruptionLevel = versionUpdate == .required ? .critical : .active
  86. alertIssuer?.issueAlert(Alert(identifier: alertIdentifier, foregroundContent: alertContent, backgroundContent: alertContent, trigger: .immediate, interruptionLevel: interruptionLevel))
  87. recordLastAlertDate()
  88. }
  89. private func noAlertNecessary() {
  90. lastVersionCheckAlertDate = nil
  91. }
  92. private var firstAlert: Bool {
  93. return lastVersionCheckAlertDate == nil
  94. }
  95. private func recordLastAlertDate() {
  96. lastVersionCheckAlertDate = Date()
  97. }
  98. }
  99. struct SupportMenuItem : View {
  100. let mockSupport: MockSupport
  101. @State var showActionSheet: Bool = false
  102. private var buttons: [ActionSheet.Button] {
  103. VersionUpdate.allCases.map { versionUpdate in
  104. let setter = { mockSupport.versionUpdate = versionUpdate }
  105. switch versionUpdate {
  106. case .required:
  107. return ActionSheet.Button.destructive(Text(versionUpdate.localizedDescription), action: setter)
  108. default:
  109. return ActionSheet.Button.default(Text(versionUpdate.localizedDescription), action: setter)
  110. }
  111. } +
  112. [.cancel(Text("Cancel"))]
  113. }
  114. private var actionSheet: ActionSheet {
  115. ActionSheet(title: Text("Version Check Response"), message: Text("How should the simulator respond to a version check?"), buttons: buttons)
  116. }
  117. var body: some View {
  118. Button(action: {
  119. self.showActionSheet.toggle()
  120. }) {
  121. Text("Mock Version Check \(currentVersionUpdate)")
  122. }
  123. .actionSheet(isPresented: $showActionSheet, content: {
  124. self.actionSheet
  125. })
  126. Button(action: { mockSupport.lastVersionCheckAlertDate = nil } ) {
  127. Text("Clear Last Version Check Alert Date")
  128. }
  129. }
  130. var currentVersionUpdate: String {
  131. return mockSupport.versionUpdate.map { "(\($0.rawValue))" } ?? ""
  132. }
  133. }