Cache accounts

This commit is contained in:
Shadowfacts 2018-09-18 12:59:07 -04:00
parent 89b9cd29aa
commit e971c7f61c
16 changed files with 158 additions and 109 deletions

View File

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

View File

@ -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 = "<group>"; };
04DACE8D212CC7CC009840C4 /* AvatarCache.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AvatarCache.swift; sourceTree = "<group>"; };
04ED00B021481ED800567C53 /* SteppedProgressView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SteppedProgressView.swift; sourceTree = "<group>"; };
D6028B9A2150811100F223B9 /* StatusCache.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatusCache.swift; sourceTree = "<group>"; };
D6028B9A2150811100F223B9 /* MastodonCache.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MastodonCache.swift; sourceTree = "<group>"; };
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 = "<group>"; };
D61099AE2144B0CC00432DC2 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
@ -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 */,

View File

@ -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 {

View File

@ -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<NSString, Status>()
private static let accounts = NSCache<NSString, Account>()
// 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)
}
}

View File

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

View File

@ -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 {

View File

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

View File

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

View File

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

View File

@ -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<NSString, Status>()
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) }
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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? {