2022-04-13 14:43:16 +02:00
|
|
|
//
|
|
|
|
// DiscoveryNewsViewModel.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK on 2022-4-13.
|
|
|
|
//
|
|
|
|
|
|
|
|
import os.log
|
|
|
|
import UIKit
|
|
|
|
import Combine
|
|
|
|
import GameplayKit
|
|
|
|
import CoreData
|
|
|
|
import CoreDataStack
|
|
|
|
import MastodonSDK
|
|
|
|
|
|
|
|
final class DiscoveryNewsViewModel {
|
|
|
|
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
|
|
|
|
// input
|
|
|
|
let context: AppContext
|
|
|
|
let listBatchFetchViewModel = ListBatchFetchViewModel()
|
|
|
|
|
|
|
|
// output
|
|
|
|
@Published var links: [Mastodon.Entity.Link] = []
|
|
|
|
var diffableDataSource: UITableViewDiffableDataSource<DiscoverySection, DiscoveryItem>?
|
|
|
|
private(set) lazy var stateMachine: GKStateMachine = {
|
|
|
|
let stateMachine = GKStateMachine(states: [
|
|
|
|
State.Initial(viewModel: self),
|
|
|
|
State.Reloading(viewModel: self),
|
|
|
|
State.Fail(viewModel: self),
|
|
|
|
State.Idle(viewModel: self),
|
|
|
|
State.Loading(viewModel: self),
|
|
|
|
State.NoMore(viewModel: self),
|
|
|
|
])
|
|
|
|
stateMachine.enter(State.Initial.self)
|
|
|
|
return stateMachine
|
|
|
|
}()
|
|
|
|
|
|
|
|
let didLoadLatest = PassthroughSubject<Void, Never>()
|
2022-04-15 11:17:39 +02:00
|
|
|
@Published var isServerSupportEndpoint = true
|
|
|
|
|
2022-04-13 14:43:16 +02:00
|
|
|
init(context: AppContext) {
|
|
|
|
self.context = context
|
|
|
|
// end init
|
2022-04-15 11:17:39 +02:00
|
|
|
|
|
|
|
Task {
|
|
|
|
await checkServerEndpoint()
|
|
|
|
} // end Task
|
2022-04-13 14:43:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
deinit {
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-04-15 11:17:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
extension DiscoveryNewsViewModel {
|
|
|
|
func checkServerEndpoint() async {
|
|
|
|
guard let authenticationBox = context.authenticationService.activeMastodonAuthenticationBox.value else { return }
|
|
|
|
|
|
|
|
do {
|
|
|
|
_ = try await context.apiService.trendLinks(
|
|
|
|
domain: authenticationBox.domain,
|
|
|
|
query: .init(offset: nil, limit: nil)
|
|
|
|
)
|
|
|
|
} catch let error as Mastodon.API.Error where error.httpResponseStatus.code == 404 {
|
|
|
|
isServerSupportEndpoint = false
|
|
|
|
} catch {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|