ViewModifiers.swift 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 = NotificationCenter.default
  56. .publisher(for: UIResponder.keyboardWillShowNotification)
  57. .map { $0.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! CGRect }
  58. .map(\.height)
  59. private let keyboardHeightOnHiding = 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. extension View {
  73. func roundedBackground() -> some View {
  74. modifier(RoundedBackground())
  75. }
  76. func buttonBackground() -> some View {
  77. modifier(RoundedBackground(color: .accentColor))
  78. }
  79. func navigationLink<V: BaseView>(to screen: Screen, from view: V) -> some View {
  80. modifier(Link(destination: view.viewModel.view(for: screen)))
  81. }
  82. func adaptsToSoftwareKeyboard() -> some View {
  83. modifier(AdaptsToSoftwareKeyboard())
  84. }
  85. func modal<V: BaseView>(for screen: Screen?, from view: V) -> some View {
  86. onTapGesture {
  87. view.viewModel.showModal(for: screen)
  88. }
  89. }
  90. func asAny() -> AnyView { .init(self) }
  91. }