From ee5e0493553f84a6c189aca8296beb2b31ced8ab Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Mon, 27 Apr 2020 19:25:41 -0400 Subject: [PATCH] Use CoreData for bookmarks and search results --- Pachyderm/Model/Status.swift | 4 ++-- .../UnbookmarkStatusActivity.swift | 2 +- .../Bookmarks/BookmarksTableViewController.swift | 16 ++++++++-------- .../Search/SearchResultsViewController.swift | 6 +++--- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/Pachyderm/Model/Status.swift b/Pachyderm/Model/Status.swift index fe5892e5..3662fe04 100644 --- a/Pachyderm/Model/Status.swift +++ b/Pachyderm/Model/Status.swift @@ -90,8 +90,8 @@ public class Status: Decodable { return Request(method: .post, path: "/api/v1/statuses/\(status.id)/bookmark") } - public static func unbookmark(_ status: Status) -> Request { - return Request(method: .post, path: "/api/v1/statuses/\(status.id)/unbookmark") + public static func unbookmark(_ statusID: String) -> Request { + return Request(method: .post, path: "/api/v1/statuses/\(statusID)/unbookmark") } public static func muteConversation(_ status: Status) -> Request { diff --git a/Tusker/Activities/Status Activities/UnbookmarkStatusActivity.swift b/Tusker/Activities/Status Activities/UnbookmarkStatusActivity.swift index 8cce299b..489c3206 100644 --- a/Tusker/Activities/Status Activities/UnbookmarkStatusActivity.swift +++ b/Tusker/Activities/Status Activities/UnbookmarkStatusActivity.swift @@ -26,7 +26,7 @@ class UnbookmarkStatusActivity: StatusActivity { override func perform() { guard let status = status else { return } - let request = Status.unbookmark(status) + let request = Status.unbookmark(status.id) mastodonController.run(request) { (response) in if case let .success(status, _) = response { self.mastodonController.cache.add(status: status) diff --git a/Tusker/Screens/Bookmarks/BookmarksTableViewController.swift b/Tusker/Screens/Bookmarks/BookmarksTableViewController.swift index 641c864d..323d88a3 100644 --- a/Tusker/Screens/Bookmarks/BookmarksTableViewController.swift +++ b/Tusker/Screens/Bookmarks/BookmarksTableViewController.swift @@ -45,7 +45,7 @@ class BookmarksTableViewController: EnhancedTableViewController { let request = Client.getBookmarks() mastodonController.run(request) { (response) in guard case let .success(statuses, pagination) = response else { fatalError() } - self.mastodonController.cache.addAll(statuses: statuses) + self.mastodonController.persistentContainer.addAll(statuses: statuses) self.statuses.append(contentsOf: statuses.map { ($0.id, .unknown) }) self.newer = pagination?.newer self.older = pagination?.older @@ -87,7 +87,7 @@ class BookmarksTableViewController: EnhancedTableViewController { mastodonController.run(request) { (response) in guard case let .success(newStatuses, pagination) = response else { fatalError() } self.older = pagination?.older - self.mastodonController.cache.addAll(statuses: newStatuses) + self.mastodonController.persistentContainer.addAll(statuses: newStatuses) let newIndexPaths = (self.statuses.count..<(self.statuses.count + newStatuses.count)).map { IndexPath(row: $0, section: 0) } @@ -112,12 +112,12 @@ class BookmarksTableViewController: EnhancedTableViewController { override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { let cellConfig = (tableView.cellForRow(at: indexPath) as? TableViewSwipeActionProvider)?.trailingSwipeActionsConfiguration() - guard let status = mastodonController.cache.status(for: statuses[indexPath.row].id) else { + guard let status = mastodonController.persistentContainer.status(for: statuses[indexPath.row].id) else { return cellConfig } let unbookmarkAction = UIContextualAction(style: .destructive, title: NSLocalizedString("Unbookmark", comment: "unbookmark action title")) { (action, view, completion) in - let request = Status.unbookmark(status) + 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) @@ -138,10 +138,10 @@ class BookmarksTableViewController: EnhancedTableViewController { } override func getSuggestedContextMenuActions(tableView: UITableView, indexPath: IndexPath, point: CGPoint) -> [UIAction] { - guard let status = mastodonController.cache.status(for: statuses[indexPath.row].id) else { return [] } + guard let status = mastodonController.persistentContainer.status(for: statuses[indexPath.row].id) else { return [] } return [ UIAction(title: NSLocalizedString("Unbookmark", comment: "unbookmark action title"), image: UIImage(systemName: "bookmark.fill"), identifier: .init("unbookmark"), discoverabilityTitle: nil, attributes: [], state: .off, handler: { (_) in - let request = Status.unbookmark(status) + 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) @@ -165,7 +165,7 @@ extension BookmarksTableViewController: StatusTableViewCellDelegate { extension BookmarksTableViewController: UITableViewDataSourcePrefetching { func tableView(_ tableView: UITableView, prefetchRowsAt indexPaths: [IndexPath]) { for indexPath in indexPaths { - guard let status = mastodonController.cache.status(for: statuses[indexPath.row].id) else { continue } + guard let status = mastodonController.persistentContainer.status(for: statuses[indexPath.row].id) else { continue } _ = ImageCache.avatars.get(status.account.avatar, completion: nil) for attachment in status.attachments where attachment.kind == .image { _ = ImageCache.attachments.get(attachment.url, completion: nil) @@ -175,7 +175,7 @@ extension BookmarksTableViewController: UITableViewDataSourcePrefetching { func tableView(_ tableView: UITableView, cancelPrefetchingForRowsAt indexPaths: [IndexPath]) { for indexPath in indexPaths { - guard let status = mastodonController.cache.status(for: statuses[indexPath.row].id) else { continue } + guard let status = mastodonController.persistentContainer.status(for: statuses[indexPath.row].id) else { continue } ImageCache.avatars.cancelWithoutCallback(status.account.avatar) for attachment in status.attachments where attachment.kind == .image { ImageCache.attachments.cancelWithoutCallback(attachment.url) diff --git a/Tusker/Screens/Search/SearchResultsViewController.swift b/Tusker/Screens/Search/SearchResultsViewController.swift index 9c973461..4cc83297 100644 --- a/Tusker/Screens/Search/SearchResultsViewController.swift +++ b/Tusker/Screens/Search/SearchResultsViewController.swift @@ -136,7 +136,7 @@ class SearchResultsViewController: EnhancedTableViewController { if self.onlySections.contains(.accounts) && !results.accounts.isEmpty { snapshot.appendSections([.accounts]) snapshot.appendItems(results.accounts.map { .account($0.id) }, toSection: .accounts) - self.mastodonController.cache.addAll(accounts: results.accounts) + self.mastodonController.persistentContainer.addAll(accounts: results.accounts) } if self.onlySections.contains(.hashtags) && !results.hashtags.isEmpty { snapshot.appendSections([.hashtags]) @@ -145,8 +145,8 @@ class SearchResultsViewController: EnhancedTableViewController { if self.onlySections.contains(.statuses) && !results.statuses.isEmpty { snapshot.appendSections([.statuses]) snapshot.appendItems(results.statuses.map { .status($0.id, .unknown) }, toSection: .statuses) - self.mastodonController.cache.addAll(statuses: results.statuses) - self.mastodonController.cache.addAll(accounts: results.statuses.map { $0.account }) + self.mastodonController.persistentContainer.addAll(statuses: results.statuses) + self.mastodonController.persistentContainer.addAll(accounts: results.statuses.map { $0.account }) } self.dataSource.apply(snapshot) }