ViewModifiers.swift 4.0 KB

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