Keep a shadow copy of notifications so the streamed notifications can be reconsolidated

This commit is contained in:
Thomas Ricouard 2023-01-27 17:23:41 +01:00
parent bec9ab8792
commit 1dd536c8e7
1 changed files with 10 additions and 4 deletions

View File

@ -23,6 +23,7 @@ class NotificationsViewModel: ObservableObject {
var client: Client? { var client: Client? {
didSet { didSet {
if oldValue != client { if oldValue != client {
notifications = []
consolidatedNotifications = [] consolidatedNotifications = []
} }
} }
@ -32,6 +33,7 @@ class NotificationsViewModel: ObservableObject {
@Published var selectedType: Models.Notification.NotificationType? { @Published var selectedType: Models.Notification.NotificationType? {
didSet { didSet {
if oldValue != selectedType { if oldValue != selectedType {
notifications = []
consolidatedNotifications = [] consolidatedNotifications = []
Task { Task {
await fetchNotifications() await fetchNotifications()
@ -49,6 +51,7 @@ class NotificationsViewModel: ObservableObject {
return nil return nil
} }
private var notifications: [Models.Notification] = []
private var consolidatedNotifications: [ConsolidatedNotification] = [] private var consolidatedNotifications: [ConsolidatedNotification] = []
func fetchNotifications() async { func fetchNotifications() async {
@ -61,6 +64,7 @@ class NotificationsViewModel: ObservableObject {
try await client.get(endpoint: Notifications.notifications(sinceId: nil, try await client.get(endpoint: Notifications.notifications(sinceId: nil,
maxId: nil, maxId: nil,
types: queryTypes)) types: queryTypes))
self.notifications = notifications
consolidatedNotifications = notifications.consolidated() consolidatedNotifications = notifications.consolidated()
nextPageState = notifications.count < 15 ? .none : .hasNextPage nextPageState = notifications.count < 15 ? .none : .hasNextPage
} else if let first = consolidatedNotifications.first { } else if let first = consolidatedNotifications.first {
@ -72,6 +76,7 @@ class NotificationsViewModel: ObservableObject {
newNotifications = newNotifications.filter { notification in newNotifications = newNotifications.filter { notification in
!consolidatedNotifications.contains(where: { $0.id == notification.id }) !consolidatedNotifications.contains(where: { $0.id == notification.id })
} }
self.notifications.append(contentsOf: newNotifications)
consolidatedNotifications.insert(contentsOf: newNotifications.consolidated(), at: 0) consolidatedNotifications.insert(contentsOf: newNotifications.consolidated(), at: 0)
} }
withAnimation { withAnimation {
@ -93,6 +98,7 @@ class NotificationsViewModel: ObservableObject {
maxId: lastId, maxId: lastId,
types: queryTypes)) types: queryTypes))
consolidatedNotifications.append(contentsOf: newNotifications.consolidated()) consolidatedNotifications.append(contentsOf: newNotifications.consolidated())
self.notifications.append(contentsOf: newNotifications)
state = .display(notifications: consolidatedNotifications, nextPageState: newNotifications.count < 15 ? .none : .hasNextPage) state = .display(notifications: consolidatedNotifications, nextPageState: newNotifications.count < 15 ? .none : .hasNextPage)
} catch { } catch {
state = .error(error: error) state = .error(error: error)
@ -111,11 +117,11 @@ class NotificationsViewModel: ObservableObject {
!consolidatedNotifications.contains(where: { $0.id == event.notification.id }) !consolidatedNotifications.contains(where: { $0.id == event.notification.id })
{ {
if let selectedType, event.notification.type == selectedType.rawValue { if let selectedType, event.notification.type == selectedType.rawValue {
consolidatedNotifications.insert(contentsOf: [event.notification].consolidated(), notifications.insert(event.notification, at: 0)
at: 0) consolidatedNotifications = notifications.consolidated()
} else if selectedType == nil { } else if selectedType == nil {
consolidatedNotifications.insert(contentsOf: [event.notification].consolidated(), notifications.insert(event.notification, at: 0)
at: 0) consolidatedNotifications = notifications.consolidated()
} }
state = .display(notifications: consolidatedNotifications, nextPageState: .hasNextPage) state = .display(notifications: consolidatedNotifications, nextPageState: .hasNextPage)
} }