2021-02-23 15:14:10 +01:00
|
|
|
//
|
2021-02-26 11:27:47 +01:00
|
|
|
// MastodonPickServerViewModel.swift
|
2021-02-23 15:14:10 +01:00
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by BradGao on 2021/2/23.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import Combine
|
2021-03-05 15:50:20 +01:00
|
|
|
import GameplayKit
|
2021-02-23 15:14:10 +01:00
|
|
|
import MastodonSDK
|
2021-02-25 07:09:19 +01:00
|
|
|
import CoreDataStack
|
2021-08-05 10:17:54 +02:00
|
|
|
import OrderedCollections
|
2022-01-04 11:30:21 +01:00
|
|
|
import Tabman
|
2022-10-08 07:43:06 +02:00
|
|
|
import MastodonCore
|
|
|
|
import MastodonUI
|
2022-12-20 22:54:36 +01:00
|
|
|
import MastodonLocalization
|
2021-02-23 15:14:10 +01:00
|
|
|
|
2021-02-26 11:27:47 +01:00
|
|
|
class MastodonPickServerViewModel: NSObject {
|
2022-11-11 18:44:01 +01:00
|
|
|
|
2021-03-06 05:55:52 +01:00
|
|
|
enum EmptyStateViewState {
|
2023-01-08 15:34:16 +01:00
|
|
|
case none
|
2021-03-06 05:55:52 +01:00
|
|
|
case loading
|
|
|
|
case badNetwork
|
|
|
|
}
|
|
|
|
|
2021-03-05 15:50:20 +01:00
|
|
|
var disposeBag = Set<AnyCancellable>()
|
2022-01-04 11:30:21 +01:00
|
|
|
|
|
|
|
let serverSectionHeaderView = PickServerServerSectionTableHeaderView()
|
2021-03-05 15:50:20 +01:00
|
|
|
|
|
|
|
// input
|
2021-02-23 15:14:10 +01:00
|
|
|
let context: AppContext
|
2021-03-05 15:50:20 +01:00
|
|
|
var categoryPickerItems: [CategoryPickerItem] = {
|
|
|
|
var items: [CategoryPickerItem] = []
|
2022-12-20 18:42:57 +01:00
|
|
|
items.append(.language(language: nil))
|
2022-12-21 23:35:25 +01:00
|
|
|
items.append(.signupSpeed(manuallyReviewed: nil))
|
2021-03-05 15:50:20 +01:00
|
|
|
items.append(contentsOf: APIService.stubCategories().map { CategoryPickerItem.category(category: $0) })
|
|
|
|
return items
|
|
|
|
}()
|
2022-12-28 13:50:59 +01:00
|
|
|
let selectCategoryItem = CurrentValueSubject<CategoryPickerItem, Never>(.category(category: Mastodon.Entity.Category(category: Mastodon.Entity.Category.Kind.general.rawValue, serversCount: 0)))
|
2021-03-06 07:21:52 +01:00
|
|
|
let searchText = CurrentValueSubject<String, Never>("")
|
2022-12-20 22:54:36 +01:00
|
|
|
let selectedLanguage = CurrentValueSubject<String?, Never>(nil)
|
2022-12-21 23:35:25 +01:00
|
|
|
let manualApprovalRequired = CurrentValueSubject<Bool?, Never>(nil)
|
2022-12-20 23:19:36 +01:00
|
|
|
let allLanguages = CurrentValueSubject<[Mastodon.Entity.Language], Never>([])
|
2021-03-05 15:50:20 +01:00
|
|
|
let indexedServers = CurrentValueSubject<[Mastodon.Entity.Server], Never>([])
|
2021-05-13 11:50:37 +02:00
|
|
|
let unindexedServers = CurrentValueSubject<[Mastodon.Entity.Server]?, Never>([]) // set nil when loading
|
2021-03-06 05:55:52 +01:00
|
|
|
let viewWillAppear = PassthroughSubject<Void, Never>()
|
2022-04-18 12:11:31 +02:00
|
|
|
let viewDidAppear = CurrentValueSubject<Void, Never>(Void())
|
2023-03-28 11:13:13 +02:00
|
|
|
let scrollToTop = PassthroughSubject<Void, Never>()
|
2022-05-11 13:22:22 +02:00
|
|
|
@Published var additionalTableViewInsets: UIEdgeInsets = .zero
|
|
|
|
|
2021-03-05 15:50:20 +01:00
|
|
|
// output
|
|
|
|
var diffableDataSource: UITableViewDiffableDataSource<PickServerSection, PickServerItem>?
|
|
|
|
private(set) lazy var loadIndexedServerStateMachine: GKStateMachine = {
|
|
|
|
// exclude timeline middle fetcher state
|
|
|
|
let stateMachine = GKStateMachine(states: [
|
|
|
|
LoadIndexedServerState.Initial(viewModel: self),
|
|
|
|
LoadIndexedServerState.Loading(viewModel: self),
|
|
|
|
LoadIndexedServerState.Fail(viewModel: self),
|
|
|
|
LoadIndexedServerState.Idle(viewModel: self),
|
|
|
|
])
|
|
|
|
stateMachine.enter(LoadIndexedServerState.Initial.self)
|
|
|
|
return stateMachine
|
|
|
|
}()
|
2021-03-06 07:21:52 +01:00
|
|
|
let filteredIndexedServers = CurrentValueSubject<[Mastodon.Entity.Server], Never>([])
|
2021-03-05 15:50:20 +01:00
|
|
|
let servers = CurrentValueSubject<[Mastodon.Entity.Server], Error>([])
|
2021-02-25 07:09:19 +01:00
|
|
|
let selectedServer = CurrentValueSubject<Mastodon.Entity.Server?, Never>(nil)
|
2021-06-04 12:31:57 +02:00
|
|
|
let error = CurrentValueSubject<Error?, Never>(nil)
|
2021-03-06 05:55:52 +01:00
|
|
|
|
|
|
|
let isLoadingIndexedServers = CurrentValueSubject<Bool, Never>(false)
|
2021-06-16 08:49:00 +02:00
|
|
|
let loadingIndexedServersError = CurrentValueSubject<Error?, Never>(nil)
|
2021-03-06 05:55:52 +01:00
|
|
|
let emptyStateViewState = CurrentValueSubject<EmptyStateViewState, Never>(.none)
|
2021-06-04 12:31:57 +02:00
|
|
|
|
2022-11-11 18:44:01 +01:00
|
|
|
init(context: AppContext) {
|
2021-02-23 15:14:10 +01:00
|
|
|
self.context = context
|
|
|
|
super.init()
|
2022-12-20 23:19:36 +01:00
|
|
|
|
2021-02-23 15:14:10 +01:00
|
|
|
configure()
|
|
|
|
}
|
|
|
|
|
2021-04-09 05:05:10 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension MastodonPickServerViewModel {
|
|
|
|
|
2021-02-23 15:14:10 +01:00
|
|
|
private func configure() {
|
2022-12-20 23:19:36 +01:00
|
|
|
|
|
|
|
context.apiService.languages().sink { completion in
|
|
|
|
|
|
|
|
} receiveValue: { response in
|
|
|
|
self.allLanguages.value = response.value
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
|
2021-06-16 08:49:00 +02:00
|
|
|
Publishers.CombineLatest(
|
|
|
|
isLoadingIndexedServers,
|
|
|
|
loadingIndexedServersError
|
|
|
|
)
|
|
|
|
.map { isLoadingIndexedServers, loadingIndexedServersError -> EmptyStateViewState in
|
|
|
|
if isLoadingIndexedServers {
|
|
|
|
if loadingIndexedServersError != nil {
|
|
|
|
return .badNetwork
|
2021-03-06 05:55:52 +01:00
|
|
|
} else {
|
2021-06-16 08:49:00 +02:00
|
|
|
return .loading
|
2021-03-06 05:55:52 +01:00
|
|
|
}
|
2021-06-16 08:49:00 +02:00
|
|
|
} else {
|
|
|
|
return .none
|
2021-03-06 05:55:52 +01:00
|
|
|
}
|
2021-06-16 08:49:00 +02:00
|
|
|
}
|
|
|
|
.assign(to: \.value, on: emptyStateViewState)
|
|
|
|
.store(in: &disposeBag)
|
2022-12-24 00:08:08 +01:00
|
|
|
|
2022-12-24 00:18:48 +01:00
|
|
|
|
2022-12-20 22:54:36 +01:00
|
|
|
Publishers.CombineLatest4(
|
2021-03-06 07:21:52 +01:00
|
|
|
indexedServers.eraseToAnyPublisher(),
|
|
|
|
selectCategoryItem.eraseToAnyPublisher(),
|
2022-12-20 22:54:36 +01:00
|
|
|
searchText.debounce(for: .milliseconds(300), scheduler: DispatchQueue.main).removeDuplicates(),
|
2022-12-24 00:08:08 +01:00
|
|
|
Publishers.CombineLatest(
|
|
|
|
selectedLanguage.eraseToAnyPublisher(),
|
|
|
|
manualApprovalRequired.eraseToAnyPublisher()
|
|
|
|
).map { selectedLanguage, manualApprovalRequired -> (selectedLanguage: String?, manualApprovalRequired: Bool?) in
|
|
|
|
(selectedLanguage, manualApprovalRequired)
|
|
|
|
}
|
2021-03-06 07:21:52 +01:00
|
|
|
)
|
2023-03-28 11:13:13 +02:00
|
|
|
.map { [weak self] indexedServers, selectCategoryItem, searchText, filters -> [Mastodon.Entity.Server] in
|
2022-12-24 00:18:48 +01:00
|
|
|
var indexedServers = indexedServers
|
2023-02-25 12:35:29 +01:00
|
|
|
|
2021-08-05 10:17:54 +02:00
|
|
|
var _indexedServers: [Mastodon.Entity.Server] = []
|
2023-02-25 12:35:29 +01:00
|
|
|
|
|
|
|
let sortedInstantSignupServers = indexedServers
|
|
|
|
.filter { $0.approvalRequired == false }
|
|
|
|
.sorted { $0.lastWeekUsers >= $1.lastWeekUsers }
|
|
|
|
let sortedApprovalRequiredServers = indexedServers
|
|
|
|
.filter { $0.approvalRequired }
|
|
|
|
.sorted { $0.lastWeekUsers >= $1.lastWeekUsers }
|
|
|
|
|
|
|
|
_indexedServers.append(contentsOf: sortedInstantSignupServers)
|
|
|
|
_indexedServers.append(contentsOf: sortedApprovalRequiredServers)
|
|
|
|
|
2021-08-05 10:17:54 +02:00
|
|
|
if _indexedServers.count == indexedServers.count {
|
|
|
|
indexedServers = _indexedServers
|
|
|
|
} else {
|
|
|
|
assertionFailure("should not change dataset size")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter the indexed servers by category or search text
|
2021-03-06 07:21:52 +01:00
|
|
|
switch selectCategoryItem {
|
2022-12-21 23:35:25 +01:00
|
|
|
case .language(_), .signupSpeed(_):
|
2022-12-24 00:08:08 +01:00
|
|
|
return MastodonPickServerViewModel.filterServers(servers: indexedServers, language: filters.selectedLanguage, manualApprovalRequired: filters.manualApprovalRequired, category: nil, searchText: searchText)
|
2021-03-06 07:21:52 +01:00
|
|
|
case .category(let category):
|
2023-03-28 11:13:13 +02:00
|
|
|
self?.scrollToTop.send()
|
2022-12-24 00:08:08 +01:00
|
|
|
return MastodonPickServerViewModel.filterServers(servers: indexedServers, language: filters.selectedLanguage, manualApprovalRequired: filters.manualApprovalRequired, category: category.category.rawValue, searchText: searchText)
|
2021-03-06 07:21:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.assign(to: \.value, on: filteredIndexedServers)
|
|
|
|
.store(in: &disposeBag)
|
2021-02-24 15:47:42 +01:00
|
|
|
|
2021-03-06 07:21:52 +01:00
|
|
|
searchText
|
|
|
|
.debounce(for: .milliseconds(300), scheduler: DispatchQueue.main)
|
|
|
|
.removeDuplicates()
|
|
|
|
.compactMap { [weak self] searchText -> AnyPublisher<Result<Mastodon.Response.Content<[Mastodon.Entity.Server]>, Error>, Never>? in
|
|
|
|
// Check if searchText is a valid mastodon server domain
|
|
|
|
guard let self = self else { return nil }
|
|
|
|
guard let domain = AuthenticationViewModel.parseDomain(from: searchText) else {
|
|
|
|
return Just(Result.failure(APIService.APIError.implicit(.badRequest))).eraseToAnyPublisher()
|
|
|
|
}
|
2021-05-13 11:50:37 +02:00
|
|
|
self.unindexedServers.value = nil
|
2021-07-08 08:17:35 +02:00
|
|
|
return self.context.apiService.webFinger(domain: domain)
|
|
|
|
.flatMap { domain -> AnyPublisher<Result<Mastodon.Response.Content<[Mastodon.Entity.Server]>, Error>, Never> in
|
|
|
|
return self.context.apiService.instance(domain: domain)
|
|
|
|
.map { response -> Result<Mastodon.Response.Content<[Mastodon.Entity.Server]>, Error>in
|
|
|
|
let newResponse = response.map { [Mastodon.Entity.Server(domain: domain, instance: $0)] }
|
|
|
|
return Result.success(newResponse)
|
|
|
|
}
|
|
|
|
.catch { error in
|
|
|
|
return Just(Result.failure(error))
|
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
2021-03-06 07:21:52 +01:00
|
|
|
}
|
|
|
|
.catch { error in
|
|
|
|
return Just(Result.failure(error))
|
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
.switchToLatest()
|
|
|
|
.sink(receiveValue: { [weak self] result in
|
|
|
|
guard let self = self else { return }
|
|
|
|
switch result {
|
|
|
|
case .success(let response):
|
|
|
|
self.unindexedServers.send(response.value)
|
2021-05-13 11:50:37 +02:00
|
|
|
case .failure(let error):
|
|
|
|
if let error = error as? APIService.APIError,
|
|
|
|
case let .implicit(reason) = error,
|
|
|
|
case .badRequest = reason {
|
|
|
|
self.unindexedServers.send([])
|
|
|
|
} else {
|
|
|
|
self.unindexedServers.send(nil)
|
|
|
|
}
|
2021-03-06 07:21:52 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.store(in: &disposeBag)
|
2021-02-24 15:47:42 +01:00
|
|
|
}
|
2021-03-06 07:21:52 +01:00
|
|
|
|
2022-12-30 17:59:09 +01:00
|
|
|
func chooseRandomServer() -> Mastodon.Entity.Server? {
|
|
|
|
|
2023-11-16 10:08:34 +01:00
|
|
|
let language = Locale.autoupdatingCurrent.language.languageCode?.identifier.lowercased() ?? "en"
|
2022-12-30 17:59:09 +01:00
|
|
|
|
|
|
|
let servers = indexedServers.value
|
|
|
|
guard servers.isNotEmpty else { return nil }
|
|
|
|
|
2023-02-08 23:41:49 +01:00
|
|
|
let generalServers = servers.filter {
|
|
|
|
$0.categories.contains("general")
|
|
|
|
}
|
|
|
|
|
|
|
|
let randomServer: Mastodon.Entity.Server?
|
|
|
|
|
|
|
|
let noApprovalRequired = generalServers.filter { !$0.approvalRequired }
|
|
|
|
let approvalRequired = generalServers.filter { $0.approvalRequired }
|
|
|
|
|
|
|
|
let languageMatchesWithoutApproval = noApprovalRequired.filter { $0.language.lowercased() == language }
|
|
|
|
let languageMatchesWithApproval = approvalRequired.filter { $0.language.lowercased() == language }
|
|
|
|
let languageDoesNotMatchWithoutApproval = noApprovalRequired.filter { $0.language.lowercased() != language }
|
|
|
|
let languageDoesNotMatchWithApproval = approvalRequired.filter { $0.language.lowercased() != language }
|
|
|
|
|
|
|
|
switch (
|
|
|
|
languageMatchesWithoutApproval.isEmpty,
|
|
|
|
languageMatchesWithApproval.isEmpty,
|
|
|
|
languageDoesNotMatchWithoutApproval.isEmpty,
|
|
|
|
languageDoesNotMatchWithApproval.isEmpty
|
|
|
|
) {
|
|
|
|
case (true, true, true, true):
|
|
|
|
randomServer = generalServers.randomElement()
|
|
|
|
case (true, true, true, false):
|
|
|
|
randomServer = languageDoesNotMatchWithApproval.randomElement()
|
|
|
|
case (true, true, false, _):
|
|
|
|
randomServer = languageDoesNotMatchWithoutApproval.randomElement()
|
|
|
|
case (true, false, _, _):
|
|
|
|
randomServer = languageMatchesWithApproval.randomElement()
|
|
|
|
case (false, _, _, _):
|
|
|
|
randomServer = languageMatchesWithoutApproval.randomElement()
|
|
|
|
}
|
2022-12-30 17:59:09 +01:00
|
|
|
|
2023-01-06 16:07:39 +01:00
|
|
|
return randomServer ?? servers.randomElement() ?? servers.first
|
2022-12-30 17:59:09 +01:00
|
|
|
}
|
2021-02-23 15:14:10 +01:00
|
|
|
}
|
2022-12-20 18:42:57 +01:00
|
|
|
|
2021-03-06 07:21:52 +01:00
|
|
|
extension MastodonPickServerViewModel {
|
2022-12-21 23:35:25 +01:00
|
|
|
private static func filterServers(servers: [Mastodon.Entity.Server], language: String? = nil, manualApprovalRequired: Bool? = nil, category: String?, searchText: String) -> [Mastodon.Entity.Server] {
|
2022-12-20 18:42:57 +01:00
|
|
|
let filteredServers = servers
|
|
|
|
// 1. Filter the category
|
2021-03-06 07:21:52 +01:00
|
|
|
.filter {
|
|
|
|
guard let category = category else { return true }
|
|
|
|
return $0.category.caseInsensitiveCompare(category) == .orderedSame
|
|
|
|
}
|
2022-12-20 18:42:57 +01:00
|
|
|
// 2. Filter the searchText
|
2021-03-06 07:21:52 +01:00
|
|
|
.filter {
|
|
|
|
let searchText = searchText.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
|
|
guard !searchText.isEmpty else {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return $0.domain.lowercased().contains(searchText.lowercased())
|
|
|
|
}
|
2022-12-20 18:42:57 +01:00
|
|
|
.filter {
|
|
|
|
guard let language else { return true }
|
2022-12-21 23:35:25 +01:00
|
|
|
|
2022-12-20 18:42:57 +01:00
|
|
|
return $0.language.lowercased() == language.lowercased()
|
|
|
|
}
|
2022-12-21 23:35:25 +01:00
|
|
|
.filter {
|
|
|
|
guard let manualApprovalRequired else { return true }
|
|
|
|
|
2022-12-24 00:08:08 +01:00
|
|
|
print("\($0.domain) \($0.approvalRequired) < \(manualApprovalRequired)")
|
2022-12-21 23:35:25 +01:00
|
|
|
return $0.approvalRequired == manualApprovalRequired
|
|
|
|
}
|
2022-12-20 18:42:57 +01:00
|
|
|
return filteredServers
|
2021-03-06 07:21:52 +01:00
|
|
|
}
|
|
|
|
}
|
2021-02-25 07:09:19 +01:00
|
|
|
|
|
|
|
// MARK: - SignUp methods & structs
|
2021-02-26 11:27:47 +01:00
|
|
|
extension MastodonPickServerViewModel {
|
2021-02-25 07:09:19 +01:00
|
|
|
struct SignUpResponseFirst {
|
|
|
|
let instance: Mastodon.Response.Content<Mastodon.Entity.Instance>
|
|
|
|
let application: Mastodon.Response.Content<Mastodon.Entity.Application>
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SignUpResponseSecond {
|
|
|
|
let instance: Mastodon.Response.Content<Mastodon.Entity.Instance>
|
|
|
|
let authenticateInfo: AuthenticationViewModel.AuthenticateInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SignUpResponseThird {
|
|
|
|
let instance: Mastodon.Response.Content<Mastodon.Entity.Instance>
|
|
|
|
let authenticateInfo: AuthenticationViewModel.AuthenticateInfo
|
|
|
|
let applicationToken: Mastodon.Response.Content<Mastodon.Entity.Token>
|
2021-02-24 15:47:42 +01:00
|
|
|
}
|
|
|
|
}
|
2022-01-04 11:30:21 +01:00
|
|
|
|
|
|
|
// MARK: - TMBarDataSource
|
|
|
|
extension MastodonPickServerViewModel: TMBarDataSource {
|
|
|
|
func barItem(for bar: TMBar, at index: Int) -> TMBarItemable {
|
|
|
|
let item = categoryPickerItems[index]
|
|
|
|
let barItem = TMBarItem(title: item.title)
|
|
|
|
return barItem
|
|
|
|
}
|
|
|
|
}
|