Service.swift 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. //
  2. // Service.swift
  3. // LoopKit
  4. //
  5. // Created by Darin Krauss on 5/17/19.
  6. // Copyright © 2019 LoopKit Authors. All rights reserved.
  7. //
  8. public protocol ServiceDelegate: AnyObject {
  9. /// Informs the delegate that the state of the specified service was updated and the
  10. /// delegate should persist the service.
  11. ///
  12. /// - Parameters:
  13. /// - service: The service whose state was updated.
  14. func serviceDidUpdateState(_ service: Service)
  15. /// Informs the delegate that the service has new settings that should be saved
  16. /// to Loop
  17. ///
  18. /// - Parameters:
  19. /// - settings: The settings object containing the new settings.
  20. func serviceHasNewTherapySettings(_ settings: TherapySettings)
  21. }
  22. public protocol Service: AnyObject {
  23. typealias RawStateValue = [String: Any]
  24. /// The unique identifier of this type of service.
  25. static var serviceIdentifier: String { get }
  26. /// The localized title of this type of service.
  27. static var localizedTitle: String { get }
  28. /// The delegate to notify of service updates.
  29. var serviceDelegate: ServiceDelegate? { get set }
  30. /// Initializes the service with the previously-serialized state.
  31. ///
  32. /// - Parameters:
  33. /// - rawState: The previously-serialized state of the service.
  34. init?(rawState: RawStateValue)
  35. /// The current, serializable state of the service.
  36. var rawState: RawStateValue { get }
  37. }
  38. public extension Service {
  39. var serviceIdentifier: String { return type(of: self).serviceIdentifier }
  40. var localizedTitle: String { return type(of: self).localizedTitle }
  41. }