Skip to content

Commit ca4b884

Browse files
authored
Merge pull request #2007 from nextcloud/fix/1999/autocomplete-keyboard-support
Add keyboard support to autocompletion
2 parents ef994b4 + 3d580c0 commit ca4b884

6 files changed

+60
-11
lines changed

NextcloudTalk.xcodeproj/project.pbxproj

+2
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@
236236
1FA38C9029A4B3C6008871B8 /* NCNotificationAction.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FA38C8F29A4B3C6008871B8 /* NCNotificationAction.swift */; };
237237
1FA38C9129A4B3C6008871B8 /* NCNotificationAction.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FA38C8F29A4B3C6008871B8 /* NCNotificationAction.swift */; };
238238
1FA732FC2966CBB7003D2103 /* CallFlowLayout.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FA732FB2966CBB7003D2103 /* CallFlowLayout.swift */; };
239+
1FA940B92D74F3B500DF6CDF /* UITableViewExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2CC316692CC26186007CBE16 /* UITableViewExtension.swift */; };
239240
1FAB2E7D2AC99326001214EB /* TOCropViewController in Frameworks */ = {isa = PBXBuildFile; productRef = 1FAB2E7C2AC99326001214EB /* TOCropViewController */; };
240241
1FAB2E7F2AC99367001214EB /* TOCropViewController in Frameworks */ = {isa = PBXBuildFile; productRef = 1FAB2E7E2AC99367001214EB /* TOCropViewController */; };
241242
1FAB2E832AC9EC3F001214EB /* BaseChatViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FAB2E822AC9EC3F001214EB /* BaseChatViewController.swift */; };
@@ -3184,6 +3185,7 @@
31843185
F644A2E02CE28C9A00E2ED81 /* NCChatFileStatus.swift in Sources */,
31853186
1F4DD3ED2571C688007DC98E /* EmojiUtils.swift in Sources */,
31863187
1F35F8EB2AEEBC1100044BDA /* UIResponder+SLKAdditions.m in Sources */,
3188+
1FA940B92D74F3B500DF6CDF /* UITableViewExtension.swift in Sources */,
31873189
2C62B02424C1BDCF007E460A /* NCAppBranding.m in Sources */,
31883190
1F35F90B2AEEE76C00044BDA /* ReplyMessageView.m in Sources */,
31893191
1F1C999E2909846400EACF02 /* BGTaskHelper.swift in Sources */,

NextcloudTalk/BaseChatViewController.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ import SwiftUI
483483

484484
guard let lastMessageBeforeInteraction, let tableView else { return }
485485

486-
if NCUtils.isValid(indexPath: lastMessageBeforeInteraction, forTableView: tableView) {
486+
if tableView.isValid(indexPath: lastMessageBeforeInteraction) {
487487
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(100)) {
488488
tableView.scrollToRow(at: lastMessageBeforeInteraction, at: .bottom, animated: true)
489489
}

NextcloudTalk/ChatViewController.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -1011,7 +1011,7 @@ import SwiftyAttributes
10111011

10121012
self.tableView?.reloadData()
10131013

1014-
if NCUtils.isValid(indexPath: lastHistoryMessageIP, forTableView: tableView) {
1014+
if tableView.isValid(indexPath: lastHistoryMessageIP) {
10151015
self.tableView?.scrollToRow(at: lastHistoryMessageIP, at: .top, animated: false)
10161016
}
10171017
}

NextcloudTalk/InputbarViewController.swift

+50-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import UIKit
1919
internal var autocompletionUsers: [MentionSuggestion] = []
2020
internal var mentionsDict: [String: NCMessageParameter] = [:]
2121
internal var contentView: UIView?
22+
internal var selectedAutocompletionRow: IndexPath?
2223

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

231232
// Check if "@" is still there
232233
self.textView.look(forPrefixes: self.registeredPrefixes) { prefix, word, _ in
234+
self.selectedAutocompletionRow = nil
235+
233236
if prefix?.count ?? 0 > 0 && word?.count ?? 0 > 0 {
234237
self.showAutoCompletionView(showAutocomplete)
235238
} else {
@@ -254,6 +257,38 @@ import UIKit
254257
return resultMessage
255258
}
256259

260+
override public func pressesBegan(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
261+
guard self.isAutoCompleting, !self.autocompletionUsers.isEmpty
262+
else {
263+
super.pressesBegan(presses, with: event)
264+
return
265+
}
266+
267+
let oldIndexPath = self.selectedAutocompletionRow ?? IndexPath(row: 0, section: 0)
268+
var newIndexPath: IndexPath?
269+
270+
// Support selecting the auto complete with return/enter key
271+
if presses.contains(where: { $0.key?.keyCode == .keyboardReturnOrEnter }) {
272+
self.acceptAutoCompletion(withIndexPath: oldIndexPath)
273+
274+
return
275+
} else if presses.contains(where: { $0.key?.keyCode == .keyboardUpArrow }) {
276+
newIndexPath = IndexPath(row: oldIndexPath.row - 1, section: 0)
277+
} else if presses.contains(where: { $0.key?.keyCode == .keyboardDownArrow }) {
278+
newIndexPath = IndexPath(row: oldIndexPath.row + 1, section: 0)
279+
}
280+
281+
if let newIndexPath, self.autoCompletionView.isValid(indexPath: newIndexPath) {
282+
self.selectedAutocompletionRow = newIndexPath
283+
self.autoCompletionView.reloadRows(at: [oldIndexPath, newIndexPath], with: .none)
284+
self.autoCompletionView.scrollToRow(at: newIndexPath, at: .none, animated: true)
285+
286+
return
287+
}
288+
289+
super.pressesBegan(presses, with: event)
290+
}
291+
257292
// MARK: - UITableViewDataSource methods
258293

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

354+
if let selectedAutocompletionRow, selectedAutocompletionRow == indexPath {
355+
cell.layer.borderColor = UIColor.systemGray.cgColor
356+
cell.layer.borderWidth = 2.0
357+
} else {
358+
cell.layer.borderWidth = 0.0
359+
}
360+
319361
cell.accessibilityIdentifier = AutoCompletionCellIdentifier
320362
return cell
321363
}
322364

323365
public override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
324-
guard tableView == self.autoCompletionView,
325-
indexPath.row < self.autocompletionUsers.count
326-
else { return }
366+
guard tableView == self.autoCompletionView else { return }
367+
368+
self.acceptAutoCompletion(withIndexPath: indexPath)
369+
}
370+
371+
private func acceptAutoCompletion(withIndexPath indexPath: IndexPath) {
372+
guard indexPath.row < self.autocompletionUsers.count else { return }
327373

328374
let suggestion = self.autocompletionUsers[indexPath.row]
329375

@@ -332,6 +378,7 @@ import UIKit
332378

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

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

NextcloudTalk/NCUtils.swift

-6
Original file line numberDiff line numberDiff line change
@@ -440,12 +440,6 @@ import AVFoundation
440440
)
441441
}
442442

443-
// MARK: - UITableView utils
444-
445-
public static func isValid(indexPath: IndexPath, forTableView tableView: UITableView) -> Bool {
446-
indexPath.section < tableView.numberOfSections && indexPath.row < tableView.numberOfRows(inSection: indexPath.section)
447-
}
448-
449443
// MARK: - QueryItems utils
450444

451445
public static func value(forKey key: String, fromQueryItems queryItems: NSArray) -> String? {

NextcloudTalk/UITableViewExtension.swift

+6
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,10 @@ extension UITableView {
1414

1515
return T(style: style, reuseIdentifier: identifier)
1616
}
17+
18+
func isValid(indexPath: IndexPath) -> Bool {
19+
indexPath.row >= 0 && indexPath.section >= 0 &&
20+
indexPath.section < self.numberOfSections &&
21+
indexPath.row < self.numberOfRows(inSection: indexPath.section)
22+
}
1723
}

0 commit comments

Comments
 (0)