Skip to content

Commit 68d34fa

Browse files
committed
create/destroy settings form dynamically instead of always keeping it in memory
1 parent 94aa227 commit 68d34fa

2 files changed

Lines changed: 20 additions & 19 deletions

File tree

CodeStats/CodeStatsPackage.cs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,6 @@ private static void InitializeAsync()
109109
currentPulse = new Pulse();
110110

111111
Logger.Info("Initialing settings form...");
112-
// Settings Form
113-
_settingsForm = new CodeStats.Forms.SettingsForm();
114-
_settingsForm.ConfigSaved += SettingsFormOnConfigSaved;
115-
Logger.Info("Initialized settings form"); // it takes 5 seconds to get here from Initializing Code::Stats message...
116112

117113
// Load config file
118114
_CodeStatsConfigFile = new ConfigFile();
@@ -515,9 +511,8 @@ private static Task ProcessPulses(CancellationTokenSource tokenSource)
515511
Logger.Error("Could not pulse (error 404). The entered custom endpoint (" + URL + ") is invalid. ", ex);
516512
MessageBox.Show("Could not pulse. Invalid API endpoint URL. Please make sure you entered a valid API URL in Code::Stats settings or delete the value altogether to restore the default.\nAll recorded XP from this session will be lost if you do not provide the correct API URL path!", "Code::Stats – error 404", MessageBoxButtons.OK, MessageBoxIcon.Error);
517513

518-
_settingsForm.FocusTxtAPIURL();
519-
_settingsForm.ShowAPIURLTooltip();
520514
SettingsPopup();
515+
_settingsForm.FocusTxtAPIURL();
521516
_settingsForm.ShowAPIURLTooltip();
522517
}
523518
else
@@ -707,11 +702,6 @@ public static bool EnoughTimePassed(DateTime now)
707702
return _lastActivity < now.AddMilliseconds(-1 * pulseFrequency) && _lastPulse < now.AddMilliseconds(-1 * pulseFrequency);
708703
}
709704

710-
private static void SettingsFormOnConfigSaved(object sender, EventArgs eventArgs)
711-
{
712-
GetSettings();
713-
}
714-
715705
public static void GetSettings(bool skipRead = false)
716706
{
717707
if (!skipRead) _CodeStatsConfigFile.Read();
@@ -736,10 +726,23 @@ private static void PromptApiKey()
736726

737727
private static void SettingsPopup()
738728
{
739-
_settingsForm.Visible = false;
729+
if (_settingsForm == null)
730+
{
731+
_settingsForm = new CodeStats.Forms.SettingsForm();
732+
_settingsForm.OnConfigSaved += SettingsFormOnConfigSaved;
733+
}
734+
//_settingsForm.Visible = false; // ?
740735
_settingsForm.ShowDialog();
741736
}
742737

738+
private static void SettingsFormOnConfigSaved(object sender, EventArgs eventArgs)
739+
{
740+
GetSettings();
741+
_settingsForm.OnConfigSaved -= SettingsFormOnConfigSaved;
742+
_settingsForm.Dispose();
743+
_settingsForm = null;
744+
}
745+
743746
public static void ReportStats()
744747
{
745748
Task.Run(() =>

CodeStats/Forms/SettingsForm.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace CodeStats.Forms
88
public partial class SettingsForm : Form
99
{
1010
private readonly ConfigFile _CodeStatsConfigFile;
11-
internal event EventHandler ConfigSaved;
11+
internal event EventHandler OnConfigSaved;
1212
private ToolTip apiurlToolTip;
1313

1414
public SettingsForm()
@@ -35,6 +35,7 @@ private void SettingsForm_Load(object sender, EventArgs e)
3535
{
3636
txtAPIURL.Text = _CodeStatsConfigFile.ApiUrl;
3737
}
38+
LanguageDetectionUIRefresh();
3839
}
3940
catch (Exception ex)
4041
{
@@ -101,16 +102,13 @@ private void btnOk_Click(object sender, EventArgs e)
101102

102103
_CodeStatsConfigFile.Save();
103104

104-
OnConfigSaved();
105-
//CodeStatsPackage.GetSettings(); // reload settings in main class
106-
107105
CodeStatsPackage._hasAlreadyShownInvalidApiTokenMessage = false;
108-
109106
if (chkStats.Checked && !CodeStatsPackage._reportedStats)
110107
{
111108
CodeStatsPackage.ReportStats();
112109
}
113110

111+
NotifyOnConfigSaved(); // it destroys the settings form in the handler
114112
/*}
115113
else // - kept in case we check API tokens in future
116114
{
@@ -125,9 +123,9 @@ private void btnOk_Click(object sender, EventArgs e)
125123
}
126124
}
127125

128-
protected virtual void OnConfigSaved()
126+
protected virtual void NotifyOnConfigSaved()
129127
{
130-
var handler = ConfigSaved;
128+
var handler = OnConfigSaved;
131129
if (handler != null) handler(this, EventArgs.Empty);
132130
}
133131

0 commit comments

Comments
 (0)