2021-05-18 08:25:32 +02:00
|
|
|
//
|
|
|
|
// AutoCompleteViewModel+State.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-5-17.
|
|
|
|
//
|
|
|
|
|
|
|
|
import os.log
|
|
|
|
import Foundation
|
|
|
|
import GameplayKit
|
|
|
|
import MastodonSDK
|
2022-09-30 13:28:09 +02:00
|
|
|
import MastodonCore
|
2021-05-18 08:25:32 +02:00
|
|
|
|
|
|
|
extension AutoCompleteViewModel {
|
2022-10-10 13:14:52 +02:00
|
|
|
class State: GKState {
|
2022-01-27 14:23:39 +01:00
|
|
|
|
|
|
|
let logger = Logger(subsystem: "AutoCompleteViewModel.State", category: "StateMachine")
|
|
|
|
|
|
|
|
let id = UUID()
|
|
|
|
|
2021-05-18 08:25:32 +02:00
|
|
|
weak var viewModel: AutoCompleteViewModel?
|
|
|
|
|
|
|
|
init(viewModel: AutoCompleteViewModel) {
|
|
|
|
self.viewModel = viewModel
|
|
|
|
}
|
|
|
|
|
|
|
|
override func didEnter(from previousState: GKState?) {
|
2022-01-27 14:23:39 +01:00
|
|
|
super.didEnter(from: previousState)
|
2022-10-10 13:14:52 +02:00
|
|
|
|
|
|
|
let from = previousState.flatMap { String(describing: $0) } ?? "nil"
|
|
|
|
let to = String(describing: self)
|
|
|
|
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): \(from) -> \(to)")
|
2022-01-27 14:23:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@MainActor
|
|
|
|
func enter(state: State.Type) {
|
|
|
|
stateMachine?.enter(state)
|
|
|
|
}
|
|
|
|
|
|
|
|
deinit {
|
2022-10-10 13:14:52 +02:00
|
|
|
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): [\(self.id.uuidString)] \(String(describing: self))")
|
2021-05-18 08:25:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension AutoCompleteViewModel.State {
|
|
|
|
class Initial: AutoCompleteViewModel.State {
|
|
|
|
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
|
|
|
|
guard let viewModel = viewModel else { return false }
|
|
|
|
switch stateClass {
|
|
|
|
case is Loading.Type:
|
|
|
|
return !viewModel.inputText.value.isEmpty
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Loading: AutoCompleteViewModel.State {
|
|
|
|
|
|
|
|
var previoursSearchText = ""
|
|
|
|
|
|
|
|
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
|
|
|
|
guard let viewModel = viewModel else { return false }
|
|
|
|
switch stateClass {
|
|
|
|
case is Loading.Type:
|
|
|
|
return previoursSearchText != viewModel.inputText.value
|
|
|
|
case is Fail.Type:
|
|
|
|
return true
|
|
|
|
case is Idle.Type:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override func didEnter(from previousState: GKState?) {
|
|
|
|
super.didEnter(from: previousState)
|
2022-05-07 05:42:10 +02:00
|
|
|
guard let viewModel = viewModel, let _ = stateMachine else { return }
|
2021-05-18 08:25:32 +02:00
|
|
|
|
|
|
|
let searchText = viewModel.inputText.value
|
|
|
|
let searchType = AutoCompleteViewModel.SearchType(inputText: searchText) ?? .default
|
|
|
|
if searchText != previoursSearchText {
|
|
|
|
reset(searchText: searchText)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch searchType {
|
|
|
|
case .emoji:
|
2022-01-27 14:23:39 +01:00
|
|
|
Task {
|
|
|
|
await fetchLocalEmoji(searchText: searchText)
|
|
|
|
}
|
2021-05-18 08:25:32 +02:00
|
|
|
default:
|
2022-01-27 14:23:39 +01:00
|
|
|
Task {
|
|
|
|
await queryRemoteEnitity(searchText: searchText)
|
|
|
|
}
|
2021-05-18 08:25:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
private func fetchLocalEmoji(searchText: String) async {
|
|
|
|
guard let viewModel = viewModel else {
|
|
|
|
await enter(state: Fail.self)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-13 12:42:50 +01:00
|
|
|
guard let customEmojiViewModel = viewModel.customEmojiViewModel else {
|
2022-01-27 14:23:39 +01:00
|
|
|
await enter(state: Fail.self)
|
2021-05-18 08:25:32 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let emojiTrie = customEmojiViewModel.emojiTrie.value else {
|
2022-01-27 14:23:39 +01:00
|
|
|
await enter(state: Fail.self)
|
2021-05-18 08:25:32 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let searchPattern = ArraySlice(String(searchText.dropFirst()))
|
|
|
|
let passthroughs = emojiTrie.passthrough(searchPattern)
|
|
|
|
let matchingEmojis = passthroughs
|
2021-05-18 09:06:00 +02:00
|
|
|
.map { $0.values } // [Set<Emoji>]
|
2021-05-18 08:25:32 +02:00
|
|
|
.map { set in set.compactMap { $0 as? Mastodon.Entity.Emoji } } // [[Emoji]]
|
2021-05-18 09:06:00 +02:00
|
|
|
.flatMap { $0 } // [Emoji]
|
2021-05-18 08:25:32 +02:00
|
|
|
let items: [AutoCompleteItem] = matchingEmojis.map { emoji in
|
|
|
|
AutoCompleteItem.emoji(emoji: emoji)
|
|
|
|
}
|
2022-01-27 14:23:39 +01:00
|
|
|
|
|
|
|
await enter(state: Idle.self)
|
2021-05-18 08:25:32 +02:00
|
|
|
viewModel.autoCompleteItems.value = items
|
|
|
|
}
|
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
private func queryRemoteEnitity(searchText: String) async {
|
|
|
|
guard let viewModel = viewModel else {
|
|
|
|
await enter(state: Fail.self)
|
|
|
|
return
|
|
|
|
}
|
2021-05-18 08:25:32 +02:00
|
|
|
|
|
|
|
let searchText = viewModel.inputText.value
|
|
|
|
let searchType = AutoCompleteViewModel.SearchType(inputText: searchText) ?? .default
|
|
|
|
|
|
|
|
let q = String(searchText.dropFirst())
|
|
|
|
let query = Mastodon.API.V2.Search.Query(
|
|
|
|
q: q,
|
|
|
|
type: searchType.mastodonSearchType,
|
|
|
|
maxID: nil,
|
|
|
|
offset: nil,
|
|
|
|
following: nil
|
|
|
|
)
|
2022-01-27 14:23:39 +01:00
|
|
|
|
|
|
|
do {
|
|
|
|
let response = try await viewModel.context.apiService.search(
|
|
|
|
query: query,
|
2022-10-09 14:07:57 +02:00
|
|
|
authenticationBox: viewModel.authContext.mastodonAuthenticationBox
|
2022-01-27 14:23:39 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
await enter(state: Idle.self)
|
|
|
|
|
2021-05-18 08:25:32 +02:00
|
|
|
guard viewModel.inputText.value == searchText else { return } // discard if not matching
|
|
|
|
|
|
|
|
var items: [AutoCompleteItem] = []
|
|
|
|
items.append(contentsOf: response.value.accounts.map { AutoCompleteItem.account(account: $0) })
|
|
|
|
items.append(contentsOf: response.value.hashtags.map { AutoCompleteItem.hashtag(tag: $0) })
|
2022-01-27 14:23:39 +01:00
|
|
|
|
2021-05-18 08:25:32 +02:00
|
|
|
viewModel.autoCompleteItems.value = items
|
2022-01-27 14:23:39 +01:00
|
|
|
|
|
|
|
} catch {
|
|
|
|
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): auto-complete fail: \(error.localizedDescription)")
|
|
|
|
await enter(state: Fail.self)
|
2021-05-18 08:25:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func reset(searchText: String) {
|
|
|
|
let previoursSearchType = AutoCompleteViewModel.SearchType(inputText: previoursSearchText)
|
|
|
|
previoursSearchText = searchText
|
|
|
|
let currentSearchType = AutoCompleteViewModel.SearchType(inputText: searchText)
|
|
|
|
// reset when search type change
|
|
|
|
if previoursSearchType != currentSearchType {
|
|
|
|
viewModel?.autoCompleteItems.value = []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class Idle: AutoCompleteViewModel.State {
|
|
|
|
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
|
|
|
|
switch stateClass {
|
|
|
|
case is Loading.Type:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Fail: AutoCompleteViewModel.State {
|
|
|
|
|
|
|
|
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
|
|
|
|
switch stateClass {
|
|
|
|
case is Loading.Type:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override func didEnter(from previousState: GKState?) {
|
|
|
|
super.didEnter(from: previousState)
|
|
|
|
guard let _ = viewModel, let stateMachine = stateMachine else { return }
|
|
|
|
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: retry loading 3s later…", ((#file as NSString).lastPathComponent), #line, #function)
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: retry loading", ((#file as NSString).lastPathComponent), #line, #function)
|
|
|
|
stateMachine.enter(Loading.self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|