ShortcutsConfigView.swift 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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: "Allows to bolus with shortcuts", table: "ShortcutsDetail"),
  23. isOn: $state.allowBolusByShortcuts
  24. )
  25. Picker(
  26. selection: $state.maxBolusByShortcuts,
  27. label: Text("Method to limit the bolus amount", tableName: "ShortcutsDetail")
  28. ) {
  29. ForEach(BolusShortcutLimit.allCases) { v in
  30. v != .noAllowed ? Text(v.displayName).tag(v) : nil
  31. // Text(v.displayName).tag(v)
  32. }
  33. }
  34. .disabled(!state.allowBolusByShortcuts)
  35. }
  36. }
  37. .onAppear(perform: configureView)
  38. .navigationTitle(String(localized: "Shortcuts config", table: "ShortcutsDetail"))
  39. .navigationBarTitleDisplayMode(.automatic)
  40. }
  41. private func openShortcutsApp() {
  42. let shortcutsURL = URL(string: "shortcuts://")!
  43. if UIApplication.shared.canOpenURL(shortcutsURL) {
  44. UIApplication.shared.open(shortcutsURL, options: [:], completionHandler: { success in
  45. if !success {
  46. state.router.alertMessage
  47. .send(MessageContent(
  48. content: String(localized: "Unable to open the app", table: "ShortcutsDetail"),
  49. type: .warning
  50. ))
  51. }
  52. })
  53. } else {
  54. router.alertMessage
  55. .send(MessageContent(
  56. content: String(localized: "Unable to open the app", table: "ShortcutsDetail"),
  57. type: .warning
  58. ))
  59. }
  60. }
  61. }
  62. }