forked from LykosAI/StabilityMatrix
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathCivitAiBrowserViewModel.cs
More file actions
946 lines (812 loc) · 31.5 KB
/
CivitAiBrowserViewModel.cs
File metadata and controls
946 lines (812 loc) · 31.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Reactive.Linq;
using System.Text.Json;
using AsyncAwaitBestPractices;
using Avalonia.Controls;
using Avalonia.Controls.Notifications;
using Avalonia.Threading;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using DynamicData;
using DynamicData.Binding;
using Injectio.Attributes;
using NLog;
using Refit;
using StabilityMatrix.Avalonia.Animations;
using StabilityMatrix.Avalonia.Languages;
using StabilityMatrix.Avalonia.Models;
using StabilityMatrix.Avalonia.Services;
using StabilityMatrix.Avalonia.ViewModels.Base;
using StabilityMatrix.Avalonia.ViewModels.CheckpointManager;
using StabilityMatrix.Avalonia.Views;
using StabilityMatrix.Core.Api;
using StabilityMatrix.Core.Attributes;
using StabilityMatrix.Core.Database;
using StabilityMatrix.Core.Extensions;
using StabilityMatrix.Core.Helper;
using StabilityMatrix.Core.Models;
using StabilityMatrix.Core.Models.Api;
using StabilityMatrix.Core.Models.Settings;
using StabilityMatrix.Core.Services;
using ILogger = Microsoft.Extensions.Logging.ILogger;
using Notification = Avalonia.Controls.Notifications.Notification;
namespace StabilityMatrix.Avalonia.ViewModels.CheckpointBrowser;
[View(typeof(CivitAiBrowserPage))]
[RegisterSingleton<CivitAiBrowserViewModel>]
public sealed partial class CivitAiBrowserViewModel : TabViewModelBase, IInfinitelyScroll
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private readonly CivitCompatApiManager civitApi;
private readonly ISettingsManager settingsManager;
private readonly IServiceManager<ViewModelBase> dialogFactory;
private readonly ILiteDbContext liteDbContext;
private readonly IConnectedServiceManager connectedServiceManager;
private readonly INotificationService notificationService;
private readonly ICivitBaseModelTypeService baseModelTypeService;
private readonly INavigationService<MainWindowViewModel> navigationService;
private bool dontSearch = false;
private readonly SourceCache<OrderedValue<CivitModel>, int> modelCache = new(static ov => ov.Value.Id);
private const int TargetPageItemCount = 30;
[ObservableProperty]
private IObservableCollection<CheckpointBrowserCardViewModel> modelCards =
new ObservableCollectionExtended<CheckpointBrowserCardViewModel>();
[ObservableProperty]
private string searchQuery = string.Empty;
[ObservableProperty]
private bool showNsfw;
[ObservableProperty]
private bool showMainLoadingSpinner;
[ObservableProperty]
private CivitPeriod selectedPeriod = CivitPeriod.AllTime;
[ObservableProperty]
private CivitSortMode sortMode = CivitSortMode.HighestRated;
[ObservableProperty]
private CivitModelType selectedModelType = CivitModelType.Checkpoint;
[ObservableProperty]
private bool hasSearched;
[ObservableProperty]
private bool isIndeterminate;
[ObservableProperty]
private bool noResultsFound;
[ObservableProperty]
private string noResultsText = string.Empty;
[ObservableProperty]
private ObservableCollection<string> selectedBaseModels = [];
[ObservableProperty]
private bool showSantaHats = true;
[ObservableProperty]
private string? nextPageCursor;
[ObservableProperty]
private bool hideInstalledModels;
[ObservableProperty]
private bool hideEarlyAccessModels;
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(StatsResizeFactor))]
private double resizeFactor;
private readonly SourceCache<string, string> baseModelCache = new(static s => s);
[ObservableProperty]
private IObservableCollection<BaseModelOptionViewModel> allBaseModels =
new ObservableCollectionExtended<BaseModelOptionViewModel>();
[ObservableProperty]
private bool civitUseDiscoveryApi;
public bool UseLocalCache => true;
public double StatsResizeFactor => Math.Clamp(ResizeFactor, 0.75d, 1.25d);
public IEnumerable<CivitPeriod> AllCivitPeriods =>
Enum.GetValues(typeof(CivitPeriod)).Cast<CivitPeriod>();
public IEnumerable<CivitSortMode> AllSortModes =>
Enum.GetValues(typeof(CivitSortMode)).Cast<CivitSortMode>();
public IEnumerable<CivitModelType> AllModelTypes =>
Enum.GetValues(typeof(CivitModelType))
.Cast<CivitModelType>()
.Where(t => t == CivitModelType.All || t.ConvertTo<SharedFolderType>() > 0)
.OrderBy(t => t.ToString());
public string ClearButtonText =>
SelectedBaseModels.Count == AllBaseModels.Count
? Resources.Action_ClearSelection
: Resources.Action_SelectAll;
public bool ShowFilterNumber =>
SelectedBaseModels.Count > 0 && SelectedBaseModels.Count < AllBaseModels.Count;
public CivitAiBrowserViewModel(
CivitCompatApiManager civitApi,
ISettingsManager settingsManager,
IServiceManager<ViewModelBase> dialogFactory,
ILiteDbContext liteDbContext,
IConnectedServiceManager connectedServiceManager,
INotificationService notificationService,
ICivitBaseModelTypeService baseModelTypeService,
INavigationService<MainWindowViewModel> navigationService
)
{
this.civitApi = civitApi;
this.settingsManager = settingsManager;
this.dialogFactory = dialogFactory;
this.liteDbContext = liteDbContext;
this.connectedServiceManager = connectedServiceManager;
this.notificationService = notificationService;
this.baseModelTypeService = baseModelTypeService;
this.navigationService = navigationService;
EventManager.Instance.NavigateAndFindCivitModelRequested += OnNavigateAndFindCivitModelRequested;
var filterPredicate = Observable
.FromEventPattern<PropertyChangedEventArgs>(this, nameof(PropertyChanged))
.Where(x =>
x.EventArgs.PropertyName
is nameof(HideInstalledModels)
or nameof(ShowNsfw)
or nameof(HideEarlyAccessModels)
)
.Throttle(TimeSpan.FromMilliseconds(50))
.Select(_ => (Func<CheckpointBrowserCardViewModel, bool>)FilterModelCardsPredicate)
.StartWith(FilterModelCardsPredicate)
.ObserveOn(SynchronizationContext.Current)
.AsObservable();
var sortPredicate = SortExpressionComparer<CheckpointBrowserCardViewModel>.Ascending(static x =>
x.Order
);
AddDisposable(
modelCache
.Connect()
.DeferUntilLoaded()
.Transform(ov =>
dialogFactory.Get<CheckpointBrowserCardViewModel>(vm =>
{
vm.CivitModel = ov.Value;
vm.Order = ov.Order;
return vm;
})
)
.DisposeMany()
.Filter(filterPredicate)
.SortAndBind(ModelCards, sortPredicate)
.ObserveOn(SynchronizationContext.Current)
.Subscribe()
);
AddDisposable(
baseModelCache
.Connect()
.DeferUntilLoaded()
.Transform(baseModel => new BaseModelOptionViewModel
{
ModelType = baseModel,
IsSelected = settingsManager.Settings.SelectedCivitBaseModels.Contains(baseModel),
})
.SortAndBind(
AllBaseModels,
SortExpressionComparer<BaseModelOptionViewModel>.Ascending(m => m.ModelType)
)
.WhenPropertyChanged(p => p.IsSelected)
.ObserveOn(SynchronizationContext.Current)
.Subscribe(next =>
{
if (next.Sender.IsSelected)
SelectedBaseModels.Add(next.Sender.ModelType);
else
SelectedBaseModels.Remove(next.Sender.ModelType);
OnPropertyChanged(nameof(ClearButtonText));
OnPropertyChanged(nameof(SelectedBaseModels));
OnPropertyChanged(nameof(ShowFilterNumber));
})
);
if (Design.IsDesignMode)
return;
var settingsTransactionObservable = this.WhenPropertyChanged(x => x.SelectedBaseModels)
.Throttle(TimeSpan.FromMilliseconds(50))
.Skip(1)
.ObserveOn(SynchronizationContext.Current)
.Subscribe(_ =>
{
if (!settingsManager.IsLibraryDirSet)
return;
settingsManager.Transaction(settings =>
settings.SelectedCivitBaseModels = SelectedBaseModels.ToList()
);
if (!dontSearch)
{
TrySearchAgain().SafeFireAndForget();
}
});
AddDisposable(settingsTransactionObservable);
AddDisposable(
settingsManager.RelayPropertyFor(
this,
model => model.ShowNsfw,
settings => settings.ModelBrowserNsfwEnabled,
true
)
);
AddDisposable(
settingsManager.RelayPropertyFor(
this,
model => model.HideInstalledModels,
settings => settings.HideInstalledModelsInModelBrowser,
true
)
);
AddDisposable(
settingsManager.RelayPropertyFor(
this,
model => model.ResizeFactor,
settings => settings.CivitBrowserResizeFactor,
true
)
);
AddDisposable(
settingsManager.RelayPropertyFor(
this,
model => model.HideEarlyAccessModels,
settings => settings.HideEarlyAccessModels,
true
)
);
AddDisposable(
settingsManager.RelayPropertyFor(
this,
model => model.CivitUseDiscoveryApi,
settings => settings.CivitUseDiscoveryApi,
true
)
);
EventManager.Instance.NavigateAndFindCivitAuthorRequested += OnNavigateAndFindCivitAuthorRequested;
}
private void OnNavigateAndFindCivitAuthorRequested(object? sender, string? e)
{
if (string.IsNullOrWhiteSpace(e))
return;
SearchQuery = $"@{e}";
SearchModelsCommand.ExecuteAsync(false).SafeFireAndForget();
}
private void OnNavigateAndFindCivitModelRequested(object? sender, int e)
{
if (e <= 0)
return;
SearchQuery = $"$#{e}";
SearchModelsCommand.ExecuteAsync(false).SafeFireAndForget();
}
public override void OnLoaded()
{
if (Design.IsDesignMode)
return;
var searchOptions = settingsManager.Settings.ModelSearchOptions;
// Fix SelectedModelType if someone had selected the obsolete "Model" option
if (searchOptions is { SelectedModelType: CivitModelType.Model })
{
settingsManager.Transaction(s =>
s.ModelSearchOptions = new ModelSearchOptions(
SelectedPeriod,
SortMode,
CivitModelType.Checkpoint,
string.Empty
)
);
searchOptions = settingsManager.Settings.ModelSearchOptions;
}
SelectedPeriod = searchOptions?.SelectedPeriod ?? CivitPeriod.AllTime;
SortMode = searchOptions?.SortMode ?? CivitSortMode.HighestRated;
SelectedModelType = searchOptions?.SelectedModelType ?? CivitModelType.Checkpoint;
base.OnLoaded();
}
protected override async Task OnInitialLoadedAsync()
{
if (Design.IsDesignMode)
return;
await base.OnInitialLoadedAsync();
if (settingsManager.Settings.AutoLoadCivitModels)
{
await SearchModelsCommand.ExecuteAsync(false);
}
}
public override async Task OnLoadedAsync()
{
if (Design.IsDesignMode)
return;
var baseModels = await baseModelTypeService.GetBaseModelTypes(includeAllOption: false);
baseModels = baseModels.Except(settingsManager.Settings.DisabledBaseModelTypes).ToList();
if (baseModels.Count == 0)
{
return;
}
dontSearch = true;
baseModelCache.EditDiff(baseModels, static (a, b) => a.Equals(b, StringComparison.OrdinalIgnoreCase));
dontSearch = false;
}
/// <summary>
/// Filter predicate for model cards
/// </summary>
private bool FilterModelCardsPredicate(CheckpointBrowserCardViewModel card)
{
if (HideInstalledModels && card.UpdateCardText == "Installed")
return false;
if (
HideEarlyAccessModels
&& card.CivitModel.ModelVersions != null
&& card.CivitModel.ModelVersions.All(x => x.Availability == "EarlyAccess")
)
return false;
return !card.CivitModel.Nsfw || ShowNsfw;
}
[RelayCommand]
private async Task OnUseDiscoveryToggle()
{
if (CivitUseDiscoveryApi)
{
CivitUseDiscoveryApi = false;
}
else
{
if (!await connectedServiceManager.PromptEnableCivitUseDiscoveryApi())
return;
CivitUseDiscoveryApi = true;
}
// Reset cache in case model differences
Logger.Info("Toggled Discovery API, clearing cache");
await liteDbContext.CivitModels.DeleteAllAsync();
await liteDbContext.CivitModelVersions.DeleteAllAsync();
var items = await liteDbContext.CivitModelQueryCache.DeleteAllAsync();
Logger.Info("Deleted {Count} Civit model query cache entries", items);
}
/// <summary>
/// Background update task
/// </summary>
private async Task CivitModelQuery(CivitModelsRequest request, bool isInfiniteScroll = false)
{
var timer = Stopwatch.StartNew();
var queryText = request.Query;
var models = new List<CivitModel>();
// Store original request for caching
var originalRequestStr = JsonSerializer.Serialize(request);
CivitModelsResponse? modelsResponse = null;
try
{
if (!string.IsNullOrWhiteSpace(request.CommaSeparatedModelIds))
{
// count IDs
var ids = request.CommaSeparatedModelIds.Split(',');
if (ids.Length > 100)
{
var idChunks = ids.Chunk(100);
foreach (var chunk in idChunks)
{
request.CommaSeparatedModelIds = string.Join(",", chunk);
request.Limit = 100;
var chunkModelsResponse = await civitApi.GetModels(request);
if (chunkModelsResponse.Items != null)
{
models.AddRange(chunkModelsResponse.Items);
}
}
}
else
{
modelsResponse = await civitApi.GetModels(request);
models = modelsResponse.Items;
}
}
else
{
// Auto-paginate via cursor until we fill the target page size or run out
var collectedById = new HashSet<int>();
var targetCount = request.Limit ?? TargetPageItemCount;
var safetyGuard = 0;
while (true)
{
var resp = await civitApi.GetModels(request);
modelsResponse = resp;
if (resp.Items != null)
{
foreach (var item in resp.Items)
{
if (collectedById.Add(item.Id))
{
models.Add(item);
}
}
}
// Check how many items survive local filtering
var filteredCount = models
.Where(m => m.Type.ConvertTo<SharedFolderType>() > 0)
.Count(m => m.Mode == null);
var next = resp.Metadata?.NextCursor;
if (filteredCount >= targetCount || string.IsNullOrEmpty(next))
{
break;
}
request.Cursor = next;
if (++safetyGuard >= 10)
{
// Avoid unbounded looping on unexpected cursors
break;
}
}
}
if (models is null)
{
Logger.Debug(
"CivitAI Query {Text} returned no results (in {Elapsed:F1} s)",
queryText,
timer.Elapsed.TotalSeconds
);
return;
}
Logger.Debug(
"CivitAI Query {Text} returned {Results} results (in {Elapsed:F1} s)",
queryText,
models.Count,
timer.Elapsed.TotalSeconds
);
var unknown = models.Where(m => m.Type == CivitModelType.Unknown).ToList();
if (unknown.Any())
{
var names = unknown.Select(m => m.Name).ToList();
Logger.Warn("Excluded {Unknown} unknown model types: {Models}", unknown.Count, names);
}
// Filter out unknown model types and archived/taken-down models
models = models
.Where(m => m.Type.ConvertTo<SharedFolderType>() > 0)
.Where(m => m.Mode == null)
.ToList();
var cacheNew = true;
if (UseLocalCache)
{
// Database update calls will invoke `OnModelsUpdated`
// Add to database
await liteDbContext.UpsertCivitModelAsync(models);
// Add as cache entry
var originalRequest = JsonSerializer.Deserialize<CivitModelsRequest>(originalRequestStr);
cacheNew = await liteDbContext.UpsertCivitModelQueryCacheEntryAsync(
new CivitModelQueryCacheEntry
{
Id = ObjectHash.GetMd5Guid(originalRequest),
InsertedAt = DateTimeOffset.UtcNow,
Request = request,
Items = models,
Metadata = modelsResponse?.Metadata,
}
);
}
if (cacheNew)
{
var doesBaseModelTypeMatch =
SelectedBaseModels.Count == 0
? request.BaseModels == null || request.BaseModels.Length == 0
: SelectedBaseModels.SequenceEqual(request.BaseModels ?? []);
var doesModelTypeMatch =
SelectedModelType == CivitModelType.All
? request.Types == null || request.Types.Length == 0
: SelectedModelType == request.Types?.FirstOrDefault();
if (doesBaseModelTypeMatch && doesModelTypeMatch)
{
UpdateModelCards(models, isInfiniteScroll);
}
}
NextPageCursor = modelsResponse?.Metadata?.NextCursor;
}
catch (OperationCanceledException)
{
notificationService.Show(
new Notification("Request to CivitAI timed out", "Please try again in a few minutes")
);
Logger.Warn($"CivitAI query timed out ({request})");
}
catch (HttpRequestException e)
{
notificationService.Show(
new Notification("CivitAI can't be reached right now", "Please try again in a few minutes")
);
Logger.Warn(e, $"CivitAI query HttpRequestException ({request})");
}
catch (ApiException e)
{
// Additional details
var responseContent = e.Content ?? "[No Content]";
var responseCode = e.StatusCode;
var responseCodeName = e.StatusCode.ToString();
Logger.Warn(
e,
"CivitAI query ApiException ({Request}), ({Code}: {Response})",
request,
responseCode,
responseContent
);
notificationService.Show(
new Notification(
"CivitAI can't be reached right now",
$"Please try again in a few minutes. ({responseCode}: {responseCodeName})",
NotificationType.Warning,
expiration: TimeSpan.Zero,
onClick: () =>
Dispatcher.UIThread.InvokeAsync(async () =>
await DialogHelper.CreateApiExceptionDialog(e).ShowAsync()
)
)
);
}
catch (Exception e)
{
notificationService.Show(
new Notification(
"CivitAI can't be reached right now",
$"Unknown exception during CivitAI query: {e.GetType().Name}"
)
);
Logger.Error(e, $"CivitAI query unknown exception ({request})");
}
finally
{
ShowMainLoadingSpinner = false;
UpdateResultsText();
}
}
/// <summary>
/// Updates model cards using api response object.
/// </summary>
private void UpdateModelCards(List<CivitModel>? models, bool addCards = false)
{
if (models is null)
{
modelCache.Clear();
return;
}
var startIndex = modelCache.Count;
var modelsToAdd = models.Select((m, i) => new OrderedValue<CivitModel>(startIndex + i, m));
if (addCards)
{
var newModels = modelsToAdd.Where(x => !modelCache.Keys.Contains(x.Value.Id));
modelCache.AddOrUpdate(newModels);
}
else
{
modelCache.EditDiff(modelsToAdd, static (a, b) => a.Order == b.Order && a.Value.Id == b.Value.Id);
}
// Status update
ShowMainLoadingSpinner = false;
IsIndeterminate = false;
HasSearched = true;
}
private string previousSearchQuery = string.Empty;
[RelayCommand]
private async Task SearchModels(bool isInfiniteScroll = false)
{
var timer = Stopwatch.StartNew();
if (SearchQuery != previousSearchQuery || !isInfiniteScroll)
{
// Reset page number
previousSearchQuery = SearchQuery;
NextPageCursor = null;
}
// Build request
var modelRequest = new CivitModelsRequest
{
Limit = TargetPageItemCount + 20, // Fetch a few extra to account for local filtering
Nsfw = "true", // Handled by local view filter
Sort = SortMode,
Period = SelectedPeriod,
};
if (NextPageCursor != null)
{
modelRequest.Cursor = NextPageCursor;
}
if (SelectedModelType != CivitModelType.All)
{
modelRequest.Types = [SelectedModelType];
}
if (SelectedBaseModels.Count > 0 && SelectedBaseModels.Count < AllBaseModels.Count)
{
modelRequest.BaseModels = SelectedBaseModels.ToArray();
}
if (SearchQuery.StartsWith("#"))
{
modelRequest.Tag = SearchQuery[1..];
}
else if (SearchQuery.StartsWith("@"))
{
modelRequest.Username = SearchQuery[1..];
}
else if (SearchQuery.StartsWith("$#"))
{
modelRequest.Period = CivitPeriod.AllTime;
modelRequest.BaseModels = null;
modelRequest.Types = null;
modelRequest.CommaSeparatedModelIds = SearchQuery[2..];
if (modelRequest.Sort is CivitSortMode.Favorites or CivitSortMode.Installed)
{
SortMode = CivitSortMode.HighestRated;
modelRequest.Sort = CivitSortMode.HighestRated;
}
}
else if (SearchQuery.StartsWith("https://civitai.com/models/"))
{
/* extract model ID from URL, could be one of:
https://civitai.com/models/443821?modelVersionId=1957537
https://civitai.com/models/443821/cyberrealistic-pony
https://civitai.com/models/443821
*/
var modelId = SearchQuery
.Replace("https://civitai.com/models/", string.Empty)
.Split(['?', '/'], StringSplitOptions.RemoveEmptyEntries)
.FirstOrDefault();
modelRequest.Period = CivitPeriod.AllTime;
modelRequest.BaseModels = null;
modelRequest.Types = null;
modelRequest.CommaSeparatedModelIds = modelId;
if (modelRequest.Sort is CivitSortMode.Favorites or CivitSortMode.Installed)
{
SortMode = CivitSortMode.HighestRated;
modelRequest.Sort = CivitSortMode.HighestRated;
}
}
else
{
modelRequest.Query = SearchQuery;
}
if (SortMode == CivitSortMode.Installed)
{
var connectedModels = await liteDbContext.LocalModelFiles.FindAsync(m =>
m.ConnectedModelInfo != null
);
connectedModels = connectedModels.Where(x => x.HasCivitMetadata);
modelRequest.CommaSeparatedModelIds = string.Join(
",",
connectedModels
.Select(c => c.ConnectedModelInfo!.ModelId)
.GroupBy(m => m)
.Select(g => g.First())
);
modelRequest.Sort = null;
modelRequest.Period = null;
}
else if (SortMode == CivitSortMode.Favorites)
{
var favoriteModels = settingsManager.Settings.FavoriteModels;
if (!favoriteModels.Any())
{
notificationService.Show(
"No Favorites",
"You have not added any models to your Favorites.",
NotificationType.Error
);
return;
}
modelRequest.CommaSeparatedModelIds = string.Join(",", favoriteModels);
modelRequest.Sort = null;
modelRequest.Period = null;
}
// See if query is cached
CivitModelQueryCacheEntry? cachedQuery = null;
if (UseLocalCache)
{
cachedQuery = await liteDbContext.TryQueryWithClearOnExceptionAsync(
liteDbContext.CivitModelQueryCache,
liteDbContext
.CivitModelQueryCache.IncludeAll()
.FindByIdAsync(ObjectHash.GetMd5Guid(modelRequest))
);
}
// If cached, update model cards
if (cachedQuery is not null)
{
var elapsed = timer.Elapsed;
Logger.Debug(
"Using cached query for {Text} [{RequestHash}] (in {Elapsed:F1} s)",
SearchQuery,
modelRequest.GetHashCode(),
elapsed.TotalSeconds
);
NextPageCursor = cachedQuery.Metadata?.NextCursor;
UpdateModelCards(cachedQuery.Items, isInfiniteScroll);
// 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)
{
CivitModelQuery(modelRequest, isInfiniteScroll).SafeFireAndForget();
Logger.Debug(
"Cached query was more than 2 minutes ago ({Seconds:F0} s), updating cache with remote query",
timeSinceCache.Value.TotalSeconds
);
}
}
else
{
// Not cached, wait for remote query
ShowMainLoadingSpinner = true;
await CivitModelQuery(modelRequest, isInfiniteScroll);
}
UpdateResultsText();
}
[RelayCommand]
private void ClearOrSelectAllBaseModels()
{
if (SelectedBaseModels.Count == AllBaseModels.Count)
AllBaseModels.ForEach(x => x.IsSelected = false);
else
AllBaseModels.ForEach(x => x.IsSelected = true);
}
[RelayCommand]
private void ShowVersionDialog(CivitModel model)
{
var versions = model.ModelVersions;
if (versions is null || versions.Count == 0)
{
notificationService.Show(
new Notification(
"Model has no versions available",
"This model has no versions available for download",
NotificationType.Warning
)
);
return;
}
var newVm = dialogFactory.Get<CivitDetailsPageViewModel>(vm =>
{
var allModelIds = ModelCards.Select(x => x.CivitModel.Id).Distinct().ToList();
var index = ModelCards
.Select((x, i) => (x.CivitModel.Id, Index: i))
.FirstOrDefault(x => x.Id == model.Id)
.Index;
vm.ModelIdList = allModelIds;
vm.CurrentIndex = index;
vm.CivitModel = model;
return vm;
});
navigationService.NavigateTo(newVm, BetterSlideNavigationTransition.PageSlideFromRight);
}
public void ClearSearchQuery()
{
SearchQuery = string.Empty;
}
public async Task LoadNextPageAsync()
{
if (NextPageCursor != null)
{
await SearchModelsCommand.ExecuteAsync(true);
}
}
partial void OnSelectedPeriodChanged(CivitPeriod value)
{
TrySearchAgain().SafeFireAndForget();
settingsManager.Transaction(s =>
s.ModelSearchOptions = new ModelSearchOptions(value, SortMode, SelectedModelType, string.Empty)
);
NextPageCursor = null;
}
partial void OnSortModeChanged(CivitSortMode value)
{
TrySearchAgain().SafeFireAndForget();
settingsManager.Transaction(s =>
s.ModelSearchOptions = new ModelSearchOptions(
SelectedPeriod,
value,
SelectedModelType,
string.Empty
)
);
NextPageCursor = null;
}
partial void OnSelectedModelTypeChanged(CivitModelType value)
{
TrySearchAgain().SafeFireAndForget();
settingsManager.Transaction(s =>
s.ModelSearchOptions = new ModelSearchOptions(SelectedPeriod, SortMode, value, string.Empty)
);
NextPageCursor = null;
}
private async Task TrySearchAgain(bool shouldUpdatePageNumber = true)
{
if (!HasSearched)
return;
modelCache.Clear();
if (shouldUpdatePageNumber)
{
NextPageCursor = null;
}
// execute command instead of calling method directly so that the IsRunning property gets updated
await SearchModelsCommand.ExecuteAsync(false);
}
private void UpdateResultsText()
{
NoResultsFound = ModelCards?.Count <= 0;
NoResultsText = "No results found";
}
public override string Header => Resources.Label_CivitAi;
}