2021-03-15 07:40:10 +01:00
|
|
|
//
|
2021-03-15 12:25:44 +01:00
|
|
|
// EmojiService+CustomEmojiViewModel.swift
|
2021-03-15 07:40:10 +01:00
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-3-15.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import Combine
|
|
|
|
import GameplayKit
|
|
|
|
import MastodonSDK
|
|
|
|
|
|
|
|
extension EmojiService {
|
2021-03-15 12:25:44 +01:00
|
|
|
final class CustomEmojiViewModel {
|
2021-03-15 07:40:10 +01:00
|
|
|
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
|
|
|
|
// input
|
|
|
|
let domain: String
|
2021-03-15 12:25:44 +01:00
|
|
|
weak var service: EmojiService?
|
2021-03-15 07:40:10 +01:00
|
|
|
|
|
|
|
// output
|
|
|
|
private(set) lazy var stateMachine: GKStateMachine = {
|
|
|
|
// exclude timeline middle fetcher state
|
|
|
|
let stateMachine = GKStateMachine(states: [
|
|
|
|
LoadState.Initial(viewModel: self),
|
|
|
|
LoadState.Loading(viewModel: self),
|
|
|
|
LoadState.Fail(viewModel: self),
|
|
|
|
LoadState.Finish(viewModel: self),
|
|
|
|
])
|
|
|
|
stateMachine.enter(LoadState.Initial.self)
|
|
|
|
return stateMachine
|
|
|
|
}()
|
|
|
|
let emojis = CurrentValueSubject<[Mastodon.Entity.Emoji], Never>([])
|
2021-05-06 10:24:21 +02:00
|
|
|
let emojiDict = CurrentValueSubject<[String: [Mastodon.Entity.Emoji]], Never>([:])
|
|
|
|
|
|
|
|
private var learnedEmoji: Set<String> = Set()
|
2021-03-15 07:40:10 +01:00
|
|
|
|
2021-03-15 12:25:44 +01:00
|
|
|
init(domain: String, service: EmojiService) {
|
2021-03-15 07:40:10 +01:00
|
|
|
self.domain = domain
|
2021-03-15 12:25:44 +01:00
|
|
|
self.service = service
|
2021-05-06 10:24:21 +02:00
|
|
|
|
|
|
|
emojis
|
|
|
|
.map { Dictionary(grouping: $0, by: { $0.shortcode }) }
|
|
|
|
.assign(to: \.value, on: emojiDict)
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
}
|
|
|
|
|
|
|
|
func emoji(shortcode: String) -> Mastodon.Entity.Emoji? {
|
|
|
|
if !learnedEmoji.contains(shortcode) {
|
|
|
|
learnedEmoji.insert(shortcode)
|
|
|
|
|
|
|
|
DispatchQueue.global().async {
|
|
|
|
UITextChecker.learnWord(shortcode)
|
|
|
|
UITextChecker.learnWord(":" + shortcode + ":")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return emojiDict.value[shortcode]?.first
|
2021-03-15 07:40:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|