Allow refreshing conversations

Closes #157
This commit is contained in:
Shadowfacts 2023-02-22 21:52:45 -05:00
parent 71fa3910a1
commit 266868376d
2 changed files with 32 additions and 6 deletions

View File

@ -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<Section, Item>!
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<Section, Item>()
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 {

View File

@ -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