-
-
Save chrismays/90a1984dfaecc8b409d1c099348b286c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private struct OnFirstAppear: ViewModifier { | |
let perform: () -> Void | |
let `else`: () -> Void | |
@State private var firstTime = true | |
func body(content: Content) -> some View { | |
content.onAppear { | |
if firstTime { | |
firstTime = false | |
perform() | |
} else { | |
`else`() | |
} | |
} | |
} | |
} | |
private struct OnNotFirstAppear: ViewModifier { | |
let perform: () -> Void | |
@State private var firstTime = true | |
func body(content: Content) -> some View { | |
content.onAppear { | |
if firstTime { | |
firstTime = false | |
} else { | |
perform() | |
} | |
} | |
} | |
} | |
extension View { | |
func onFirstAppear(perform: @escaping () -> Void, else: @escaping () -> Void = { }) -> some View { | |
modifier(OnFirstAppear(perform: perform, else: `else`)) | |
} | |
func onNotFirstAppear(perform: @escaping () -> Void) -> some View { | |
modifier(OnNotFirstAppear(perform: perform)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment