damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

VisibilityTracker.swift (1005B)


      1 //
      2 //  VisibilityTracker.swift
      3 //  damus
      4 //
      5 //  Created by Daniel D’Aquino on 2024-03-18.
      6 // 
      7 //  Based on code examples shown in this article: https://medium.com/@jackvanderpump/how-to-detect-is-an-element-is-visible-in-swiftui-9ff58ca72339
      8 
      9 import Foundation
     10 import SwiftUI
     11 
     12 extension View {
     13     func on_visibility_change(perform visibility_change_notifier: @escaping (Bool) -> Void, edge: Alignment = .center) -> some View {
     14         self.modifier(VisibilityTracker(visibility_change_notifier: visibility_change_notifier, edge: edge))
     15     }
     16 }
     17 
     18 struct VisibilityTracker: ViewModifier {
     19     let visibility_change_notifier: (Bool) -> Void
     20     let edge: Alignment
     21 
     22     func body(content: Content) -> some View {
     23     content
     24       .overlay(
     25         LazyVStack {
     26           Color.clear
     27             .onAppear {
     28                 visibility_change_notifier(true)
     29             }
     30             .onDisappear {
     31                 visibility_change_notifier(false)
     32             }
     33         },
     34         alignment: edge)
     35     }
     36 }