Skip to content

Commit f4e490c

Browse files
committed
refactor: Improve commit selection logic and handling
- Refactor commit selection logic to track last selected commit separately and improve multi-selection handling. - Refactor commit selection logic and remove commented auto-navigation code. - Pass the selected commit item to the histories view model when the selection changes.
1 parent 37439d2 commit f4e490c

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

src/ViewModels/Histories.cs

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ public List<Models.Commit> Commits
2424
get => _commits;
2525
set
2626
{
27-
var lastSelected = AutoSelectedCommit;
27+
// LastSelectedCommit = AutoSelectedCommit;
2828
if (SetProperty(ref _commits, value))
2929
{
30-
if (value.Count > 0 && lastSelected != null)
31-
AutoSelectedCommit = value.Find(x => x.SHA == lastSelected.SHA);
30+
// if (value.Count > 0 && lastSelected != null)
31+
// AutoSelectedCommit = value.Find(x => x.SHA == lastSelected.SHA);
3232
}
3333
}
3434
}
@@ -45,6 +45,12 @@ public Models.Commit AutoSelectedCommit
4545
set => SetProperty(ref _autoSelectedCommit, value);
4646
}
4747

48+
public Models.Commit LastSelectedCommit
49+
{
50+
get => _lastSelectedCommit;
51+
set => SetProperty(ref _lastSelectedCommit, value);
52+
}
53+
4854
public List<Models.Commit> LastSelectedCommits
4955
{
5056
get => _lastSelectedCommits;
@@ -104,6 +110,8 @@ public void Dispose()
104110
_repo = null;
105111
_graph = null;
106112
_autoSelectedCommit = null;
113+
_lastSelectedCommits = null;
114+
_lastSelectedCommit = null;
107115
_detailContext?.Dispose();
108116
_detailContext = null;
109117
}
@@ -158,19 +166,32 @@ public void NavigateTo(string commitSHA)
158166
});
159167
}
160168

161-
public void Select(IList commits)
169+
/// <summary>
170+
///
171+
/// notes: if multi selects, `AutoSelectedCommit` which `Binding Mode=OneWay` will not be updated,
172+
/// so that we could not get the lastSelectedCommit automatically
173+
///
174+
/// </summary>
175+
/// <param name="commits"></param>
176+
public void Select(IList commits, object selectedCommit)
162177
{
163178
if (commits.Count == 0)
164179
{
165180
_repo.SelectedSearchedCommit = null;
166181
DetailContext = null;
182+
return;
167183
}
168184
else if (commits.Count == 1)
169185
{
170186
var commit = (commits[0] as Models.Commit)!;
171187
if (_repo.SelectedSearchedCommit == null || _repo.SelectedSearchedCommit.SHA != commit.SHA)
172188
_repo.SelectedSearchedCommit = _repo.SearchedCommits.Find(x => x.SHA == commit.SHA);
173189

190+
var a = GetTopHeadsInCurHistory(commit, out var pathindex, 16);
191+
// var ii = GetRelatedIndexsInCurHistory(_autoSelectedCommit);
192+
193+
a.Append(commit);
194+
MarkLastSelectedCommitsAsSelected();
174195
AutoSelectedCommit = commit;
175196
NavigationId = _navigationId + 1;
176197

@@ -199,12 +220,24 @@ public void Select(IList commits)
199220
DetailContext = new Models.Count(commits.Count);
200221
}
201222

223+
LastSelectedCommit = selectedCommit as Models.Commit;
202224
_repo.SelectedCommits = commits.Cast<Models.Commit>().ToList();
203225
}
204226

205-
public void MarkCommitsAsSelected(IList<Models.Commit> commits)
227+
public void MarkLastSelectedCommitsAsSelected(params IList<Models.Commit> commits)
206228
{
207-
LastSelectedCommits = _commits.Where(x => commits.Any(y => y.SHA == x.SHA)).ToList();
229+
var availableCommits = Commits.Where(x => commits.Any(y => y.SHA == x.SHA)).ToList();
230+
231+
// if LastSelectedCommits is not empty, find the AutoSelectedCommit or get last one
232+
if (availableCommits.Count > 0 && LastSelectedCommit != null)
233+
{
234+
// find in oldAvailable or get last one
235+
AutoSelectedCommit = availableCommits.Find(x => x.SHA == LastSelectedCommit.SHA)
236+
?? availableCommits.FirstOrDefault();
237+
}
238+
// set selectItem above(1item), now select others too
239+
LastSelectedCommits = availableCommits;
240+
NavigationId = _navigationId + 1;
208241
}
209242

210243
public async Task<bool> CheckoutBranchByDecoratorAsync(Models.Decorator decorator)
@@ -417,6 +450,7 @@ private void NavigateTo(Models.Commit commit)
417450
private bool _isLoading = true;
418451
private List<Models.Commit> _commits = new List<Models.Commit>();
419452
private List<Models.Commit> _lastSelectedCommits = [];
453+
private Models.Commit _lastSelectedCommit = null;
420454
private Models.CommitGraph _graph = null;
421455
private Models.Commit _autoSelectedCommit = null;
422456
private Models.Bisect _bisect = null;

src/ViewModels/Repository.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1379,10 +1379,12 @@ public void RefreshCommits()
13791379

13801380
BisectState = _histories.UpdateBisectInfo();
13811381

1382-
_histories.MarkCommitsAsSelected(oldSelectedCommits);
1382+
_histories.MarkLastSelectedCommitsAsSelected(oldSelectedCommits);
13831383

13841384
if (!string.IsNullOrEmpty(_navigateToCommitDelayed))
13851385
NavigateToCommit(_navigateToCommitDelayed);
1386+
// else if (_histories.AutoSelectedCommit != null)
1387+
// NavigateToCommit(_histories.AutoSelectedCommit.SHA);
13861388
}
13871389

13881390
_navigateToCommitDelayed = string.Empty;

src/Views/Histories.axaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ private void OnScrollToTopPointerPressed(object sender, PointerPressedEventArgs
230230
private void OnCommitListSelectionChanged(object _, SelectionChangedEventArgs e)
231231
{
232232
if (DataContext is ViewModels.Histories histories)
233-
histories.Select(CommitListContainer.SelectedItems);
233+
histories.Select(CommitListContainer.SelectedItems, CommitListContainer.SelectedItem);
234234

235235
e.Handled = true;
236236
}

0 commit comments

Comments
 (0)