From 62c7a30bbc8f06a9d35db7b2f602951f848f34e3 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sat, 9 Apr 2022 11:52:09 -0400 Subject: [PATCH] Add emoji picker button to compose Closes #144 --- Tusker/Screens/Compose/ComposeAutocompleteView.swift | 6 ++++++ Tusker/Screens/Compose/ComposeEmojiTextField.swift | 6 +++++- .../Screens/Compose/ComposeHostingController.swift | 12 ++++++++++++ Tusker/Screens/Compose/ComposeUIState.swift | 4 ++++ Tusker/Screens/Compose/MainComposeTextView.swift | 6 +++++- 5 files changed, 32 insertions(+), 2 deletions(-) diff --git a/Tusker/Screens/Compose/ComposeAutocompleteView.swift b/Tusker/Screens/Compose/ComposeAutocompleteView.swift index 07672a10..5792624b 100644 --- a/Tusker/Screens/Compose/ComposeAutocompleteView.swift +++ b/Tusker/Screens/Compose/ComposeAutocompleteView.swift @@ -234,6 +234,12 @@ struct ComposeAutocompleteEmojisView: View { } else { horizontalScrollView .onReceive(uiState.$autocompleteState, perform: queryChanged) + .onAppear { + if uiState.shouldEmojiAutocompletionBeginExpanded { + expanded = true + uiState.shouldEmojiAutocompletionBeginExpanded = false + } + } } } diff --git a/Tusker/Screens/Compose/ComposeEmojiTextField.swift b/Tusker/Screens/Compose/ComposeEmojiTextField.swift index 0517ea09..d003f5eb 100644 --- a/Tusker/Screens/Compose/ComposeEmojiTextField.swift +++ b/Tusker/Screens/Compose/ComposeEmojiTextField.swift @@ -84,7 +84,7 @@ struct ComposeEmojiTextField: UIViewRepresentable { var skipSettingTextOnNextUpdate = false var toolbarElements: [ComposeUIState.ToolbarElement] { - [] + [.emojiPicker] } @objc func didChange(_ textField: UITextField) { @@ -108,6 +108,10 @@ struct ComposeEmojiTextField: UIViewRepresentable { self.updateAutocompleteState(textField: textField) } + func beginAutocompletingEmoji() { + textField?.insertText(":") + } + func applyFormat(_ format: StatusFormat) { } diff --git a/Tusker/Screens/Compose/ComposeHostingController.swift b/Tusker/Screens/Compose/ComposeHostingController.swift index e4f8d68e..d3c31b92 100644 --- a/Tusker/Screens/Compose/ComposeHostingController.swift +++ b/Tusker/Screens/Compose/ComposeHostingController.swift @@ -145,6 +145,10 @@ class ComposeHostingController: UIHostingController { 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)) if input?.toolbarElements.contains(.formattingButtons) == true, @@ -311,6 +315,14 @@ class ComposeHostingController: UIHostingController { uiState.currentInput?.applyFormat(format) } + @objc func emojiPickerButtonPressed() { + guard uiState.autocompleteState == nil else { + return + } + uiState.shouldEmojiAutocompletionBeginExpanded = true + uiState.currentInput?.beginAutocompletingEmoji() + } + @objc func draftsButtonPresed() { let draftsVC = DraftsTableViewController(account: mastodonController.accountInfo!, exclude: draft) draftsVC.delegate = self diff --git a/Tusker/Screens/Compose/ComposeUIState.swift b/Tusker/Screens/Compose/ComposeUIState.swift index 9e625324..0e038b73 100644 --- a/Tusker/Screens/Compose/ComposeUIState.swift +++ b/Tusker/Screens/Compose/ComposeUIState.swift @@ -31,6 +31,7 @@ class ComposeUIState: ObservableObject { var composeDrawingMode: ComposeDrawingMode? + var shouldEmojiAutocompletionBeginExpanded = false @Published var currentInput: ComposeInput? init(draft: Draft) { @@ -66,10 +67,13 @@ protocol ComposeInput: AnyObject { func autocomplete(with string: String) func applyFormat(_ format: StatusFormat) + + func beginAutocompletingEmoji() } extension ComposeUIState { enum ToolbarElement { + case emojiPicker case formattingButtons } } diff --git a/Tusker/Screens/Compose/MainComposeTextView.swift b/Tusker/Screens/Compose/MainComposeTextView.swift index 3795a6b6..2ab8b872 100644 --- a/Tusker/Screens/Compose/MainComposeTextView.swift +++ b/Tusker/Screens/Compose/MainComposeTextView.swift @@ -139,7 +139,7 @@ struct MainComposeWrappedTextView: UIViewRepresentable { var skipSettingTextOnNextUpdate = false var toolbarElements: [ComposeUIState.ToolbarElement] { - [.formattingButtons] + [.emojiPicker, .formattingButtons] } init(text: Binding, uiState: ComposeUIState, didChange: @escaping (UITextView) -> Void) { @@ -193,6 +193,10 @@ struct MainComposeWrappedTextView: UIViewRepresentable { self.updateAutocompleteState() } + func beginAutocompletingEmoji() { + textView?.insertText(":") + } + func autocomplete(with string: String) { guard let textView = textView, let text = textView.text,