Skip to content

Commit

Permalink
Merge pull request #119 from ionite34/search-improvements
Browse files Browse the repository at this point in the history
Search Improvements
  • Loading branch information
mohnjiles authored Jun 27, 2023
2 parents 25777b0 + a55e8f5 commit a218e07
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
4 changes: 2 additions & 2 deletions StabilityMatrix/CheckpointBrowserPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
<ui:TextBox
HorizontalAlignment="Stretch"
Margin="8,0,0,0"
PlaceholderText="Query"
PlaceholderText="Search models, #tags, or @users"
Text="{Binding SearchQuery, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

<ui:Button
Expand Down Expand Up @@ -244,7 +244,7 @@
FontSize="20"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Text="No results found"
Text="{Binding NoResultsText, FallbackValue=No results found}"
Visibility="{Binding NoResultsFound, Converter={StaticResource BoolToVisibilityConverter}}" />

<ui:ProgressRing
Expand Down
41 changes: 31 additions & 10 deletions StabilityMatrix/ViewModels/CheckpointBrowserViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public partial class CheckpointBrowserViewModel : ObservableObject
[ObservableProperty] private bool canGoToPreviousPage;
[ObservableProperty] private bool isIndeterminate;
[ObservableProperty] private bool noResultsFound;
[ObservableProperty] private string noResultsText;

public IEnumerable<CivitPeriod> AllCivitPeriods => Enum.GetValues(typeof(CivitPeriod)).Cast<CivitPeriod>();
public IEnumerable<CivitSortMode> AllSortModes => Enum.GetValues(typeof(CivitSortMode)).Cast<CivitSortMode>();
Expand Down Expand Up @@ -142,7 +143,6 @@ private async Task CivitModelQuery(CivitModelsRequest request)
{
Logger.Debug("Cache entry already exists, not updating model cards");
}
NoResultsFound = !models.Any();
}
catch (ApiException e)
{
Expand All @@ -153,6 +153,7 @@ private async Task CivitModelQuery(CivitModelsRequest request)
finally
{
ShowMainLoadingSpinner = false;
UpdateResultsText();
}
}

Expand All @@ -164,15 +165,13 @@ private void UpdateModelCards(IEnumerable<CivitModel>? models, CivitMetadata? me
if (models is null)
{
ModelCards?.Clear();
NoResultsFound = true;
}
else
{
var updateCards = models
.Select(model => new CheckpointBrowserCardViewModel(model,
downloadService, snackbarService, settingsManager));
ModelCards = new ObservableCollection<CheckpointBrowserCardViewModel>(updateCards);
NoResultsFound = false;
}
TotalPages = metadata?.TotalPages ?? 1;
CanGoToPreviousPage = CurrentPageNumber > 1;
Expand Down Expand Up @@ -206,8 +205,20 @@ private async Task SearchModels()
Sort = SortMode,
Period = SelectedPeriod,
Page = CurrentPageNumber,
Tag = SearchQuery
};

if (SearchQuery.StartsWith("#"))
{
modelRequest.Tag = SearchQuery[1..];
}
else if (SearchQuery.StartsWith("@"))
{
modelRequest.Username = SearchQuery[1..];
}
else
{
modelRequest.Query = SearchQuery;
}

if (SelectedModelType != CivitModelType.All)
{
Expand All @@ -220,7 +231,7 @@ private async Task SearchModels()
.FindByIdAsync(ObjectHash.GetMd5Guid(modelRequest));

// If cached, update model cards
if (cachedQuery?.Items is not null && cachedQuery.Items.Any())
if (cachedQuery is not null)
{
var elapsed = timer.Elapsed;
Logger.Debug("Using cached query for {Text} [{RequestHash}] (in {Elapsed:F1} s)",
Expand All @@ -230,21 +241,22 @@ private async Task SearchModels()
// Start remote query (background mode)
// Skip when last query was less than 2 min ago
var timeSinceCache = DateTimeOffset.UtcNow - cachedQuery.InsertedAt;
if (timeSinceCache?.TotalMinutes < 2)
if (timeSinceCache?.TotalMinutes >= 2)
{
Logger.Debug("Cached query was less than 2 minutes ago ({Seconds:F0} s), skipping remote query",
CivitModelQuery(modelRequest).SafeFireAndForget();
Logger.Debug(
"Cached query was more than 2 minutes ago ({Seconds:F0} s), updating cache with remote query",
timeSinceCache.Value.TotalSeconds);
return;
}

CivitModelQuery(modelRequest).SafeFireAndForget();
}
else
{
// Not cached, wait for remote query
ShowMainLoadingSpinner = true;
await CivitModelQuery(modelRequest);
}

UpdateResultsText();
}

[RelayCommand]
Expand Down Expand Up @@ -282,6 +294,7 @@ partial void OnShowNsfwChanged(bool value)
{
settingsManager.SetModelBrowserNsfwEnabled(value);
ModelCardsView?.Refresh();
UpdateResultsText();
}

partial void OnSelectedPeriodChanged(CivitPeriod oldValue, CivitPeriod newValue)
Expand Down Expand Up @@ -312,4 +325,12 @@ private async Task TrySearchAgain(bool shouldUpdatePageNumber = true)
// execute command instead of calling method directly so that the IsRunning property gets updated
await SearchModelsCommand.ExecuteAsync(null);
}

private void UpdateResultsText()
{
NoResultsFound = ModelCardsView?.IsEmpty ?? true;
NoResultsText = ModelCards?.Count == 0
? "No results found"
: $"{ModelCards?.Count} results hidden by filters";
}
}

0 comments on commit a218e07

Please sign in to comment.