22using System . Collections . Generic ;
33using System . IO ;
44using System . Threading . Tasks ;
5-
6- using Avalonia . Collections ;
75using Avalonia . Threading ;
8-
96using CommunityToolkit . Mvvm . ComponentModel ;
107
118namespace SourceGit . ViewModels
@@ -17,15 +14,27 @@ public class FileHistoriesRevisionFile(string path, object content = null, bool
1714 public bool CanOpenWithDefaultEditor { get ; set ; } = canOpenWithDefaultEditor ;
1815 }
1916
17+ public class FileHistoriesSingleRevisionViewMode
18+ {
19+ public bool IsDiff
20+ {
21+ get ;
22+ set ;
23+ } = true ;
24+ }
25+
2026 public class FileHistoriesSingleRevision : ObservableObject
2127 {
2228 public bool IsDiffMode
2329 {
24- get => _isDiffMode ;
30+ get => _viewMode . IsDiff ;
2531 set
2632 {
27- if ( SetProperty ( ref _isDiffMode , value ) )
33+ if ( _viewMode . IsDiff != value )
34+ {
35+ _viewMode . IsDiff = value ;
2836 RefreshViewContent ( ) ;
37+ }
2938 }
3039 }
3140
@@ -35,17 +44,24 @@ public object ViewContent
3544 set => SetProperty ( ref _viewContent , value ) ;
3645 }
3746
38- public FileHistoriesSingleRevision ( string repo , Models . FileVersion revision , bool prevIsDiffMode )
47+ public FileHistoriesSingleRevision ( string repo , Models . FileVersion revision , FileHistoriesSingleRevisionViewMode viewMode )
3948 {
4049 _repo = repo ;
4150 _file = revision . Path ;
4251 _revision = revision ;
43- _isDiffMode = prevIsDiffMode ;
52+ _viewMode = viewMode ;
4453 _viewContent = null ;
4554
4655 RefreshViewContent ( ) ;
4756 }
4857
58+ public void SetRevision ( Models . FileVersion revision )
59+ {
60+ _file = revision . Path ;
61+ _revision = revision ;
62+ RefreshViewContent ( ) ;
63+ }
64+
4965 public async Task < bool > ResetToSelectedRevisionAsync ( )
5066 {
5167 return await new Commands . Checkout ( _repo )
@@ -72,7 +88,7 @@ await Commands.SaveRevisionFile
7288
7389 private void RefreshViewContent ( )
7490 {
75- if ( _isDiffMode )
91+ if ( _viewMode . IsDiff )
7692 {
7793 ViewContent = new DiffContext ( _repo , new ( _revision ) , _viewContent as DiffContext ) ;
7894 return ;
@@ -155,7 +171,7 @@ private async Task<object> GetRevisionFileContentAsync(Models.Object obj)
155171 private string _repo = null ;
156172 private string _file = null ;
157173 private Models . FileVersion _revision = null ;
158- private bool _isDiffMode = false ;
174+ private FileHistoriesSingleRevisionViewMode _viewMode = null ;
159175 private object _viewContent = null ;
160176 }
161177
@@ -226,11 +242,15 @@ public List<Models.FileVersion> Revisions
226242 set => SetProperty ( ref _revisions , value ) ;
227243 }
228244
229- public AvaloniaList < Models . FileVersion > SelectedRevisions
245+ public List < Models . FileVersion > SelectedRevisions
230246 {
231- get ;
232- set ;
233- } = [ ] ;
247+ get => _selectedRevisions ;
248+ set
249+ {
250+ if ( SetProperty ( ref _selectedRevisions , value ) )
251+ RefreshViewContent ( ) ;
252+ }
253+ }
234254
235255 public object ViewContent
236256 {
@@ -257,23 +277,8 @@ public FileHistories(string repo, string file, string commit = null)
257277 {
258278 IsLoading = false ;
259279 Revisions = revisions ;
260- if ( revisions . Count > 0 )
261- SelectedRevisions . Add ( revisions [ 0 ] ) ;
262280 } ) ;
263281 } ) ;
264-
265- SelectedRevisions . CollectionChanged += ( _ , _ ) =>
266- {
267- if ( _viewContent is FileHistoriesSingleRevision singleRevision )
268- _prevIsDiffMode = singleRevision . IsDiffMode ;
269-
270- ViewContent = SelectedRevisions . Count switch
271- {
272- 1 => new FileHistoriesSingleRevision ( _repo , SelectedRevisions [ 0 ] , _prevIsDiffMode ) ,
273- 2 => new FileHistoriesCompareRevisions ( _repo , SelectedRevisions [ 0 ] , SelectedRevisions [ 1 ] ) ,
274- _ => SelectedRevisions . Count ,
275- } ;
276- } ;
277282 }
278283
279284 public void NavigateToCommit ( Models . FileVersion revision )
@@ -303,10 +308,35 @@ public string GetCommitFullMessage(Models.FileVersion revision)
303308 return msg ;
304309 }
305310
311+ private void RefreshViewContent ( )
312+ {
313+ var count = _selectedRevisions ? . Count ?? 0 ;
314+ if ( count == 0 )
315+ {
316+ ViewContent = null ;
317+ }
318+ else if ( count == 1 )
319+ {
320+ if ( _viewContent is FileHistoriesSingleRevision single )
321+ single . SetRevision ( _selectedRevisions [ 0 ] ) ;
322+ else
323+ ViewContent = new FileHistoriesSingleRevision ( _repo , _selectedRevisions [ 0 ] , _viewMode ) ;
324+ }
325+ else if ( count == 2 )
326+ {
327+ ViewContent = new FileHistoriesCompareRevisions ( _repo , _selectedRevisions [ 0 ] , _selectedRevisions [ 1 ] ) ;
328+ }
329+ else
330+ {
331+ ViewContent = _selectedRevisions . Count ;
332+ }
333+ }
334+
306335 private readonly string _repo = null ;
307336 private bool _isLoading = true ;
308- private bool _prevIsDiffMode = true ;
337+ private FileHistoriesSingleRevisionViewMode _viewMode = new ( ) ;
309338 private List < Models . FileVersion > _revisions = null ;
339+ private List < Models . FileVersion > _selectedRevisions = [ ] ;
310340 private Dictionary < string , string > _fullCommitMessages = new ( ) ;
311341 private object _viewContent = null ;
312342 }
0 commit comments