2021-03-25 08:56:17 +01:00
|
|
|
//
|
|
|
|
// CustomEmojiPickerInputViewModel.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-3-25.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import Combine
|
2021-07-22 13:34:24 +02:00
|
|
|
import MetaTextKit
|
2021-03-25 08:56:17 +01:00
|
|
|
|
|
|
|
final class CustomEmojiPickerInputViewModel {
|
|
|
|
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
|
2021-06-07 08:22:03 +02:00
|
|
|
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() {
|
2021-06-07 08:22:03 +02:00
|
|
|
customEmojiReplaceableTextInputReferences.removeAll(where: { element in
|
2021-03-25 08:56:17 +01:00
|
|
|
element.value == nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-06-07 08:22:03 +02:00
|
|
|
func append(customEmojiReplaceableTextInput textInput: CustomEmojiReplaceableTextInput) {
|
2021-03-25 08:56:17 +01:00
|
|
|
removeEmptyReferences()
|
|
|
|
|
2021-06-07 08:22:03 +02:00
|
|
|
let isContains = customEmojiReplaceableTextInputReferences.contains(where: { element in
|
2021-03-25 08:56:17 +01:00
|
|
|
element.value === textInput
|
|
|
|
})
|
|
|
|
guard !isContains else {
|
|
|
|
return
|
|
|
|
}
|
2021-06-07 08:22:03 +02:00
|
|
|
customEmojiReplaceableTextInputReferences.append(CustomEmojiReplaceableTextInputReference(value: textInput))
|
2021-03-25 08:56:17 +01:00
|
|
|
}
|
|
|
|
|
2021-06-07 08:22:03 +02:00
|
|
|
func insertText(_ text: String) -> CustomEmojiReplaceableTextInputReference? {
|
2021-03-25 08:56:17 +01:00
|
|
|
removeEmptyReferences()
|
|
|
|
|
2021-06-07 08:22:03 +02:00
|
|
|
for reference in customEmojiReplaceableTextInputReferences {
|
2021-06-30 09:30:25 +02:00
|
|
|
guard let textInput = reference.value else { continue }
|
|
|
|
guard textInput.isFirstResponder == true else { continue }
|
2021-07-21 13:47:20 +02:00
|
|
|
guard let selectedTextRange = textInput.selectedTextRange else { continue }
|
2021-06-30 09:30:25 +02:00
|
|
|
|
|
|
|
textInput.insertText(text)
|
|
|
|
|
|
|
|
// due to insert text render as attachment
|
|
|
|
// the cursor reset logic not works
|
|
|
|
// hack with hard code +2 offset
|
|
|
|
assert(text.hasSuffix(": "))
|
2021-07-21 13:47:20 +02:00
|
|
|
guard text.hasPrefix(":") && text.hasSuffix(": ") else { continue }
|
|
|
|
|
|
|
|
if let _ = textInput as? MetaTextView {
|
|
|
|
if let newPosition = textInput.position(from: selectedTextRange.start, offset: 2) {
|
|
|
|
let newSelectedTextRange = textInput.textRange(from: newPosition, to: newPosition)
|
|
|
|
textInput.selectedTextRange = newSelectedTextRange
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if let newPosition = textInput.position(from: selectedTextRange.start, offset: text.length) {
|
|
|
|
let newSelectedTextRange = textInput.textRange(from: newPosition, to: newPosition)
|
|
|
|
textInput.selectedTextRange = newSelectedTextRange
|
|
|
|
}
|
2021-06-30 09:30:25 +02:00
|
|
|
}
|
|
|
|
|
2021-03-25 08:56:17 +01:00
|
|
|
return reference
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|