diff --git a/damus/Models/NotificationsModel.swift b/damus/Models/NotificationsModel.swift index bf45e1c7..634109f4 100644 --- a/damus/Models/NotificationsModel.swift +++ b/damus/Models/NotificationsModel.swift @@ -45,7 +45,8 @@ enum NotificationItem { } } -class NotificationsModel: ObservableObject { +class NotificationsModel: ObservableObject, ScrollQueue { + var incoming_zaps: [Zap] var incoming_events: [NostrEvent] var should_queue: Bool @@ -73,6 +74,10 @@ class NotificationsModel: ObservableObject { self.notifications = [] } + func set_should_queue(_ val: Bool) { + self.should_queue = val + } + func uniq_pubkeys() -> [String] { var pks = Set() diff --git a/damus/Util/EventHolder.swift b/damus/Util/EventHolder.swift index 5e3b58d8..58e0a221 100644 --- a/damus/Util/EventHolder.swift +++ b/damus/Util/EventHolder.swift @@ -8,12 +8,16 @@ import Foundation /// Used for holding back events until they're ready to be displayed -class EventHolder: ObservableObject { +class EventHolder: ObservableObject, ScrollQueue { private var has_event: Set @Published var events: [NostrEvent] @Published var incoming: [NostrEvent] var should_queue: Bool + func set_should_queue(_ val: Bool) { + self.should_queue = val + } + var queued: Int { return incoming.count } diff --git a/damus/Views/Notifications/NotificationsView.swift b/damus/Views/Notifications/NotificationsView.swift index 13b1cefe..6d0c03db 100644 --- a/damus/Views/Notifications/NotificationsView.swift +++ b/damus/Views/Notifications/NotificationsView.swift @@ -22,6 +22,12 @@ struct NotificationsView: View { NotificationItemView(state: state, item: item) } } + .background(GeometryReader { proxy -> Color in + DispatchQueue.main.async { + handle_scroll_queue(proxy, queue: self.notifications) + } + return Color.clear + }) .padding(.horizontal) } .onReceive(handle_notify(.scroll_to_top)) { notif in diff --git a/damus/Views/TimelineView.swift b/damus/Views/TimelineView.swift index e2a68452..a549ce3d 100644 --- a/damus/Views/TimelineView.swift +++ b/damus/Views/TimelineView.swift @@ -27,17 +27,6 @@ struct TimelineView: View { MainContent } - func handle_scroll(_ proxy: GeometryProxy) { - let offset = -proxy.frame(in: .named("scroll")).origin.y - guard offset >= 0 else { - return - } - let val = offset > 0 - if self.events.should_queue != val { - self.events.should_queue = val - } - } - var realtime_bar_opacity: Double { colorScheme == .dark ? 0.2 : 0.1 } @@ -55,7 +44,7 @@ struct TimelineView: View { .disabled(loading) .background(GeometryReader { proxy -> Color in DispatchQueue.main.async { - handle_scroll(proxy) + handle_scroll_queue(proxy, queue: self.events) } return Color.clear }) @@ -82,3 +71,18 @@ struct TimelineView_Previews: PreviewProvider { } +protocol ScrollQueue { + var should_queue: Bool { get } + func set_should_queue(_ val: Bool) +} + +func handle_scroll_queue(_ proxy: GeometryProxy, queue: ScrollQueue) { + let offset = -proxy.frame(in: .named("scroll")).origin.y + guard offset >= 0 else { + return + } + let val = offset > 0 + if queue.should_queue != val { + queue.set_should_queue(val) + } +}