2021-04-13 13:46:42 +02:00
|
|
|
//
|
|
|
|
// ThreadViewModel.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-4-12.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import Combine
|
|
|
|
import CoreData
|
|
|
|
import CoreDataStack
|
|
|
|
import GameplayKit
|
|
|
|
import MastodonSDK
|
2021-07-23 13:10:27 +02:00
|
|
|
import MastodonMeta
|
2022-01-27 14:23:39 +01:00
|
|
|
import MastodonAsset
|
2022-10-08 07:43:06 +02:00
|
|
|
import MastodonCore
|
2022-01-27 14:23:39 +01:00
|
|
|
import MastodonLocalization
|
2021-04-13 13:46:42 +02:00
|
|
|
|
|
|
|
class ThreadViewModel {
|
|
|
|
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
2021-06-21 10:38:59 +02:00
|
|
|
var rootItemObserver: AnyCancellable?
|
2021-04-13 13:46:42 +02:00
|
|
|
|
|
|
|
// input
|
|
|
|
let context: AppContext
|
2022-10-09 14:07:57 +02:00
|
|
|
let authContext: AuthContext
|
2022-01-27 14:23:39 +01:00
|
|
|
let mastodonStatusThreadViewModel: MastodonStatusThreadViewModel
|
2021-04-13 13:46:42 +02:00
|
|
|
|
|
|
|
// output
|
2022-01-27 14:23:39 +01:00
|
|
|
var diffableDataSource: UITableViewDiffableDataSource<StatusSection, StatusItem>?
|
|
|
|
@Published var root: StatusItem.Thread?
|
|
|
|
@Published var threadContext: ThreadContext?
|
2023-03-02 11:06:13 +01:00
|
|
|
@Published var hasPendingStatusEditReload = false
|
2022-01-27 14:23:39 +01:00
|
|
|
|
2023-12-14 09:56:50 +01:00
|
|
|
let onDismiss = PassthroughSubject<MastodonStatus, Never>()
|
2023-12-14 10:11:05 +01:00
|
|
|
let onEdit = PassthroughSubject<MastodonStatus, Never>()
|
2023-12-14 09:56:50 +01:00
|
|
|
|
2021-04-13 13:46:42 +02:00
|
|
|
private(set) lazy var loadThreadStateMachine: GKStateMachine = {
|
|
|
|
let stateMachine = GKStateMachine(states: [
|
|
|
|
LoadThreadState.Initial(viewModel: self),
|
|
|
|
LoadThreadState.Loading(viewModel: self),
|
|
|
|
LoadThreadState.Fail(viewModel: self),
|
|
|
|
LoadThreadState.NoMore(viewModel: self),
|
|
|
|
|
|
|
|
])
|
|
|
|
stateMachine.enter(LoadThreadState.Initial.self)
|
|
|
|
return stateMachine
|
|
|
|
}()
|
2022-01-27 14:23:39 +01:00
|
|
|
@Published var navigationBarTitle: MastodonMetaContent?
|
2021-04-13 13:46:42 +02:00
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
init(
|
|
|
|
context: AppContext,
|
2022-10-09 14:07:57 +02:00
|
|
|
authContext: AuthContext,
|
2022-01-27 14:23:39 +01:00
|
|
|
optionalRoot: StatusItem.Thread?
|
|
|
|
) {
|
2021-04-13 13:46:42 +02:00
|
|
|
self.context = context
|
2022-10-09 14:07:57 +02:00
|
|
|
self.authContext = authContext
|
2022-01-27 14:23:39 +01:00
|
|
|
self.root = optionalRoot
|
|
|
|
self.mastodonStatusThreadViewModel = MastodonStatusThreadViewModel(context: context)
|
|
|
|
// end init
|
2023-11-22 12:32:04 +01:00
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
$root
|
2021-04-13 13:46:42 +02:00
|
|
|
.receive(on: DispatchQueue.main)
|
2022-01-27 14:23:39 +01:00
|
|
|
.sink { [weak self] root in
|
|
|
|
guard let self = self else { return }
|
|
|
|
guard case let .root(threadContext) = root else { return }
|
2023-11-22 12:32:04 +01:00
|
|
|
let status = threadContext.status
|
2021-04-13 13:46:42 +02:00
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
// bind threadContext
|
|
|
|
self.threadContext = .init(
|
2023-11-22 12:32:04 +01:00
|
|
|
domain: authContext.mastodonAuthenticationBox.domain, //status.domain,
|
2022-01-27 14:23:39 +01:00
|
|
|
statusID: status.id,
|
2023-11-22 12:32:04 +01:00
|
|
|
replyToID: status.entity.inReplyToID
|
2022-01-27 14:23:39 +01:00
|
|
|
)
|
2021-04-13 13:46:42 +02:00
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
// bind titleView
|
|
|
|
self.navigationBarTitle = {
|
2023-11-22 12:32:04 +01:00
|
|
|
let title = L10n.Scene.Thread.title(status.entity.account.displayNameWithFallback)
|
|
|
|
let content = MastodonContent(content: title, emojis: status.entity.account.emojis?.asDictionary ?? [:])
|
2022-01-27 14:23:39 +01:00
|
|
|
return try? MastodonMetaContent.convert(document: content)
|
|
|
|
}()
|
2021-04-13 13:46:42 +02:00
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2023-03-02 11:06:13 +01:00
|
|
|
|
|
|
|
context.publisherService
|
|
|
|
.statusPublishResult
|
|
|
|
.sink { [weak self] value in
|
2023-12-14 10:11:05 +01:00
|
|
|
guard let self else { return }
|
2023-12-13 15:09:37 +01:00
|
|
|
if case let Result.success(result) = value {
|
|
|
|
switch result {
|
2023-12-14 10:11:05 +01:00
|
|
|
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)
|
2023-12-13 15:09:37 +01:00
|
|
|
case .post:
|
|
|
|
self.loadThreadStateMachine.enter(LoadThreadState.Loading.self)
|
|
|
|
}
|
2023-03-02 11:06:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2021-04-13 13:46:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension ThreadViewModel {
|
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
struct ThreadContext {
|
2021-04-13 13:46:42 +02:00
|
|
|
let domain: String
|
|
|
|
let statusID: Mastodon.Entity.Status.ID
|
|
|
|
let replyToID: Mastodon.Entity.Status.ID?
|
|
|
|
}
|
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
}
|