FirstAppear.swift 662 B

12345678910111213141516171819202122232425262728293031
  1. //
  2. // FirstAppear.swift
  3. // Omnipod
  4. //
  5. // Created by Joe Moran on 9/24/23.
  6. // Copyright © 2023 LoopKit Authors. All rights reserved.
  7. //
  8. import SwiftUI
  9. extension View {
  10. func onFirstAppear(_ action: @escaping () -> ()) -> some View {
  11. modifier(FirstAppear(action: action))
  12. }
  13. }
  14. private struct FirstAppear: ViewModifier {
  15. let action: () -> ()
  16. // State used to insure action is invoked here only once
  17. @State private var hasAppeared = false
  18. func body(content: Content) -> some View {
  19. content.onAppear {
  20. guard !hasAppeared else { return }
  21. hasAppeared = true
  22. action()
  23. }
  24. }
  25. }