Vernissage/WidgetsKit/Sources/WidgetsKit/ViewModifiers/FirstAppear.swift

29 lines
654 B
Swift

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