2022-04-13 14:43:16 +02:00
|
|
|
//
|
|
|
|
// DiscoveryNewsViewModel.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK on 2022-4-13.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import Combine
|
|
|
|
import GameplayKit
|
|
|
|
import CoreData
|
|
|
|
import CoreDataStack
|
|
|
|
import MastodonSDK
|
2022-10-08 07:43:06 +02:00
|
|
|
import MastodonCore
|
2022-04-13 14:43:16 +02:00
|
|
|
|
|
|
|
final class DiscoveryNewsViewModel {
|
|
|
|
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
|
|
|
|
// input
|
|
|
|
let context: AppContext
|
2022-10-09 14:07:57 +02:00
|
|
|
let authContext: AuthContext
|
2022-04-13 14:43:16 +02:00
|
|
|
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-10-09 14:07:57 +02:00
|
|
|
init(context: AppContext, authContext: AuthContext) {
|
2022-04-13 14:43:16 +02:00
|
|
|
self.context = context
|
2022-10-09 14:07:57 +02:00
|
|
|
self.authContext = authContext
|
2022-04-13 14:43:16 +02:00
|
|
|
// end init
|
2022-04-15 11:17:39 +02:00
|
|
|
|
|
|
|
Task {
|
|
|
|
await checkServerEndpoint()
|
|
|
|
} // end Task
|
2022-04-13 14:43:16 +02:00
|
|
|
}
|
|
|
|
}
|
2022-04-15 11:17:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
extension DiscoveryNewsViewModel {
|
|
|
|
func checkServerEndpoint() async {
|
|
|
|
do {
|
|
|
|
_ = try await context.apiService.trendLinks(
|
2022-10-09 14:07:57 +02:00
|
|
|
domain: authContext.mastodonAuthenticationBox.domain,
|
2023-08-21 11:29:48 +02:00
|
|
|
query: .init(offset: nil, limit: nil),
|
|
|
|
authenticationBox: authContext.mastodonAuthenticationBox
|
2022-04-15 11:17:39 +02:00
|
|
|
)
|
|
|
|
} catch let error as Mastodon.API.Error where error.httpResponseStatus.code == 404 {
|
|
|
|
isServerSupportEndpoint = false
|
|
|
|
} catch {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|