ListStateIntent.swift 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import AppIntents
  2. import Foundation
  3. @available(iOS 16.0, *) struct ListStateIntent: AppIntent {
  4. // Title of the action in the Shortcuts app
  5. static var title: LocalizedStringResource = "List last state available with Trio"
  6. // Description of the action in the Shortcuts app
  7. static var description = IntentDescription(
  8. "Allow to list the last Blood Glucose, trends, IOB and COB available in Trio"
  9. )
  10. static var parameterSummary: some ParameterSummary {
  11. Summary("List all states of Trio")
  12. }
  13. @MainActor func perform() async throws -> some ReturnsValue<StateResults> & ShowsSnippetView {
  14. let context = CoreDataStack.shared.persistentContainer.viewContext
  15. let stateIntent = StateIntentRequest()
  16. let glucoseValues = try? stateIntent.getLastGlucose(onContext: context)
  17. let iob_cob_value = try? stateIntent.getIobAndCob(onContext: context)
  18. guard let glucoseValue = glucoseValues else { throw StateIntentError.NoBG }
  19. guard let iob_cob = iob_cob_value else { throw StateIntentError.NoIOBCOB }
  20. let BG = StateResults(
  21. glucose: glucoseValue.glucose,
  22. trend: glucoseValue.trend,
  23. delta: glucoseValue.delta,
  24. date: glucoseValue.dateGlucose,
  25. iob: iob_cob.iob,
  26. cob: iob_cob.cob,
  27. unit: stateIntent.settingsManager.settings.units
  28. )
  29. return .result(
  30. value: BG,
  31. view: ListStateView(state: BG)
  32. )
  33. }
  34. }