Replace a bunch of MastodonCache uses with CoreData

This commit is contained in:
Shadowfacts 2020-05-02 19:52:35 -04:00
parent 5786c24846
commit d6c506488b
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
18 changed files with 57 additions and 61 deletions

View File

@ -29,9 +29,7 @@ class FollowAccountActivity: AccountActivity {
let request = Account.follow(account.id) let request = Account.follow(account.id)
mastodonController.run(request) { (response) in mastodonController.run(request) { (response) in
if case let .success(relationship, _) = response { if case .failure(_) = response {
self.mastodonController.cache.add(relationship: relationship)
} else {
// todo: display error message // todo: display error message
UINotificationFeedbackGenerator().notificationOccurred(.error) UINotificationFeedbackGenerator().notificationOccurred(.error)
fatalError() fatalError()

View File

@ -29,7 +29,7 @@ class BookmarkStatusActivity: StatusActivity {
let request = Status.bookmark(status) let request = Status.bookmark(status)
mastodonController.run(request) { (response) in mastodonController.run(request) { (response) in
if case let .success(status, _) = response { if case let .success(status, _) = response {
self.mastodonController.cache.add(status: status) self.mastodonController.persistentContainer.addOrUpdate(status: status, incrementReferenceCount: false)
} else { } else {
// todo: display error message // todo: display error message
UINotificationFeedbackGenerator().notificationOccurred(.error) UINotificationFeedbackGenerator().notificationOccurred(.error)

View File

@ -28,7 +28,7 @@ class PinStatusActivity: StatusActivity {
let request = Status.pin(status) let request = Status.pin(status)
mastodonController.run(request) { (response) in mastodonController.run(request) { (response) in
if case let .success(status, _) = response { if case let .success(status, _) = response {
self.mastodonController.cache.add(status: status) self.mastodonController.persistentContainer.addOrUpdate(status: status, incrementReferenceCount: false)
} else { } else {
// todo: display error message // todo: display error message
UINotificationFeedbackGenerator().notificationOccurred(.error) UINotificationFeedbackGenerator().notificationOccurred(.error)

View File

@ -29,7 +29,7 @@ class UnbookmarkStatusActivity: StatusActivity {
let request = Status.unbookmark(status.id) let request = Status.unbookmark(status.id)
mastodonController.run(request) { (response) in mastodonController.run(request) { (response) in
if case let .success(status, _) = response { if case let .success(status, _) = response {
self.mastodonController.cache.add(status: status) self.mastodonController.persistentContainer.addOrUpdate(status: status, incrementReferenceCount: false)
} else { } else {
// todo: display error message // todo: display error message
UINotificationFeedbackGenerator().notificationOccurred(.error) UINotificationFeedbackGenerator().notificationOccurred(.error)

View File

@ -28,7 +28,7 @@ class UnpinStatusActivity: StatusActivity {
let request = Status.unpin(status) let request = Status.unpin(status)
mastodonController.run(request) { (response) in mastodonController.run(request) { (response) in
if case let .success(status, _) = response { if case let .success(status, _) = response {
self.mastodonController.cache.add(status: status) self.mastodonController.persistentContainer.addOrUpdate(status: status, incrementReferenceCount: false)
} else { } else {
// todo: display error message // todo: display error message
UINotificationFeedbackGenerator().notificationOccurred(.error) UINotificationFeedbackGenerator().notificationOccurred(.error)

View File

@ -120,7 +120,7 @@ class BookmarksTableViewController: EnhancedTableViewController {
let request = Status.unbookmark(status.id) let request = Status.unbookmark(status.id)
self.mastodonController.run(request) { (response) in self.mastodonController.run(request) { (response) in
guard case let .success(newStatus, _) = response else { fatalError() } guard case let .success(newStatus, _) = response else { fatalError() }
self.mastodonController.cache.add(status: newStatus) self.mastodonController.persistentContainer.addOrUpdate(status: newStatus, incrementReferenceCount: false)
self.statuses.remove(at: indexPath.row) self.statuses.remove(at: indexPath.row)
} }
} }
@ -144,7 +144,7 @@ class BookmarksTableViewController: EnhancedTableViewController {
let request = Status.unbookmark(status.id) let request = Status.unbookmark(status.id)
self.mastodonController.run(request) { (response) in self.mastodonController.run(request) { (response) in
guard case let .success(newStatus, _) = response else { fatalError() } guard case let .success(newStatus, _) = response else { fatalError() }
self.mastodonController.cache.add(status: newStatus) self.mastodonController.persistentContainer.addOrUpdate(status: newStatus, incrementReferenceCount: false)
self.statuses.remove(at: indexPath.row) self.statuses.remove(at: indexPath.row)
} }
}) })

View File

@ -72,7 +72,7 @@ class ComposeViewController: UIViewController {
self.mastodonController = mastodonController self.mastodonController = mastodonController
self.inReplyToID = inReplyToID self.inReplyToID = inReplyToID
if let inReplyToID = inReplyToID, let inReplyTo = mastodonController.cache.status(for: inReplyToID) { if let inReplyToID = inReplyToID, let inReplyTo = mastodonController.persistentContainer.status(for: inReplyToID) {
accountsToMention = [inReplyTo.account.acct] + inReplyTo.mentions.map { $0.acct } accountsToMention = [inReplyTo.account.acct] + inReplyTo.mentions.map { $0.acct }
} else { } else {
accountsToMention = [] accountsToMention = []
@ -164,17 +164,21 @@ class ComposeViewController: UIViewController {
} }
if let inReplyToID = inReplyToID { if let inReplyToID = inReplyToID {
if let status = mastodonController.cache.status(for: inReplyToID) { if let status = mastodonController.persistentContainer.status(for: inReplyToID) {
updateInReplyTo(inReplyTo: status) updateInReplyTo(inReplyTo: status)
} else { } else {
let loadingVC = LoadingViewController() let loadingVC = LoadingViewController()
embedChild(loadingVC) embedChild(loadingVC)
mastodonController.cache.status(for: inReplyToID) { (status) in let request = Client.getStatus(id: inReplyToID)
guard let status = status else { return } mastodonController.run(request) { (response) in
DispatchQueue.main.async { guard case let .success(status, _) = response else { return }
self.updateInReplyTo(inReplyTo: status) self.mastodonController.persistentContainer.addOrUpdate(status: status, incrementReferenceCount: true) {
loadingVC.removeViewAndController() guard let status = self.mastodonController.persistentContainer.status(for: inReplyToID) else { return }
DispatchQueue.main.async {
self.updateInReplyTo(inReplyTo: status)
loadingVC.removeViewAndController()
}
} }
} }
} }
@ -186,7 +190,7 @@ class ComposeViewController: UIViewController {
} }
} }
func updateInReplyTo(inReplyTo: Status) { func updateInReplyTo(inReplyTo: StatusMO) {
visibility = inReplyTo.visibility visibility = inReplyTo.visibility
if Preferences.shared.contentWarningCopyMode == .doNotCopy { if Preferences.shared.contentWarningCopyMode == .doNotCopy {
contentWarningEnabled = false contentWarningEnabled = false
@ -470,7 +474,7 @@ class ComposeViewController: UIViewController {
self.mastodonController.run(request) { (response) in self.mastodonController.run(request) { (response) in
guard case let .success(status, _) = response else { fatalError() } guard case let .success(status, _) = response else { fatalError() }
self.postedStatus = status self.postedStatus = status
self.mastodonController.cache.add(status: status) // self.mastodonController.persistentContainer.addOrUpdate(status: status, incrementReferenceCount: true)
if let draft = self.currentDraft { if let draft = self.currentDraft {
DraftsManager.shared.remove(draft) DraftsManager.shared.remove(draft)
@ -481,8 +485,8 @@ class ComposeViewController: UIViewController {
self.dismiss(animated: true) self.dismiss(animated: true)
// todo: this doesn't work // todo: this doesn't work
let conversationVC = ConversationTableViewController(for: status.id, mastodonController: self.mastodonController) // let conversationVC = ConversationTableViewController(for: status.id, mastodonController: self.mastodonController)
self.show(conversationVC, sender: self) // self.show(conversationVC, sender: self)
self.xcbSession?.complete(with: .success, additionalData: [ self.xcbSession?.complete(with: .success, additionalData: [
"statusURL": status.url?.absoluteString, "statusURL": status.url?.absoluteString,

View File

@ -80,7 +80,7 @@ class EditListAccountsViewController: EnhancedTableViewController {
self.nextRange = pagination?.older self.nextRange = pagination?.older
self.mastodonController.cache.addAll(accounts: accounts) self.mastodonController.persistentContainer.addAll(accounts: accounts)
var snapshot = self.dataSource.snapshot() var snapshot = self.dataSource.snapshot()
snapshot.deleteSections([.accounts]) snapshot.deleteSections([.accounts])

View File

@ -142,9 +142,8 @@ class NotificationsTableViewController: EnhancedTableViewController {
} }
self.groups.append(contentsOf: groups) self.groups.append(contentsOf: groups)
self.mastodonController.cache.addAll(notifications: newNotifications) self.mastodonController.persistentContainer.addAll(statuses: newNotifications.compactMap { $0.status })
self.mastodonController.cache.addAll(statuses: newNotifications.compactMap { $0.status }) self.mastodonController.persistentContainer.addAll(accounts: newNotifications.map { $0.account })
self.mastodonController.cache.addAll(accounts: newNotifications.map { $0.account })
self.older = pagination?.older self.older = pagination?.older
@ -220,9 +219,8 @@ class NotificationsTableViewController: EnhancedTableViewController {
self.groups.insert(contentsOf: groups, at: 0) self.groups.insert(contentsOf: groups, at: 0)
self.mastodonController.cache.addAll(notifications: newNotifications) self.mastodonController.persistentContainer.addAll(statuses: newNotifications.compactMap { $0.status })
self.mastodonController.cache.addAll(statuses: newNotifications.compactMap { $0.status }) self.mastodonController.persistentContainer.addAll(accounts: newNotifications.map { $0.account })
self.mastodonController.cache.addAll(accounts: newNotifications.map { $0.account })
if let newer = pagination?.newer { if let newer = pagination?.newer {
self.newer = newer self.newer = newer

View File

@ -78,8 +78,9 @@ class ProfileTableViewController: EnhancedTableViewController {
} else { } else {
loadingVC = LoadingViewController() loadingVC = LoadingViewController()
embedChild(loadingVC!) embedChild(loadingVC!)
mastodonController.cache.account(for: accountID) { (account) in let request = Client.getAccount(id: accountID)
guard let account = account else { mastodonController.run(request) { (response) in
guard case let .success(account, _) = response else {
let alert = UIAlertController(title: "Something Went Wrong", message: "Couldn't load the selected account", preferredStyle: .alert) let alert = UIAlertController(title: "Something Went Wrong", message: "Couldn't load the selected account", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (_) in alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (_) in
self.navigationController!.popViewController(animated: true) self.navigationController!.popViewController(animated: true)
@ -279,11 +280,10 @@ extension ProfileTableViewController: ProfileHeaderTableViewCellDelegate {
func showMoreOptions(cell: ProfileHeaderTableViewCell) { func showMoreOptions(cell: ProfileHeaderTableViewCell) {
let account = mastodonController.persistentContainer.account(for: accountID)! let account = mastodonController.persistentContainer.account(for: accountID)!
mastodonController.cache.relationship(for: account.id) { [weak self] (relationship) in let request = Client.getRelationships(accounts: [account.id])
guard let self = self else { return } mastodonController.run(request) { (response) in
var customActivities: [UIActivity] = [OpenInSafariActivity()] var customActivities: [UIActivity] = [OpenInSafariActivity()]
if let relationship = relationship { if case let .success(results, _) = response, let relationship = results.first {
let toggleFollowActivity = relationship.following ? UnfollowAccountActivity() : FollowAccountActivity() let toggleFollowActivity = relationship.following ? UnfollowAccountActivity() : FollowAccountActivity()
customActivities.insert(toggleFollowActivity, at: 0) customActivities.insert(toggleFollowActivity, at: 0)
} }

View File

@ -82,7 +82,7 @@ class StatusActionAccountListTableViewController: EnhancedTableViewController {
let request = actionType == .favorite ? Status.getFavourites(status.id) : Status.getReblogs(status.id) let request = actionType == .favorite ? Status.getFavourites(status.id) : Status.getReblogs(status.id)
mastodonController.run(request) { (response) in mastodonController.run(request) { (response) in
guard case let .success(accounts, _) = response else { fatalError() } guard case let .success(accounts, _) = response else { fatalError() }
self.mastodonController.cache.addAll(accounts: accounts) self.mastodonController.persistentContainer.addAll(accounts: accounts)
DispatchQueue.main.async { DispatchQueue.main.async {
self.accountIDs = accounts.map { $0.id } self.accountIDs = accounts.map { $0.id }
self.tableView.tableFooterView = nil self.tableView.tableFooterView = nil

View File

@ -30,7 +30,7 @@ extension MenuPreviewProvider {
func actionsForProfile(accountID: String, sourceView: UIView?) -> [UIAction] { func actionsForProfile(accountID: String, sourceView: UIView?) -> [UIAction] {
guard let mastodonController = mastodonController, guard let mastodonController = mastodonController,
let account = mastodonController.cache.account(for: accountID) else { return [] } let account = mastodonController.persistentContainer.account(for: accountID) else { return [] }
return [ return [
createAction(identifier: "sendmessage", title: "Send Message", systemImageName: "envelope", handler: { (_) in createAction(identifier: "sendmessage", title: "Send Message", systemImageName: "envelope", handler: { (_) in
self.navigationDelegate?.compose(mentioning: account.acct) self.navigationDelegate?.compose(mentioning: account.acct)
@ -61,7 +61,7 @@ extension MenuPreviewProvider {
func actionsForStatus(statusID: String, sourceView: UIView?) -> [UIAction] { func actionsForStatus(statusID: String, sourceView: UIView?) -> [UIAction] {
guard let mastodonController = mastodonController, guard let mastodonController = mastodonController,
let status = mastodonController.cache.status(for: statusID) else { return [] } let status = mastodonController.persistentContainer.status(for: statusID) else { return [] }
return [ return [
createAction(identifier: "reply", title: "Reply", systemImageName: "arrowshape.turn.up.left", handler: { (_) in createAction(identifier: "reply", title: "Reply", systemImageName: "arrowshape.turn.up.left", handler: { (_) in
self.navigationDelegate?.reply(to: statusID) self.navigationDelegate?.reply(to: statusID)

View File

@ -203,17 +203,14 @@ extension TuskerNavigationDelegate where Self: UIViewController {
} }
private func moreOptions(forStatus statusID: String) -> UIActivityViewController { private func moreOptions(forStatus statusID: String) -> UIActivityViewController {
guard let status = apiController.cache.status(for: statusID) else { fatalError("Missing cached status \(statusID)") } guard let status = apiController.persistentContainer.status(for: statusID) else { fatalError("Missing cached status \(statusID)") }
guard let url = status.url else { fatalError("Missing url for status \(statusID)") } guard let url = status.url else { fatalError("Missing url for status \(statusID)") }
var customActivites: [UIActivity] = [OpenInSafariActivity()] var customActivites: [UIActivity] = [OpenInSafariActivity()]
if let bookmarked = status.bookmarked { customActivites.insert(status.bookmarked ? UnbookmarkStatusActivity() : BookmarkStatusActivity(), at: 0)
customActivites.insert(bookmarked ? UnbookmarkStatusActivity() : BookmarkStatusActivity(), at: 0)
} if status.account.id == apiController.account.id {
customActivites.insert(status.pinned ? UnpinStatusActivity() : PinStatusActivity(), at: 1)
if status.account == apiController.account,
let pinned = status.pinned {
customActivites.insert(pinned ? UnpinStatusActivity() : PinStatusActivity(), at: 1)
} }
let activityController = UIActivityViewController(activityItems: [url, status], applicationActivities: customActivites) let activityController = UIActivityViewController(activityItems: [url, status], applicationActivities: customActivites)
@ -222,7 +219,7 @@ extension TuskerNavigationDelegate where Self: UIViewController {
} }
private func moreOptions(forAccount accountID: String) -> UIActivityViewController { private func moreOptions(forAccount accountID: String) -> UIActivityViewController {
guard let account = apiController.cache.account(for: accountID) else { fatalError("Missing cached account \(accountID)") } guard let account = apiController.persistentContainer.account(for: accountID) else { fatalError("Missing cached account \(accountID)") }
return moreOptions(forURL: account.url) return moreOptions(forURL: account.url)
} }

View File

@ -39,7 +39,7 @@ class ComposeStatusReplyView: UIView {
avatarImageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: avatarImageView) avatarImageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: avatarImageView)
} }
func updateUI(for status: Status) { func updateUI(for status: StatusMO) {
displayNameLabel.updateForAccountDisplayName(account: status.account) displayNameLabel.updateForAccountDisplayName(account: status.account)
usernameLabel.text = "@\(status.account.acct)" usernameLabel.text = "@\(status.account.acct)"
statusContentTextView.overrideMastodonController = mastodonController statusContentTextView.overrideMastodonController = mastodonController

View File

@ -88,14 +88,13 @@ class ProfileHeaderTableViewCell: UITableViewCell {
noteTextView.setTextFromHtml(account.note) noteTextView.setTextFromHtml(account.note)
noteTextView.setEmojis(account.emojis) noteTextView.setEmojis(account.emojis)
// don't show relationship label for the user's own account
if accountID != mastodonController.account.id { if accountID != mastodonController.account.id {
// don't show relationship label for the user's own account let request = Client.getRelationships(accounts: [accountID])
if let relationship = mastodonController.cache.relationship(for: accountID) { mastodonController.run(request) { (response) in
followsYouLabel.isHidden = !relationship.followedBy if case let .success(results, _) = response, let relationship = results.first {
} else {
mastodonController.cache.relationship(for: accountID) { relationship in
DispatchQueue.main.async { DispatchQueue.main.async {
self.followsYouLabel.isHidden = !(relationship?.followedBy ?? false) self.followsYouLabel.isHidden = !relationship.followedBy
} }
} }
} }

View File

@ -251,18 +251,18 @@ class BaseStatusTableViewCell: UITableViewCell {
} }
@IBAction func favoritePressed() { @IBAction func favoritePressed() {
guard let status = mastodonController.cache.status(for: statusID) else { fatalError("Missing cached status \(statusID!)") } guard let status = mastodonController.persistentContainer.status(for: statusID) else { fatalError("Missing cached status \(statusID!)") }
let oldValue = favorited let oldValue = favorited
favorited = !favorited favorited = !favorited
let realStatus: Status = status.reblog ?? status let realStatus = status.reblog ?? status
let request = (favorited ? Status.favourite : Status.unfavourite)(realStatus.id) let request = (favorited ? Status.favourite : Status.unfavourite)(realStatus.id)
mastodonController.run(request) { response in mastodonController.run(request) { response in
DispatchQueue.main.async { DispatchQueue.main.async {
if case let .success(newStatus, _) = response { if case let .success(newStatus, _) = response {
self.favorited = newStatus.favourited ?? false self.favorited = newStatus.favourited ?? false
self.mastodonController.cache.add(status: newStatus) self.mastodonController.persistentContainer.addOrUpdate(status: newStatus, incrementReferenceCount: false)
UIImpactFeedbackGenerator(style: .light).impactOccurred() UIImpactFeedbackGenerator(style: .light).impactOccurred()
} else { } else {
self.favorited = oldValue self.favorited = oldValue
@ -276,18 +276,18 @@ class BaseStatusTableViewCell: UITableViewCell {
} }
@IBAction func reblogPressed() { @IBAction func reblogPressed() {
guard let status = mastodonController.cache.status(for: statusID) else { fatalError("Missing cached status \(statusID!)") } guard let status = mastodonController.persistentContainer.status(for: statusID) else { fatalError("Missing cached status \(statusID!)") }
let oldValue = reblogged let oldValue = reblogged
reblogged = !reblogged reblogged = !reblogged
let realStatus: Status = status.reblog ?? status let realStatus = status.reblog ?? status
let request = (reblogged ? Status.reblog : Status.unreblog)(realStatus.id) let request = (reblogged ? Status.reblog : Status.unreblog)(realStatus.id)
mastodonController.run(request) { response in mastodonController.run(request) { response in
DispatchQueue.main.async { DispatchQueue.main.async {
if case let .success(newStatus, _) = response { if case let .success(newStatus, _) = response {
self.reblogged = newStatus.reblogged ?? false self.reblogged = newStatus.reblogged ?? false
self.mastodonController.cache.add(status: newStatus) self.mastodonController.persistentContainer.addOrUpdate(status: newStatus, incrementReferenceCount: false)
UIImpactFeedbackGenerator(style: .light).impactOccurred() UIImpactFeedbackGenerator(style: .light).impactOccurred()
} else { } else {
self.reblogged = oldValue self.reblogged = oldValue

View File

@ -136,7 +136,7 @@ class TimelineStatusTableViewCell: BaseStatusTableViewCell {
func reply() { func reply() {
if Preferences.shared.mentionReblogger, if Preferences.shared.mentionReblogger,
let rebloggerID = rebloggerID, let rebloggerID = rebloggerID,
let rebloggerAccount = mastodonController.cache.account(for: rebloggerID) { let rebloggerAccount = mastodonController.persistentContainer.account(for: rebloggerID) {
delegate?.reply(to: statusID, mentioningAcct: rebloggerAccount.acct) delegate?.reply(to: statusID, mentioningAcct: rebloggerAccount.acct)
} else { } else {
delegate?.reply(to: statusID) delegate?.reply(to: statusID)

View File

@ -27,7 +27,7 @@ class StatusContentTextView: ContentTextView {
let mention: Mention? let mention: Mention?
if let statusID = statusID, if let statusID = statusID,
let mastodonController = mastodonController, let mastodonController = mastodonController,
let status = mastodonController.cache.status(for: statusID) { let status = mastodonController.persistentContainer.status(for: statusID) {
mention = status.mentions.first { (mention) in mention = status.mentions.first { (mention) in
// Mastodon and Pleroma include the @ in the <a> text, GNU Social does not // Mastodon and Pleroma include the @ in the <a> text, GNU Social does not
(text.dropFirst() == mention.username || text == mention.username) && url.host == mention.url.host (text.dropFirst() == mention.username || text == mention.username) && url.host == mention.url.host
@ -42,7 +42,7 @@ class StatusContentTextView: ContentTextView {
let hashtag: Hashtag? let hashtag: Hashtag?
if let statusID = statusID, if let statusID = statusID,
let mastodonController = mastodonController, let mastodonController = mastodonController,
let status = mastodonController.cache.status(for: statusID) { let status = mastodonController.persistentContainer.status(for: statusID) {
hashtag = status.hashtags.first { (hashtag) in hashtag = status.hashtags.first { (hashtag) in
hashtag.url == url hashtag.url == url
} }