2023-01-31 12:20:49 +01:00
|
|
|
//
|
|
|
|
// https://mczachurski.dev
|
|
|
|
// Copyright © 2023 Marcin Czachurski and the repository contributors.
|
2023-03-28 10:35:38 +02:00
|
|
|
// Licensed under the Apache License 2.0.
|
2023-01-31 12:20:49 +01:00
|
|
|
//
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-01-31 12:20:49 +01:00
|
|
|
import Foundation
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
public extension View {
|
2023-04-01 12:10:59 +02:00
|
|
|
func onFirstAppear(_ action: @escaping () async -> Void) -> some View {
|
2023-01-31 12:20:49 +01:00
|
|
|
modifier(FirstAppear(action: action))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private struct FirstAppear: ViewModifier {
|
2023-04-01 12:10:59 +02:00
|
|
|
let action: () async -> Void
|
2023-01-31 12:20:49 +01:00
|
|
|
@State private var hasAppeared = false
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-01-31 12:20:49 +01:00
|
|
|
func body(content: Content) -> some View {
|
|
|
|
content.task {
|
|
|
|
guard !hasAppeared else { return }
|
|
|
|
hasAppeared = true
|
|
|
|
|
|
|
|
await action()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|