-
-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from milanvarady/Improve-search
Rework search with Ifrit
- Loading branch information
Showing
19 changed files
with
304 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// | ||
// Cask+Searchable.swift | ||
// Applite | ||
// | ||
// Created by Milán Várady on 2025.01.02. | ||
// | ||
|
||
import Foundation | ||
import Ifrit | ||
|
||
extension Cask: Searchable { | ||
nonisolated var weightedSearchProperties: [FuseProp] { | ||
return [ | ||
FuseProp(self.info.name, weight: 1), | ||
FuseProp(self.info.id, weight: 1), | ||
FuseProp(self.info.description, weight: 0.3) | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// | ||
// SearchableCaskCollection.swift | ||
// Applite | ||
// | ||
// Created by Milán Várady on 2025.01.02. | ||
// | ||
|
||
import Foundation | ||
import Ifrit | ||
|
||
@MainActor | ||
class SearchableCaskCollection: ObservableObject { | ||
@Published private(set) var casks: [Cask] = [] | ||
@Published private(set) var casksMatchingSearch: [Cask] = [] | ||
|
||
init(casks: [Cask] = []) { | ||
self.defineCasks(casks) | ||
} | ||
|
||
func defineCasks(_ casks: [Cask]) { | ||
self.casks = casks | ||
self.casksMatchingSearch = casks | ||
} | ||
|
||
func addCask(_ cask: Cask) { | ||
self.casks.append(cask) | ||
} | ||
|
||
func remove(_ cask: Cask) { | ||
self.casks.removeAll(where: { $0 == cask }) | ||
self.casksMatchingSearch.removeAll(where: { $0 == cask }) | ||
} | ||
|
||
func removeAll() { | ||
self.casks.removeAll() | ||
self.casksMatchingSearch.removeAll() | ||
} | ||
|
||
func search(query: String, diffScroreThreshold: Double = 0.2, limitResults: Int = 20) async { | ||
guard !query.isEmpty else { | ||
self.casksMatchingSearch = self.casks | ||
return | ||
} | ||
|
||
let fuse = Fuse() | ||
let searchResults = await fuse.search(query, in: self.casks, by: \Cask.weightedSearchProperties) | ||
|
||
var matchedCasks: [Cask] = [] | ||
|
||
for result in searchResults { | ||
guard result.diffScore <= diffScroreThreshold else { | ||
break | ||
} | ||
|
||
guard matchedCasks.count <= limitResults else { | ||
break | ||
} | ||
|
||
matchedCasks.append(self.casks[result.index]) | ||
} | ||
|
||
self.casksMatchingSearch = matchedCasks | ||
} | ||
|
||
func filterSearch(by filter: ([Cask]) -> [Cask]) { | ||
self.casksMatchingSearch = filter(casksMatchingSearch) | ||
} | ||
|
||
func setReserveCapacity(_ capacity: Int) { | ||
self.casks.reserveCapacity(capacity) | ||
self.casksMatchingSearch.reserveCapacity(capacity) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.