From 57c3c64e71ae156855f8b6c84cd71d7d692a8b0a Mon Sep 17 00:00:00 2001 From: Mike Vink <59492084+ivi-vink@users.noreply.github.com> Date: Mon, 20 Jan 2025 20:45:21 +0100 Subject: Squashed 'mut/keuze/' content from commit 720bb27 git-subtree-dir: mut/keuze git-subtree-split: 720bb270aeb006ec2f3ca1104e78785de31f2630 --- kies/DataSource.swift | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 kies/DataSource.swift (limited to 'kies/DataSource.swift') 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] + } + +} -- cgit v1.2.3