MockSupport.swift 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 supportIdentifier = "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, completion: @escaping (Result<VersionUpdate?, Error>) -> Void) {
  29. maybeIssueAlert(versionUpdate ?? .none)
  30. completion(.success(versionUpdate))
  31. }
  32. public weak var delegate: SupportUIDelegate?
  33. public func configurationMenuItems() -> [AnyView] {
  34. return []
  35. }
  36. public func supportMenuItem(supportInfoProvider: SupportInfoProvider, urlHandler: @escaping (URL) -> Void) -> AnyView? {
  37. return AnyView(SupportMenuItem(mockSupport: self))
  38. }
  39. public func softwareUpdateView(bundleIdentifier: String, currentVersion: String, guidanceColors: GuidanceColors, openAppStore: (() -> Void)?) -> AnyView? {
  40. return AnyView(
  41. Button("versionUpdate: \(versionUpdate!.localizedDescription)\n\nbundleIdentifier: \(bundleIdentifier)\n\ncurrentVersion: \(currentVersion)") {
  42. openAppStore?()
  43. }
  44. )
  45. }
  46. }
  47. extension MockSupport {
  48. var alertCadence: TimeInterval {
  49. return TimeInterval.minutes(1)
  50. }
  51. private var appName: String {
  52. return Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") as! String
  53. }
  54. private func maybeIssueAlert(_ versionUpdate: VersionUpdate) {
  55. guard versionUpdate >= .recommended else {
  56. noAlertNecessary()
  57. return
  58. }
  59. let alertIdentifier = Alert.Identifier(managerIdentifier: MockSupport.supportIdentifier, alertIdentifier: versionUpdate.rawValue)
  60. let alertContent: LoopKit.Alert.Content
  61. if firstAlert {
  62. alertContent = Alert.Content(title: versionUpdate.localizedDescription,
  63. body: String(format: LocalizedString("""
  64. Your %1$@ app is out of date. It will continue to work, but we recommend updating to the latest version.
  65. Go to %2$@ Settings > Software Update to complete.
  66. """, comment: "Alert content body for first software update alert (1: app name)(2: app name)"), appName, appName),
  67. acknowledgeActionButtonLabel: LocalizedString("OK", comment: "Default acknowledgement"))
  68. } else if let lastVersionCheckAlertDate = lastVersionCheckAlertDate,
  69. abs(lastVersionCheckAlertDate.timeIntervalSinceNow) > alertCadence {
  70. alertContent = Alert.Content(title: LocalizedString("Update Reminder", comment: "Recurring software update alert title"),
  71. body: String(format: LocalizedString("""
  72. A software update is recommended to continue using the %1$@ app.
  73. Go to %2$@ Settings > Software Update to install the latest version.
  74. """, comment: "Alert content body for recurring software update alert"), appName, appName),
  75. acknowledgeActionButtonLabel: LocalizedString("OK", comment: "Default acknowledgement"))
  76. } else {
  77. return
  78. }
  79. let interruptionLevel: LoopKit.Alert.InterruptionLevel = versionUpdate == .required ? .critical : .active
  80. alertIssuer?.issueAlert(Alert(identifier: alertIdentifier, foregroundContent: alertContent, backgroundContent: alertContent, trigger: .immediate, interruptionLevel: interruptionLevel))
  81. recordLastAlertDate()
  82. }
  83. private func noAlertNecessary() {
  84. lastVersionCheckAlertDate = nil
  85. }
  86. private var firstAlert: Bool {
  87. return lastVersionCheckAlertDate == nil
  88. }
  89. private func recordLastAlertDate() {
  90. lastVersionCheckAlertDate = Date()
  91. }
  92. }
  93. struct SupportMenuItem : View {
  94. let mockSupport: MockSupport
  95. @State var showActionSheet: Bool = false
  96. private var buttons: [ActionSheet.Button] {
  97. VersionUpdate.allCases.map { versionUpdate in
  98. let setter = { mockSupport.versionUpdate = versionUpdate }
  99. switch versionUpdate {
  100. case .required:
  101. return ActionSheet.Button.destructive(Text(versionUpdate.localizedDescription), action: setter)
  102. default:
  103. return ActionSheet.Button.default(Text(versionUpdate.localizedDescription), action: setter)
  104. }
  105. } +
  106. [.cancel(Text("Cancel"))]
  107. }
  108. private var actionSheet: ActionSheet {
  109. ActionSheet(title: Text("Version Check Response"), message: Text("How should the simulator respond to a version check?"), buttons: buttons)
  110. }
  111. var body: some View {
  112. Button(action: {
  113. self.showActionSheet.toggle()
  114. }) {
  115. Text("Mock Version Check \(currentVersionUpdate)")
  116. }
  117. .actionSheet(isPresented: $showActionSheet, content: {
  118. self.actionSheet
  119. })
  120. Button(action: { mockSupport.lastVersionCheckAlertDate = nil } ) {
  121. Text("Clear Last Version Check Alert Date")
  122. }
  123. }
  124. var currentVersionUpdate: String {
  125. return mockSupport.versionUpdate.map { "(\($0.rawValue))" } ?? ""
  126. }
  127. }