Service.swift 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 delegate should persist the service. May
  10. /// be invoked prior to the service completing setup.
  11. ///
  12. /// - Parameters:
  13. /// - service: The service that updated state.
  14. func serviceDidUpdateState(_ service: Service)
  15. /// Informs the delegate that the service wants deletion.
  16. ///
  17. /// - Parameters:
  18. /// - service: The service that wants deletion.
  19. func serviceWantsDeletion(_ service: Service)
  20. }
  21. public protocol Service: AnyObject {
  22. typealias RawStateValue = [String: Any]
  23. /// The unique identifier of this type of service.
  24. static var serviceIdentifier: String { get }
  25. /// The localized title of this type of service.
  26. static var localizedTitle: String { get }
  27. /// The delegate to notify of service updates.
  28. var serviceDelegate: ServiceDelegate? { get set }
  29. /// Initializes the service with the previously-serialized state.
  30. ///
  31. /// - Parameters:
  32. /// - rawState: The previously-serialized state of the service.
  33. init?(rawState: RawStateValue)
  34. /// The current, serializable state of the service.
  35. var rawState: RawStateValue { get }
  36. /// Is the service onboarded and ready for use?
  37. var isOnboarded: Bool { get }
  38. }
  39. public extension Service {
  40. var serviceIdentifier: String { return type(of: self).serviceIdentifier }
  41. var localizedTitle: String { return type(of: self).localizedTitle }
  42. }