From 137a537f6865a336e01761c7b4c92e7772a098f4 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sat, 4 Feb 2023 13:14:03 -0500 Subject: [PATCH] Extract loading and local updating handling code from bookmarks VC into separate VC --- Tusker.xcodeproj/project.pbxproj | 10 ++- .../BookmarksViewController.swift | 27 +++++++ ...ocalPredicateStatusesViewController.swift} | 79 ++++++++++--------- 3 files changed, 77 insertions(+), 39 deletions(-) create mode 100644 Tusker/Screens/Local Predicate Statuses List/BookmarksViewController.swift rename Tusker/Screens/{Bookmarks/BookmarksViewController.swift => Local Predicate Statuses List/LocalPredicateStatusesViewController.swift} (85%) diff --git a/Tusker.xcodeproj/project.pbxproj b/Tusker.xcodeproj/project.pbxproj index 63b979d8..f6da57a7 100644 --- a/Tusker.xcodeproj/project.pbxproj +++ b/Tusker.xcodeproj/project.pbxproj @@ -295,6 +295,7 @@ D6BED174212667E900F02DA0 /* TimelineStatusTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6BED173212667E900F02DA0 /* TimelineStatusTableViewCell.swift */; }; D6C143DA253510F4007DC240 /* ComposeEmojiTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6C143D9253510F4007DC240 /* ComposeEmojiTextField.swift */; }; D6C1B2082545D1EC00DAAA66 /* StatusCardView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6C1B2072545D1EC00DAAA66 /* StatusCardView.swift */; }; + D6C3F4F5298ED0890009FCFF /* LocalPredicateStatusesViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6C3F4F4298ED0890009FCFF /* LocalPredicateStatusesViewController.swift */; }; D6C693EF216192C2007D6A6D /* TuskerNavigationDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6C693EE216192C2007D6A6D /* TuskerNavigationDelegate.swift */; }; D6C693FC2162FE6F007D6A6D /* LoadingViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6C693FB2162FE6F007D6A6D /* LoadingViewController.swift */; }; D6C693FE2162FEEA007D6A6D /* UIViewController+Children.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6C693FD2162FEEA007D6A6D /* UIViewController+Children.swift */; }; @@ -702,6 +703,7 @@ D6BED173212667E900F02DA0 /* TimelineStatusTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimelineStatusTableViewCell.swift; sourceTree = ""; }; D6C143D9253510F4007DC240 /* ComposeEmojiTextField.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ComposeEmojiTextField.swift; sourceTree = ""; }; D6C1B2072545D1EC00DAAA66 /* StatusCardView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatusCardView.swift; sourceTree = ""; }; + D6C3F4F4298ED0890009FCFF /* LocalPredicateStatusesViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocalPredicateStatusesViewController.swift; sourceTree = ""; }; D6C693EE216192C2007D6A6D /* TuskerNavigationDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TuskerNavigationDelegate.swift; sourceTree = ""; }; D6C693FB2162FE6F007D6A6D /* LoadingViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoadingViewController.swift; sourceTree = ""; }; D6C693FD2162FEEA007D6A6D /* UIViewController+Children.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIViewController+Children.swift"; sourceTree = ""; }; @@ -937,12 +939,13 @@ path = Explore; sourceTree = ""; }; - D627944823A6AD5100D38C68 /* Bookmarks */ = { + D627944823A6AD5100D38C68 /* Local Predicate Statuses List */ = { isa = PBXGroup; children = ( + D6C3F4F4298ED0890009FCFF /* LocalPredicateStatusesViewController.swift */, D6DD8FFE2984D327002AD3FD /* BookmarksViewController.swift */, ); - path = Bookmarks; + path = "Local Predicate Statuses List"; sourceTree = ""; }; D627944B23A9A02400D38C68 /* Lists */ = { @@ -1005,7 +1008,6 @@ D6A3BC822321F69400FD64D5 /* Account List */, D6B053A023BD2BED00A066FA /* Asset Picker */, 0411610522B457290030A9B7 /* Attachment Gallery */, - D627944823A6AD5100D38C68 /* Bookmarks */, D641C787213DD862004B4513 /* Compose */, D641C785213DD83B004B4513 /* Conversation */, D6F2E960249E772F005846BB /* Crash Reporter */, @@ -1014,6 +1016,7 @@ D61F759729384D4200C0B37F /* Customize Timelines */, D641C788213DD86D004B4513 /* Large Image */, D627944B23A9A02400D38C68 /* Lists */, + D627944823A6AD5100D38C68 /* Local Predicate Statuses List */, D641C782213DD7F0004B4513 /* Main */, D6F6A555291F4F0C00F496A8 /* Mute */, D641C786213DD852004B4513 /* Notifications */, @@ -1939,6 +1942,7 @@ D6285B5321EA708700FE4B39 /* StatusFormat.swift in Sources */, D6DD353D22F28CD000A9563A /* ContentWarningCopyMode.swift in Sources */, 0427033A22B31269000D31B6 /* AdvancedPrefsView.swift in Sources */, + D6C3F4F5298ED0890009FCFF /* LocalPredicateStatusesViewController.swift in Sources */, D662AEEF263A3B880082A153 /* PollFinishedTableViewCell.swift in Sources */, D626493C23C1000300612E6E /* AlbumTableViewCell.swift in Sources */, D62275A624F1C81800B82A16 /* ComposeReplyView.swift in Sources */, diff --git a/Tusker/Screens/Local Predicate Statuses List/BookmarksViewController.swift b/Tusker/Screens/Local Predicate Statuses List/BookmarksViewController.swift new file mode 100644 index 00000000..b6701f7c --- /dev/null +++ b/Tusker/Screens/Local Predicate Statuses List/BookmarksViewController.swift @@ -0,0 +1,27 @@ +// +// BookmarksViewController.swift +// Tusker +// +// Created by Shadowfacts on 12/15/19. +// Copyright © 2019 Shadowfacts. All rights reserved. +// + +import UIKit +import Pachyderm + +class BookmarksViewController: LocalPredicateStatusesViewController { + + init(mastodonController: MastodonController) { + super.init( + predicate: { $0.bookmarked ?? false }, + predicateTitle: "Bookmarks", + request: { Client.getBookmarks(range: $0) }, + mastodonController: mastodonController + ) + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + +} diff --git a/Tusker/Screens/Bookmarks/BookmarksViewController.swift b/Tusker/Screens/Local Predicate Statuses List/LocalPredicateStatusesViewController.swift similarity index 85% rename from Tusker/Screens/Bookmarks/BookmarksViewController.swift rename to Tusker/Screens/Local Predicate Statuses List/LocalPredicateStatusesViewController.swift index cd77c8a0..b2af71bc 100644 --- a/Tusker/Screens/Bookmarks/BookmarksViewController.swift +++ b/Tusker/Screens/Local Predicate Statuses List/LocalPredicateStatusesViewController.swift @@ -1,20 +1,23 @@ // -// BookmarksViewController.swift +// LocalPredicateStatusesViewController.swift // Tusker // -// Created by Shadowfacts on 12/15/19. -// Copyright © 2019 Shadowfacts. All rights reserved. +// Created by Shadowfacts on 2/4/23. +// Copyright © 2023 Shadowfacts. All rights reserved. // import UIKit import Pachyderm import CoreData -class BookmarksViewController: UIViewController, CollectionViewController, RefreshableViewController { +class LocalPredicateStatusesViewController: UIViewController, CollectionViewController, RefreshableViewController { private static let pageSize = 40 - + let mastodonController: MastodonController + private let predicate: (StatusMO) -> Bool + private let predicateTitle: String + private let request: (RequestRange) -> Request<[Status]> var collectionView: UICollectionView! { view as? UICollectionView @@ -25,12 +28,15 @@ class BookmarksViewController: UIViewController, CollectionViewController, Refre private var newer: RequestRange? private var older: RequestRange? - init(mastodonController: MastodonController) { + init(predicate: @escaping (StatusMO) -> Bool, predicateTitle: String, request: @escaping (RequestRange) -> Request<[Status]>, mastodonController: MastodonController) { self.mastodonController = mastodonController + self.predicate = predicate + self.predicateTitle = predicateTitle + self.request = request super.init(nibName: nil, bundle: nil) - title = NSLocalizedString("Bookmarks", comment: "bookmarks screen title") + self.title = predicateTitle } required init?(coder: NSCoder) { @@ -50,7 +56,7 @@ class BookmarksViewController: UIViewController, CollectionViewController, Refre return sectionConfig } var config = sectionConfig - if item.hideIndicators { + if item.hideSeparators { config.topSeparatorVisibility = .hidden config.bottomSeparatorVisibility = .hidden } else { @@ -100,7 +106,7 @@ class BookmarksViewController: UIViewController, CollectionViewController, Refre collectionView.refreshControl!.addTarget(self, action: #selector(refresh), for: .valueChanged) #endif - addKeyCommand(MenuController.refreshCommand(discoverabilityTitle: "Refresh Bookmarks")) + addKeyCommand(MenuController.refreshCommand(discoverabilityTitle: "Refresh \(predicateTitle)")) NotificationCenter.default.addObserver(self, selector: #selector(handleStatusDeleted), name: .statusDeleted, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(managedObjectsDidChange), name: .NSManagedObjectContextObjectsDidChange, object: mastodonController.persistentContainer.viewContext) @@ -129,12 +135,12 @@ class BookmarksViewController: UIViewController, CollectionViewController, Refre state = .loadingInitial var snapshot = NSDiffableDataSourceSnapshot() - snapshot.appendSections([.bookmarks]) + snapshot.appendSections([.statuses]) snapshot.appendItems([.loadingIndicator]) await apply(snapshot: snapshot, animatingDifferences: false) do { - let req = Client.getBookmarks(range: .count(BookmarksViewController.pageSize)) + let req = request(.count(Self.pageSize)) let (statuses, pagination) = try await mastodonController.run(req) newer = pagination?.newer older = pagination?.older @@ -142,14 +148,14 @@ class BookmarksViewController: UIViewController, CollectionViewController, Refre await mastodonController.persistentContainer.addAll(statuses: statuses) var snapshot = NSDiffableDataSourceSnapshot() - snapshot.appendSections([.bookmarks]) + snapshot.appendSections([.statuses]) snapshot.appendItems(statuses.map { .status(id: $0.id, state: .unknown, addedLocally: false) }) await apply(snapshot: snapshot, animatingDifferences: true) state = .loaded } catch { - let config = ToastConfiguration(from: error, with: "Error Loading Bookmarks", in: self) { [weak self] toast in + let config = ToastConfiguration(from: error, with: "Error Loading \(predicateTitle)", in: self) { [weak self] toast in toast.dismissToast(animated: true) await self?.loadInitial() } @@ -174,7 +180,7 @@ class BookmarksViewController: UIViewController, CollectionViewController, Refre await apply(snapshot: snapshot, animatingDifferences: false) do { - let req = Client.getBookmarks(range: older.withCount(BookmarksViewController.pageSize)) + let req = request(older.withCount(Self.pageSize)) let (statuses, pagination) = try await mastodonController.run(req) self.older = pagination?.older @@ -184,12 +190,13 @@ class BookmarksViewController: UIViewController, CollectionViewController, Refre snapshot.appendItems(statuses.map { .status(id: $0.id, state: .unknown, addedLocally: false) }) await apply(snapshot: snapshot, animatingDifferences: true) } catch { - let config = ToastConfiguration(from: error, with: "Error Loading Older Bookmarks", in: self) { [weak self] toast in + let config = ToastConfiguration(from: error, with: "Error Loading Older \(predicateTitle)", in: self) { [weak self] toast in toast.dismissToast(animated: true) await self?.loadOlder() } showToast(configuration: config, animated: true) + var snapshot = dataSource.snapshot() snapshot.deleteItems([.loadingIndicator]) await apply(snapshot: snapshot, animatingDifferences: false) } @@ -234,7 +241,7 @@ class BookmarksViewController: UIViewController, CollectionViewController, Refre } var hasChanges = false if let inserted = notification.userInfo?[NSInsertedObjectsKey] as? Set { - for case let status as StatusMO in inserted where status.bookmarked == true { + for case let status as StatusMO in inserted where predicate(status) { prepend(item: .status(id: status.id, state: .unknown, addedLocally: true)) hasChanges = true } @@ -243,10 +250,10 @@ class BookmarksViewController: UIViewController, CollectionViewController, Refre for case let status as StatusMO in updated { let item = Item.status(id: status.id, state: .unknown, addedLocally: true) let exists = snapshot.itemIdentifiers.contains(item) - if status.bookmarked == true && !exists { + if predicate(status) && !exists { prepend(item: item) hasChanges = true - } else if status.bookmarked == false && exists { + } else if !predicate(status) && exists { snapshot.deleteItems([item]) hasChanges = true } @@ -272,22 +279,21 @@ class BookmarksViewController: UIViewController, CollectionViewController, Refre state = .loadingNewer Task { - do { - let req = Client.getBookmarks(range: newer.withCount(BookmarksViewController.pageSize)) + let req = request(newer.withCount(Self.pageSize)) let (statuses, pagination) = try await mastodonController.run(req) self.newer = pagination?.newer await mastodonController.persistentContainer.addAll(statuses: statuses) var snapshot = dataSource.snapshot() - let localItems: [String: CollapseState] = Dictionary(uniqueKeysWithValues: snapshot.itemIdentifiers.compactMap({ + let localItems: [String: CollapseState] = Dictionary(uniqueKeysWithValues: snapshot.itemIdentifiers.compactMap { if case .status(id: let id, state: let state, addedLocally: true) = $0 { return (id, state) } else { return nil } - })) + }) var newItems: [Item] = [] for status in statuses { let state: CollapseState @@ -305,8 +311,9 @@ class BookmarksViewController: UIViewController, CollectionViewController, Refre snapshot.appendItems(newItems) } await apply(snapshot: snapshot, animatingDifferences: true) + } catch { - let config = ToastConfiguration(from: error, with: "Error Refreshing Bookmarks", in: self) { [weak self] toast in + let config = ToastConfiguration(from: error, with: "Error Refreshing \(predicateTitle)", in: self) { [weak self] toast in toast.dismissToast(animated: true) self?.refresh() } @@ -322,15 +329,15 @@ class BookmarksViewController: UIViewController, CollectionViewController, Refre } -extension BookmarksViewController { +extension LocalPredicateStatusesViewController { enum Section { - case bookmarks + case statuses } enum Item: Equatable, Hashable { case status(id: String, state: CollapseState, addedLocally: Bool) case loadingIndicator - var hideIndicators: Bool { + var hideSeparators: Bool { switch self { case .loadingIndicator: return true @@ -362,7 +369,7 @@ extension BookmarksViewController { } } -extension BookmarksViewController { +extension LocalPredicateStatusesViewController { enum State { case unloaded case loadingInitial @@ -372,7 +379,7 @@ extension BookmarksViewController { } } -extension BookmarksViewController: UICollectionViewDelegate { +extension LocalPredicateStatusesViewController: UICollectionViewDelegate { func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) { if indexPath.section == 0, indexPath.row == collectionView.numberOfItems(inSection: indexPath.section) - 1 { @@ -397,7 +404,7 @@ extension BookmarksViewController: UICollectionViewDelegate { } func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? { - (collectionView.cellForItem(at: indexPath) as? TimelineStatusCollectionViewCell)?.contextMenuConfiguration() + return (collectionView.cellForItem(at: indexPath) as? TimelineStatusCollectionViewCell)?.contextMenuConfiguration() } func collectionView(_ collectionView: UICollectionView, willPerformPreviewActionForMenuWith configuration: UIContextMenuConfiguration, animator: UIContextMenuInteractionCommitAnimating) { @@ -405,17 +412,17 @@ extension BookmarksViewController: UICollectionViewDelegate { } } -extension BookmarksViewController: UICollectionViewDragDelegate { +extension LocalPredicateStatusesViewController: UICollectionViewDragDelegate { func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] { - (collectionView.cellForItem(at: indexPath) as? TimelineStatusCollectionViewCell)?.dragItemsForBeginning(session: session) ?? [] + return (collectionView.cellForItem(at: indexPath) as? TimelineStatusCollectionViewCell)?.dragItemsForBeginning(session: session) ?? [] } } -extension BookmarksViewController: TuskerNavigationDelegate { +extension LocalPredicateStatusesViewController: TuskerNavigationDelegate { var apiController: MastodonController! { mastodonController } } -extension BookmarksViewController: StatusCollectionViewCellDelegate { +extension LocalPredicateStatusesViewController: StatusCollectionViewCellDelegate { func statusCellNeedsReconfigure(_ cell: StatusCollectionViewCell, animated: Bool, completion: (() -> Void)?) { if let indexPath = collectionView.indexPath(for: cell) { var snapshot = dataSource.snapshot() @@ -425,17 +432,17 @@ extension BookmarksViewController: StatusCollectionViewCellDelegate { } func statusCellShowFiltered(_ cell: StatusCollectionViewCell) { - // bookmarks aren't filtered + // filtering isn't supported here } } -extension BookmarksViewController: TabBarScrollableViewController { +extension LocalPredicateStatusesViewController: TabBarScrollableViewController { func tabBarScrollToTop() { collectionView.scrollToTop() } } -extension BookmarksViewController: StatusBarTappableViewController { +extension LocalPredicateStatusesViewController: StatusBarTappableViewController { func handleStatusBarTapped(xPosition: CGFloat) -> StatusBarTapActionResult { collectionView.scrollToTop() return .stop