VersionUpdate.swift 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. //
  2. // VersionUpdate.swift
  3. // LoopKit
  4. //
  5. // Created by Rick Pasetto on 9/8/21.
  6. // Copyright © 2021 LoopKit Authors. All rights reserved.
  7. //
  8. import Foundation
  9. // Note: order is important for VersionUpdate. Later version updates are more critical than earlier ones. Do not reorder!
  10. public enum VersionUpdate: Comparable, CaseIterable {
  11. /// No version update needed.
  12. case none
  13. /// A update is available, but it is just informational (supported update).
  14. case available
  15. /// The version is unsupported; the app needs to be updated to the latest "supported" version. Not a critical update.
  16. case recommended
  17. /// The app must be updated immediately.
  18. case required
  19. }
  20. extension VersionUpdate: RawRepresentable {
  21. public typealias RawValue = String
  22. public init?(rawValue: RawValue) {
  23. switch rawValue {
  24. case "none": self = .none
  25. case "available": self = .available
  26. case "recommended": self = .recommended
  27. case "required": self = .required
  28. default: return nil
  29. }
  30. }
  31. public var rawValue: RawValue {
  32. switch self {
  33. case .none: return "none"
  34. case .available: return "available"
  35. case .recommended: return "recommended"
  36. case .required: return "required"
  37. }
  38. }
  39. }
  40. extension VersionUpdate {
  41. public static let `default` = VersionUpdate.none
  42. public var softwareUpdateAvailable: Bool { self > .none }
  43. }
  44. extension VersionUpdate {
  45. public var localizedDescription: String {
  46. switch self {
  47. case .none:
  48. return LocalizedString("No Update", comment: "Description of no software update needed")
  49. case .available:
  50. return LocalizedString("Update Available", comment: "Description of informational software update needed")
  51. case .recommended:
  52. return LocalizedString("Recommended Update", comment: "Description of supported software update needed")
  53. case .required:
  54. return LocalizedString("Critical Update", comment: "Description of critical software update needed")
  55. }
  56. }
  57. }
  58. public extension Notification.Name {
  59. static let SoftwareUpdateAvailable = Notification.Name(rawValue: "com.loopkit.Loop.SoftwareUpdateAvailable")
  60. }