MockSupport.swift 6.0 KB

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