ViewModifiers.swift 3.7 KB

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