From 266868376d5401ae95bf579520ad9bf99ab17754 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Wed, 22 Feb 2023 21:52:45 -0500 Subject: [PATCH] Allow refreshing conversations Closes #157 --- ...ConversationCollectionViewController.swift | 27 +++++++++++++++---- .../ConversationViewController.swift | 11 +++++++- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/Tusker/Screens/Conversation/ConversationCollectionViewController.swift b/Tusker/Screens/Conversation/ConversationCollectionViewController.swift index 519caa24..f0274978 100644 --- a/Tusker/Screens/Conversation/ConversationCollectionViewController.swift +++ b/Tusker/Screens/Conversation/ConversationCollectionViewController.swift @@ -9,8 +9,9 @@ import UIKit import Pachyderm -class ConversationCollectionViewController: UIViewController, CollectionViewController { +class ConversationCollectionViewController: UIViewController, CollectionViewController, RefreshableViewController { + private unowned let conversationViewController: ConversationViewController private let mastodonController: MastodonController private let mainStatusID: String private let mainStatusState: CollapseState @@ -22,11 +23,12 @@ class ConversationCollectionViewController: UIViewController, CollectionViewCont } private var dataSource: UICollectionViewDiffableDataSource! - init(for mainStatusID: String, state: CollapseState, mastodonController: MastodonController) { + init(for mainStatusID: String, state: CollapseState, conversationViewController: ConversationViewController) { self.mainStatusID = mainStatusID self.mainStatusState = state self.statusIDToScrollToOnLoad = mainStatusID - self.mastodonController = mastodonController + self.conversationViewController = conversationViewController + self.mastodonController = conversationViewController.mastodonController super.init(nibName: nil, bundle: nil) } @@ -68,6 +70,11 @@ class ConversationCollectionViewController: UIViewController, CollectionViewCont collectionView.dragDelegate = self collectionView.allowsFocus = true + #if !targetEnvironment(macCatalyst) + collectionView.refreshControl = UIRefreshControl() + collectionView.refreshControl!.addTarget(self, action: #selector(refresh), for: .valueChanged) + #endif + dataSource = createDataSource() } @@ -132,9 +139,10 @@ class ConversationCollectionViewController: UIViewController, CollectionViewCont } func addTree(_ tree: ConversationTree, mainStatus: StatusMO) { - var snapshot = dataSource.snapshot() - snapshot.deleteItems([.loadingIndicator]) + var snapshot = NSDiffableDataSourceSnapshot() + snapshot.appendSections([.ancestors, .mainStatus]) let mainStatusItem = Item.status(id: mainStatusID, node: tree.mainStatus, state: mainStatusState, prevLink: mainStatus.inReplyToID != nil, nextLink: false) + snapshot.appendItems([mainStatusItem], toSection: .mainStatus) let parentItems = tree.ancestors.enumerated().map { index, node in Item.status(id: node.status.id, node: node, state: .unknown, prevLink: index > 0, nextLink: true) } @@ -240,6 +248,15 @@ class ConversationCollectionViewController: UIViewController, CollectionViewCont } } + @objc func refresh() { + Task { + await conversationViewController.refreshContext() + #if !targetEnvironment(macCatalyst) + self.collectionView.refreshControl!.endRefreshing() + #endif + } + } + } extension ConversationCollectionViewController { diff --git a/Tusker/Screens/Conversation/ConversationViewController.swift b/Tusker/Screens/Conversation/ConversationViewController.swift index 23841b86..cb88c164 100644 --- a/Tusker/Screens/Conversation/ConversationViewController.swift +++ b/Tusker/Screens/Conversation/ConversationViewController.swift @@ -227,7 +227,7 @@ class ConversationViewController: UIViewController { } private func mainStatusLoaded(_ mainStatus: StatusMO) { - let vc = ConversationCollectionViewController(for: mainStatus.id, state: mainStatusState, mastodonController: mastodonController) + let vc = ConversationCollectionViewController(for: mainStatus.id, state: mainStatusState, conversationViewController: self) vc.statusIDToScrollToOnLoad = statusIDToScrollToOnLoad ?? mainStatus.id vc.showStatusesAutomatically = showStatusesAutomatically vc.addMainStatus(mainStatus) @@ -290,6 +290,15 @@ class ConversationViewController: UIViewController { } } + func refreshContext() async { + guard case .localID(let id) = mode, + let status = mastodonController.persistentContainer.status(for: id), + case .displaying(_) = state else { + return + } + await loadTree(for: status) + } + private func showMainStatusNotFound() { let notFoundView = StatusNotFoundView(frame: .zero) notFoundView.translatesAutoresizingMaskIntoConstraints = false