ListStateIntent.swift 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. 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 Trio"
  10. )
  11. static var parameterSummary: some ParameterSummary {
  12. Summary("List all states of Trio")
  13. }
  14. @MainActor func perform() async throws -> some ReturnsValue<StateResults> & ShowsSnippetView {
  15. let context = CoreDataStack.shared.persistentContainer.viewContext
  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. let iob_text = String(format: "%.2f", iob_cob.iob)
  30. let cob_text = String(format: "%.2f", iob_cob.cob)
  31. return .result(
  32. value: BG,
  33. view: ListStateView(state: BG)
  34. )
  35. }
  36. }