ShortcutsConfigView.swift 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import Combine
  2. import SwiftUI
  3. import Swinject
  4. import UIKit
  5. extension ShortcutsConfig {
  6. struct RootView: BaseView {
  7. let resolver: Resolver
  8. @StateObject var state = StateModel()
  9. var body: some View {
  10. Form {
  11. Section(header: Text("Shortcuts", tableName: "ShortcutsDetail")) {
  12. Text(
  13. "The application lets you create automations using shortcuts. Go to the Shortcuts application to create new automations.",
  14. tableName: "ShortcutsDetail"
  15. )
  16. Button(String(localized: "Open Shortcuts app", table: "ShortcutsDetail")) {
  17. openShortcutsApp()
  18. }
  19. }
  20. Section(header: Text("Options", tableName: "ShorcutsDetail")) {
  21. Toggle(
  22. String(localized: "Allow bolusing with shortcuts", table: "ShortcutsDetail"),
  23. isOn: $state.allowBolusByShortcuts
  24. )
  25. }
  26. }
  27. .onAppear(perform: configureView)
  28. .navigationTitle(String(localized: "Shortcuts config", table: "ShortcutsDetail"))
  29. .navigationBarTitleDisplayMode(.automatic)
  30. }
  31. private func openShortcutsApp() {
  32. let shortcutsURL = URL(string: "shortcuts://")!
  33. if UIApplication.shared.canOpenURL(shortcutsURL) {
  34. UIApplication.shared.open(shortcutsURL, options: [:], completionHandler: { success in
  35. if !success {
  36. state.router.alertMessage
  37. .send(MessageContent(
  38. content: String(localized: "Unable to open the app", table: "ShortcutsDetail"),
  39. type: .warning
  40. ))
  41. }
  42. })
  43. } else {
  44. router.alertMessage
  45. .send(MessageContent(
  46. content: String(localized: "Unable to open the app", table: "ShortcutsDetail"),
  47. type: .warning
  48. ))
  49. }
  50. }
  51. }
  52. }