From 9167260e3044637c155e0f2d152732f6062e0921 Mon Sep 17 00:00:00 2001 From: Marcus Kida Date: Thu, 14 Dec 2023 09:56:50 +0100 Subject: [PATCH] Update datasource on delete statuses in timelines and thread (IOS-212) --- ...hreadViewController+DataSourceProvider.swift | 4 ++++ .../Scene/Thread/ThreadViewController.swift | 17 +++++++++++++++++ Mastodon/Scene/Thread/ThreadViewModel.swift | 2 ++ 3 files changed, 23 insertions(+) diff --git a/Mastodon/Scene/Thread/ThreadViewController+DataSourceProvider.swift b/Mastodon/Scene/Thread/ThreadViewController+DataSourceProvider.swift index a2f83a1dd..f3373340b 100644 --- a/Mastodon/Scene/Thread/ThreadViewController+DataSourceProvider.swift +++ b/Mastodon/Scene/Thread/ThreadViewController+DataSourceProvider.swift @@ -60,6 +60,10 @@ extension ThreadViewController: DataSourceProvider { } func delete(status: MastodonStatus) { + if viewModel.root?.record.id == status.id { + viewModel.root = nil + viewModel.onDismiss.send(status) + } viewModel.mastodonStatusThreadViewModel.ancestors.handleDelete(status: status, for: viewModel) viewModel.mastodonStatusThreadViewModel.descendants.handleDelete(status: status, for: viewModel) } diff --git a/Mastodon/Scene/Thread/ThreadViewController.swift b/Mastodon/Scene/Thread/ThreadViewController.swift index 584f60753..0dcd473a1 100644 --- a/Mastodon/Scene/Thread/ThreadViewController.swift +++ b/Mastodon/Scene/Thread/ThreadViewController.swift @@ -14,6 +14,7 @@ import MastodonAsset import MastodonCore import MastodonUI import MastodonLocalization +import MastodonSDK final class ThreadViewController: UIViewController, NeedsDependency, MediaPreviewableViewController { @@ -70,6 +71,14 @@ extension ThreadViewController { } .store(in: &disposeBag) + viewModel.onDismiss + .receive(on: DispatchQueue.main) + .sink(receiveValue: { [weak self] status in + self?.navigationController?.popViewController(animated: true) + self?.navigationController?.notifyChildrenAboutStatusDeletion(status) + }) + .store(in: &disposeBag) + tableView.translatesAutoresizingMaskIntoConstraints = false view.addSubview(tableView) tableView.pinToParent() @@ -182,3 +191,11 @@ extension ThreadViewController: StatusTableViewControllerNavigateable { statusKeyCommandHandler(sender) } } + +extension UINavigationController { + func notifyChildrenAboutStatusDeletion(_ status: MastodonStatus) { + viewControllers.compactMap { $0 as? DataSourceProvider }.forEach { provider in + provider?.delete(status: status ) + } + } +} diff --git a/Mastodon/Scene/Thread/ThreadViewModel.swift b/Mastodon/Scene/Thread/ThreadViewModel.swift index c12360215..2a2e76a66 100644 --- a/Mastodon/Scene/Thread/ThreadViewModel.swift +++ b/Mastodon/Scene/Thread/ThreadViewModel.swift @@ -32,6 +32,8 @@ class ThreadViewModel { @Published var threadContext: ThreadContext? @Published var hasPendingStatusEditReload = false + let onDismiss = PassthroughSubject() + private(set) lazy var loadThreadStateMachine: GKStateMachine = { let stateMachine = GKStateMachine(states: [ LoadThreadState.Initial(viewModel: self),