Skip to content

Commit 75b7724

Browse files
committed
refactor: implement IDisposable instead of calling custom Cleanup
Signed-off-by: leo <[email protected]>
1 parent 550493b commit 75b7724

File tree

8 files changed

+42
-34
lines changed

8 files changed

+42
-34
lines changed

src/Models/Count.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
3+
namespace SourceGit.Models
4+
{
5+
public class Count : IDisposable
6+
{
7+
public int Value { get; set; } = 0;
8+
9+
public Count(int value)
10+
{
11+
Value = value;
12+
}
13+
14+
public void Dispose()
15+
{
16+
// Ignore
17+
}
18+
}
19+
}

src/ViewModels/CommitDetail.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
namespace SourceGit.ViewModels
1717
{
18-
public partial class CommitDetail : ObservableObject
18+
public partial class CommitDetail : ObservableObject, IDisposable
1919
{
2020
public int ActivePageIndex
2121
{
@@ -137,7 +137,7 @@ public CommitDetail(Repository repo)
137137
WebLinks = Models.CommitLink.Get(repo.Remotes);
138138
}
139139

140-
public void Cleanup()
140+
public void Dispose()
141141
{
142142
_repo = null;
143143
_commit = null;

src/ViewModels/Histories.cs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace SourceGit.ViewModels
1414
{
15-
public class Histories : ObservableObject
15+
public class Histories : ObservableObject, IDisposable
1616
{
1717
public Repository Repo
1818
{
@@ -57,7 +57,7 @@ public long NavigationId
5757
private set => SetProperty(ref _navigationId, value);
5858
}
5959

60-
public object DetailContext
60+
public IDisposable DetailContext
6161
{
6262
get => _detailContext;
6363
set => SetProperty(ref _detailContext, value);
@@ -98,23 +98,13 @@ public Histories(Repository repo)
9898
_repo = repo;
9999
}
100100

101-
public void Cleanup()
101+
public void Dispose()
102102
{
103-
Commits = new List<Models.Commit>();
104-
103+
Commits = [];
105104
_repo = null;
106105
_graph = null;
107106
_autoSelectedCommit = null;
108-
109-
if (_detailContext is CommitDetail cd)
110-
{
111-
cd.Cleanup();
112-
}
113-
else if (_detailContext is RevisionCompare rc)
114-
{
115-
rc.Cleanup();
116-
}
117-
107+
_detailContext?.Dispose();
118108
_detailContext = null;
119109
}
120110

@@ -220,7 +210,7 @@ public void Select(IList commits)
220210
else
221211
{
222212
_repo.SelectedSearchedCommit = null;
223-
DetailContext = commits.Count;
213+
DetailContext = new Models.Count(commits.Count);
224214
}
225215
}
226216

@@ -1256,7 +1246,7 @@ private string GetPatchFileName(string dir, Models.Commit commit, int index = 0)
12561246
private Models.CommitGraph _graph = null;
12571247
private Models.Commit _autoSelectedCommit = null;
12581248
private long _navigationId = 0;
1259-
private object _detailContext = null;
1249+
private IDisposable _detailContext = null;
12601250

12611251
private Models.Bisect _bisect = null;
12621252

src/ViewModels/Repository.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -546,9 +546,9 @@ public void Close()
546546
_historiesFilterMode = Models.FilterMode.None;
547547

548548
_watcher?.Dispose();
549-
_histories.Cleanup();
550-
_workingCopy.Cleanup();
551-
_stashesPage.Cleanup();
549+
_histories.Dispose();
550+
_workingCopy.Dispose();
551+
_stashesPage.Dispose();
552552

553553
_watcher = null;
554554
_histories = null;

src/ViewModels/RevisionCompare.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace SourceGit.ViewModels
1212
{
13-
public class RevisionCompare : ObservableObject
13+
public class RevisionCompare : ObservableObject, IDisposable
1414
{
1515
public object StartPoint
1616
{
@@ -83,7 +83,7 @@ public RevisionCompare(string repo, Models.Commit startPoint, Models.Commit endP
8383
Task.Run(Refresh);
8484
}
8585

86-
public void Cleanup()
86+
public void Dispose()
8787
{
8888
_repo = null;
8989
_startPoint = null;

src/ViewModels/StashesPage.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace SourceGit.ViewModels
1313
{
14-
public class StashesPage : ObservableObject
14+
public class StashesPage : ObservableObject, IDisposable
1515
{
1616
public List<Models.Stash> Stashes
1717
{
@@ -125,14 +125,13 @@ public StashesPage(Repository repo)
125125
_repo = repo;
126126
}
127127

128-
public void Cleanup()
128+
public void Dispose()
129129
{
130+
_stashes?.Clear();
131+
_changes?.Clear();
132+
130133
_repo = null;
131-
if (_stashes != null)
132-
_stashes.Clear();
133134
_selectedStash = null;
134-
if (_changes != null)
135-
_changes.Clear();
136135
_selectedChange = null;
137136
_diffContext = null;
138137
}

src/ViewModels/WorkingCopy.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace SourceGit.ViewModels
1313
{
14-
public class WorkingCopy : ObservableObject
14+
public class WorkingCopy : ObservableObject, IDisposable
1515
{
1616
public bool IncludeUntracked
1717
{
@@ -210,7 +210,7 @@ public WorkingCopy(Repository repo)
210210
_repo = repo;
211211
}
212212

213-
public void Cleanup()
213+
public void Dispose()
214214
{
215215
_repo = null;
216216
_inProgressContext = null;

src/Views/Histories.axaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@
257257
<v:RevisionCompare/>
258258
</DataTemplate>
259259

260-
<DataTemplate DataType="x:Int32">
260+
<DataTemplate DataType="m:Count">
261261
<StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center">
262262
<Path Width="128" Height="128"
263263
Data="{StaticResource Icons.Detail}"
@@ -268,7 +268,7 @@
268268
Margin="0,16"
269269
FontSize="24" FontWeight="Bold"
270270
Foreground="{DynamicResource Brush.FG2}"
271-
Text="{Binding Converter={x:Static c:StringConverters.FormatByResourceKey}, ConverterParameter='Histories.Selected'}"/>
271+
Text="{Binding Value, Converter={x:Static c:StringConverters.FormatByResourceKey}, ConverterParameter='Histories.Selected'}"/>
272272
</StackPanel>
273273
</DataTemplate>
274274
</ContentControl.DataTemplates>

0 commit comments

Comments
 (0)