Skip to content

Commit a218e07

Browse files
authored
Merge pull request #119 from ionite34/search-improvements
Search Improvements
2 parents 25777b0 + a55e8f5 commit a218e07

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

StabilityMatrix/CheckpointBrowserPage.xaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@
139139
<ui:TextBox
140140
HorizontalAlignment="Stretch"
141141
Margin="8,0,0,0"
142-
PlaceholderText="Query"
142+
PlaceholderText="Search models, #tags, or @users"
143143
Text="{Binding SearchQuery, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
144144

145145
<ui:Button
@@ -244,7 +244,7 @@
244244
FontSize="20"
245245
HorizontalAlignment="Center"
246246
VerticalAlignment="Center"
247-
Text="No results found"
247+
Text="{Binding NoResultsText, FallbackValue=No results found}"
248248
Visibility="{Binding NoResultsFound, Converter={StaticResource BoolToVisibilityConverter}}" />
249249

250250
<ui:ProgressRing

StabilityMatrix/ViewModels/CheckpointBrowserViewModel.cs

+31-10
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public partial class CheckpointBrowserViewModel : ObservableObject
4747
[ObservableProperty] private bool canGoToPreviousPage;
4848
[ObservableProperty] private bool isIndeterminate;
4949
[ObservableProperty] private bool noResultsFound;
50+
[ObservableProperty] private string noResultsText;
5051

5152
public IEnumerable<CivitPeriod> AllCivitPeriods => Enum.GetValues(typeof(CivitPeriod)).Cast<CivitPeriod>();
5253
public IEnumerable<CivitSortMode> AllSortModes => Enum.GetValues(typeof(CivitSortMode)).Cast<CivitSortMode>();
@@ -142,7 +143,6 @@ private async Task CivitModelQuery(CivitModelsRequest request)
142143
{
143144
Logger.Debug("Cache entry already exists, not updating model cards");
144145
}
145-
NoResultsFound = !models.Any();
146146
}
147147
catch (ApiException e)
148148
{
@@ -153,6 +153,7 @@ private async Task CivitModelQuery(CivitModelsRequest request)
153153
finally
154154
{
155155
ShowMainLoadingSpinner = false;
156+
UpdateResultsText();
156157
}
157158
}
158159

@@ -164,15 +165,13 @@ private void UpdateModelCards(IEnumerable<CivitModel>? models, CivitMetadata? me
164165
if (models is null)
165166
{
166167
ModelCards?.Clear();
167-
NoResultsFound = true;
168168
}
169169
else
170170
{
171171
var updateCards = models
172172
.Select(model => new CheckpointBrowserCardViewModel(model,
173173
downloadService, snackbarService, settingsManager));
174174
ModelCards = new ObservableCollection<CheckpointBrowserCardViewModel>(updateCards);
175-
NoResultsFound = false;
176175
}
177176
TotalPages = metadata?.TotalPages ?? 1;
178177
CanGoToPreviousPage = CurrentPageNumber > 1;
@@ -206,8 +205,20 @@ private async Task SearchModels()
206205
Sort = SortMode,
207206
Period = SelectedPeriod,
208207
Page = CurrentPageNumber,
209-
Tag = SearchQuery
210208
};
209+
210+
if (SearchQuery.StartsWith("#"))
211+
{
212+
modelRequest.Tag = SearchQuery[1..];
213+
}
214+
else if (SearchQuery.StartsWith("@"))
215+
{
216+
modelRequest.Username = SearchQuery[1..];
217+
}
218+
else
219+
{
220+
modelRequest.Query = SearchQuery;
221+
}
211222

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

222233
// If cached, update model cards
223-
if (cachedQuery?.Items is not null && cachedQuery.Items.Any())
234+
if (cachedQuery is not null)
224235
{
225236
var elapsed = timer.Elapsed;
226237
Logger.Debug("Using cached query for {Text} [{RequestHash}] (in {Elapsed:F1} s)",
@@ -230,21 +241,22 @@ private async Task SearchModels()
230241
// Start remote query (background mode)
231242
// Skip when last query was less than 2 min ago
232243
var timeSinceCache = DateTimeOffset.UtcNow - cachedQuery.InsertedAt;
233-
if (timeSinceCache?.TotalMinutes < 2)
244+
if (timeSinceCache?.TotalMinutes >= 2)
234245
{
235-
Logger.Debug("Cached query was less than 2 minutes ago ({Seconds:F0} s), skipping remote query",
246+
CivitModelQuery(modelRequest).SafeFireAndForget();
247+
Logger.Debug(
248+
"Cached query was more than 2 minutes ago ({Seconds:F0} s), updating cache with remote query",
236249
timeSinceCache.Value.TotalSeconds);
237-
return;
238250
}
239-
240-
CivitModelQuery(modelRequest).SafeFireAndForget();
241251
}
242252
else
243253
{
244254
// Not cached, wait for remote query
245255
ShowMainLoadingSpinner = true;
246256
await CivitModelQuery(modelRequest);
247257
}
258+
259+
UpdateResultsText();
248260
}
249261

250262
[RelayCommand]
@@ -282,6 +294,7 @@ partial void OnShowNsfwChanged(bool value)
282294
{
283295
settingsManager.SetModelBrowserNsfwEnabled(value);
284296
ModelCardsView?.Refresh();
297+
UpdateResultsText();
285298
}
286299

287300
partial void OnSelectedPeriodChanged(CivitPeriod oldValue, CivitPeriod newValue)
@@ -312,4 +325,12 @@ private async Task TrySearchAgain(bool shouldUpdatePageNumber = true)
312325
// execute command instead of calling method directly so that the IsRunning property gets updated
313326
await SearchModelsCommand.ExecuteAsync(null);
314327
}
328+
329+
private void UpdateResultsText()
330+
{
331+
NoResultsFound = ModelCardsView?.IsEmpty ?? true;
332+
NoResultsText = ModelCards?.Count == 0
333+
? "No results found"
334+
: $"{ModelCards?.Count} results hidden by filters";
335+
}
315336
}

0 commit comments

Comments
 (0)