@@ -19,6 +19,7 @@ import UIKit
19
19
internal var autocompletionUsers : [ MentionSuggestion ] = [ ]
20
20
internal var mentionsDict : [ String : NCMessageParameter ] = [ : ]
21
21
internal var contentView : UIView ?
22
+ internal var selectedAutocompletionRow : IndexPath ?
22
23
23
24
public init ? ( forRoom room: NCRoom , withAccount account: TalkAccount , tableViewStyle style: UITableView . Style ) {
24
25
self . room = room
@@ -230,6 +231,8 @@ import UIKit
230
231
231
232
// Check if "@" is still there
232
233
self . textView. look ( forPrefixes: self . registeredPrefixes) { prefix, word, _ in
234
+ self . selectedAutocompletionRow = nil
235
+
233
236
if prefix? . count ?? 0 > 0 && word? . count ?? 0 > 0 {
234
237
self . showAutoCompletionView ( showAutocomplete)
235
238
} else {
@@ -254,6 +257,38 @@ import UIKit
254
257
return resultMessage
255
258
}
256
259
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
+
257
292
// MARK: - UITableViewDataSource methods
258
293
259
294
public override func numberOfSections( in tableView: UITableView ) -> Int {
@@ -316,14 +351,25 @@ import UIKit
316
351
cell. avatarButton. setActorAvatar ( forId: suggestion. mention. id, withType: suggestion. source, withDisplayName: suggestion. mention. label, withRoomToken: self . room. token, using: self . account)
317
352
}
318
353
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
+
319
361
cell. accessibilityIdentifier = AutoCompletionCellIdentifier
320
362
return cell
321
363
}
322
364
323
365
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 }
327
373
328
374
let suggestion = self . autocompletionUsers [ indexPath. row]
329
375
@@ -332,6 +378,7 @@ import UIKit
332
378
333
379
let mentionWithWhitespace = suggestion. mention. label + " "
334
380
self . acceptAutoCompletion ( with: mentionWithWhitespace, keepPrefix: true )
381
+ self . selectedAutocompletionRow = nil
335
382
}
336
383
337
384
public override func tableView( _ tableView: UITableView , heightForRowAt indexPath: IndexPath ) -> CGFloat {
0 commit comments