Skip to content

Commit

Permalink
Fix notification crash
Browse files Browse the repository at this point in the history
  • Loading branch information
gh123man committed Jun 27, 2023
1 parent 30262b5 commit 0b808ac
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
14 changes: 14 additions & 0 deletions Examples/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@ struct ScrollViewLoaderTestApp: App {

struct ContentView: View {


var body: some View {
NavigationView {
NavigationLink(destination: DetailsSearch()) {
Text("Go to details")
.padding()
}
}


}
}

struct DetailsSearch: View {
@State var data: [Int] = Array(0..<1)

var body: some View {
Expand Down
9 changes: 7 additions & 2 deletions Sources/ScrollViewLoader/ScrollViewLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ struct DelegateHolder<Content: View>: View {
content
.introspectScrollView { scrollView in
scrollView.delegate = offsetNotifier
scrollView.addObserver(offsetNotifier, forKeyPath: "contentSize", context: nil)
offsetNotifier.scrollView = scrollView
offsetNotifier.scrollViewDidScroll(scrollView)
}
}
Expand All @@ -38,14 +38,19 @@ class ScrollOffsetNotifier: NSObject, UIScrollViewDelegate, ObservableObject {
private var canNotify = true
private var trigger: OffsetTrigger
private var oldContentHeight: Double = 0
weak var scrollView: UIScrollView? {
didSet {
scrollView?.addObserver(self, forKeyPath: "contentSize", context: nil)
}
}

init(offsetTrigger: OffsetTrigger, onNotify: @escaping () async -> ()) {
self.trigger = offsetTrigger
self.onNotify = onNotify
}

deinit {
removeObserver(self, forKeyPath: "contentSize")
scrollView?.removeObserver(self, forKeyPath: "contentSize")
}

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) {
Expand Down

0 comments on commit 0b808ac

Please sign in to comment.