Update posts on timeline and thread if edited (IOS-211)

This commit is contained in:
Marcus Kida 2023-12-14 10:11:05 +01:00
parent 9167260e30
commit d759a4b69a
No known key found for this signature in database
GPG Key ID: 19FF64E08013CA40
2 changed files with 25 additions and 5 deletions

View File

@ -79,6 +79,13 @@ extension ThreadViewController {
})
.store(in: &disposeBag)
viewModel.onEdit
.receive(on: DispatchQueue.main)
.sink(receiveValue: { [weak self] status in
self?.navigationController?.notifyChildrenAboutStatusUpdate(status)
})
.store(in: &disposeBag)
tableView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(tableView)
tableView.pinToParent()
@ -198,4 +205,10 @@ extension UINavigationController {
provider?.delete(status: status )
}
}
func notifyChildrenAboutStatusUpdate(_ status: MastodonStatus) {
viewControllers.compactMap { $0 as? DataSourceProvider }.forEach { provider in
provider?.update(status: status )
}
}
}

View File

@ -33,6 +33,7 @@ class ThreadViewModel {
@Published var hasPendingStatusEditReload = false
let onDismiss = PassthroughSubject<MastodonStatus, Never>()
let onEdit = PassthroughSubject<MastodonStatus, Never>()
private(set) lazy var loadThreadStateMachine: GKStateMachine = {
let stateMachine = GKStateMachine(states: [
@ -84,14 +85,20 @@ class ThreadViewModel {
context.publisherService
.statusPublishResult
.sink { [weak self] value in
guard let self else { return }
if case let Result.success(result) = value {
switch result {
case .edit:
self?.hasPendingStatusEditReload = true
case .post:
guard let self else { return }
case let .edit(content):
let status = content.value
let mastodonStatus = MastodonStatus.fromEntity(status)
self.hasPendingStatusEditReload = true
if status.id == root?.record.id {
self.root = .root(context: .init(status: mastodonStatus))
}
self.loadThreadStateMachine.enter(LoadThreadState.Loading.self)
self.onEdit.send(mastodonStatus)
case .post:
self.loadThreadStateMachine.enter(LoadThreadState.Loading.self)
}
}
}