another.im-ios/ConversationsClassic/View/UIToolkit/View+OnLoad.swift

28 lines
566 B
Swift
Raw Normal View History

2024-06-19 15:15:27 +00:00
import SwiftUI
// MARK: - On load
extension View {
func onLoad(_ action: @escaping () -> Void) -> some View {
modifier(ViewDidLoadModifier(action))
}
}
private struct ViewDidLoadModifier: ViewModifier {
private let action: () -> Void
@State private var didLoad = false
init(_ action: @escaping () -> Void) {
self.action = action
}
func body(content: Content) -> some View {
content.onAppear {
if !didLoad {
didLoad.toggle()
action()
}
}
}
}