summaryrefslogtreecommitdiff
path: root/kies/DataSource.swift
diff options
context:
space:
mode:
authorMike Vink <59492084+ivi-vink@users.noreply.github.com>2025-01-20 20:45:21 +0100
committerMike Vink <59492084+ivi-vink@users.noreply.github.com>2025-01-20 20:45:21 +0100
commit57c3c64e71ae156855f8b6c84cd71d7d692a8b0a (patch)
treee3b25d811a89570b6a3ef880aa0e8992069c5773 /kies/DataSource.swift
Squashed 'mut/keuze/' content from commit 720bb27
git-subtree-dir: mut/keuze git-subtree-split: 720bb270aeb006ec2f3ca1104e78785de31f2630
Diffstat (limited to 'kies/DataSource.swift')
-rw-r--r--kies/DataSource.swift43
1 files changed, 43 insertions, 0 deletions
diff --git a/kies/DataSource.swift b/kies/DataSource.swift
new file mode 100644
index 0000000..23c1f8e
--- /dev/null
+++ b/kies/DataSource.swift
@@ -0,0 +1,43 @@
+//
+// Sorter.swift
+// kies
+//
+// Created by Thomas Billiet on 11/04/2019.
+// Copyright © 2019 Thomas Billiet. All rights reserved.
+//
+
+import Foundation
+import Cocoa
+
+class DataSource: NSObject, NSTableViewDataSource {
+ var items = [String]()
+ var sortedItems = [String]()
+ var matches = [String: String]()
+ var scores = [String: Float]()
+
+ func updateItems(_ items: [String]) {
+ self.items = items
+ self.sortedItems = items
+ }
+
+ func updateSort(query: String) {
+ for item in items {
+ let match = item.lowercased().longestCommonSubsequence(query.lowercased())
+ matches[item] = match
+ scores[item] = Float(match.count) / Float(query.count)
+ }
+
+ sortedItems = items.sorted(by: { (a, b) -> Bool in
+ scores[a]! > scores[b]!
+ })
+ }
+
+ func numberOfRows(in tableView: NSTableView) -> Int {
+ return sortedItems.count
+ }
+
+ func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any? {
+ return sortedItems[row]
+ }
+
+}