Skip to content

Commit

Permalink
Merge pull request #21 from nolanblew/fix_search
Browse files Browse the repository at this point in the history
Fix Broken Search
  • Loading branch information
nolanblew authored Nov 6, 2024
2 parents 900b8b4 + ef85e4b commit 6b6e295
Showing 1 changed file with 32 additions and 26 deletions.
58 changes: 32 additions & 26 deletions AnimeCharacters/Pages/Animes.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,27 @@ public partial class Animes

public User CurrentUser { get; set; }

public string SearchFilter { get; set; }
private string _searchFilter;
public string SearchFilter
{
get => _searchFilter;
set
{
_searchFilter = value;
UpdateHeaderContents();
}
}

public bool IsBusy { get; set; } = true;

public List<LibraryEntry> FilteredLibraryEntries
{
get
{
if (string.IsNullOrWhiteSpace(SearchFilter))
{
return _LibraryEntries?
return _LibraryEntries?
.Values?
.OrderByDescending(e => e.ProgressedAt)?
.ToList();
}

var lowerFilter = SearchFilter.ToLower();
return _LibraryEntries?.Values?
.Where(e => _MatchesSearch(e.Anime))?
.OrderByDescending(e => e.ProgressedAt)
.ToList();
}
}

Expand Down Expand Up @@ -105,8 +105,18 @@ private void UpdateHeaderContents()

foreach (var header in headerStates)
{
header.Content = FilteredLibraryEntries.Where(header.FilterCondition).ToList();
var filteredContent = FilteredLibraryEntries
.Where(header.FilterCondition);

if (!string.IsNullOrWhiteSpace(SearchFilter))
{
filteredContent = filteredContent.Where(entry => _MatchesSearch(entry.Anime));
}

header.Content = filteredContent.ToList();
}

StateHasChanged();
}

// Method to toggle the collapsed/expanded state of a header
Expand Down Expand Up @@ -382,28 +392,24 @@ bool _MatchesSearch(Anime anime)

var lowerFilter = SearchFilter.ToLower();

var titleSplit = anime.Title.ToLower().Split(' ');
if (titleSplit.Any(t => t.StartsWith(lowerFilter)))
// Check main title
if (anime.Title.ToLower().Contains(lowerFilter))
{
return true;
}

if (!string.IsNullOrWhiteSpace(anime.RomanjiTitle))
// Check Romanji title
if (!string.IsNullOrWhiteSpace(anime.RomanjiTitle) &&
anime.RomanjiTitle.ToLower().Contains(lowerFilter))
{
var romanjiTitleSplit = anime.Title.ToLower().Split(' ');
if (romanjiTitleSplit.Any(t => t.StartsWith(lowerFilter)))
{
return true;
}
return true;
}

if (!string.IsNullOrWhiteSpace(anime.EnglishTitle))
// Check English title
if (!string.IsNullOrWhiteSpace(anime.EnglishTitle) &&
anime.EnglishTitle.ToLower().Contains(lowerFilter))
{
var englishTitleSplit = anime.EnglishTitle.ToLower().Split(' ');
if (englishTitleSplit.Any(t => t.StartsWith(lowerFilter)))
{
return true;
}
return true;
}

return false;
Expand Down

0 comments on commit 6b6e295

Please sign in to comment.