ViewModifiers.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. {
  74. @Binding var text: String
  75. func body(content: Content) -> some View {
  76. HStack {
  77. content
  78. if !text.isEmpty {
  79. Button { self.text = "" }
  80. label: {
  81. Image(systemName: "delete.left")
  82. .foregroundColor(.gray)
  83. }
  84. }
  85. }
  86. }
  87. }
  88. extension View {
  89. func roundedBackground() -> some View {
  90. modifier(RoundedBackground())
  91. }
  92. func buttonBackground() -> some View {
  93. modifier(RoundedBackground(color: .accentColor))
  94. }
  95. func navigationLink<V: BaseView>(to screen: Screen, from view: V) -> some View {
  96. modifier(Link(destination: view.viewModel.view(for: screen)))
  97. }
  98. func adaptsToSoftwareKeyboard() -> some View {
  99. modifier(AdaptsToSoftwareKeyboard())
  100. }
  101. func modal<V: BaseView>(for screen: Screen?, from view: V) -> some View {
  102. onTapGesture {
  103. view.viewModel.showModal(for: screen)
  104. }
  105. }
  106. func asAny() -> AnyView { .init(self) }
  107. }