SupportUI.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //
  2. // SupportUI.swift
  3. // LoopKitUI
  4. //
  5. // Created by Darin Krauss on 12/10/20.
  6. // Copyright © 2020 LoopKit Authors. All rights reserved.
  7. //
  8. import SwiftUI
  9. import HealthKit
  10. import LoopKit
  11. public protocol SupportInfoProvider {
  12. var pumpStatus: PumpManagerStatus? { get }
  13. var cgmStatus: CGMManagerStatus? { get }
  14. var localizedAppNameAndVersion: String { get }
  15. func generateIssueReport(completion: @escaping (String) -> Void)
  16. }
  17. public struct LoopScenario: Hashable {
  18. public let name: String
  19. public let url: URL
  20. public init(name: String, url: URL) {
  21. self.name = name
  22. self.url = url
  23. }
  24. }
  25. public enum SettingsMenuSection: Equatable {
  26. case configuration
  27. case support
  28. case custom(localizedTitle: String)
  29. public var customLocalizedTitle: String? {
  30. switch self {
  31. case .custom(let title):
  32. return title
  33. default:
  34. return nil
  35. }
  36. }
  37. }
  38. public struct CustomMenuItem {
  39. public let section: SettingsMenuSection
  40. public let view: AnyView
  41. public init(section: SettingsMenuSection, view: AnyView) {
  42. self.section = section
  43. self.view = view
  44. }
  45. }
  46. public protocol SupportUIDelegate: AlertIssuer, SupportInfoProvider {
  47. func openURL(url: URL)
  48. }
  49. public struct DeviceWhitelist: Hashable {
  50. public let cgmDevices: [String]
  51. public let pumpDevices: [String]
  52. public init(cgmDevices: [String] = [], pumpDevices: [String] = []) {
  53. self.cgmDevices = cgmDevices
  54. self.pumpDevices = pumpDevices
  55. }
  56. }
  57. public protocol SupportUI: Pluggable {
  58. typealias RawStateValue = [String: Any]
  59. /// Provides configuration menu items.
  60. ///
  61. /// - Returns: An array of views that will be added to the configuration section of settings.
  62. func configurationMenuItems() -> [CustomMenuItem]
  63. ///
  64. /// Check whether the given app version for the given `bundleIdentifier` needs an update. Services should return their last result, if known.
  65. /// If version cannot be checked or checking version fails, nil should be returned.
  66. ///
  67. /// - Parameters:
  68. /// - bundleIdentifier: The host app's `bundleIdentifier` (a.k.a. `CFBundleIdentifier`) string.
  69. /// - currentVersion: The host app's current version (i.e. `CFBundleVersion`).
  70. /// - returns: A VersionUpdate object describing the update status
  71. func checkVersion(bundleIdentifier: String, currentVersion: String) async -> VersionUpdate?
  72. /// Provides screen for software update UI.
  73. ///
  74. /// - Parameters:
  75. /// - bundleIdentifier: The host app's bundle identifier (e.g. `Bundle.main.bundleIdentifier`).
  76. /// - currentVersion: The host app's current version (i.e. `CFBundleVersion`).
  77. /// - guidanceColors: Colors to use for warnings, etc.
  78. /// - openAppStore: Function to open up the App Store for the host app.
  79. /// - Returns: A view that will be opened when a software update is available from this service.
  80. func softwareUpdateView(bundleIdentifier: String,
  81. currentVersion: String,
  82. guidanceColors: GuidanceColors,
  83. openAppStore: (() -> Void)?
  84. ) -> AnyView?
  85. /// Get the scenario(s) from the provided URL(s)
  86. ///
  87. /// - Parameters:
  88. /// - scenarioURLs: the URL(s) of the scenario(s) to get
  89. /// - Returns: The scenario(s) matching the provided scenarioURLs
  90. func getScenarios(from scenarioURLs: [URL]) -> [LoopScenario]
  91. /// Called right before Loop resets UserDefaults and Documents storage
  92. /// Use this to store any temp values that need to be restored after a reset occurs
  93. func loopWillReset()
  94. /// Called right after Loop resets UserDefaults and Documents storage
  95. /// Use this to restore any values that were cached before a reset occurred
  96. func loopDidReset()
  97. /// Initializes the support with the previously-serialized state.
  98. ///
  99. /// - Parameters:
  100. /// - rawState: The previously-serialized state of the support.
  101. init?(rawState: RawStateValue)
  102. /// The current, serializable state of the support.
  103. var rawState: RawStateValue { get }
  104. /// A delegate for SupportUI to use (see `SupportUIDelegate`).
  105. var delegate: SupportUIDelegate? { get set }
  106. var showsDeleteTestDataUI: Bool { get }
  107. var deviceIdentifierWhitelist: DeviceWhitelist { get }
  108. }
  109. extension SupportUI {
  110. public var deviceIdentifierWhitelist: DeviceWhitelist {
  111. DeviceWhitelist()
  112. }
  113. public var showsDeleteTestDataUI: Bool {
  114. true
  115. }
  116. }