Skip to content

Commit c49834f

Browse files
committed
Add model update on version card open
1 parent 810fa20 commit c49834f

File tree

3 files changed

+75
-6
lines changed

3 files changed

+75
-6
lines changed

StabilityMatrix.Avalonia/ViewModels/CheckpointBrowser/CheckpointBrowserCardViewModel.cs

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Linq;
55
using System.Text.RegularExpressions;
66
using System.Threading.Tasks;
7+
using AsyncAwaitBestPractices;
78
using Avalonia.Controls;
89
using Avalonia.Controls.Notifications;
910
using Avalonia.Threading;
@@ -17,7 +18,9 @@
1718
using StabilityMatrix.Avalonia.ViewModels.Base;
1819
using StabilityMatrix.Avalonia.ViewModels.Dialogs;
1920
using StabilityMatrix.Avalonia.Views.Dialogs;
21+
using StabilityMatrix.Core.Api;
2022
using StabilityMatrix.Core.Attributes;
23+
using StabilityMatrix.Core.Database;
2124
using StabilityMatrix.Core.Extensions;
2225
using StabilityMatrix.Core.Helper;
2326
using StabilityMatrix.Core.Models;
@@ -42,6 +45,8 @@ public partial class CheckpointBrowserCardViewModel : ProgressViewModel
4245
private readonly INotificationService notificationService;
4346
private readonly IModelIndexService modelIndexService;
4447
private readonly IModelImportService modelImportService;
48+
private readonly ILiteDbContext liteDbContext;
49+
private readonly CivitCompatApiManager civitApi;
4550

4651
public Action<CheckpointBrowserCardViewModel>? OnDownloadStart { get; set; }
4752

@@ -90,7 +95,9 @@ public CheckpointBrowserCardViewModel(
9095
IServiceManager<ViewModelBase> dialogFactory,
9196
INotificationService notificationService,
9297
IModelIndexService modelIndexService,
93-
IModelImportService modelImportService
98+
IModelImportService modelImportService,
99+
ILiteDbContext liteDbContext,
100+
CivitCompatApiManager civitApi
94101
)
95102
{
96103
this.downloadService = downloadService;
@@ -100,6 +107,8 @@ IModelImportService modelImportService
100107
this.notificationService = notificationService;
101108
this.modelIndexService = modelIndexService;
102109
this.modelImportService = modelImportService;
110+
this.liteDbContext = liteDbContext;
111+
this.civitApi = civitApi;
103112

104113
// Update image when nsfw setting changes
105114
AddDisposable(
@@ -260,6 +269,63 @@ private async Task ShowVersionDialog(CivitModel model)
260269
.ToImmutableArray();
261270
viewModel.SelectedVersionViewModel = viewModel.Versions[0];
262271

272+
// Update with latest version (including files) if we have no files
273+
if (model.ModelVersions?.FirstOrDefault()?.Files is not { Count: > 0 })
274+
{
275+
Task.Run(async () =>
276+
{
277+
Logger.Debug("No files found for model {ModelId}. Updating versions...", model.Id);
278+
279+
var latestModel = await civitApi.GetModelById(model.Id);
280+
var latestVersions = latestModel.ModelVersions ?? [];
281+
282+
// Update our model
283+
civitModel.Description = latestModel.Description;
284+
civitModel = latestModel;
285+
foreach (var version in latestVersions)
286+
{
287+
if (version.Files is not { Count: > 0 })
288+
continue;
289+
290+
var targetVersion = model.ModelVersions?.FirstOrDefault(v => v.Id == version.Id);
291+
if (targetVersion is null)
292+
continue;
293+
294+
targetVersion.Files = version.Files;
295+
targetVersion.Description = version.Description;
296+
targetVersion.DownloadUrl = version.DownloadUrl;
297+
}
298+
299+
// Reinitialize
300+
Logger.Debug("Updating Versions dialog");
301+
Dispatcher.UIThread.Post(() =>
302+
{
303+
var newHtmlDescription =
304+
$"""<html><body class="markdown-body">{model.Description}</body></html>""";
305+
306+
viewModel.Dialog = dialog;
307+
viewModel.Title = latestModel.Name;
308+
309+
viewModel.Description = newHtmlDescription;
310+
viewModel.CivitModel = latestModel;
311+
viewModel.Versions = (latestModel.ModelVersions ?? [])
312+
.Where(v => !settingsManager.Settings.HideEarlyAccessModels || !v.IsEarlyAccess)
313+
.Select(version => new ModelVersionViewModel(modelIndexService, version))
314+
.ToImmutableArray();
315+
viewModel.SelectedVersionViewModel = viewModel.Versions[0];
316+
});
317+
318+
// Save to db
319+
var upsertResult = await liteDbContext.UpsertCivitModelAsync(latestModel);
320+
Logger.Debug(
321+
"Update model {ModelId} with latest version: {Result}",
322+
model.Id,
323+
upsertResult
324+
);
325+
})
326+
.SafeFireAndForget(e => Logger.Error(e, "Failed to update model {ModelId}", model.Id));
327+
}
328+
263329
dialog.Content = new SelectModelVersionDialog { DataContext = viewModel };
264330

265331
var result = await dialog.ShowAsync();

StabilityMatrix.Avalonia/ViewModels/Dialogs/SelectModelVersionViewModel.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,14 @@ INotificationService notificationService
3838
private readonly IDownloadService downloadService = downloadService;
3939

4040
public required ContentDialog Dialog { get; set; }
41-
public required IReadOnlyList<ModelVersionViewModel> Versions { get; set; }
41+
4242
public required string Description { get; set; }
4343
public new required string Title { get; set; }
4444
public required CivitModel CivitModel { get; set; }
4545

46+
[ObservableProperty]
47+
public IReadOnlyList<ModelVersionViewModel> versions = [];
48+
4649
[ObservableProperty]
4750
private Bitmap? previewImage;
4851

StabilityMatrix.Core/Api/CivitCompatApiManager.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public Task<CivitModelsResponse> GetModels(CivitModelsRequest request)
2424
{
2525
if (ShouldUseDiscoveryApi)
2626
{
27-
logger.LogInformation($"Using Discovery API for {nameof(GetModels)}");
27+
logger.LogDebug($"Using Discovery API for {nameof(GetModels)}");
2828
return discoveryApi.GetModels(request, transcodeAnimToImage: true, transcodeVideoToImage: true);
2929
}
3030

@@ -33,11 +33,11 @@ public Task<CivitModelsResponse> GetModels(CivitModelsRequest request)
3333

3434
public Task<CivitModel> GetModelById(int id)
3535
{
36-
if (ShouldUseDiscoveryApi)
36+
/*if (ShouldUseDiscoveryApi)
3737
{
38-
logger.LogInformation($"Using Discovery API for {nameof(GetModelById)}");
38+
logger.LogDebug($"Using Discovery API for {nameof(GetModelById)}");
3939
return discoveryApi.GetModelById(id);
40-
}
40+
}*/
4141
return civitApi.GetModelById(id);
4242
}
4343

0 commit comments

Comments
 (0)