Skip to content

Commit

Permalink
feat(autocompletion): Add keyboard support
Browse files Browse the repository at this point in the history
Signed-off-by: Marcel Müller <[email protected]>
  • Loading branch information
SystemKeeper committed Mar 2, 2025
1 parent 3f40323 commit 401e1fe
Showing 1 changed file with 51 additions and 3 deletions.
54 changes: 51 additions & 3 deletions NextcloudTalk/InputbarViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import UIKit
internal var autocompletionUsers: [MentionSuggestion] = []
internal var mentionsDict: [String: NCMessageParameter] = [:]
internal var contentView: UIView?
internal var selectedAutocompletionRow: IndexPath?

public init?(forRoom room: NCRoom, withAccount account: TalkAccount, tableViewStyle style: UITableView.Style) {
self.room = room
Expand Down Expand Up @@ -230,6 +231,8 @@ import UIKit

// Check if "@" is still there
self.textView.look(forPrefixes: self.registeredPrefixes) { prefix, word, _ in
self.selectedAutocompletionRow = nil

if prefix?.count ?? 0 > 0 && word?.count ?? 0 > 0 {
self.showAutoCompletionView(showAutocomplete)
} else {
Expand All @@ -254,6 +257,39 @@ import UIKit
return resultMessage
}

override public func pressesBegan(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
guard self.isAutoCompleting,
!self.autocompletionUsers.isEmpty,
let firstVisibleRow = self.autoCompletionView.indexPathsForVisibleRows?.first
else {
super.pressesBegan(presses, with: event)
return
}

var oldIndexPath: IndexPath?
var newIndexPath: IndexPath?

// Support selecting the auto complete with return/enter key
if presses.contains(where: { $0.key?.keyCode == .keyboardReturnOrEnter }) {
let indexPath = self.selectedAutocompletionRow ?? firstVisibleRow
self.acceptAutoCompletion(withIndexPath: indexPath)

return
} else if presses.contains(where: { $0.key?.keyCode == .keyboardUpArrow }) {
oldIndexPath = self.selectedAutocompletionRow ?? firstVisibleRow
newIndexPath = IndexPath(row: oldIndexPath!.row - 1, section: 0)
} else if presses.contains(where: { $0.key?.keyCode == .keyboardDownArrow }) {
oldIndexPath = self.selectedAutocompletionRow ?? firstVisibleRow
newIndexPath = IndexPath(row: oldIndexPath!.row + 1, section: 0)
}

if let oldIndexPath, let newIndexPath, self.autoCompletionView.isValid(indexPath: newIndexPath) {
self.selectedAutocompletionRow = newIndexPath
self.autoCompletionView.reloadRows(at: [oldIndexPath, newIndexPath], with: .none)
self.autoCompletionView.scrollToRow(at: newIndexPath, at: .none, animated: true)
}
}

// MARK: - UITableViewDataSource methods

public override func numberOfSections(in tableView: UITableView) -> Int {
Expand Down Expand Up @@ -316,14 +352,25 @@ import UIKit
cell.avatarButton.setActorAvatar(forId: suggestion.mention.id, withType: suggestion.source, withDisplayName: suggestion.mention.label, withRoomToken: self.room.token, using: self.account)
}

if let selectedAutocompletionRow, selectedAutocompletionRow == indexPath {
cell.layer.borderColor = UIColor.systemGray.cgColor
cell.layer.borderWidth = 2.0
} else {
cell.layer.borderWidth = 0.0
}

cell.accessibilityIdentifier = AutoCompletionCellIdentifier
return cell
}

public override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard tableView == self.autoCompletionView,
indexPath.row < self.autocompletionUsers.count
else { return }
guard tableView == self.autoCompletionView else { return }

self.acceptAutoCompletion(withIndexPath: indexPath)
}

private func acceptAutoCompletion(withIndexPath indexPath: IndexPath) {
guard indexPath.row < self.autocompletionUsers.count else { return }

let suggestion = self.autocompletionUsers[indexPath.row]

Expand All @@ -332,6 +379,7 @@ import UIKit

let mentionWithWhitespace = suggestion.mention.label + " "
self.acceptAutoCompletion(with: mentionWithWhitespace, keepPrefix: true)
self.selectedAutocompletionRow = nil
}

public override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
Expand Down

0 comments on commit 401e1fe

Please sign in to comment.