From 82c56d2bd1605ac0db7d1bd6116b45a55429cdb7 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Wed, 31 Jul 2019 20:01:00 -0600 Subject: [PATCH] Use MastodonCache for notifications In preparation for using timeline segments for notifications --- Tusker/MastodonCache.swift | 21 +++++++++++++--- .../NotificationsTableViewController.swift | 24 ++++++++++++------- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/Tusker/MastodonCache.swift b/Tusker/MastodonCache.swift index 5a1ba31a..d06a64af 100644 --- a/Tusker/MastodonCache.swift +++ b/Tusker/MastodonCache.swift @@ -11,12 +11,10 @@ import Pachyderm class MastodonCache { -// private static let statuses = NSDictionary() -// private static let accounts = NSDictionary() -// private static let relationships = NSCache() private static var statuses = [String: Status]() private static var accounts = [String: Account]() private static var relationships = [String: Relationship]() + private static var notifications = [String: Pachyderm.Notification]() // MARK: - Statuses static func status(for id: String) -> Status? { @@ -111,4 +109,21 @@ class MastodonCache { relationships.forEach(add) } + // MARK: - Notifications + static func notification(for id: String) -> Pachyderm.Notification? { + return notifications[id] + } + + static func set(notification: Pachyderm.Notification, id: String) { + notifications[id] = notification + } + + static func add(notification: Pachyderm.Notification) { + set(notification: notification, id: notification.id) + } + + static func addAll(notifications: [Pachyderm.Notification]) { + notifications.forEach(add) + } + } diff --git a/Tusker/Screens/Notifications/NotificationsTableViewController.swift b/Tusker/Screens/Notifications/NotificationsTableViewController.swift index 461f7fe6..6127080d 100644 --- a/Tusker/Screens/Notifications/NotificationsTableViewController.swift +++ b/Tusker/Screens/Notifications/NotificationsTableViewController.swift @@ -11,7 +11,7 @@ import Pachyderm class NotificationsTableViewController: EnhancedTableViewController { - var notifications: [Pachyderm.Notification] = [] { + var notificationIDs: [String] = [] { didSet { DispatchQueue.main.async { self.tableView.reloadData() @@ -51,7 +51,8 @@ class NotificationsTableViewController: EnhancedTableViewController { let request = MastodonController.client.getNotifications() MastodonController.client.run(request) { result in guard case let .success(notifications, pagination) = result else { fatalError() } - self.notifications = notifications + self.notificationIDs = notifications.map { $0.id } + MastodonCache.addAll(notifications: notifications) MastodonCache.addAll(statuses: notifications.compactMap { $0.status }) MastodonCache.addAll(accounts: notifications.map { $0.account }) self.newer = pagination?.newer @@ -78,12 +79,12 @@ class NotificationsTableViewController: EnhancedTableViewController { } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { - return notifications.count + return notificationIDs.count } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { - let notification = notifications[indexPath.row] + guard let notification = MastodonCache.notification(for: notificationIDs[indexPath.row]) else { fatalError() } switch notification.kind { case .mention: @@ -107,14 +108,17 @@ class NotificationsTableViewController: EnhancedTableViewController { override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { - if indexPath.row == notifications.count - 1 { + if indexPath.row == notificationIDs.count - 1 { guard let older = older else { return } let request = MastodonController.client.getNotifications(range: older) MastodonController.client.run(request) { result in guard case let .success(newNotifications, pagination) = result else { fatalError() } self.older = pagination?.older - self.notifications.append(contentsOf: newNotifications) + MastodonCache.addAll(notifications: newNotifications) + MastodonCache.addAll(statuses: newNotifications.compactMap { $0.status }) + MastodonCache.addAll(accounts: newNotifications.map { $0.account }) + self.notificationIDs.append(contentsOf: newNotifications.map { $0.id }) } } } @@ -138,8 +142,10 @@ class NotificationsTableViewController: EnhancedTableViewController { MastodonController.client.run(request) { result in guard case let .success(newNotifications, pagination) = result else { fatalError() } self.newer = pagination?.newer + MastodonCache.addAll(notifications: newNotifications) MastodonCache.addAll(statuses: newNotifications.compactMap { $0.status }) - self.notifications.insert(contentsOf: newNotifications, at: 0) + MastodonCache.addAll(accounts: newNotifications.map { $0.account }) + self.notificationIDs.insert(contentsOf: newNotifications.map { $0.id }, at: 0) DispatchQueue.main.async { self.refreshControl?.endRefreshing() @@ -156,14 +162,14 @@ extension NotificationsTableViewController: StatusTableViewCellDelegate {} extension NotificationsTableViewController: UITableViewDataSourcePrefetching { func tableView(_ tableView: UITableView, prefetchRowsAt indexPaths: [IndexPath]) { for indexPath in indexPaths { - let notification = notifications[indexPath.row] + guard let notification = MastodonCache.notification(for: notificationIDs[indexPath.row]) else { continue } ImageCache.avatars.get(notification.account.avatar, completion: nil) } } func tableView(_ tableView: UITableView, cancelPrefetchingForRowsAt indexPaths: [IndexPath]) { for indexPath in indexPaths { - let notification = notifications[indexPath.row] + guard let notification = MastodonCache.notification(for: notificationIDs[indexPath.row]) else { continue } ImageCache.avatars.cancel(notification.account.url) } }