ViewModifiers.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. ZStack {
  60. NavigationLink(destination: NavigationLazyView(destination().asAny(), screen: screen)) {
  61. EmptyView()
  62. }.hidden()
  63. content
  64. }
  65. }
  66. }
  67. struct AdaptsToSoftwareKeyboard: ViewModifier {
  68. @State var currentHeight: CGFloat = 0
  69. func body(content: Content) -> some View {
  70. content
  71. .padding(.bottom, currentHeight).animation(.easeOut(duration: 0.25))
  72. .edgesIgnoringSafeArea(currentHeight == 0 ? Edge.Set() : .bottom)
  73. .onAppear(perform: subscribeToKeyboardChanges)
  74. }
  75. private let keyboardHeightOnOpening = Foundation.NotificationCenter.default
  76. .publisher(for: UIResponder.keyboardWillShowNotification)
  77. .map { $0.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! CGRect }
  78. .map(\.height)
  79. private let keyboardHeightOnHiding = Foundation.NotificationCenter.default
  80. .publisher(for: UIResponder.keyboardWillHideNotification)
  81. .map { _ in CGFloat(0) }
  82. private func subscribeToKeyboardChanges() {
  83. _ = Publishers.Merge(keyboardHeightOnOpening, keyboardHeightOnHiding)
  84. .subscribe(on: DispatchQueue.main)
  85. .sink { height in
  86. if self.currentHeight == 0 || height == 0 {
  87. self.currentHeight = height
  88. }
  89. }
  90. }
  91. }
  92. struct ClearButton: ViewModifier {
  93. @Binding var text: String
  94. func body(content: Content) -> some View {
  95. HStack {
  96. content
  97. if !text.isEmpty {
  98. Button { self.text = "" }
  99. label: {
  100. Image(systemName: "delete.left")
  101. .foregroundColor(.gray)
  102. }
  103. }
  104. }
  105. }
  106. }
  107. struct ChevronCell: ViewModifier {
  108. func body(content: Content) -> some View {
  109. HStack {
  110. content
  111. Spacer()
  112. Image(systemName: "chevron.forward").foregroundColor(.secondary)
  113. }.contentShape(Rectangle())
  114. }
  115. }
  116. extension View {
  117. func roundedBackground() -> some View {
  118. modifier(RoundedBackground())
  119. }
  120. func buttonBackground() -> some View {
  121. modifier(RoundedBackground(color: .accentColor))
  122. }
  123. func navigationLink<V: BaseView>(to screen: Screen, from view: V) -> some View {
  124. modifier(Link(destination: view.viewModel.view(for: screen), screen: screen))
  125. }
  126. func adaptsToSoftwareKeyboard() -> some View {
  127. modifier(AdaptsToSoftwareKeyboard())
  128. }
  129. func modal<V: BaseView>(for screen: Screen?, from view: V) -> some View {
  130. onTapGesture {
  131. view.viewModel.showModal(for: screen)
  132. }
  133. }
  134. func asAny() -> AnyView { .init(self) }
  135. func chevronCell() -> some View {
  136. modifier(ChevronCell())
  137. }
  138. }