Add emoji picker button to compose

Closes #144
This commit is contained in:
Shadowfacts 2022-04-09 11:52:09 -04:00
parent abf6ff8115
commit 62c7a30bbc
5 changed files with 32 additions and 2 deletions

View File

@ -234,6 +234,12 @@ struct ComposeAutocompleteEmojisView: View {
} else { } else {
horizontalScrollView horizontalScrollView
.onReceive(uiState.$autocompleteState, perform: queryChanged) .onReceive(uiState.$autocompleteState, perform: queryChanged)
.onAppear {
if uiState.shouldEmojiAutocompletionBeginExpanded {
expanded = true
uiState.shouldEmojiAutocompletionBeginExpanded = false
}
}
} }
} }

View File

@ -84,7 +84,7 @@ struct ComposeEmojiTextField: UIViewRepresentable {
var skipSettingTextOnNextUpdate = false var skipSettingTextOnNextUpdate = false
var toolbarElements: [ComposeUIState.ToolbarElement] { var toolbarElements: [ComposeUIState.ToolbarElement] {
[] [.emojiPicker]
} }
@objc func didChange(_ textField: UITextField) { @objc func didChange(_ textField: UITextField) {
@ -108,6 +108,10 @@ struct ComposeEmojiTextField: UIViewRepresentable {
self.updateAutocompleteState(textField: textField) self.updateAutocompleteState(textField: textField)
} }
func beginAutocompletingEmoji() {
textField?.insertText(":")
}
func applyFormat(_ format: StatusFormat) { func applyFormat(_ format: StatusFormat) {
} }

View File

@ -145,6 +145,10 @@ class ComposeHostingController: UIHostingController<ComposeContainerView> {
localOnlyChanged(draft.localOnly) localOnlyChanged(draft.localOnly)
} }
if input?.toolbarElements.contains(.emojiPicker) == true {
items.append(UIBarButtonItem(image: UIImage(systemName: "face.smiling"), style: .plain, target: self, action: #selector(emojiPickerButtonPressed)))
}
items.append(UIBarButtonItem(systemItem: .flexibleSpace)) items.append(UIBarButtonItem(systemItem: .flexibleSpace))
if input?.toolbarElements.contains(.formattingButtons) == true, if input?.toolbarElements.contains(.formattingButtons) == true,
@ -311,6 +315,14 @@ class ComposeHostingController: UIHostingController<ComposeContainerView> {
uiState.currentInput?.applyFormat(format) uiState.currentInput?.applyFormat(format)
} }
@objc func emojiPickerButtonPressed() {
guard uiState.autocompleteState == nil else {
return
}
uiState.shouldEmojiAutocompletionBeginExpanded = true
uiState.currentInput?.beginAutocompletingEmoji()
}
@objc func draftsButtonPresed() { @objc func draftsButtonPresed() {
let draftsVC = DraftsTableViewController(account: mastodonController.accountInfo!, exclude: draft) let draftsVC = DraftsTableViewController(account: mastodonController.accountInfo!, exclude: draft)
draftsVC.delegate = self draftsVC.delegate = self

View File

@ -31,6 +31,7 @@ class ComposeUIState: ObservableObject {
var composeDrawingMode: ComposeDrawingMode? var composeDrawingMode: ComposeDrawingMode?
var shouldEmojiAutocompletionBeginExpanded = false
@Published var currentInput: ComposeInput? @Published var currentInput: ComposeInput?
init(draft: Draft) { init(draft: Draft) {
@ -66,10 +67,13 @@ protocol ComposeInput: AnyObject {
func autocomplete(with string: String) func autocomplete(with string: String)
func applyFormat(_ format: StatusFormat) func applyFormat(_ format: StatusFormat)
func beginAutocompletingEmoji()
} }
extension ComposeUIState { extension ComposeUIState {
enum ToolbarElement { enum ToolbarElement {
case emojiPicker
case formattingButtons case formattingButtons
} }
} }

View File

@ -139,7 +139,7 @@ struct MainComposeWrappedTextView: UIViewRepresentable {
var skipSettingTextOnNextUpdate = false var skipSettingTextOnNextUpdate = false
var toolbarElements: [ComposeUIState.ToolbarElement] { var toolbarElements: [ComposeUIState.ToolbarElement] {
[.formattingButtons] [.emojiPicker, .formattingButtons]
} }
init(text: Binding<String>, uiState: ComposeUIState, didChange: @escaping (UITextView) -> Void) { init(text: Binding<String>, uiState: ComposeUIState, didChange: @escaping (UITextView) -> Void) {
@ -193,6 +193,10 @@ struct MainComposeWrappedTextView: UIViewRepresentable {
self.updateAutocompleteState() self.updateAutocompleteState()
} }
func beginAutocompletingEmoji() {
textView?.insertText(":")
}
func autocomplete(with string: String) { func autocomplete(with string: String) {
guard let textView = textView, guard let textView = textView,
let text = textView.text, let text = textView.text,