From e971c7f61c3677a6844b5a4c3095cc678f709bb4 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Tue, 18 Sep 2018 12:59:07 -0400 Subject: [PATCH] Cache accounts --- Pachyderm/Model/Account.swift | 4 +- Tusker.xcodeproj/project.pbxproj | 8 +- .../UIViewController+Delegates.swift | 8 +- Tusker/MastodonCache.swift | 79 +++++++++++++++++++ .../Compose/ComposeViewController.swift | 4 +- .../ConversationViewController.swift | 6 +- .../NotificationsTableViewController.swift | 4 +- .../Profile/ProfileTableViewController.swift | 24 +++--- .../TimelineTableViewController.swift | 6 +- Tusker/StatusCache.swift | 44 ----------- .../ActionNotificationTableViewCell.swift | 8 +- .../FollowNotificationTableViewCell.swift | 9 ++- .../ProfileHeaderTableViewCell.swift | 9 ++- .../ConversationMainStatusTableViewCell.swift | 16 ++-- Tusker/Views/Status/StatusTableViewCell.swift | 36 +++++---- Tusker/Views/StatusContentLabel.swift | 2 +- 16 files changed, 158 insertions(+), 109 deletions(-) create mode 100644 Tusker/MastodonCache.swift delete mode 100644 Tusker/StatusCache.swift diff --git a/Pachyderm/Model/Account.swift b/Pachyderm/Model/Account.swift index 7c7338b9..7acd7a88 100644 --- a/Pachyderm/Model/Account.swift +++ b/Pachyderm/Model/Account.swift @@ -53,8 +53,8 @@ public class Account: Decodable { return request } - public static func getStatuses(_ account: Account, range: RequestRange = .default, onlyMedia: Bool? = nil, pinned: Bool? = nil, excludeReplies: Bool? = nil) -> Request<[Status]> { - var request = Request<[Status]>(method: .get, path: "/api/v1/accounts/\(account.id)/statuses", queryParameters: [ + public static func getStatuses(_ accountID: String, range: RequestRange = .default, onlyMedia: Bool? = nil, pinned: Bool? = nil, excludeReplies: Bool? = nil) -> Request<[Status]> { + var request = Request<[Status]>(method: .get, path: "/api/v1/accounts/\(accountID)/statuses", queryParameters: [ "only_media" => onlyMedia, "pinned" => pinned, "exclude_replies" => excludeReplies diff --git a/Tusker.xcodeproj/project.pbxproj b/Tusker.xcodeproj/project.pbxproj index 5044ff09..0a8db48e 100644 --- a/Tusker.xcodeproj/project.pbxproj +++ b/Tusker.xcodeproj/project.pbxproj @@ -10,7 +10,7 @@ 04DACE8C212CB14B009840C4 /* MainTabBarViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04DACE8B212CB14B009840C4 /* MainTabBarViewController.swift */; }; 04DACE8E212CC7CC009840C4 /* AvatarCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04DACE8D212CC7CC009840C4 /* AvatarCache.swift */; }; 04ED00B121481ED800567C53 /* SteppedProgressView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04ED00B021481ED800567C53 /* SteppedProgressView.swift */; }; - D6028B9B2150811100F223B9 /* StatusCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6028B9A2150811100F223B9 /* StatusCache.swift */; }; + D6028B9B2150811100F223B9 /* MastodonCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6028B9A2150811100F223B9 /* MastodonCache.swift */; }; D61099B42144B0CC00432DC2 /* Pachyderm.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D61099AB2144B0CC00432DC2 /* Pachyderm.framework */; }; D61099BB2144B0CC00432DC2 /* PachydermTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D61099BA2144B0CC00432DC2 /* PachydermTests.swift */; }; D61099BD2144B0CC00432DC2 /* Pachyderm.h in Headers */ = {isa = PBXBuildFile; fileRef = D61099AD2144B0CC00432DC2 /* Pachyderm.h */; settings = {ATTRIBUTES = (Public, ); }; }; @@ -165,7 +165,7 @@ 04DACE8B212CB14B009840C4 /* MainTabBarViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainTabBarViewController.swift; sourceTree = ""; }; 04DACE8D212CC7CC009840C4 /* AvatarCache.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AvatarCache.swift; sourceTree = ""; }; 04ED00B021481ED800567C53 /* SteppedProgressView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SteppedProgressView.swift; sourceTree = ""; }; - D6028B9A2150811100F223B9 /* StatusCache.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatusCache.swift; sourceTree = ""; }; + D6028B9A2150811100F223B9 /* MastodonCache.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MastodonCache.swift; sourceTree = ""; }; D61099AB2144B0CC00432DC2 /* Pachyderm.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pachyderm.framework; sourceTree = BUILT_PRODUCTS_DIR; }; D61099AD2144B0CC00432DC2 /* Pachyderm.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Pachyderm.h; sourceTree = ""; }; D61099AE2144B0CC00432DC2 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; @@ -615,7 +615,7 @@ D6D4DDCF212518A000E1C4BB /* AppDelegate.swift */, D64D0AAC2128D88B005A6F37 /* LocalData.swift */, 04DACE8D212CC7CC009840C4 /* AvatarCache.swift */, - D6028B9A2150811100F223B9 /* StatusCache.swift */, + D6028B9A2150811100F223B9 /* MastodonCache.swift */, D663626021360A9600C9CBA2 /* Preferences */, D667E5F62135C2ED0057A976 /* Extensions */, D6F953F121251A2F00CF0F2B /* Controllers */, @@ -925,7 +925,7 @@ D667E5F52135BCD50057A976 /* ConversationViewController.swift in Sources */, D6F953F021251A2900CF0F2B /* MastodonController.swift in Sources */, D66362712136338600C9CBA2 /* ComposeViewController.swift in Sources */, - D6028B9B2150811100F223B9 /* StatusCache.swift in Sources */, + D6028B9B2150811100F223B9 /* MastodonCache.swift in Sources */, D646C958213B367000269FB5 /* LargeImageShrinkAnimationController.swift in Sources */, D646C956213B365700269FB5 /* LargeImageExpandAnimationController.swift in Sources */, D667E5F82135C3040057A976 /* Mastodon+Equatable.swift in Sources */, diff --git a/Tusker/Extensions/UIViewController+Delegates.swift b/Tusker/Extensions/UIViewController+Delegates.swift index 395731b8..0aace6a7 100644 --- a/Tusker/Extensions/UIViewController+Delegates.swift +++ b/Tusker/Extensions/UIViewController+Delegates.swift @@ -12,17 +12,17 @@ import SafariServices extension StatusTableViewCellDelegate where Self: UIViewController { - func selected(account: Account) { + func selected(account accountID: String) { // don't open if the account is the same as the current one if let profileController = self as? ProfileTableViewController, - profileController.account == account { + profileController.accountID == accountID { return } guard let navigationController = navigationController else { fatalError("Can't show profile VC when not in navigation controller") } - let vc = ProfileTableViewController.create(for: account) + let vc = ProfileTableViewController.create(for: accountID) navigationController.pushViewController(vc, animated: true) } @@ -78,7 +78,7 @@ extension StatusTableViewCellDelegate where Self: UIViewController { } func showMoreOptions(status statusID: String) { - guard let status = StatusCache.get(id: statusID) else { fatalError("Missing cached status \(statusID)") } + guard let status = MastodonCache.status(for: statusID) else { fatalError("Missing cached status \(statusID)") } let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) if let url = status.url { diff --git a/Tusker/MastodonCache.swift b/Tusker/MastodonCache.swift new file mode 100644 index 00000000..e8e1badd --- /dev/null +++ b/Tusker/MastodonCache.swift @@ -0,0 +1,79 @@ +// +// StatusCache.swift +// Tusker +// +// Created by Shadowfacts on 9/17/18. +// Copyright © 2018 Shadowfacts. All rights reserved. +// + +import Foundation +import Pachyderm + +class MastodonCache { + + private static let statuses = NSCache() + private static let accounts = NSCache() + + // MARK: - Statuses + static func status(for id: String) -> Status? { + return statuses.object(forKey: id as NSString) + } + + static func set(status: Status, for id: String) { + statuses.setObject(status, forKey: id as NSString) + add(account: status.account) + if let reblog = status.reblog { + add(account: reblog.account) + } + } + + static func status(for id: String, completion: @escaping (Status?) -> Void) { + let request = MastodonController.shared.client.getStatus(id: id) + MastodonController.shared.client.run(request) { response in + guard case let .success(status, _) = response else { + completion(nil) + return + } + set(status: status, for: id) + completion(status) + } + } + + static func add(status: Status) { + set(status: status, for: status.id) + } + + static func addAll(statuses: [Status]) { + statuses.forEach(add) + } + + // MARK: - Accounts + static func account(for id: String) -> Account? { + return accounts.object(forKey: id as NSString) + } + + static func set(account: Account, for id: String) { + accounts.setObject(account, forKey: id as NSString) + } + + static func account(for id: String, completion: @escaping (Account?) -> Void) { + let request = MastodonController.shared.client.getAccount(id: id) + MastodonController.shared.client.run(request) { response in + guard case let .success(account, _) = response else { + completion(nil) + return + } + set(account: account, for: account.id) + completion(account) + } + } + + static func add(account: Account) { + set(account: account, for: account.id) + } + + static func addAll(accounts: [Account]) { + accounts.forEach(add) + } + +} diff --git a/Tusker/Screens/Compose/ComposeViewController.swift b/Tusker/Screens/Compose/ComposeViewController.swift index 16e92e19..fb428081 100644 --- a/Tusker/Screens/Compose/ComposeViewController.swift +++ b/Tusker/Screens/Compose/ComposeViewController.swift @@ -69,7 +69,7 @@ class ComposeViewController: UIViewController { statusTextView.inputAccessoryView = toolbar if let inReplyToID = inReplyToID, - let inReplyTo = StatusCache.get(id: inReplyToID) { + let inReplyTo = MastodonCache.status(for: inReplyToID) { inReplyToDisplayNameLabel.text = inReplyTo.account.realDisplayName inReplyToUsernameLabel.text = "@\(inReplyTo.account.username)" inReplyToContentLabel.statusID = inReplyToID @@ -228,7 +228,7 @@ class ComposeViewController: UIViewController { MastodonController.shared.client.run(request) { response in guard case let .success(status, _) = response else { fatalError() } self.status = status - StatusCache.add(status) + MastodonCache.add(status: status) DispatchQueue.main.async { self.progressView.step() self.performSegue(withIdentifier: "postComplete", sender: self) diff --git a/Tusker/Screens/Conversation/ConversationViewController.swift b/Tusker/Screens/Conversation/ConversationViewController.swift index 43463f8d..d6446743 100644 --- a/Tusker/Screens/Conversation/ConversationViewController.swift +++ b/Tusker/Screens/Conversation/ConversationViewController.swift @@ -40,14 +40,14 @@ class ConversationViewController: UIViewController, UITableViewDataSource, UITab statusIDs = [mainStatusID] - guard let mainStatus = StatusCache.get(id: mainStatusID) else { fatalError("Missing cached status \(mainStatusID!)") } + guard let mainStatus = MastodonCache.status(for: mainStatusID) else { fatalError("Missing cached status \(mainStatusID!)") } let request = Status.getContext(mainStatus) MastodonController.shared.client.run(request) { response in guard case let .success(context, _) = response else { fatalError() } let parents = self.getDirectParents(of: mainStatus, from: context.ancestors) - StatusCache.addAll(parents) - StatusCache.addAll(context.descendants) + MastodonCache.addAll(statuses: parents) + MastodonCache.addAll(statuses: context.descendants) self.statusIDs = parents.map { $0.id } + [self.mainStatusID] + context.descendants.map { $0.id } let indexPath = IndexPath(row: self.statusIDs.firstIndex(of: self.mainStatusID)!, section: 0) DispatchQueue.main.async { diff --git a/Tusker/Screens/Notifications/NotificationsTableViewController.swift b/Tusker/Screens/Notifications/NotificationsTableViewController.swift index 36118b39..80c3a791 100644 --- a/Tusker/Screens/Notifications/NotificationsTableViewController.swift +++ b/Tusker/Screens/Notifications/NotificationsTableViewController.swift @@ -47,7 +47,7 @@ class NotificationsTableViewController: UITableViewController { MastodonController.shared.client.run(request) { result in guard case let .success(notifications, pagination) = result else { fatalError() } self.notifications = notifications - StatusCache.addAll(notifications.compactMap { $0.status }) + MastodonCache.addAll(statuses: notifications.compactMap { $0.status }) self.newer = pagination?.newer self.older = pagination?.older } @@ -140,7 +140,7 @@ class NotificationsTableViewController: UITableViewController { MastodonController.shared.client.run(request) { result in guard case let .success(newNotifications, pagination) = result else { fatalError() } self.newer = pagination?.newer - StatusCache.addAll(newNotifications.compactMap { $0.status }) + MastodonCache.addAll(statuses: newNotifications.compactMap { $0.status }) self.notifications.insert(contentsOf: newNotifications, at: 0) DispatchQueue.main.async { self.refreshControl?.endRefreshing() diff --git a/Tusker/Screens/Profile/ProfileTableViewController.swift b/Tusker/Screens/Profile/ProfileTableViewController.swift index 6fabc3f3..c51d5170 100644 --- a/Tusker/Screens/Profile/ProfileTableViewController.swift +++ b/Tusker/Screens/Profile/ProfileTableViewController.swift @@ -12,13 +12,13 @@ import SafariServices class ProfileTableViewController: UITableViewController, PreferencesAdaptive { - static func create(for account: Account) -> UIViewController { + static func create(for accountID: String) -> UIViewController { guard let profileController = UIStoryboard(name: "Profile", bundle: nil).instantiateInitialViewController() as? ProfileTableViewController else { fatalError() } - profileController.account = account + profileController.accountID = accountID return profileController } - var account: Account! + var accountID: String! var statusIDs: [String] = [] { didSet { @@ -32,7 +32,7 @@ class ProfileTableViewController: UITableViewController, PreferencesAdaptive { var newer: RequestRange? func getStatuses(for range: RequestRange = .default, completion: @escaping Client.Callback<[Status]>) { - let request = Account.getStatuses(account, range: range, onlyMedia: false, pinned: false, excludeReplies: !Preferences.shared.showRepliesInProfiles) + let request = Account.getStatuses(accountID, range: range, onlyMedia: false, pinned: false, excludeReplies: !Preferences.shared.showRepliesInProfiles) MastodonController.shared.client.run(request, completion: completion) } @@ -49,7 +49,7 @@ class ProfileTableViewController: UITableViewController, PreferencesAdaptive { getStatuses { response in guard case let .success(statuses, pagination) = response else { fatalError() } - StatusCache.addAll(statuses) + MastodonCache.addAll(statuses: statuses) self.statusIDs = statuses.map { $0.id } self.older = pagination?.older self.newer = pagination?.newer @@ -68,10 +68,12 @@ class ProfileTableViewController: UITableViewController, PreferencesAdaptive { } func updateUIForPreferences() { + guard let account = MastodonCache.account(for: accountID) else { fatalError("Missing cached account \(accountID!)") } navigationItem.title = account.realDisplayName } func sendMessageMentioning() { + guard let account = MastodonCache.account(for: accountID) else { fatalError("Missing cached account \(accountID!)") } let vc = ComposeViewController.create(mentioning: account) present(vc, animated: true) } @@ -108,7 +110,7 @@ class ProfileTableViewController: UITableViewController, PreferencesAdaptive { case 0: guard let cell = tableView.dequeueReusableCell(withIdentifier: "headerCell", for: indexPath) as? ProfileHeaderTableViewCell else { fatalError() } cell.selectionStyle = .none - cell.updateUI(for: account) + cell.updateUI(for: accountID) cell.delegate = self return cell case 1: @@ -129,7 +131,7 @@ class ProfileTableViewController: UITableViewController, PreferencesAdaptive { getStatuses(for: older) { response in guard case let .success(newStatuses, pagination) = response else { fatalError() } self.older = pagination?.older - StatusCache.addAll(newStatuses) + MastodonCache.addAll(statuses: newStatuses) self.statusIDs.append(contentsOf: newStatuses.map { $0.id }) } } @@ -153,7 +155,7 @@ class ProfileTableViewController: UITableViewController, PreferencesAdaptive { getStatuses(for: newer) { response in guard case let .success(newStatuses, pagination) = response else { fatalError() } self.newer = pagination?.newer - StatusCache.addAll(newStatuses) + MastodonCache.addAll(statuses: newStatuses) self.statusIDs.insert(contentsOf: newStatuses.map { $0.id }, at: 0) DispatchQueue.main.async { self.refreshControl?.endRefreshing() @@ -170,13 +172,15 @@ class ProfileTableViewController: UITableViewController, PreferencesAdaptive { extension ProfileTableViewController: ProfileHeaderTableViewCellDelegate { func showMoreOptions() { + let account = MastodonCache.account(for: accountID)! + let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) alert.addAction(UIAlertAction(title: "Open in Safari...", style: .default, handler: { _ in - let vc = SFSafariViewController(url: self.account.url) + let vc = SFSafariViewController(url: account.url) self.present(vc, animated: true) })) alert.addAction(UIAlertAction(title: "Share...", style: .default, handler: { _ in - let vc = UIActivityViewController(activityItems: [self.account.url], applicationActivities: nil) + let vc = UIActivityViewController(activityItems: [account.url], applicationActivities: nil) self.present(vc, animated: true) })) alert.addAction(UIAlertAction(title: "Send Message...", style: .default, handler: { _ in diff --git a/Tusker/Screens/Timeline/TimelineTableViewController.swift b/Tusker/Screens/Timeline/TimelineTableViewController.swift index 9bced785..e753d633 100644 --- a/Tusker/Screens/Timeline/TimelineTableViewController.swift +++ b/Tusker/Screens/Timeline/TimelineTableViewController.swift @@ -74,7 +74,7 @@ class TimelineTableViewController: UITableViewController { MastodonController.shared.client.run(request) { response in guard case let .success(statuses, pagination) = response else { fatalError() } self.statusIDs = statuses.map { $0.id } - StatusCache.addAll(statuses) + MastodonCache.addAll(statuses: statuses) self.newer = pagination?.newer self.older = pagination?.older } @@ -131,7 +131,7 @@ class TimelineTableViewController: UITableViewController { MastodonController.shared.client.run(request) { response in guard case let .success(newStatuses, pagination) = response else { fatalError() } self.older = pagination?.older - StatusCache.addAll(newStatuses) + MastodonCache.addAll(statuses: newStatuses) self.statusIDs.append(contentsOf: newStatuses.map { $0.id }) } } @@ -156,7 +156,7 @@ class TimelineTableViewController: UITableViewController { MastodonController.shared.client.run(request) { response in guard case let .success(newStatuses, pagination) = response else { fatalError() } self.newer = pagination?.newer - StatusCache.addAll(newStatuses) + MastodonCache.addAll(statuses: newStatuses) self.statusIDs.insert(contentsOf: newStatuses.map { $0.id }, at: 0) DispatchQueue.main.async { self.refreshControl?.endRefreshing() diff --git a/Tusker/StatusCache.swift b/Tusker/StatusCache.swift deleted file mode 100644 index 29872248..00000000 --- a/Tusker/StatusCache.swift +++ /dev/null @@ -1,44 +0,0 @@ -// -// StatusCache.swift -// Tusker -// -// Created by Shadowfacts on 9/17/18. -// Copyright © 2018 Shadowfacts. All rights reserved. -// - -import Foundation -import Pachyderm - -class StatusCache { - - static let cache = NSCache() - - static func get(id: String) -> Status? { - return cache.object(forKey: id as NSString) - } - - static func set(id: String, status: Status) { - cache.setObject(status, forKey: id as NSString) - } - - static func get(id: String, completion: @escaping (Status?) -> Void) { - let request = MastodonController.shared.client.getStatus(id: id) - MastodonController.shared.client.run(request) { response in - guard case let .success(status, _) = response else { - completion(nil) - return - } - set(id: id, status: status) - completion(status) - } - } - - static func add(_ status: Status) { - set(id: status.id, status: status) - } - - static func addAll(_ statuses: [Status]) { - statuses.forEach { set(id: $0.id, status: $0) } - } - -} diff --git a/Tusker/Views/Notifications/ActionNotificationTableViewCell.swift b/Tusker/Views/Notifications/ActionNotificationTableViewCell.swift index 29dcc7e4..1ccd2f8c 100644 --- a/Tusker/Views/Notifications/ActionNotificationTableViewCell.swift +++ b/Tusker/Views/Notifications/ActionNotificationTableViewCell.swift @@ -44,7 +44,7 @@ class ActionNotificationTableViewCell: UITableViewCell, PreferencesAdaptive { } func updateUIForPreferences() { - guard let status = StatusCache.get(id: statusID) else { fatalError("Missing cached status \(statusID!)") } + guard let status = MastodonCache.status(for: statusID) else { fatalError("Missing cached status \(statusID!)") } opAvatarImageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: opAvatarImageView) actionAvatarImageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: actionAvatarImageView) @@ -133,7 +133,7 @@ class ActionNotificationTableViewCell: UITableViewCell, PreferencesAdaptive { } func updateTimestamp() { - guard let status = StatusCache.get(id: statusID) else { fatalError("Missing cached status \(statusID!)") } + guard let status = MastodonCache.status(for: statusID) else { fatalError("Missing cached status \(statusID!)") } timestampLabel.text = status.createdAt.timeAgoString() let delay: DispatchTimeInterval? @@ -176,11 +176,11 @@ class ActionNotificationTableViewCell: UITableViewCell, PreferencesAdaptive { } @objc func accountPressed() { - delegate?.selected(account: notification.status!.account) + delegate?.selected(account: notification.status!.account.id) } @objc func actionPressed() { - delegate?.selected(account: notification.account) + delegate?.selected(account: notification.account.id) } } diff --git a/Tusker/Views/Notifications/FollowNotificationTableViewCell.swift b/Tusker/Views/Notifications/FollowNotificationTableViewCell.swift index b7ac327d..493bbb40 100644 --- a/Tusker/Views/Notifications/FollowNotificationTableViewCell.swift +++ b/Tusker/Views/Notifications/FollowNotificationTableViewCell.swift @@ -20,7 +20,7 @@ class FollowNotificationTableViewCell: UITableViewCell, PreferencesAdaptive { @IBOutlet weak var usernameLabel: UILabel! var notification: Pachyderm.Notification! - var account: Account! + var accountID: String! var avatarURL: URL? var updateTimestampWorkItem: DispatchWorkItem? @@ -32,6 +32,8 @@ class FollowNotificationTableViewCell: UITableViewCell, PreferencesAdaptive { } func updateUIForPreferences() { + let account = MastodonCache.account(for: accountID)! + avatarImageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: avatarImageView) followLabel.text = "Followed by \(account.realDisplayName)" displayNameLabel.text = account.realDisplayName @@ -39,7 +41,8 @@ class FollowNotificationTableViewCell: UITableViewCell, PreferencesAdaptive { func updateUI(for notification: Pachyderm.Notification) { self.notification = notification - self.account = notification.account + let account = notification.account + self.accountID = account.id updateUIForPreferences() @@ -88,7 +91,7 @@ class FollowNotificationTableViewCell: UITableViewCell, PreferencesAdaptive { super.setSelected(selected, animated: animated) if selected { - delegate?.selected(account: account) + delegate?.selected(account: accountID) } } diff --git a/Tusker/Views/Profile Header/ProfileHeaderTableViewCell.swift b/Tusker/Views/Profile Header/ProfileHeaderTableViewCell.swift index ac3ba648..7f99507f 100644 --- a/Tusker/Views/Profile Header/ProfileHeaderTableViewCell.swift +++ b/Tusker/Views/Profile Header/ProfileHeaderTableViewCell.swift @@ -26,7 +26,7 @@ class ProfileHeaderTableViewCell: UITableViewCell, PreferencesAdaptive { @IBOutlet weak var avatarImageView: UIImageView! @IBOutlet weak var headerImageView: UIImageView! - var account: Account! + var accountID: String! var avatarURL: URL? @@ -42,13 +42,16 @@ class ProfileHeaderTableViewCell: UITableViewCell, PreferencesAdaptive { } func updateUIForPreferences() { + guard let account = MastodonCache.account(for: accountID) else { fatalError("Missing cached account \(accountID!)") } + avatarContainerView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: avatarContainerView) avatarImageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: avatarImageView) displayNameLabel.text = account.realDisplayName } - func updateUI(for account: Account) { - self.account = account + func updateUI(for accountID: String) { + self.accountID = accountID + guard let account = MastodonCache.account(for: accountID) else { fatalError("Missing cached account \(accountID)") } updateUIForPreferences() diff --git a/Tusker/Views/Status/ConversationMainStatusTableViewCell.swift b/Tusker/Views/Status/ConversationMainStatusTableViewCell.swift index e8b32416..37bc9362 100644 --- a/Tusker/Views/Status/ConversationMainStatusTableViewCell.swift +++ b/Tusker/Views/Status/ConversationMainStatusTableViewCell.swift @@ -23,7 +23,7 @@ class ConversationMainStatusTableViewCell: UITableViewCell, PreferencesAdaptive @IBOutlet weak var reblogButton: UIButton! var statusID: String! - var account: Account! + var accountID: String! var favorited: Bool = false { didSet { @@ -53,6 +53,8 @@ class ConversationMainStatusTableViewCell: UITableViewCell, PreferencesAdaptive } func updateUIForPreferences() { + guard let account = MastodonCache.account(for: accountID) else { fatalError("Missing cached account \(accountID!)") } + avatarImageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: avatarImageView) displayNameLabel.text = account.realDisplayName } @@ -60,7 +62,7 @@ class ConversationMainStatusTableViewCell: UITableViewCell, PreferencesAdaptive func updateUI(for statusID: String) { self.statusID = statusID - guard let status = StatusCache.get(id: statusID) else { fatalError("Missing cached status \(statusID)") } + guard let status = MastodonCache.status(for: statusID) else { fatalError("Missing cached status \(statusID)") } let account: Account if let reblog = status.reblog { @@ -68,7 +70,7 @@ class ConversationMainStatusTableViewCell: UITableViewCell, PreferencesAdaptive } else { account = status.account } - self.account = account + self.accountID = account.id updateUIForPreferences() @@ -118,7 +120,7 @@ class ConversationMainStatusTableViewCell: UITableViewCell, PreferencesAdaptive } func updateTimestamp() { - guard let status = StatusCache.get(id: statusID) else { fatalError("Missing cached status \(statusID!)") } + guard let status = MastodonCache.status(for: statusID) else { fatalError("Missing cached status \(statusID!)") } timestampLabel.text = status.createdAt.timeAgoString() let delay: DispatchTimeInterval? @@ -158,7 +160,7 @@ class ConversationMainStatusTableViewCell: UITableViewCell, PreferencesAdaptive } @objc func accountPressed() { - delegate?.selected(account: account) + delegate?.selected(account: accountID) } @IBAction func replyPressed(_ sender: Any) { @@ -166,7 +168,7 @@ class ConversationMainStatusTableViewCell: UITableViewCell, PreferencesAdaptive } @IBAction func favoritePressed(_ sender: Any) { - guard let status = StatusCache.get(id: statusID) else { fatalError("Missing cached status \(statusID!)") } + guard let status = MastodonCache.status(for: statusID) else { fatalError("Missing cached status \(statusID!)") } favorited = !favorited @@ -189,7 +191,7 @@ class ConversationMainStatusTableViewCell: UITableViewCell, PreferencesAdaptive } @IBAction func reblogPressed(_ sender: Any) { - guard let status = StatusCache.get(id: statusID) else { fatalError("Missing cached status \(statusID!)") } + guard let status = MastodonCache.status(for: statusID) else { fatalError("Missing cached status \(statusID!)") } reblogged = !reblogged diff --git a/Tusker/Views/Status/StatusTableViewCell.swift b/Tusker/Views/Status/StatusTableViewCell.swift index 861e0e24..a181dbd8 100644 --- a/Tusker/Views/Status/StatusTableViewCell.swift +++ b/Tusker/Views/Status/StatusTableViewCell.swift @@ -11,7 +11,7 @@ import Pachyderm protocol StatusTableViewCellDelegate { - func selected(account: Account) + func selected(account accountID: String) func selected(mention: Mention) @@ -44,8 +44,8 @@ class StatusTableViewCell: UITableViewCell, PreferencesAdaptive { @IBOutlet weak var reblogButton: UIButton! var statusID: String! - var account: Account! - var reblogger: Account? + var accountID: String! + var rebloggerID: String? var favorited: Bool = false { didSet { @@ -78,8 +78,10 @@ class StatusTableViewCell: UITableViewCell, PreferencesAdaptive { } func updateUIForPreferences() { + guard let account = MastodonCache.account(for: accountID) else { fatalError("") } avatarImageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: avatarImageView) - if let reblogger = reblogger { + if let rebloggerID = rebloggerID, + let reblogger = MastodonCache.account(for: rebloggerID) { reblogLabel.text = "Reblogged by \(reblogger.realDisplayName)" } displayNameLabel.text = account.realDisplayName @@ -87,19 +89,19 @@ class StatusTableViewCell: UITableViewCell, PreferencesAdaptive { func updateUI(for statusID: String) { self.statusID = statusID - guard let status = StatusCache.get(id: statusID) else { fatalError("Missing cached status \(statusID)") } + guard let status = MastodonCache.status(for: statusID) else { fatalError("Missing cached status \(statusID)") } let account: Account if let reblog = status.reblog { account = reblog.account - reblogger = status.account + rebloggerID = status.account.id reblogLabel.isHidden = false } else { account = status.account reblogLabel.isHidden = true - reblogger = nil + rebloggerID = nil } - self.account = account + self.accountID = account.id updateUIForPreferences() @@ -151,7 +153,7 @@ class StatusTableViewCell: UITableViewCell, PreferencesAdaptive { } func updateTimestamp() { - guard let status = StatusCache.get(id: statusID) else { fatalError("Missing cached status \(statusID!)") } + guard let status = MastodonCache.status(for: statusID) else { fatalError("Missing cached status \(statusID!)") } timestampLabel.text = status.createdAt.timeAgoString() let delay: DispatchTimeInterval? @@ -204,16 +206,16 @@ class StatusTableViewCell: UITableViewCell, PreferencesAdaptive { } @objc func accountPressed() { - delegate?.selected(account: account) + delegate?.selected(account: accountID) } @objc func reblogLabelPressed() { - guard let reblogger = reblogger else { return } - delegate?.selected(account: reblogger) + guard let rebloggerID = rebloggerID else { return } + delegate?.selected(account: rebloggerID) } @IBAction func favoritePressed(_ sender: Any) { - guard let status = StatusCache.get(id: statusID) else { fatalError("Missing cached status \(statusID!)") } + guard let status = MastodonCache.status(for: statusID) else { fatalError("Missing cached status \(statusID!)") } let oldValue = favorited favorited = !favorited @@ -238,7 +240,7 @@ class StatusTableViewCell: UITableViewCell, PreferencesAdaptive { } @IBAction func reblogPressed(_ sender: Any) { - guard let status = StatusCache.get(id: statusID) else { fatalError("Missing cached status \(statusID!)") } + guard let status = MastodonCache.status(for: statusID) else { fatalError("Missing cached status \(statusID!)") } let oldValue = reblogged reblogged = !reblogged @@ -283,7 +285,7 @@ extension StatusTableViewCell: TableViewSwipeActionProvider { } func leadingSwipeActionsConfiguration() -> UISwipeActionsConfiguration? { - guard let status = StatusCache.get(id: statusID) else { fatalError("Missing cached status \(statusID!)") } + guard let status = MastodonCache.status(for: statusID) else { fatalError("Missing cached status \(statusID!)") } let favoriteTitle: String let favoriteRequest: Request @@ -306,7 +308,7 @@ extension StatusTableViewCell: TableViewSwipeActionProvider { return } completion(true) - StatusCache.add(status) + MastodonCache.add(status: status) self.updateUI(for: self.statusID) } }) @@ -334,7 +336,7 @@ extension StatusTableViewCell: TableViewSwipeActionProvider { return } completion(true) - StatusCache.add(status) + MastodonCache.add(status: status) self.updateUI(for: self.statusID) } }) diff --git a/Tusker/Views/StatusContentLabel.swift b/Tusker/Views/StatusContentLabel.swift index 8c15eff7..42323e7e 100644 --- a/Tusker/Views/StatusContentLabel.swift +++ b/Tusker/Views/StatusContentLabel.swift @@ -17,7 +17,7 @@ class StatusContentLabel: HTMLContentLabel { } } var status: Status! { - return StatusCache.get(id: statusID) + return MastodonCache.status(for: statusID) } override func getMention(for url: URL, text: String) -> Mention? {