From 192a390dbc46e208c882dceb467ddfb828e3832e Mon Sep 17 00:00:00 2001 From: Sombrio Date: Tue, 29 Sep 2020 01:02:51 -0300 Subject: [PATCH] :sparkles: added list of recent used ips in the client (#7) * Update README * Update README * :sparkles: added list of recent used ips in the client * rollbacked client version bump * added combo clearing before loading new items * Rewrite configuration Co-authored-by: AeonLucid --- src/Impostor.Client/Core/Configuration.cs | 65 +++++++++++++++++++ src/Impostor.Client/Forms/FrmMain.Designer.cs | 41 ++++++------ src/Impostor.Client/Forms/FrmMain.cs | 65 +++++++++++++------ 3 files changed, 132 insertions(+), 39 deletions(-) create mode 100644 src/Impostor.Client/Core/Configuration.cs diff --git a/src/Impostor.Client/Core/Configuration.cs b/src/Impostor.Client/Core/Configuration.cs new file mode 100644 index 0000000..63b7ab5 --- /dev/null +++ b/src/Impostor.Client/Core/Configuration.cs @@ -0,0 +1,65 @@ +using System; +using System.Collections.Generic; +using System.IO; + +namespace Impostor.Client.Core +{ + internal class Configuration + { + private const string FileRecentIps = @"recent_ips.txt"; + private const int MaxRecentIps = 5; + + private readonly string _baseDir; + private readonly string _recentIpsPath; + private readonly List _recentIps; + + public Configuration() + { + var appData = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)); + + _baseDir = Path.Combine(appData, "Impostor"); + _recentIpsPath = Path.Combine(_baseDir, FileRecentIps); + _recentIps = new List(); + } + + public IReadOnlyList RecentIps => _recentIps; + + public void Load() + { + if (File.Exists(_recentIpsPath)) + { + _recentIps.AddRange(File.ReadAllLines(_recentIpsPath)); + } + } + + public void Save() + { + Directory.CreateDirectory(_baseDir); + + if (!Directory.Exists(_baseDir)) + { + return; + } + + if (_recentIps.Count > 0) + { + File.WriteAllLines(_recentIpsPath, _recentIps); + } + } + + public void AddIp(string ip) + { + if (_recentIps.Contains(ip)) + { + _recentIps.Remove(ip); + } + + _recentIps.Insert(0, ip); + + if (_recentIps.Count > MaxRecentIps) + { + _recentIps.RemoveAt(MaxRecentIps); + } + } + } +} diff --git a/src/Impostor.Client/Forms/FrmMain.Designer.cs b/src/Impostor.Client/Forms/FrmMain.Designer.cs index ca2be07..3baba9d 100644 --- a/src/Impostor.Client/Forms/FrmMain.Designer.cs +++ b/src/Impostor.Client/Forms/FrmMain.Designer.cs @@ -33,9 +33,9 @@ this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.buttonLaunch = new System.Windows.Forms.Button(); - this.textIp = new System.Windows.Forms.TextBox(); this.lblUrl = new System.Windows.Forms.Label(); this.label3 = new System.Windows.Forms.Label(); + this.comboIp = new System.Windows.Forms.ComboBox(); this.SuspendLayout(); // // label1 @@ -45,7 +45,7 @@ this.label1.Location = new System.Drawing.Point(28, 139); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(60, 13); - this.label1.TabIndex = 0; + this.label1.TabIndex = 1; this.label1.Text = "IP Address"; // // label2 @@ -55,7 +55,7 @@ this.label2.Location = new System.Drawing.Point(28, 23); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(225, 91); - this.label2.TabIndex = 1; + this.label2.TabIndex = 0; this.label2.Text = "Welcome to Impostor\r\n\r\nPlease enter in the IP Address of the \r\nserver you would l" + "ike to use for Among Us\r\n\r\nIf you want to stop playing on the server, \r\nsimply s" + "elect another region"; @@ -66,30 +66,21 @@ this.buttonLaunch.Location = new System.Drawing.Point(179, 155); this.buttonLaunch.Name = "buttonLaunch"; this.buttonLaunch.Size = new System.Drawing.Size(74, 22); - this.buttonLaunch.TabIndex = 2; + this.buttonLaunch.TabIndex = 3; this.buttonLaunch.Text = "Save"; this.buttonLaunch.UseVisualStyleBackColor = true; this.buttonLaunch.Click += new System.EventHandler(this.buttonLaunch_Click); // - // textIp - // - this.textIp.Font = new System.Drawing.Font("Segoe UI", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); - this.textIp.Location = new System.Drawing.Point(31, 155); - this.textIp.Name = "textIp"; - this.textIp.Size = new System.Drawing.Size(141, 22); - this.textIp.TabIndex = 3; - this.textIp.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textIp_KeyDown); - // // lblUrl // this.lblUrl.AutoSize = true; this.lblUrl.Cursor = System.Windows.Forms.Cursors.Hand; this.lblUrl.Font = new System.Drawing.Font("Segoe UI", 8.25F, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.lblUrl.ForeColor = System.Drawing.SystemColors.Highlight; - this.lblUrl.Location = new System.Drawing.Point(39, 215); + this.lblUrl.Location = new System.Drawing.Point(39, 232); this.lblUrl.Name = "lblUrl"; this.lblUrl.Size = new System.Drawing.Size(212, 13); - this.lblUrl.TabIndex = 4; + this.lblUrl.TabIndex = 5; this.lblUrl.Text = "https://github.com/AeonLucid/Impostor"; this.lblUrl.Click += new System.EventHandler(this.lblUrl_Click); // @@ -97,20 +88,30 @@ // this.label3.AutoSize = true; this.label3.Font = new System.Drawing.Font("Segoe UI", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); - this.label3.Location = new System.Drawing.Point(54, 199); + this.label3.Location = new System.Drawing.Point(54, 216); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(182, 13); - this.label3.TabIndex = 5; + this.label3.TabIndex = 6; this.label3.Text = "Source code and latest versions at\r\n"; // + // comboIp + // + this.comboIp.Font = new System.Drawing.Font("Segoe UI", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.comboIp.FormattingEnabled = true; + this.comboIp.Location = new System.Drawing.Point(31, 155); + this.comboIp.Name = "comboIp"; + this.comboIp.Size = new System.Drawing.Size(141, 21); + this.comboIp.TabIndex = 2; + this.comboIp.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textIp_KeyDown); + // // FrmMain // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(283, 253); + this.ClientSize = new System.Drawing.Size(283, 262); + this.Controls.Add(this.comboIp); this.Controls.Add(this.label3); this.Controls.Add(this.lblUrl); - this.Controls.Add(this.textIp); this.Controls.Add(this.buttonLaunch); this.Controls.Add(this.label2); this.Controls.Add(this.label1); @@ -133,8 +134,8 @@ private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Button buttonLaunch; - private System.Windows.Forms.TextBox textIp; private System.Windows.Forms.Label lblUrl; private System.Windows.Forms.Label label3; + private System.Windows.Forms.ComboBox comboIp; } } \ No newline at end of file diff --git a/src/Impostor.Client/Forms/FrmMain.cs b/src/Impostor.Client/Forms/FrmMain.cs index b180e92..5d08f9e 100644 --- a/src/Impostor.Client/Forms/FrmMain.cs +++ b/src/Impostor.Client/Forms/FrmMain.cs @@ -1,22 +1,25 @@ using System; using System.Diagnostics; -using System.Threading.Tasks; +using System.Linq; using System.Windows.Forms; using Impostor.Client.Core; using Impostor.Client.Core.Events; +using ErrorEventArgs = Impostor.Client.Core.Events.ErrorEventArgs; namespace Impostor.Client.Forms { public partial class FrmMain : Form { + private readonly Configuration _config; private readonly AmongUsModifier _modifier; - + public FrmMain() { InitializeComponent(); AcceptButton = buttonLaunch; - + + _config = new Configuration(); _modifier = new AmongUsModifier(); _modifier.Error += ModifierOnError; _modifier.Saved += ModifierOnSaved; @@ -28,58 +31,82 @@ namespace Impostor.Client.Forms MessageBoxButtons.OK, MessageBoxIcon.Error); - textIp.Text = string.Empty; - textIp.Focus(); - - textIp.Enabled = true; + comboIp.Text = string.Empty; + comboIp.Focus(); + + comboIp.Enabled = true; buttonLaunch.Enabled = true; } private void ModifierOnSaved(object sender, SavedEventArgs e) { - MessageBox.Show("The IP Address was saved, please (re)start Among Us.", "Success", - MessageBoxButtons.OK, + MessageBox.Show("The IP Address was saved, please (re)start Among Us.", "Success", + MessageBoxButtons.OK, MessageBoxIcon.Information); - textIp.Text = e.IpAddress; - textIp.Enabled = true; + comboIp.Text = e.IpAddress; + comboIp.Enabled = true; buttonLaunch.Enabled = true; + + _config.AddIp(e.IpAddress); + _config.Save(); + + RefreshComboIps(); } private void FrmMain_Load(object sender, EventArgs e) { + _config.Load(); + + RefreshComboIps(); + if (_modifier.TryLoadIp(out var ipAddress)) { - textIp.Text = ipAddress; + comboIp.Text = ipAddress; } } private void FrmMain_Shown(object sender, EventArgs e) { - textIp.Focus(); + comboIp.Focus(); } private void textIp_KeyDown(object sender, KeyEventArgs e) { - if (e.KeyCode == Keys.Enter) + if (e.KeyCode != Keys.Enter) { - e.Handled = true; - - buttonLaunch_Click(this, EventArgs.Empty); + return; } + + e.Handled = true; + + buttonLaunch_Click(this, EventArgs.Empty); } private async void buttonLaunch_Click(object sender, EventArgs e) { - textIp.Enabled = false; + comboIp.Enabled = false; buttonLaunch.Enabled = false; - await _modifier.SaveIp(textIp.Text); + await _modifier.SaveIp(comboIp.Text); } private void lblUrl_Click(object sender, EventArgs e) { Process.Start("https://github.com/AeonLucid/Impostor"); } + + private void RefreshComboIps() + { + comboIp.Items.Clear(); + + if (_config.RecentIps.Count > 0) + { + foreach (var ip in _config.RecentIps) + { + comboIp.Items.Add(ip); + } + } + } } } \ No newline at end of file -- 2.39.5