Last active
June 29, 2021 11:18
-
-
Save mattyoung/c63ea82495b3ad2d418a3997cc6509b5 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