44using System . Linq ;
55using System . Text . RegularExpressions ;
66using System . Threading . Tasks ;
7+ using AsyncAwaitBestPractices ;
78using Avalonia . Controls ;
89using Avalonia . Controls . Notifications ;
910using Avalonia . Threading ;
1718using StabilityMatrix . Avalonia . ViewModels . Base ;
1819using StabilityMatrix . Avalonia . ViewModels . Dialogs ;
1920using StabilityMatrix . Avalonia . Views . Dialogs ;
21+ using StabilityMatrix . Core . Api ;
2022using StabilityMatrix . Core . Attributes ;
23+ using StabilityMatrix . Core . Database ;
2124using StabilityMatrix . Core . Extensions ;
2225using StabilityMatrix . Core . Helper ;
2326using 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 ( ) ;
0 commit comments