Skip to content

Commit d3e505a

Browse files
committed
Track last auto reload
* Limit API usage by tracking the date and time of the last full reload and using the difference between that, the user's chosen interval, and now for the first auto reload timer interval, if necessary #73.
1 parent 3bdedac commit d3e505a

File tree

4 files changed

+54
-3
lines changed

4 files changed

+54
-3
lines changed

SAM/MainWindow.xaml.cs

+20-3
Original file line numberDiff line numberDiff line change
@@ -391,9 +391,21 @@ private void LoadSettings()
391391

392392
if (settings.User.AutoReloadEnabled)
393393
{
394+
int interval = settings.User.AutoReloadInterval;
395+
396+
if (settings.User.LastAutoReload.HasValue == true)
397+
{
398+
double minutesSince = (DateTime.Now - settings.User.LastAutoReload.Value).TotalMinutes;
399+
400+
if (minutesSince < interval)
401+
{
402+
interval -= Convert.ToInt32(minutesSince);
403+
}
404+
}
405+
394406
autoReloadApiTimer = new System.Timers.Timer();
395407
autoReloadApiTimer.Elapsed += AutoReloadApiTimer_Elapsed;
396-
autoReloadApiTimer.Interval = 60000 * settings.User.AutoReloadInterval;
408+
autoReloadApiTimer.Interval = 60000 * interval;
397409
autoReloadApiTimer.Start();
398410
}
399411
else
@@ -433,9 +445,11 @@ private void AutoReloadApiTimer_Elapsed(object sender, System.Timers.ElapsedEven
433445
{
434446
ReloadAccountsAsync();
435447
});
448+
449+
autoReloadApiTimer.Interval = settings.User.AutoReloadInterval;
436450
}
437451

438-
public async void RefreshWindow()
452+
public void RefreshWindow()
439453
{
440454
decryptedAccounts = new List<Account>();
441455

@@ -492,7 +506,7 @@ public async void RefreshWindow()
492506

493507
AccountsDataGrid.ItemsSource = encryptedAccounts;
494508

495-
if (firstLoad == true && settings.User.AutoReloadEnabled == true)
509+
if (firstLoad == true && settings.User.AutoReloadEnabled == true && Utils.ShouldAutoReload(settings.User.LastAutoReload, settings.User.AutoReloadInterval))
496510
{
497511
firstLoad = false;
498512
ReloadAccountsAsync();
@@ -610,6 +624,9 @@ public async Task ReloadAccountsAsync()
610624
}
611625
}
612626

627+
settings.File.Write(SAMSettings.LAST_AUTO_RELOAD, DateTime.Now.ToString(), SAMSettings.SECTION_STEAM);
628+
settings.User.LastAutoReload = DateTime.Now;
629+
613630
SerializeAccounts();
614631

615632
Title = "SAM";

SAM/SAMSettings.cs

+2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class SAMSettings
5151
public const string STEAM_API_KEY = "ApiKey";
5252
public const string AUTO_RELOAD_ENABLED = "AutoReloadEnabled";
5353
public const string AUTO_RELOAD_INTERVAL = "AutoReloadInterval";
54+
public const string LAST_AUTO_RELOAD = "LastAutoReload";
5455

5556
public const string CAFE_APP_LAUNCH_PARAMETER = "cafeapplaunch";
5657
public const string CLEAR_BETA_PARAMETER = "clearbeta";
@@ -118,6 +119,7 @@ class SAMSettings
118119
{ STEAM_API_KEY, SECTION_STEAM },
119120
{ AUTO_RELOAD_ENABLED, SECTION_STEAM},
120121
{ AUTO_RELOAD_INTERVAL, SECTION_STEAM },
122+
{ LAST_AUTO_RELOAD, SECTION_STEAM },
121123

122124
{ CAFE_APP_LAUNCH_PARAMETER, SECTION_PARAMETERS },
123125
{ CLEAR_BETA_PARAMETER, SECTION_PARAMETERS },

SAM/UserSettings.cs

+15
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,20 @@ class UserSettings
5252
public string ApiKey { get { return (string)KeyValuePairs[SAMSettings.STEAM_API_KEY]; } set { KeyValuePairs[SAMSettings.STEAM_API_KEY] = value; } }
5353
public bool AutoReloadEnabled { get { return (bool)KeyValuePairs[SAMSettings.AUTO_RELOAD_ENABLED]; } set { KeyValuePairs[SAMSettings.AUTO_RELOAD_ENABLED] = value; } }
5454
public int AutoReloadInterval { get { return (int)KeyValuePairs[SAMSettings.AUTO_RELOAD_INTERVAL]; } set { KeyValuePairs[SAMSettings.AUTO_RELOAD_INTERVAL] = value; } }
55+
public DateTime? LastAutoReload {
56+
57+
get {
58+
try
59+
{
60+
return Convert.ToDateTime(KeyValuePairs[SAMSettings.LAST_AUTO_RELOAD]);
61+
}
62+
catch
63+
{
64+
return null;
65+
}
66+
}
67+
set { KeyValuePairs[SAMSettings.LAST_AUTO_RELOAD] = value; }
68+
}
5569

5670
#endregion
5771

@@ -119,6 +133,7 @@ class UserSettings
119133
{ SAMSettings.STEAM_API_KEY, string.Empty },
120134
{ SAMSettings.AUTO_RELOAD_ENABLED, false },
121135
{ SAMSettings.AUTO_RELOAD_INTERVAL, 30 },
136+
{ SAMSettings.LAST_AUTO_RELOAD, string.Empty },
122137

123138
{ SAMSettings.THEME, "BaseDark" },
124139
{ SAMSettings.ACCENT, "Blue" },

SAM/Utils.cs

+17
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,23 @@ public static bool ApiKeyExists()
712712
return apiKey != null && apiKey.Length == API_KEY_LENGTH;
713713
}
714714

715+
public static bool ShouldAutoReload(DateTime? lastReload, int interval)
716+
{
717+
if (lastReload.HasValue == false)
718+
{
719+
return true;
720+
}
721+
722+
DateTime offset = lastReload.Value.AddMinutes(interval);
723+
724+
if (offset.CompareTo(DateTime.Now) <= 0)
725+
{
726+
return true;
727+
}
728+
729+
return false;
730+
}
731+
715732
public static bool IsSpecialCharacter(char c)
716733
{
717734
foreach (char special in specialChars)

0 commit comments

Comments
 (0)