@@ -731,43 +731,37 @@ private async Task DeleteModelVersion(CivitModelVersion modelVersion)
731731 }
732732
733733 [ RelayCommand ]
734- public async Task NextModel ( )
735- {
736- if ( ! CanGoNext )
737- return ;
738-
739- try
740- {
741- var modelId = ModelIdList [ ++ CurrentIndex ] ;
742- CivitModel = await civitApi . GetModelById ( modelId ) ;
743- ReloadCachesForNewModel ( ) ;
744- }
745- catch ( Exception e )
746- {
747- logger . LogError ( e , "Failed to load CivitModel {Id}" , CivitModel . Id ) ;
748- notificationService . Show (
749- Resources . Label_UnexpectedErrorOccurred ,
750- e . Message ,
751- NotificationType . Error
752- ) ;
753- }
754- }
734+ public Task NextModel ( ) => CanGoNext ? NavigateToModelByIndexOffset ( 1 ) : Task . CompletedTask ;
755735
756736 [ RelayCommand ]
757- public async Task PreviousModel ( )
737+ public Task PreviousModel ( ) => CanGoPrevious ? NavigateToModelByIndexOffset ( - 1 ) : Task . CompletedTask ;
738+
739+ private async Task NavigateToModelByIndexOffset ( int offset )
758740 {
759- if ( ! CanGoPrevious )
760- return ;
741+ var newIndex = CurrentIndex + offset ;
742+ var modelId = ModelIdList [ newIndex ] ;
761743
762744 try
763745 {
764- var modelId = ModelIdList [ -- CurrentIndex ] ;
765- CivitModel = await civitApi . GetModelById ( modelId ) ;
766- ReloadCachesForNewModel ( ) ;
746+ var newModel = await civitApi . GetModelById ( modelId ) ;
747+ CivitModel = newModel ;
748+ CurrentIndex = newIndex ;
749+
750+ // reload caches for new model
751+ modelVersionCache . EditDiff ( CivitModel . ModelVersions ?? [ ] , ( a , b ) => a . Id == b . Id ) ;
752+ SelectedVersion = ModelVersions . FirstOrDefault ( ) ;
753+
754+ imageCache . EditDiff ( SelectedVersion ? . ModelVersion . Images ?? [ ] , ( a , b ) => a . Url == b . Url ) ;
755+ civitFileCache . EditDiff ( SelectedVersion ? . ModelVersion . Files ?? [ ] , ( a , b ) => a . Id == b . Id ) ;
756+
757+ Description = $ """ <html><body class="markdown-body">{ CivitModel . Description } </body></html>""" ;
758+ ModelVersionDescription = string . IsNullOrWhiteSpace ( SelectedVersion ? . ModelVersion . Description )
759+ ? string . Empty
760+ : $ """ <html><body class="markdown-body">{ SelectedVersion . ModelVersion . Description } </body></html>""" ;
767761 }
768762 catch ( Exception e )
769763 {
770- logger . LogError ( e , "Failed to load CivitModel {Id}" , CivitModel . Id ) ;
764+ logger . LogError ( e , "Failed to load CivitModel {Id}" , modelId ) ;
771765 notificationService . Show (
772766 Resources . Label_UnexpectedErrorOccurred ,
773767 e . Message ,
@@ -776,20 +770,6 @@ public async Task PreviousModel()
776770 }
777771 }
778772
779- private void ReloadCachesForNewModel ( )
780- {
781- modelVersionCache . EditDiff ( CivitModel . ModelVersions ?? [ ] , ( a , b ) => a . Id == b . Id ) ;
782- SelectedVersion = ModelVersions . FirstOrDefault ( ) ;
783-
784- imageCache . EditDiff ( SelectedVersion ? . ModelVersion . Images ?? [ ] , ( a , b ) => a . Url == b . Url ) ;
785- civitFileCache . EditDiff ( SelectedVersion ? . ModelVersion . Files ?? [ ] , ( a , b ) => a . Id == b . Id ) ;
786-
787- Description = $ """ <html><body class="markdown-body">{ CivitModel . Description } </body></html>""" ;
788- ModelVersionDescription = string . IsNullOrWhiteSpace ( SelectedVersion ? . ModelVersion . Description )
789- ? string . Empty
790- : $ """ <html><body class="markdown-body">{ SelectedVersion . ModelVersion . Description } </body></html>""" ;
791- }
792-
793773 private void VmOnNavigateToModelRequested ( object ? sender , int modelId )
794774 {
795775 if ( sender is not ImageViewerViewModel vm )
0 commit comments