2021-05-18 08:25:32 +02:00
|
|
|
//
|
|
|
|
// AutoCompleteViewModel.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-5-17.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import Combine
|
|
|
|
import GameplayKit
|
|
|
|
import MastodonSDK
|
2022-09-30 13:28:09 +02:00
|
|
|
import MastodonCore
|
2021-05-18 08:25:32 +02:00
|
|
|
|
|
|
|
final class AutoCompleteViewModel {
|
|
|
|
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
|
|
|
|
// input
|
|
|
|
let context: AppContext
|
2022-10-09 14:07:57 +02:00
|
|
|
let authContext: AuthContext
|
2022-09-30 13:28:09 +02:00
|
|
|
public let inputText = CurrentValueSubject<String, Never>("") // contains "@" or "#" prefix
|
|
|
|
public let symbolBoundingRect = CurrentValueSubject<CGRect, Never>(.zero)
|
2022-11-13 12:42:50 +01:00
|
|
|
public let customEmojiViewModel: EmojiService.CustomEmojiViewModel?
|
2021-05-18 08:25:32 +02:00
|
|
|
|
|
|
|
// output
|
2022-09-30 13:28:09 +02:00
|
|
|
public var autoCompleteItems = CurrentValueSubject<[AutoCompleteItem], Never>([])
|
|
|
|
public var diffableDataSource: UITableViewDiffableDataSource<AutoCompleteSection, AutoCompleteItem>!
|
2021-05-18 08:25:32 +02:00
|
|
|
private(set) lazy var stateMachine: GKStateMachine = {
|
|
|
|
// exclude timeline middle fetcher state
|
|
|
|
let stateMachine = GKStateMachine(states: [
|
|
|
|
State.Initial(viewModel: self),
|
|
|
|
State.Loading(viewModel: self),
|
|
|
|
State.Fail(viewModel: self),
|
|
|
|
State.Idle(viewModel: self),
|
|
|
|
])
|
|
|
|
stateMachine.enter(State.Initial.self)
|
|
|
|
return stateMachine
|
|
|
|
}()
|
|
|
|
|
2022-10-09 14:07:57 +02:00
|
|
|
init(context: AppContext, authContext: AuthContext) {
|
2021-05-18 08:25:32 +02:00
|
|
|
self.context = context
|
2022-10-09 14:07:57 +02:00
|
|
|
self.authContext = authContext
|
2022-11-13 12:42:50 +01:00
|
|
|
self.customEmojiViewModel = context.emojiService.dequeueCustomEmojiViewModel(for: authContext.mastodonAuthenticationBox.domain)
|
|
|
|
// end init
|
2021-05-18 08:25:32 +02:00
|
|
|
|
|
|
|
autoCompleteItems
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { [weak self] items in
|
|
|
|
guard let self = self else { return }
|
|
|
|
guard let diffableDataSource = self.diffableDataSource else { return }
|
|
|
|
|
|
|
|
var snapshot = NSDiffableDataSourceSnapshot<AutoCompleteSection, AutoCompleteItem>()
|
|
|
|
snapshot.appendSections([.main])
|
2022-05-13 07:59:54 +02:00
|
|
|
snapshot.appendItems(items.removingDuplicates(), toSection: .main)
|
2021-05-18 08:25:32 +02:00
|
|
|
if let currentState = self.stateMachine.currentState {
|
|
|
|
switch currentState {
|
|
|
|
case is State.Loading, is State.Fail:
|
|
|
|
if items.isEmpty {
|
|
|
|
snapshot.appendItems([.bottomLoader], toSection: .main)
|
|
|
|
}
|
|
|
|
case is State.Idle:
|
|
|
|
// TODO: handle no results
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
diffableDataSource.defaultRowAnimation = .fade
|
|
|
|
diffableDataSource.apply(snapshot)
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
|
|
|
|
inputText
|
|
|
|
.removeDuplicates()
|
2022-11-14 11:53:09 +01:00
|
|
|
.throttle(for: .milliseconds(200), scheduler: DispatchQueue.main, latest: true)
|
2021-05-18 08:25:32 +02:00
|
|
|
.sink { [weak self] inputText in
|
|
|
|
guard let self = self else { return }
|
|
|
|
self.stateMachine.enter(State.Loading.self)
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension AutoCompleteViewModel {
|
|
|
|
enum SearchType {
|
|
|
|
case accounts
|
|
|
|
case hashtags
|
|
|
|
case emoji
|
|
|
|
case `default`
|
|
|
|
|
|
|
|
public var mastodonSearchType: Mastodon.API.V2.Search.SearchType? {
|
|
|
|
switch self {
|
|
|
|
case .accounts: return .accounts
|
|
|
|
case .hashtags: return .hashtags
|
|
|
|
case .emoji: return nil
|
|
|
|
case .default: return .default
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
init?(inputText: String) {
|
|
|
|
let prefix = inputText.first ?? Character("_")
|
|
|
|
switch prefix {
|
|
|
|
case "@": self = .accounts
|
|
|
|
case "#": self = .hashtags
|
|
|
|
case ":": self = .emoji
|
|
|
|
default: return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|