ListStateIntent.swift 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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 Open-iAPS"
  6. var stateIntent = StateIntentRequest()
  7. // Description of the action in the Shortcuts app
  8. static var description = IntentDescription(
  9. "Allow to list the last Blood Glucose, trends, IOB and COB available in Open-iAPS"
  10. )
  11. static var parameterSummary: some ParameterSummary {
  12. Summary("List all states of Open-iAPS")
  13. }
  14. @MainActor func perform() async throws -> some ReturnsValue<StateiAPSResults> & ShowsSnippetView {
  15. let glucoseValues = try? stateIntent.getLastBG()
  16. let iob_cob_value = try? stateIntent.getIOB_COB()
  17. guard let glucoseValue = glucoseValues else { throw StateIntentError.NoBG }
  18. guard let iob_cob = iob_cob_value else { throw StateIntentError.NoIOBCOB }
  19. let BG = StateiAPSResults(
  20. glucose: glucoseValue.glucose,
  21. trend: glucoseValue.trend,
  22. delta: glucoseValue.delta,
  23. date: glucoseValue.dateGlucose,
  24. iob: iob_cob.iob,
  25. cob: iob_cob.cob,
  26. unit: stateIntent.settingsManager.settings.units
  27. )
  28. let iob_text = String(format: "%.2f", iob_cob.iob)
  29. let cob_text = String(format: "%.2f", iob_cob.cob)
  30. return .result(
  31. value: BG,
  32. view: ListStateView(state: BG)
  33. )
  34. }
  35. }