MainStateModel.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import LoopKitUI
  2. import SwiftMessages
  3. import SwiftUI
  4. import Swinject
  5. extension Main {
  6. final class StateModel: BaseStateModel<Provider> {
  7. private(set) var modal: Modal?
  8. @Published var isModalPresented = false
  9. @Published var isSecondaryModalPresented = false
  10. @Published var secondaryModalView: AnyView? = nil
  11. override func subscribe() {
  12. router.mainModalScreen
  13. .map { $0?.modal(resolver: self.resolver!) }
  14. .removeDuplicates { $0?.id == $1?.id }
  15. .receive(on: DispatchQueue.main)
  16. .sink { modal in
  17. self.modal = modal
  18. self.isModalPresented = modal != nil
  19. }
  20. .store(in: &lifetime)
  21. $isModalPresented
  22. .filter { !$0 }
  23. .sink { _ in
  24. self.router.mainModalScreen.send(nil)
  25. }
  26. .store(in: &lifetime)
  27. router.alertMessage
  28. .receive(on: DispatchQueue.main)
  29. .sink { message in
  30. var config = SwiftMessages.defaultConfig
  31. let view = MessageView.viewFromNib(layout: .cardView)
  32. let titleContent: String
  33. view.configureContent(
  34. title: "title",
  35. body: NSLocalizedString(message.content, comment: "Info message"),
  36. iconImage: nil,
  37. iconText: nil,
  38. buttonImage: nil,
  39. buttonTitle: nil,
  40. buttonTapHandler: nil
  41. )
  42. switch message.type {
  43. case .info:
  44. view.backgroundColor = .secondarySystemGroupedBackground
  45. config.duration = .automatic
  46. titleContent = NSLocalizedString("Info", comment: "Info title")
  47. case .warning:
  48. view.configureTheme(.warning, iconStyle: .subtle)
  49. config.duration = .forever
  50. view.button?.setImage(Icon.warningSubtle.image, for: .normal)
  51. titleContent = NSLocalizedString("Warning", comment: "Warning title")
  52. view.buttonTapHandler = { _ in
  53. SwiftMessages.hide()
  54. }
  55. case .errorPump:
  56. view.configureTheme(.error, iconStyle: .subtle)
  57. config.duration = .forever
  58. view.button?.setImage(Icon.errorSubtle.image, for: .normal)
  59. titleContent = NSLocalizedString("Error", comment: "Error title")
  60. view.buttonTapHandler = { _ in
  61. SwiftMessages.hide()
  62. // display the pump configuration immediatly
  63. if let pump = self.provider.deviceManager.pumpManager,
  64. let bluetooth = self.provider.bluetoothProvider
  65. {
  66. let view = PumpConfig.PumpSettingsView(
  67. pumpManager: pump,
  68. bluetoothManager: bluetooth,
  69. completionDelegate: self
  70. ).asAny()
  71. self.router.mainSecondaryModalView.send(view)
  72. }
  73. }
  74. }
  75. view.titleLabel?.text = titleContent
  76. config.dimMode = .gray(interactive: true)
  77. SwiftMessages.show(config: config, view: view)
  78. }
  79. .store(in: &lifetime)
  80. router.mainSecondaryModalView
  81. .receive(on: DispatchQueue.main)
  82. .sink { view in
  83. self.secondaryModalView = view
  84. self.isSecondaryModalPresented = view != nil
  85. }
  86. .store(in: &lifetime)
  87. $isSecondaryModalPresented
  88. .removeDuplicates()
  89. .filter { !$0 }
  90. .sink { _ in
  91. self.router.mainSecondaryModalView.send(nil)
  92. }
  93. .store(in: &lifetime)
  94. }
  95. }
  96. }
  97. extension Main.StateModel: CompletionDelegate {
  98. func completionNotifyingDidComplete(_: CompletionNotifying) {
  99. // close the window
  100. router.mainSecondaryModalView.send(nil)
  101. }
  102. }