Impressia/Vernissage/ViewModifiers/FirstAppear.swift

29 lines
651 B
Swift
Raw Normal View History

//
// https://mczachurski.dev
// Copyright © 2023 Marcin Czachurski and the repository contributors.
// Licensed under the MIT License.
//
import Foundation
import SwiftUI
public extension View {
func onFirstAppear(_ action: @escaping () async -> ()) -> some View {
modifier(FirstAppear(action: action))
}
}
private struct FirstAppear: ViewModifier {
let action: () async -> ()
@State private var hasAppeared = false
func body(content: Content) -> some View {
content.task {
guard !hasAppeared else { return }
hasAppeared = true
await action()
}
}
}