mastodon-ios/Mastodon/Scene/Compose/View/CustomEmojiPickerInputViewM...

59 lines
1.6 KiB
Swift
Raw Normal View History

2021-03-25 08:56:17 +01:00
//
// CustomEmojiPickerInputViewModel.swift
// Mastodon
//
// Created by MainasuK Cirno on 2021-3-25.
//
import UIKit
import Combine
final class CustomEmojiPickerInputViewModel {
var disposeBag = Set<AnyCancellable>()
private var customEmojiReplaceableTextInputReferences: [CustomEmojiReplaceableTextInputReference] = []
2021-03-25 08:56:17 +01:00
// input
weak var customEmojiPickerInputView: CustomEmojiPickerInputView?
// output
let isCustomEmojiComposing = CurrentValueSubject<Bool, Never>(false)
}
extension CustomEmojiPickerInputViewModel {
private func removeEmptyReferences() {
customEmojiReplaceableTextInputReferences.removeAll(where: { element in
2021-03-25 08:56:17 +01:00
element.value == nil
})
}
func append(customEmojiReplaceableTextInput textInput: CustomEmojiReplaceableTextInput) {
2021-03-25 08:56:17 +01:00
removeEmptyReferences()
let isContains = customEmojiReplaceableTextInputReferences.contains(where: { element in
2021-03-25 08:56:17 +01:00
element.value === textInput
})
guard !isContains else {
return
}
customEmojiReplaceableTextInputReferences.append(CustomEmojiReplaceableTextInputReference(value: textInput))
2021-03-25 08:56:17 +01:00
}
func insertText(_ text: String) -> CustomEmojiReplaceableTextInputReference? {
2021-03-25 08:56:17 +01:00
removeEmptyReferences()
for reference in customEmojiReplaceableTextInputReferences {
2021-03-25 08:56:17 +01:00
guard reference.value?.isFirstResponder == true else { continue }
reference.value?.insertText(text)
return reference
}
return nil
}
}