diff --git a/Tusker/Activities/Account Activities/FollowAccountActivity.swift b/Tusker/Activities/Account Activities/FollowAccountActivity.swift index ae89aa7e..8c048f83 100644 --- a/Tusker/Activities/Account Activities/FollowAccountActivity.swift +++ b/Tusker/Activities/Account Activities/FollowAccountActivity.swift @@ -29,9 +29,7 @@ class FollowAccountActivity: AccountActivity { let request = Account.follow(account.id) mastodonController.run(request) { (response) in - if case let .success(relationship, _) = response { - self.mastodonController.cache.add(relationship: relationship) - } else { + if case .failure(_) = response { // todo: display error message UINotificationFeedbackGenerator().notificationOccurred(.error) fatalError() diff --git a/Tusker/Activities/Status Activities/BookmarkStatusActivity.swift b/Tusker/Activities/Status Activities/BookmarkStatusActivity.swift index 585f3471..76f21030 100644 --- a/Tusker/Activities/Status Activities/BookmarkStatusActivity.swift +++ b/Tusker/Activities/Status Activities/BookmarkStatusActivity.swift @@ -29,7 +29,7 @@ class BookmarkStatusActivity: StatusActivity { let request = Status.bookmark(status) mastodonController.run(request) { (response) in if case let .success(status, _) = response { - self.mastodonController.cache.add(status: status) + self.mastodonController.persistentContainer.addOrUpdate(status: status, incrementReferenceCount: false) } else { // todo: display error message UINotificationFeedbackGenerator().notificationOccurred(.error) diff --git a/Tusker/Activities/Status Activities/PinStatusActivity.swift b/Tusker/Activities/Status Activities/PinStatusActivity.swift index 40ef6cfd..035423e0 100644 --- a/Tusker/Activities/Status Activities/PinStatusActivity.swift +++ b/Tusker/Activities/Status Activities/PinStatusActivity.swift @@ -28,7 +28,7 @@ class PinStatusActivity: StatusActivity { let request = Status.pin(status) mastodonController.run(request) { (response) in if case let .success(status, _) = response { - self.mastodonController.cache.add(status: status) + self.mastodonController.persistentContainer.addOrUpdate(status: status, incrementReferenceCount: false) } else { // todo: display error message UINotificationFeedbackGenerator().notificationOccurred(.error) diff --git a/Tusker/Activities/Status Activities/UnbookmarkStatusActivity.swift b/Tusker/Activities/Status Activities/UnbookmarkStatusActivity.swift index 489c3206..953c8ce3 100644 --- a/Tusker/Activities/Status Activities/UnbookmarkStatusActivity.swift +++ b/Tusker/Activities/Status Activities/UnbookmarkStatusActivity.swift @@ -29,7 +29,7 @@ class UnbookmarkStatusActivity: StatusActivity { let request = Status.unbookmark(status.id) mastodonController.run(request) { (response) in if case let .success(status, _) = response { - self.mastodonController.cache.add(status: status) + self.mastodonController.persistentContainer.addOrUpdate(status: status, incrementReferenceCount: false) } else { // todo: display error message UINotificationFeedbackGenerator().notificationOccurred(.error) diff --git a/Tusker/Activities/Status Activities/UnpinStatusActivity.swift b/Tusker/Activities/Status Activities/UnpinStatusActivity.swift index a22df120..421c3a3e 100644 --- a/Tusker/Activities/Status Activities/UnpinStatusActivity.swift +++ b/Tusker/Activities/Status Activities/UnpinStatusActivity.swift @@ -28,7 +28,7 @@ class UnpinStatusActivity: StatusActivity { let request = Status.unpin(status) mastodonController.run(request) { (response) in if case let .success(status, _) = response { - self.mastodonController.cache.add(status: status) + self.mastodonController.persistentContainer.addOrUpdate(status: status, incrementReferenceCount: false) } else { // todo: display error message UINotificationFeedbackGenerator().notificationOccurred(.error) diff --git a/Tusker/Screens/Bookmarks/BookmarksTableViewController.swift b/Tusker/Screens/Bookmarks/BookmarksTableViewController.swift index 323d88a3..02e59b95 100644 --- a/Tusker/Screens/Bookmarks/BookmarksTableViewController.swift +++ b/Tusker/Screens/Bookmarks/BookmarksTableViewController.swift @@ -120,7 +120,7 @@ class BookmarksTableViewController: EnhancedTableViewController { let request = Status.unbookmark(status.id) self.mastodonController.run(request) { (response) in 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) } } @@ -144,7 +144,7 @@ class BookmarksTableViewController: EnhancedTableViewController { let request = Status.unbookmark(status.id) self.mastodonController.run(request) { (response) in 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) } }) diff --git a/Tusker/Screens/Compose/ComposeViewController.swift b/Tusker/Screens/Compose/ComposeViewController.swift index 1f274144..a69e80bb 100644 --- a/Tusker/Screens/Compose/ComposeViewController.swift +++ b/Tusker/Screens/Compose/ComposeViewController.swift @@ -72,7 +72,7 @@ class ComposeViewController: UIViewController { self.mastodonController = mastodonController 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 } } else { accountsToMention = [] @@ -164,17 +164,21 @@ class ComposeViewController: UIViewController { } if let inReplyToID = inReplyToID { - if let status = mastodonController.cache.status(for: inReplyToID) { + if let status = mastodonController.persistentContainer.status(for: inReplyToID) { updateInReplyTo(inReplyTo: status) } else { let loadingVC = LoadingViewController() embedChild(loadingVC) - mastodonController.cache.status(for: inReplyToID) { (status) in - guard let status = status else { return } - DispatchQueue.main.async { - self.updateInReplyTo(inReplyTo: status) - loadingVC.removeViewAndController() + let request = Client.getStatus(id: inReplyToID) + mastodonController.run(request) { (response) in + guard case let .success(status, _) = response else { return } + self.mastodonController.persistentContainer.addOrUpdate(status: status, incrementReferenceCount: true) { + 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 if Preferences.shared.contentWarningCopyMode == .doNotCopy { contentWarningEnabled = false @@ -470,7 +474,7 @@ class ComposeViewController: UIViewController { self.mastodonController.run(request) { (response) in guard case let .success(status, _) = response else { fatalError() } self.postedStatus = status - self.mastodonController.cache.add(status: status) +// self.mastodonController.persistentContainer.addOrUpdate(status: status, incrementReferenceCount: true) if let draft = self.currentDraft { DraftsManager.shared.remove(draft) @@ -481,8 +485,8 @@ class ComposeViewController: UIViewController { self.dismiss(animated: true) // todo: this doesn't work - let conversationVC = ConversationTableViewController(for: status.id, mastodonController: self.mastodonController) - self.show(conversationVC, sender: self) +// let conversationVC = ConversationTableViewController(for: status.id, mastodonController: self.mastodonController) +// self.show(conversationVC, sender: self) self.xcbSession?.complete(with: .success, additionalData: [ "statusURL": status.url?.absoluteString, diff --git a/Tusker/Screens/Lists/EditListAccountsViewController.swift b/Tusker/Screens/Lists/EditListAccountsViewController.swift index be46daf8..6857aee7 100644 --- a/Tusker/Screens/Lists/EditListAccountsViewController.swift +++ b/Tusker/Screens/Lists/EditListAccountsViewController.swift @@ -80,7 +80,7 @@ class EditListAccountsViewController: EnhancedTableViewController { self.nextRange = pagination?.older - self.mastodonController.cache.addAll(accounts: accounts) + self.mastodonController.persistentContainer.addAll(accounts: accounts) var snapshot = self.dataSource.snapshot() snapshot.deleteSections([.accounts]) diff --git a/Tusker/Screens/Notifications/NotificationsTableViewController.swift b/Tusker/Screens/Notifications/NotificationsTableViewController.swift index f0366927..f93743a6 100644 --- a/Tusker/Screens/Notifications/NotificationsTableViewController.swift +++ b/Tusker/Screens/Notifications/NotificationsTableViewController.swift @@ -142,9 +142,8 @@ class NotificationsTableViewController: EnhancedTableViewController { } self.groups.append(contentsOf: groups) - self.mastodonController.cache.addAll(notifications: newNotifications) - self.mastodonController.cache.addAll(statuses: newNotifications.compactMap { $0.status }) - self.mastodonController.cache.addAll(accounts: newNotifications.map { $0.account }) + self.mastodonController.persistentContainer.addAll(statuses: newNotifications.compactMap { $0.status }) + self.mastodonController.persistentContainer.addAll(accounts: newNotifications.map { $0.account }) self.older = pagination?.older @@ -220,9 +219,8 @@ class NotificationsTableViewController: EnhancedTableViewController { self.groups.insert(contentsOf: groups, at: 0) - self.mastodonController.cache.addAll(notifications: newNotifications) - self.mastodonController.cache.addAll(statuses: newNotifications.compactMap { $0.status }) - self.mastodonController.cache.addAll(accounts: newNotifications.map { $0.account }) + self.mastodonController.persistentContainer.addAll(statuses: newNotifications.compactMap { $0.status }) + self.mastodonController.persistentContainer.addAll(accounts: newNotifications.map { $0.account }) if let newer = pagination?.newer { self.newer = newer diff --git a/Tusker/Screens/Profile/ProfileTableViewController.swift b/Tusker/Screens/Profile/ProfileTableViewController.swift index c0387b15..0469e923 100644 --- a/Tusker/Screens/Profile/ProfileTableViewController.swift +++ b/Tusker/Screens/Profile/ProfileTableViewController.swift @@ -78,8 +78,9 @@ class ProfileTableViewController: EnhancedTableViewController { } else { loadingVC = LoadingViewController() embedChild(loadingVC!) - mastodonController.cache.account(for: accountID) { (account) in - guard let account = account else { + let request = Client.getAccount(id: accountID) + 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) alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (_) in self.navigationController!.popViewController(animated: true) @@ -279,11 +280,10 @@ extension ProfileTableViewController: ProfileHeaderTableViewCellDelegate { func showMoreOptions(cell: ProfileHeaderTableViewCell) { let account = mastodonController.persistentContainer.account(for: accountID)! - mastodonController.cache.relationship(for: account.id) { [weak self] (relationship) in - guard let self = self else { return } - + let request = Client.getRelationships(accounts: [account.id]) + mastodonController.run(request) { (response) in var customActivities: [UIActivity] = [OpenInSafariActivity()] - if let relationship = relationship { + if case let .success(results, _) = response, let relationship = results.first { let toggleFollowActivity = relationship.following ? UnfollowAccountActivity() : FollowAccountActivity() customActivities.insert(toggleFollowActivity, at: 0) } diff --git a/Tusker/Screens/Status Action Account List/StatusActionAccountListTableViewController.swift b/Tusker/Screens/Status Action Account List/StatusActionAccountListTableViewController.swift index 7e367d6c..e5e9f850 100644 --- a/Tusker/Screens/Status Action Account List/StatusActionAccountListTableViewController.swift +++ b/Tusker/Screens/Status Action Account List/StatusActionAccountListTableViewController.swift @@ -82,7 +82,7 @@ class StatusActionAccountListTableViewController: EnhancedTableViewController { let request = actionType == .favorite ? Status.getFavourites(status.id) : Status.getReblogs(status.id) mastodonController.run(request) { (response) in guard case let .success(accounts, _) = response else { fatalError() } - self.mastodonController.cache.addAll(accounts: accounts) + self.mastodonController.persistentContainer.addAll(accounts: accounts) DispatchQueue.main.async { self.accountIDs = accounts.map { $0.id } self.tableView.tableFooterView = nil diff --git a/Tusker/Screens/Utilities/Previewing.swift b/Tusker/Screens/Utilities/Previewing.swift index 3b85f264..dc5b9073 100644 --- a/Tusker/Screens/Utilities/Previewing.swift +++ b/Tusker/Screens/Utilities/Previewing.swift @@ -30,7 +30,7 @@ extension MenuPreviewProvider { func actionsForProfile(accountID: String, sourceView: UIView?) -> [UIAction] { guard let mastodonController = mastodonController, - let account = mastodonController.cache.account(for: accountID) else { return [] } + let account = mastodonController.persistentContainer.account(for: accountID) else { return [] } return [ createAction(identifier: "sendmessage", title: "Send Message", systemImageName: "envelope", handler: { (_) in self.navigationDelegate?.compose(mentioning: account.acct) @@ -61,7 +61,7 @@ extension MenuPreviewProvider { func actionsForStatus(statusID: String, sourceView: UIView?) -> [UIAction] { guard let mastodonController = mastodonController, - let status = mastodonController.cache.status(for: statusID) else { return [] } + let status = mastodonController.persistentContainer.status(for: statusID) else { return [] } return [ createAction(identifier: "reply", title: "Reply", systemImageName: "arrowshape.turn.up.left", handler: { (_) in self.navigationDelegate?.reply(to: statusID) diff --git a/Tusker/TuskerNavigationDelegate.swift b/Tusker/TuskerNavigationDelegate.swift index 977e7795..377b9734 100644 --- a/Tusker/TuskerNavigationDelegate.swift +++ b/Tusker/TuskerNavigationDelegate.swift @@ -203,17 +203,14 @@ extension TuskerNavigationDelegate where Self: UIViewController { } 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)") } var customActivites: [UIActivity] = [OpenInSafariActivity()] - if let bookmarked = status.bookmarked { - customActivites.insert(bookmarked ? UnbookmarkStatusActivity() : BookmarkStatusActivity(), at: 0) - } - - if status.account == apiController.account, - let pinned = status.pinned { - customActivites.insert(pinned ? UnpinStatusActivity() : PinStatusActivity(), at: 1) + customActivites.insert(status.bookmarked ? UnbookmarkStatusActivity() : BookmarkStatusActivity(), at: 0) + + if status.account.id == apiController.account.id { + customActivites.insert(status.pinned ? UnpinStatusActivity() : PinStatusActivity(), at: 1) } let activityController = UIActivityViewController(activityItems: [url, status], applicationActivities: customActivites) @@ -222,7 +219,7 @@ extension TuskerNavigationDelegate where Self: UIViewController { } 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) } diff --git a/Tusker/Views/Compose Status Reply/ComposeStatusReplyView.swift b/Tusker/Views/Compose Status Reply/ComposeStatusReplyView.swift index a49cd9fa..b2ca19f8 100644 --- a/Tusker/Views/Compose Status Reply/ComposeStatusReplyView.swift +++ b/Tusker/Views/Compose Status Reply/ComposeStatusReplyView.swift @@ -39,7 +39,7 @@ class ComposeStatusReplyView: UIView { avatarImageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: avatarImageView) } - func updateUI(for status: Status) { + func updateUI(for status: StatusMO) { displayNameLabel.updateForAccountDisplayName(account: status.account) usernameLabel.text = "@\(status.account.acct)" statusContentTextView.overrideMastodonController = mastodonController diff --git a/Tusker/Views/Profile Header/ProfileHeaderTableViewCell.swift b/Tusker/Views/Profile Header/ProfileHeaderTableViewCell.swift index d881683b..9634fc4f 100644 --- a/Tusker/Views/Profile Header/ProfileHeaderTableViewCell.swift +++ b/Tusker/Views/Profile Header/ProfileHeaderTableViewCell.swift @@ -88,14 +88,13 @@ class ProfileHeaderTableViewCell: UITableViewCell { noteTextView.setTextFromHtml(account.note) noteTextView.setEmojis(account.emojis) + // don't show relationship label for the user's own account if accountID != mastodonController.account.id { - // don't show relationship label for the user's own account - if let relationship = mastodonController.cache.relationship(for: accountID) { - followsYouLabel.isHidden = !relationship.followedBy - } else { - mastodonController.cache.relationship(for: accountID) { relationship in + let request = Client.getRelationships(accounts: [accountID]) + mastodonController.run(request) { (response) in + if case let .success(results, _) = response, let relationship = results.first { DispatchQueue.main.async { - self.followsYouLabel.isHidden = !(relationship?.followedBy ?? false) + self.followsYouLabel.isHidden = !relationship.followedBy } } } diff --git a/Tusker/Views/Status/BaseStatusTableViewCell.swift b/Tusker/Views/Status/BaseStatusTableViewCell.swift index 8e5ecad5..159e6db8 100644 --- a/Tusker/Views/Status/BaseStatusTableViewCell.swift +++ b/Tusker/Views/Status/BaseStatusTableViewCell.swift @@ -251,18 +251,18 @@ class BaseStatusTableViewCell: UITableViewCell { } @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 favorited = !favorited - let realStatus: Status = status.reblog ?? status + let realStatus = status.reblog ?? status let request = (favorited ? Status.favourite : Status.unfavourite)(realStatus.id) mastodonController.run(request) { response in DispatchQueue.main.async { if case let .success(newStatus, _) = response { self.favorited = newStatus.favourited ?? false - self.mastodonController.cache.add(status: newStatus) + self.mastodonController.persistentContainer.addOrUpdate(status: newStatus, incrementReferenceCount: false) UIImpactFeedbackGenerator(style: .light).impactOccurred() } else { self.favorited = oldValue @@ -276,18 +276,18 @@ class BaseStatusTableViewCell: UITableViewCell { } @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 reblogged = !reblogged - let realStatus: Status = status.reblog ?? status + let realStatus = status.reblog ?? status let request = (reblogged ? Status.reblog : Status.unreblog)(realStatus.id) mastodonController.run(request) { response in DispatchQueue.main.async { if case let .success(newStatus, _) = response { self.reblogged = newStatus.reblogged ?? false - self.mastodonController.cache.add(status: newStatus) + self.mastodonController.persistentContainer.addOrUpdate(status: newStatus, incrementReferenceCount: false) UIImpactFeedbackGenerator(style: .light).impactOccurred() } else { self.reblogged = oldValue diff --git a/Tusker/Views/Status/TimelineStatusTableViewCell.swift b/Tusker/Views/Status/TimelineStatusTableViewCell.swift index afffdfc5..90a5e8fc 100644 --- a/Tusker/Views/Status/TimelineStatusTableViewCell.swift +++ b/Tusker/Views/Status/TimelineStatusTableViewCell.swift @@ -136,7 +136,7 @@ class TimelineStatusTableViewCell: BaseStatusTableViewCell { func reply() { if Preferences.shared.mentionReblogger, let rebloggerID = rebloggerID, - let rebloggerAccount = mastodonController.cache.account(for: rebloggerID) { + let rebloggerAccount = mastodonController.persistentContainer.account(for: rebloggerID) { delegate?.reply(to: statusID, mentioningAcct: rebloggerAccount.acct) } else { delegate?.reply(to: statusID) diff --git a/Tusker/Views/StatusContentTextView.swift b/Tusker/Views/StatusContentTextView.swift index 813d6347..943eb64d 100644 --- a/Tusker/Views/StatusContentTextView.swift +++ b/Tusker/Views/StatusContentTextView.swift @@ -27,7 +27,7 @@ class StatusContentTextView: ContentTextView { let mention: Mention? if let statusID = statusID, let mastodonController = mastodonController, - let status = mastodonController.cache.status(for: statusID) { + let status = mastodonController.persistentContainer.status(for: statusID) { mention = status.mentions.first { (mention) in // Mastodon and Pleroma include the @ in the text, GNU Social does not (text.dropFirst() == mention.username || text == mention.username) && url.host == mention.url.host @@ -42,7 +42,7 @@ class StatusContentTextView: ContentTextView { let hashtag: Hashtag? if let statusID = statusID, let mastodonController = mastodonController, - let status = mastodonController.cache.status(for: statusID) { + let status = mastodonController.persistentContainer.status(for: statusID) { hashtag = status.hashtags.first { (hashtag) in hashtag.url == url }