View+Extension.swift 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //
  2. // View+Extension.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 29/12/22.
  6. // Copyright © 2022 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import SwiftUI
  24. extension View {
  25. func complexModifier<V: View>(@ViewBuilder _ closure: (Self) -> V) -> some View {
  26. closure(self)
  27. }
  28. /// Use this on preview views that are used in snapshot testing. It prevents the snapashot library from complaining that the view has a size of 0
  29. /// Check: https://github.com/pointfreeco/swift-snapshot-testing/issues/738
  30. func frameForPreview() -> some View {
  31. return frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
  32. }
  33. func hiddenConditionally(isHidden: Bool) -> some View {
  34. isHidden ? AnyView(self.hidden()) : AnyView(self)
  35. }
  36. /// Applies the given transform if the given condition evaluates to `true`.
  37. /// - Parameters:
  38. /// - condition: The condition to evaluate.
  39. /// - transform: The transform to apply to the source `View`.
  40. /// - Returns: Either the original `View` or the modified `View` if the condition is `true`.
  41. @ViewBuilder func `if`<Content: View>(_ condition: Bool, transform: (Self) -> Content) -> some View {
  42. if condition {
  43. transform(self)
  44. } else {
  45. self
  46. }
  47. }
  48. func onFirstAppear(perform action: @escaping () -> Void) -> some View {
  49. modifier(ViewFirstAppearModifier(perform: action))
  50. }
  51. }
  52. struct ViewFirstAppearModifier: ViewModifier {
  53. @State private var didAppearBefore = false
  54. private let action: () -> Void
  55. init(perform action: @escaping () -> Void) {
  56. self.action = action
  57. }
  58. func body(content: Content) -> some View {
  59. content.onAppear {
  60. guard !didAppearBefore else { return }
  61. didAppearBefore = true
  62. action()
  63. }
  64. }
  65. }
  66. var isRunningForPreviews: Bool {
  67. return ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1"
  68. }