Use MastodonCache for notifications

In preparation for using timeline segments for notifications
This commit is contained in:
Shadowfacts 2019-07-31 20:01:00 -06:00
parent d9b21a0196
commit 82c56d2bd1
Signed by untrusted user: shadowfacts
GPG Key ID: 94A5AB95422746E5
2 changed files with 33 additions and 12 deletions

View File

@ -11,12 +11,10 @@ import Pachyderm
class MastodonCache {
// private static let statuses = NSDictionary<NSString, Status>()
// private static let accounts = NSDictionary<NSString, Account>()
// private static let relationships = NSCache<NSString, Relationship>()
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)
}
}

View File

@ -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)
}
}