ViewModifiers.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import Combine
  2. import SwiftUI
  3. struct RoundedBackground: ViewModifier {
  4. private let color: Color
  5. init(color: Color = Color("CapsuleColor")) {
  6. self.color = color
  7. }
  8. func body(content: Content) -> some View {
  9. content
  10. .padding()
  11. .background(
  12. RoundedRectangle(cornerRadius: 8, style: .continuous)
  13. .fill()
  14. .foregroundColor(color)
  15. )
  16. }
  17. }
  18. struct CapsulaBackground: ViewModifier {
  19. private let color: Color
  20. init(color: Color = Color("CapsuleColor")) {
  21. self.color = color
  22. }
  23. func body(content: Content) -> some View {
  24. content
  25. .padding()
  26. .background(
  27. Capsule()
  28. .fill()
  29. .foregroundColor(color)
  30. )
  31. }
  32. }
  33. private let navigationCache = LRUCache<Screen.ID, AnyView>(capacity: 10)
  34. struct NavigationLazyView: View {
  35. let build: () -> AnyView
  36. let screen: Screen
  37. init(_ build: @autoclosure @escaping () -> AnyView, screen: Screen) {
  38. self.build = build
  39. self.screen = screen
  40. }
  41. var body: AnyView {
  42. if navigationCache[screen.id] == nil {
  43. navigationCache[screen.id] = build()
  44. }
  45. return navigationCache[screen.id]!
  46. .onDisappear {
  47. navigationCache[screen.id] = nil
  48. }.asAny()
  49. }
  50. }
  51. struct Link<T>: ViewModifier where T: View {
  52. private let destination: () -> T
  53. let screen: Screen
  54. init(destination: @autoclosure @escaping () -> T, screen: Screen) {
  55. self.destination = destination
  56. self.screen = screen
  57. }
  58. func body(content: Content) -> some View {
  59. NavigationLink(destination: NavigationLazyView(destination().asAny(), screen: screen)) {
  60. content
  61. }
  62. }
  63. }
  64. struct AdaptsToSoftwareKeyboard: ViewModifier {
  65. @State var currentHeight: CGFloat = 0
  66. func body(content: Content) -> some View {
  67. content
  68. .padding(.bottom, currentHeight).animation(.easeOut(duration: 0.25))
  69. .edgesIgnoringSafeArea(currentHeight == 0 ? Edge.Set() : .bottom)
  70. .onAppear(perform: subscribeToKeyboardChanges)
  71. }
  72. private let keyboardHeightOnOpening = Foundation.NotificationCenter.default
  73. .publisher(for: UIResponder.keyboardWillShowNotification)
  74. .map { $0.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! CGRect }
  75. .map(\.height)
  76. private let keyboardHeightOnHiding = Foundation.NotificationCenter.default
  77. .publisher(for: UIResponder.keyboardWillHideNotification)
  78. .map { _ in CGFloat(0) }
  79. private func subscribeToKeyboardChanges() {
  80. _ = Publishers.Merge(keyboardHeightOnOpening, keyboardHeightOnHiding)
  81. .subscribe(on: DispatchQueue.main)
  82. .sink { height in
  83. if self.currentHeight == 0 || height == 0 {
  84. self.currentHeight = height
  85. }
  86. }
  87. }
  88. }
  89. struct ClearButton: ViewModifier {
  90. @Binding var text: String
  91. func body(content: Content) -> some View {
  92. HStack {
  93. content
  94. if !text.isEmpty {
  95. Button { self.text = "" }
  96. label: {
  97. Image(systemName: "delete.left")
  98. .foregroundColor(.gray)
  99. }
  100. }
  101. }
  102. }
  103. }
  104. extension View {
  105. func roundedBackground() -> some View {
  106. modifier(RoundedBackground())
  107. }
  108. func buttonBackground() -> some View {
  109. modifier(RoundedBackground(color: .accentColor))
  110. }
  111. func navigationLink<V: BaseView>(to screen: Screen, from view: V) -> some View {
  112. modifier(Link(destination: view.viewModel.view(for: screen), screen: screen))
  113. }
  114. func adaptsToSoftwareKeyboard() -> some View {
  115. modifier(AdaptsToSoftwareKeyboard())
  116. }
  117. func modal<V: BaseView>(for screen: Screen?, from view: V) -> some View {
  118. onTapGesture {
  119. view.viewModel.showModal(for: screen)
  120. }
  121. }
  122. func asAny() -> AnyView { .init(self) }
  123. }