commit 4b5c2172133f0dfe44226131a09cbab16b30c2a7
parent 75fd8de456e427444db5c6369491afbaa758229f
Author: William Casarin <jb55@jb55.com>
Date: Sun, 26 Feb 2023 14:14:25 -0800
Add scroll queue detection in notification view
This will stop injecting events into the timeline if you're scrolling
Diffstat:
4 files changed, 33 insertions(+), 14 deletions(-)
diff --git 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<String>()
diff --git 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<String>
@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
@@ -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
@@ -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)
+ }
+}