29 lines
651 B
Swift
29 lines
651 B
Swift
|
//
|
||
|
// 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()
|
||
|
}
|
||
|
}
|
||
|
}
|