// // DeferredViewModifier.swift // PadelClub // // Created by Razmig Sarkissian on 02/03/2024. // import SwiftUI /// Defers the rendering of a view for the given period. /// /// For example: /// /// ```swift /// Text("Hello, world!") /// .deferredRendering(for: .seconds(5)) /// ``` /// /// will not display the text "Hello, world!" until five seconds after the /// view is initially rendered. If the view is destroyed within the delay, /// it never renders. /// /// This is based on code xwritten by Yonat and Charlton Provatas on /// Stack Overflow, see https://stackoverflow.com/a/74765430/1558022 /// private struct DeferredViewModifier: ViewModifier { let delay: DispatchTimeInterval func body(content: Content) -> some View { _content(content) .onAppear { DispatchQueue.main.asyncAfter(deadline: .now() + delay) { self.shouldHide = true } } } @ViewBuilder private func _content(_ content: Content) -> some View { if shouldHide == false { content } else { content.hidden() } } @State private var shouldHide = false } extension View { func toastFormatted() -> some View { self .font(.title3) .foregroundStyle(.black) .frame(height: 28) .padding() .background { RoundedRectangle(cornerRadius: 20, style: .continuous) .fill(.white) } .shadow(radius: 2) .offset(y: -64) } func deferredRendering(for delay: DispatchTimeInterval) -> some View { modifier(DeferredViewModifier(delay: delay)) } }