Skip to content

Commit 51d401b

Browse files
authored
Merge pull request #123 from gadfly3173/feat/auto-fetch-interval
feat: Add config of auto fetch interval
2 parents 62838e5 + 7520a2a commit 51d401b

File tree

5 files changed

+66
-7
lines changed

5 files changed

+66
-7
lines changed

src/Commands/Fetch.cs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,30 @@ protected override void OnReadline(string line)
6262

6363
public class AutoFetch
6464
{
65-
private const double INTERVAL = 10 * 60;
66-
6765
public static bool IsEnabled
6866
{
6967
get;
7068
set;
7169
} = false;
7270

71+
public static int Interval
72+
{
73+
get => _interval;
74+
set
75+
{
76+
if (value < 1)
77+
return;
78+
_interval = value;
79+
lock (_lock)
80+
{
81+
foreach (var job in _jobs)
82+
{
83+
job.Value.NextRunTimepoint = DateTime.Now.AddMinutes(Convert.ToDouble(_interval));
84+
}
85+
}
86+
}
87+
}
88+
7389
class Job
7490
{
7591
public Fetch Cmd = null;
@@ -104,7 +120,7 @@ static AutoFetch()
104120
foreach (var job in uptodate)
105121
{
106122
job.Cmd.Exec();
107-
job.NextRunTimepoint = DateTime.Now.AddSeconds(INTERVAL);
123+
job.NextRunTimepoint = DateTime.Now.AddMinutes(Convert.ToDouble(Interval));
108124
}
109125

110126
Thread.Sleep(2000);
@@ -117,7 +133,7 @@ public static void AddRepository(string repo)
117133
var job = new Job
118134
{
119135
Cmd = new Fetch(repo, "--all", true, null) { RaiseError = false },
120-
NextRunTimepoint = DateTime.Now.AddSeconds(INTERVAL),
136+
NextRunTimepoint = DateTime.Now.AddMinutes(Convert.ToDouble(Interval)),
121137
};
122138

123139
lock (_lock)
@@ -147,12 +163,13 @@ public static void MarkFetched(string repo)
147163
{
148164
if (_jobs.TryGetValue(repo, out var value))
149165
{
150-
value.NextRunTimepoint = DateTime.Now.AddSeconds(INTERVAL);
166+
value.NextRunTimepoint = DateTime.Now.AddMinutes(Convert.ToDouble(Interval));
151167
}
152168
}
153169
}
154170

155171
private static readonly Dictionary<string, Job> _jobs = new Dictionary<string, Job>();
156172
private static readonly object _lock = new object();
173+
private static int _interval = 10;
157174
}
158175
}

src/Resources/Locales/en_US.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@
274274
<x:String x:Key="Text.Preference.General.UseFixedTabWidth" xml:space="preserve">Use fixed tab width in titlebar</x:String>
275275
<x:String x:Key="Text.Preference.Git" xml:space="preserve">GIT</x:String>
276276
<x:String x:Key="Text.Preference.Git.AutoFetch" xml:space="preserve">Fetch remotes automatically</x:String>
277+
<x:String x:Key="Text.Preference.Git.AutoFetchInterval" xml:space="preserve">Auto Fetch Interval</x:String>
277278
<x:String x:Key="Text.Preference.Git.CRLF" xml:space="preserve">Enable Auto CRLF</x:String>
278279
<x:String x:Key="Text.Preference.Git.DefaultCloneDir" xml:space="preserve">Default Clone Dir</x:String>
279280
<x:String x:Key="Text.Preference.Git.Email" xml:space="preserve">User Email</x:String>

src/Resources/Locales/zh_CN.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@
274274
<x:String x:Key="Text.Preference.General.UseFixedTabWidth" xml:space="preserve">使用固定宽度的标题栏标签</x:String>
275275
<x:String x:Key="Text.Preference.Git" xml:space="preserve">GIT配置</x:String>
276276
<x:String x:Key="Text.Preference.Git.AutoFetch" xml:space="preserve">启用定时自动拉取远程更新</x:String>
277+
<x:String x:Key="Text.Preference.Git.AutoFetchInterval" xml:space="preserve">自动拉取间隔</x:String>
277278
<x:String x:Key="Text.Preference.Git.CRLF" xml:space="preserve">自动换行转换</x:String>
278279
<x:String x:Key="Text.Preference.Git.DefaultCloneDir" xml:space="preserve">默认克隆路径</x:String>
279280
<x:String x:Key="Text.Preference.Git.Email" xml:space="preserve">邮箱</x:String>

src/ViewModels/Preference.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,23 @@ public bool GitAutoFetch
232232
}
233233
}
234234

235+
public int? GitAutoFetchInterval
236+
{
237+
get => Commands.AutoFetch.Interval;
238+
set
239+
{
240+
if (value is null or < 1)
241+
{
242+
return;
243+
}
244+
if (Commands.AutoFetch.Interval != value)
245+
{
246+
Commands.AutoFetch.Interval = (int)value;
247+
OnPropertyChanged(nameof(GitAutoFetchInterval));
248+
}
249+
}
250+
}
251+
235252
public int ExternalMergeToolType
236253
{
237254
get => _externalMergeToolType;
@@ -406,7 +423,8 @@ public static void SortByRenamedNode(RepositoryNode node)
406423
});
407424

408425
container.Clear();
409-
foreach (var one in list) container.Add(one);
426+
foreach (var one in list)
427+
container.Add(one);
410428
}
411429

412430
public static Repository FindRepository(string path)

src/Views/Preference.axaml

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@
231231
<TextBlock Classes="tab_header" Text="{DynamicResource Text.Preference.Git}"/>
232232
</TabItem.Header>
233233

234-
<Grid Margin="8" RowDefinitions="32,32,Auto,32,32,32,32,32" ColumnDefinitions="Auto,*">
234+
<Grid Margin="8" RowDefinitions="32,32,Auto,32,32,32,32,32,32" ColumnDefinitions="Auto,*">
235235
<TextBlock Grid.Row="0" Grid.Column="0"
236236
Text="{DynamicResource Text.Preference.Git.Path}"
237237
HorizontalAlignment="Right"
@@ -351,6 +351,28 @@
351351
<CheckBox Grid.Row="7" Grid.Column="1"
352352
Content="{DynamicResource Text.Preference.Git.AutoFetch}"
353353
IsChecked="{Binding GitAutoFetch, Mode=TwoWay}"/>
354+
355+
<TextBlock Grid.Row="8" Grid.Column="0"
356+
IsVisible="{Binding GitAutoFetch}"
357+
Text="{DynamicResource Text.Preference.Git.AutoFetchInterval}"
358+
HorizontalAlignment="Right"
359+
Margin="0,0,16,0"/>
360+
<Grid Grid.Row="8" Grid.Column="1" ColumnDefinitions="*,Auto" IsVisible="{Binding GitAutoFetch}">
361+
<NumericUpDown Grid.Column="0"
362+
Minimum="1" Maximum="60" Increment="1"
363+
Height="28"
364+
Padding="4"
365+
BorderThickness="1" BorderBrush="{DynamicResource Brush.Border1}"
366+
CornerRadius="3"
367+
ParsingNumberStyle="Integer"
368+
FormatString="0"
369+
Value="{Binding GitAutoFetchInterval, Mode=TwoWay, FallbackValue=10}">
370+
</NumericUpDown>
371+
<TextBlock Grid.Column="1"
372+
VerticalAlignment="Center"
373+
Margin="5,0,0,0"
374+
Text="min (1-60)" />
375+
</Grid>
354376
</Grid>
355377
</TabItem>
356378

0 commit comments

Comments
 (0)