@@ -170,6 +170,7 @@ await Task.Run(async () =>
170170 } ) ;
171171
172172 CurrentSelectedAssetLibrary = AssetLibraries . OrderByDescending ( d => d . IsDefault ) . FirstOrDefault ( ) ;
173+ AllowArchiveLoad = true ;
173174 }
174175
175176 public async Task LoadInstalledArchivesAsync ( )
@@ -187,14 +188,79 @@ await Task.Run(async () =>
187188
188189 await foreach ( var archive in archivesQuery . OrderBy ( d => d . ArchiveName . ToLower ( ) ) . AsAsyncEnumerable ( ) )
189190 {
190- var files = await _dbContext . AssetFiles . Where ( d => d . ArchiveId == archive . Id ) . ToListAsync ( ) ;
191- archive . AssetFiles = files ;
192-
193- var node = InstalledArchivesTree . LoadArchive ( archive ) ;
191+ var node = InstalledArchivesTree . LoadArchiveLazy ( archive ) ;
194192 DisplayedInstalledArchives . Add ( node ) ;
195- InstalledAssetsCount = InstalledArchivesTree . Count ;
193+
194+ InstalledAssetsCount ++ ;
195+ }
196+ } ) ;
197+ }
198+
199+ public async Task LoadArchiveTreeFilesAsync ( TreeNode archiveNode )
200+ {
201+ if ( ! archiveNode . IsLazyLoad || archiveNode . HasLoadedChildren || archiveNode . DbId == null )
202+ return ;
203+
204+ try
205+ {
206+ StatusText = $ "Loading files for { archiveNode . Title } ...";
207+ StatusBarColor = Brushes . DodgerBlue ;
208+
209+ await Task . Run ( async ( ) =>
210+ {
211+ var archive = await _dbContext . Archives
212+ . Include ( a => a . AssetFiles )
213+ . FirstOrDefaultAsync ( a => a . Id == archiveNode . DbId ) ;
214+
215+ if ( archive != null )
216+ {
217+ Dispatcher . UIThread . Post ( ( ) => { InstalledArchiveTree . LoadArchiveFiles ( archiveNode , archive ) ; } ) ;
218+ }
219+ } ) ;
220+
221+ StatusText = "Ready" ;
222+ StatusBarColor = Brushes . Green ;
223+ }
224+ catch ( Exception ex )
225+ {
226+ StatusText = $ "Error loading files: { ex . Message } ";
227+ StatusBarColor = Brushes . Red ;
228+ }
229+ }
230+
231+ public async Task LoadArchiveFilesFromDiskAsync ( List < string > filePaths )
232+ {
233+ StatusBarMax = 100 * filePaths . Count ;
234+ StatusProgress = 0 ;
235+ StatusBarColor = Brushes . DodgerBlue ;
236+ AllowArchiveLoad = false ;
237+
238+ var index = 0 ;
239+
240+ await Task . Run ( async ( ) =>
241+ {
242+ foreach ( var path in filePaths )
243+ {
244+ index ++ ;
245+ var existingStatusProgress = index * 100 ;
246+ IProgress < string > messageProgress = new Progress < string > ( s => StatusText = s ) ;
247+ var percentageProgress = new Progress < int > ( p => StatusProgress = existingStatusProgress + p ) ;
248+
249+ using var loader = new DazArchiveLoader ( path ) ;
250+ var result = await loader . LoadArchiveAsync ( messageProgress , percentageProgress ) ;
251+
252+ Dispatcher . UIThread . Post ( ( ) =>
253+ {
254+ LoadedArchives . AddRange ( result . Where ( d => d . ContainedFiles . Count > 0 ) ) ;
255+ UpdateInstallButton ( ) ;
256+ } ) ;
257+ messageProgress . Report ( $ "Finished loading { Path . GetFileName ( path ) } ") ;
196258 }
197259 } ) ;
260+
261+ StatusBarMax = 100 ;
262+ StatusBarColor = Brushes . Green ;
263+ AllowArchiveLoad = true ;
198264 }
199265
200266 private void RemoveLoadedArchive ( LoadedArchive loadedArchiveOld )
@@ -256,11 +322,8 @@ await Task.Run(async () =>
256322 }
257323 } ) ;
258324
259- await Task . Run ( async ( ) =>
260- {
261- await LoadInstalledArchivesAsync ( ) ;
262- } ) ;
263-
325+ await Task . Run ( async ( ) => { await LoadInstalledArchivesAsync ( ) ; } ) ;
326+
264327 messageProgress . Report ( $ "Installed { archivesToInstall . Count } archives") ;
265328 percentageProgress . Report ( 100 ) ;
266329 StatusBarColor = Brushes . Green ;
@@ -331,46 +394,8 @@ await Task.Run(async () =>
331394 StatusBarColor = Brushes . Green ;
332395 } ) ;
333396
334- await Task . Run ( async ( ) =>
335- {
336- await LoadInstalledArchivesAsync ( ) ;
337- } ) ;
338-
339- AllowArchiveLoad = true ;
340- }
341-
342- public async Task LoadArchiveFilesAsync ( List < string > filePaths )
343- {
344- StatusBarMax = 100 * filePaths . Count ;
345- StatusProgress = 0 ;
346- StatusBarColor = Brushes . DodgerBlue ;
347- AllowArchiveLoad = false ;
348-
349- var index = 0 ;
397+ await Task . Run ( async ( ) => { await LoadInstalledArchivesAsync ( ) ; } ) ;
350398
351- await Task . Run ( async ( ) =>
352- {
353- foreach ( var path in filePaths )
354- {
355- index ++ ;
356- var existingStatusProgress = index * 100 ;
357- IProgress < string > messageProgress = new Progress < string > ( s => StatusText = s ) ;
358- var percentageProgress = new Progress < int > ( p => StatusProgress = existingStatusProgress + p ) ;
359-
360- using var loader = new DazArchiveLoader ( path ) ;
361- var result = await loader . LoadArchiveAsync ( messageProgress , percentageProgress ) ;
362-
363- Dispatcher . UIThread . Post ( ( ) =>
364- {
365- LoadedArchives . AddRange ( result . Where ( d => d . ContainedFiles . Count > 0 ) ) ;
366- UpdateInstallButton ( ) ;
367- } ) ;
368- messageProgress . Report ( $ "Finished loading { Path . GetFileName ( path ) } ") ;
369- }
370- } ) ;
371-
372- StatusBarMax = 100 ;
373- StatusBarColor = Brushes . Green ;
374399 AllowArchiveLoad = true ;
375400 }
376401
@@ -380,12 +405,13 @@ private void FilterInstalledAssetsTree(string? searchTerm)
380405 if ( string . IsNullOrWhiteSpace ( searchTerm ) )
381406 {
382407 DisplayedInstalledArchives . AddRange ( InstalledArchivesTree ) ;
383-
384408 return ;
385409 }
386410
387411 var filtered = InstalledArchivesTree
388- . Select ( node => FilterTree ( node , searchTerm ) )
412+ . Where ( node => node . Title . Contains ( searchTerm , StringComparison . OrdinalIgnoreCase ) )
413+ . Select ( node =>
414+ node is { IsLazyLoad : true , HasLoadedChildren : false } ? node : FilterTree ( node , searchTerm ) )
389415 . Where ( node => node is not null )
390416 . Select ( node => node ! ) ;
391417
@@ -394,14 +420,12 @@ private void FilterInstalledAssetsTree(string? searchTerm)
394420
395421 private static TreeNode ? FilterTree ( TreeNode node , string searchTerm )
396422 {
397- // Filter children recursively
398423 var filteredChildren = node . Children
399424 . Select ( child => FilterTree ( child , searchTerm ) )
400425 . Where ( child => child is not null )
401426 . Select ( child => child ! )
402427 . ToList ( ) ;
403428
404- // Check if this node matches or has any matching children
405429 var isMatch = node . Title . Contains ( searchTerm , StringComparison . OrdinalIgnoreCase ) ;
406430
407431 if ( isMatch || filteredChildren . Count > 0 )
0 commit comments