Skip to content

Commit

Permalink
core: better search matching
Browse files Browse the repository at this point in the history
- order of words wont matter
- u can use artist + title and still get a match
  • Loading branch information
MSOB7YY committed Jan 1, 2024
1 parent 30fb059 commit 439a8da
Showing 1 changed file with 68 additions and 18 deletions.
86 changes: 68 additions & 18 deletions lib/controller/search_sort_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,19 @@ class SearchSortController {
},
isolateFunction: (itemsSendPort) async {
final params = {
'tracks': Indexer.inst.allTracksMappedByPath.values.map((e) => e.toJson()).toList(),
'tracks': Indexer.inst.allTracksMappedByPath.values
.map((e) => {
'title': e.title,
'artist': e.originalArtist,
'album': e.album,
'albumArtist': e.albumArtist,
'genre': e.originalGenre,
'composer': e.composer,
'year': e.year,
'comment': e.comment,
'path': e.path,
})
.toList(),
'artistsSplitConfig': ArtistsSplitConfig.settings().toMap(),
'genresSplitConfig': GenresSplitConfig.settings().toMap(),
'filters': settings.trackSearchFilter.cast<TrackSearchFilter>(),
Expand Down Expand Up @@ -259,6 +271,7 @@ class SearchSortController {
});
}

// TODO: comment search filter
static void _searchTracksIsolate(Map params) {
final tracks = params['tracks'] as List<Map>;
final artistsSplitConfig = ArtistsSplitConfig.fromMap(params['artistsSplitConfig']);
Expand All @@ -271,15 +284,6 @@ class SearchSortController {

sendPort.send(receivePort.sendPort);

final tracksExtended = <TrackExtended>[];
for (final tr in tracks) {
final trExt = TrackExtended.fromJson(
tr.cast(),
artistsSplitConfig: artistsSplitConfig,
genresSplitConfig: genresSplitConfig,
);
tracksExtended.add(trExt);
}
final tsfMap = <TrackSearchFilter, bool>{};
tsf.loop((f, _) => tsfMap[f] = true);

Expand All @@ -295,6 +299,47 @@ class SearchSortController {
final function = _functionOfCleanup(cleanup);
String textCleanedForSearch(String textToClean) => function(textToClean);

Iterable<String> splitThis(String? property, bool split) => !split || property == null ? [] : property.split(' ').map((e) => textCleanedForSearch(e));

final tracksExtended = <({
String path,
Iterable<String> splitTitle,
Iterable<String> splitFilename,
Iterable<String> splitAlbum,
Iterable<String> splitAlbumArtist,
List<String> splitArtist,
List<String> splitGenre,
Iterable<String> splitComposer,
String year,
})>[];
for (final trMap in tracks) {
final path = trMap['path'] as String;
tracksExtended.add(
(
path: path,
splitTitle: splitThis(trMap['title'], stitle),
splitFilename: splitThis(path.getFilename, sfilename),
splitAlbum: splitThis(trMap['album'], salbum),
splitAlbumArtist: splitThis(trMap['albumArtist'], salbumartist),
splitArtist: sartist
? Indexer.splitArtist(
title: trMap['title'],
originalArtist: trMap['artist'],
config: artistsSplitConfig,
)
: [],
splitGenre: sgenre
? Indexer.splitGenre(
trMap['genre'],
config: genresSplitConfig,
)
: [],
splitComposer: splitThis(trMap['composer'], scomposer),
year: textCleanedForSearch(trMap['year'].toString()),
),
);
}

receivePort.listen((p) {
if (p is String && p == 'dispose') {
receivePort.close();
Expand All @@ -305,17 +350,22 @@ class SearchSortController {
final temp = p['temp'] as bool;

final lctext = textCleanedForSearch(text);
final lctextSplit = text.split(' ').map((e) => textCleanedForSearch(e));

bool isMatch(Iterable<String> propertySplit) {
return lctextSplit.every((element) => propertySplit.any((p) => p.contains(element)));
}

final result = <Track>[];
tracksExtended.loop((trExt, index) {
if ((stitle && textCleanedForSearch(trExt.title).contains(lctext)) ||
(sfilename && textCleanedForSearch((trExt.path).getFilename).contains(lctext)) ||
(salbum && textCleanedForSearch(trExt.album).contains(lctext)) ||
(salbumartist && textCleanedForSearch(trExt.albumArtist).contains(lctext)) ||
(sartist && (trExt.artistsList).any((element) => textCleanedForSearch(element).contains(lctext))) ||
(sgenre && (trExt.genresList).any((element) => textCleanedForSearch(element).contains(lctext))) ||
(scomposer && textCleanedForSearch(trExt.composer).contains(lctext)) ||
(syear && textCleanedForSearch((trExt.year).toString()).contains(lctext))) {
if ((stitle && isMatch(trExt.splitTitle)) ||
(sfilename && isMatch(trExt.splitFilename)) ||
(salbum && isMatch(trExt.splitAlbum)) ||
(salbumartist && isMatch(trExt.splitAlbumArtist)) ||
(sartist && isMatch(trExt.splitArtist)) ||
(sgenre && isMatch(trExt.splitGenre)) ||
(scomposer && isMatch(trExt.splitComposer)) ||
(syear && trExt.year.contains(lctext))) {
result.add(Track(trExt.path));
}
});
Expand Down

0 comments on commit 439a8da

Please sign in to comment.