Skip to content

Commit e3bb8d2

Browse files
mpaganimpagani
authored andcommitted
feat: Add UserActivityTracker to monitor user activity
Introduce a new `UserActivityTracker` class in the `SourceGit.Models` namespace to track user activity and determine when to perform auto-fetch operations. Update the `App.axaml.cs` file to initialize the tracker at startup and modify `Repository.cs` to utilize this new functionality, ensuring auto-fetching occurs only during user inactivity.
1 parent 6d298be commit e3bb8d2

File tree

3 files changed

+82
-3
lines changed

3 files changed

+82
-3
lines changed

src/App.axaml.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,7 @@ private void TryLaunchAsNormal(IClassicDesktopStyleApplicationLifetime desktop)
575575

576576
_launcher = new ViewModels.Launcher(startupRepo);
577577
desktop.MainWindow = new Views.Launcher() { DataContext = _launcher };
578+
Models.UserActivityTracker.Instance.Initialize();
578579
desktop.ShutdownMode = ShutdownMode.OnMainWindowClose;
579580

580581
#if !DISABLE_UPDATE_DETECTION

src/Models/UserActivityTracker.cs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using System;
2+
using System.Threading;
3+
4+
namespace SourceGit.Models
5+
{
6+
public class UserActivityTracker
7+
{
8+
private static readonly Lazy<UserActivityTracker> _instance = new(() => new UserActivityTracker());
9+
private bool _isWindowActive = false;
10+
private DateTime _lastActivity = DateTime.MinValue;
11+
private readonly Lock _lockObject = new();
12+
private readonly int _minIdleSecondsBeforeAutoFetch = 15;
13+
14+
private void OnUserActivity(object sender, EventArgs e) => UpdateLastActivity();
15+
16+
private void OnWindowActivated(object sender, EventArgs e)
17+
{
18+
lock (_lockObject)
19+
{
20+
_isWindowActive = true;
21+
_lastActivity = DateTime.Now;
22+
}
23+
}
24+
25+
private void OnWindowDeactivated(object sender, EventArgs e)
26+
{
27+
lock (_lockObject)
28+
_isWindowActive = false;
29+
}
30+
31+
public void Initialize()
32+
{
33+
lock (_lockObject)
34+
{
35+
_lastActivity = DateTime.Now;
36+
_isWindowActive = true;
37+
}
38+
39+
if (App.Current?.ApplicationLifetime is Avalonia.Controls.ApplicationLifetimes.IClassicDesktopStyleApplicationLifetime desktop)
40+
if (desktop.MainWindow != null)
41+
{
42+
desktop.MainWindow.Activated += OnWindowActivated;
43+
desktop.MainWindow.Deactivated += OnWindowDeactivated;
44+
desktop.MainWindow.KeyDown += OnUserActivity;
45+
desktop.MainWindow.PointerPressed += OnUserActivity;
46+
desktop.MainWindow.PointerMoved += OnUserActivity;
47+
desktop.MainWindow.PointerWheelChanged += OnUserActivity;
48+
}
49+
}
50+
51+
public bool ShouldPerformAutoFetch(DateTime lastFetchTime, int intervalMinutes)
52+
{
53+
var now = DateTime.Now;
54+
55+
if (now < lastFetchTime.AddMinutes(intervalMinutes))
56+
return false;
57+
58+
lock (_lockObject)
59+
{
60+
if (!_isWindowActive)
61+
return true;
62+
63+
var timeSinceLastActivity = now - _lastActivity;
64+
65+
if (timeSinceLastActivity.TotalSeconds >= _minIdleSecondsBeforeAutoFetch)
66+
return true;
67+
68+
return false;
69+
}
70+
}
71+
72+
public void UpdateLastActivity()
73+
{
74+
lock (_lockObject)
75+
_lastActivity = DateTime.Now;
76+
}
77+
78+
public static UserActivityTracker Instance => _instance.Value;
79+
}
80+
}

src/ViewModels/Repository.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3074,9 +3074,7 @@ private async void AutoFetchImpl(object sender)
30743074
if (File.Exists(lockFile))
30753075
return;
30763076

3077-
var now = DateTime.Now;
3078-
var desire = _lastFetchTime.AddMinutes(_settings.AutoFetchInterval);
3079-
if (desire > now)
3077+
if (!Models.UserActivityTracker.Instance.ShouldPerformAutoFetch(_lastFetchTime, _settings.AutoFetchInterval))
30803078
return;
30813079

30823080
var remotes = new List<string>();

0 commit comments

Comments
 (0)