2021-02-07 07:42:50 +01:00
|
|
|
//
|
|
|
|
// HomeTimelineViewModel.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by sxiaojian on 2021/2/5.
|
|
|
|
//
|
|
|
|
|
|
|
|
import func AVFoundation.AVMakeRect
|
|
|
|
import UIKit
|
|
|
|
import AVKit
|
|
|
|
import Combine
|
|
|
|
import CoreData
|
|
|
|
import CoreDataStack
|
|
|
|
import GameplayKit
|
|
|
|
import AlamofireImage
|
2022-10-08 07:43:06 +02:00
|
|
|
import MastodonCore
|
2022-10-10 13:14:52 +02:00
|
|
|
import MastodonUI
|
2023-12-01 10:47:18 +01:00
|
|
|
import MastodonSDK
|
2021-02-07 07:42:50 +01:00
|
|
|
|
|
|
|
final class HomeTimelineViewModel: NSObject {
|
|
|
|
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
var observations = Set<NSKeyValueObservation>()
|
|
|
|
|
|
|
|
// input
|
|
|
|
let context: AppContext
|
2022-10-09 14:07:57 +02:00
|
|
|
let authContext: AuthContext
|
2024-01-08 11:17:40 +01:00
|
|
|
let dataController: FeedDataController
|
2021-03-29 11:44:52 +02:00
|
|
|
let homeTimelineNavigationBarTitleViewModel: HomeTimelineNavigationBarTitleViewModel
|
2022-02-10 09:43:26 +01:00
|
|
|
let listBatchFetchViewModel = ListBatchFetchViewModel()
|
2023-05-23 12:23:50 +02:00
|
|
|
|
|
|
|
var presentedSuggestions = false
|
2022-02-10 09:43:26 +01:00
|
|
|
|
|
|
|
@Published var lastAutomaticFetchTimestamp: Date? = nil
|
2022-01-27 14:23:39 +01:00
|
|
|
@Published var scrollPositionRecord: ScrollPositionRecord? = nil
|
2022-02-10 09:43:26 +01:00
|
|
|
@Published var displaySettingBarButtonItem = true
|
2023-03-02 11:06:13 +01:00
|
|
|
@Published var hasPendingStatusEditReload = false
|
2021-03-15 13:03:40 +01:00
|
|
|
|
2021-02-07 07:42:50 +01:00
|
|
|
weak var tableView: UITableView?
|
|
|
|
weak var timelineMiddleLoaderTableViewCellDelegate: TimelineMiddleLoaderTableViewCellDelegate?
|
|
|
|
|
2021-04-22 09:45:32 +02:00
|
|
|
let timelineIsEmpty = CurrentValueSubject<Bool, Never>(false)
|
2021-04-22 04:11:19 +02:00
|
|
|
let homeTimelineNeedRefresh = PassthroughSubject<Void, Never>()
|
2021-06-17 13:43:16 +02:00
|
|
|
|
2021-02-07 07:42:50 +01:00
|
|
|
// output
|
2022-01-27 14:23:39 +01:00
|
|
|
var diffableDataSource: UITableViewDiffableDataSource<StatusSection, StatusItem>?
|
|
|
|
let didLoadLatest = PassthroughSubject<Void, Never>()
|
|
|
|
|
2021-02-07 07:42:50 +01:00
|
|
|
// top loader
|
|
|
|
private(set) lazy var loadLatestStateMachine: GKStateMachine = {
|
|
|
|
// exclude timeline middle fetcher state
|
|
|
|
let stateMachine = GKStateMachine(states: [
|
|
|
|
LoadLatestState.Initial(viewModel: self),
|
|
|
|
LoadLatestState.Loading(viewModel: self),
|
2023-02-07 11:35:25 +01:00
|
|
|
LoadLatestState.LoadingManually(viewModel: self),
|
2021-02-07 07:42:50 +01:00
|
|
|
LoadLatestState.Fail(viewModel: self),
|
|
|
|
LoadLatestState.Idle(viewModel: self),
|
|
|
|
])
|
|
|
|
stateMachine.enter(LoadLatestState.Initial.self)
|
|
|
|
return stateMachine
|
|
|
|
}()
|
|
|
|
lazy var loadLatestStateMachinePublisher = CurrentValueSubject<LoadLatestState?, Never>(nil)
|
2022-01-27 14:23:39 +01:00
|
|
|
|
2021-02-07 07:42:50 +01:00
|
|
|
// bottom loader
|
2021-07-23 13:10:27 +02:00
|
|
|
private(set) lazy var loadOldestStateMachine: GKStateMachine = {
|
2021-02-07 07:42:50 +01:00
|
|
|
// exclude timeline middle fetcher state
|
|
|
|
let stateMachine = GKStateMachine(states: [
|
|
|
|
LoadOldestState.Initial(viewModel: self),
|
|
|
|
LoadOldestState.Loading(viewModel: self),
|
|
|
|
LoadOldestState.Fail(viewModel: self),
|
|
|
|
LoadOldestState.Idle(viewModel: self),
|
|
|
|
LoadOldestState.NoMore(viewModel: self),
|
|
|
|
])
|
|
|
|
stateMachine.enter(LoadOldestState.Initial.self)
|
|
|
|
return stateMachine
|
|
|
|
}()
|
|
|
|
lazy var loadOldestStateMachinePublisher = CurrentValueSubject<LoadOldestState?, Never>(nil)
|
2022-01-27 14:23:39 +01:00
|
|
|
|
2021-02-07 07:42:50 +01:00
|
|
|
var cellFrameCache = NSCache<NSNumber, NSValue>()
|
|
|
|
|
2022-10-09 14:07:57 +02:00
|
|
|
init(context: AppContext, authContext: AuthContext) {
|
2021-02-07 07:42:50 +01:00
|
|
|
self.context = context
|
2022-10-09 14:07:57 +02:00
|
|
|
self.authContext = authContext
|
2024-01-08 11:17:40 +01:00
|
|
|
self.dataController = FeedDataController(context: context, authContext: authContext)
|
2021-03-29 11:44:52 +02:00
|
|
|
self.homeTimelineNavigationBarTitleViewModel = HomeTimelineNavigationBarTitleViewModel(context: context)
|
2021-02-07 07:42:50 +01:00
|
|
|
super.init()
|
2024-01-08 11:17:40 +01:00
|
|
|
self.dataController.records = (try? FileManager.default.cachedHomeTimeline(for: authContext.mastodonAuthenticationBox).map {
|
2023-12-01 10:47:18 +01:00
|
|
|
MastodonFeed.fromStatus($0, kind: .home)
|
|
|
|
}) ?? []
|
|
|
|
|
2021-04-22 04:11:19 +02:00
|
|
|
homeTimelineNeedRefresh
|
|
|
|
.sink { [weak self] _ in
|
|
|
|
self?.loadLatestStateMachine.enter(LoadLatestState.Loading.self)
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2021-07-07 14:13:33 +02:00
|
|
|
|
|
|
|
// refresh after publish post
|
2021-06-17 13:43:16 +02:00
|
|
|
homeTimelineNavigationBarTitleViewModel.isPublished
|
2021-07-07 14:13:33 +02:00
|
|
|
.delay(for: 2, scheduler: DispatchQueue.main)
|
2021-06-17 13:43:16 +02:00
|
|
|
.sink { [weak self] isPublished in
|
|
|
|
guard let self = self else { return }
|
|
|
|
self.homeTimelineNeedRefresh.send()
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2023-11-22 13:18:41 +01:00
|
|
|
|
2024-01-08 11:17:40 +01:00
|
|
|
self.dataController.$records
|
2023-12-01 10:47:18 +01:00
|
|
|
.removeDuplicates()
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink(receiveValue: { feeds in
|
|
|
|
let items: [MastodonStatus] = feeds.compactMap { feed -> MastodonStatus? in
|
|
|
|
guard let status = feed.status else { return nil }
|
|
|
|
return status
|
|
|
|
}
|
2023-12-27 10:35:00 +01:00
|
|
|
FileManager.default.cacheHomeTimeline(items: items, for: authContext.mastodonAuthenticationBox)
|
2023-12-01 10:47:18 +01:00
|
|
|
})
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
|
2024-01-08 11:17:40 +01:00
|
|
|
self.dataController.loadInitial(kind: .home)
|
2021-02-07 07:42:50 +01:00
|
|
|
}
|
|
|
|
}
|
2021-04-21 12:52:09 +02:00
|
|
|
|
2021-09-28 13:58:14 +02:00
|
|
|
extension HomeTimelineViewModel {
|
|
|
|
struct ScrollPositionRecord {
|
2022-01-27 14:23:39 +01:00
|
|
|
let item: StatusItem
|
2021-09-28 13:58:14 +02:00
|
|
|
let offset: CGFloat
|
|
|
|
let timestamp: Date
|
|
|
|
}
|
|
|
|
}
|
2022-01-27 14:23:39 +01:00
|
|
|
|
2023-02-07 00:46:35 +01:00
|
|
|
extension HomeTimelineViewModel {
|
|
|
|
func timelineDidReachEnd() {
|
2024-01-08 11:17:40 +01:00
|
|
|
dataController.loadNext(kind: .home)
|
2023-02-07 00:46:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
extension HomeTimelineViewModel {
|
|
|
|
|
|
|
|
// load timeline gap
|
|
|
|
func loadMore(item: StatusItem) async {
|
|
|
|
guard case let .feedLoader(record) = item else { return }
|
|
|
|
guard let diffableDataSource = diffableDataSource else { return }
|
|
|
|
var snapshot = diffableDataSource.snapshot()
|
|
|
|
|
2023-11-22 12:32:04 +01:00
|
|
|
guard let status = record.status else { return }
|
|
|
|
record.isLoadingMore = true
|
2023-11-22 13:18:41 +01:00
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
// reconfigure item
|
2022-12-17 20:26:20 +01:00
|
|
|
snapshot.reconfigureItems([item])
|
2022-01-27 14:23:39 +01:00
|
|
|
await updateSnapshotUsingReloadData(snapshot: snapshot)
|
2023-12-13 15:07:16 +01:00
|
|
|
|
|
|
|
await AuthenticationServiceProvider.shared.fetchAccounts(apiService: context.apiService)
|
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
// fetch data
|
2023-11-22 12:32:04 +01:00
|
|
|
let maxID = status.id
|
|
|
|
_ = try? await context.apiService.homeTimeline(
|
|
|
|
maxID: maxID,
|
|
|
|
authenticationBox: authContext.mastodonAuthenticationBox
|
|
|
|
)
|
|
|
|
|
|
|
|
record.isLoadingMore = false
|
2022-01-27 14:23:39 +01:00
|
|
|
|
|
|
|
// reconfigure item again
|
2022-12-17 20:26:20 +01:00
|
|
|
snapshot.reconfigureItems([item])
|
2022-01-27 14:23:39 +01:00
|
|
|
await updateSnapshotUsingReloadData(snapshot: snapshot)
|
|
|
|
}
|
2022-12-17 20:26:20 +01:00
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
}
|
2022-02-10 12:30:41 +01:00
|
|
|
|
|
|
|
// MARK: - SuggestionAccountViewModelDelegate
|
|
|
|
extension HomeTimelineViewModel: SuggestionAccountViewModelDelegate {
|
|
|
|
|
|
|
|
}
|
|
|
|
|