Compare commits
3 Commits
release/v1
...
3cc1631d5a
| Author | SHA1 | Date | |
|---|---|---|---|
| 3cc1631d5a | |||
| 0e8c9fc691 | |||
| a0de93f98c |
@@ -21,6 +21,12 @@
|
|||||||
}
|
}
|
||||||
"Entry"
|
"Entry"
|
||||||
{
|
{
|
||||||
|
"MsmKey" = "8:_058C8316BA034FF38880D3C8838A3F74"
|
||||||
|
"OwnerKey" = "8:_UNDEFINED"
|
||||||
|
"MsmSig" = "8:_UNDEFINED"
|
||||||
|
}
|
||||||
|
"Entry"
|
||||||
|
{
|
||||||
"MsmKey" = "8:_39292C9AEE694F0B982DDECDC0233E12"
|
"MsmKey" = "8:_39292C9AEE694F0B982DDECDC0233E12"
|
||||||
"OwnerKey" = "8:_UNDEFINED"
|
"OwnerKey" = "8:_UNDEFINED"
|
||||||
"MsmSig" = "8:_UNDEFINED"
|
"MsmSig" = "8:_UNDEFINED"
|
||||||
@@ -160,6 +166,26 @@
|
|||||||
"IsDependency" = "11:FALSE"
|
"IsDependency" = "11:FALSE"
|
||||||
"IsolateTo" = "8:"
|
"IsolateTo" = "8:"
|
||||||
}
|
}
|
||||||
|
"{1FB2D0AE-D3B9-43D4-B9DD-F88EC61E35DE}:_058C8316BA034FF38880D3C8838A3F74"
|
||||||
|
{
|
||||||
|
"SourcePath" = "8:E:\\Development\\Scripts\\CM3Merge\\merged-cars.json"
|
||||||
|
"TargetName" = "8:car_data,json"
|
||||||
|
"Tag" = "8:"
|
||||||
|
"Folder" = "8:_91D306E0AE6B4CA09DB4A07129FBDD93"
|
||||||
|
"Condition" = "8:"
|
||||||
|
"Transitive" = "11:TRUE"
|
||||||
|
"Vital" = "11:TRUE"
|
||||||
|
"ReadOnly" = "11:FALSE"
|
||||||
|
"Hidden" = "11:FALSE"
|
||||||
|
"System" = "11:FALSE"
|
||||||
|
"Permanent" = "11:FALSE"
|
||||||
|
"SharedLegacy" = "11:FALSE"
|
||||||
|
"PackageAs" = "3:1"
|
||||||
|
"Register" = "3:1"
|
||||||
|
"Exclude" = "11:FALSE"
|
||||||
|
"IsDependency" = "11:FALSE"
|
||||||
|
"IsolateTo" = "8:"
|
||||||
|
}
|
||||||
"{1FB2D0AE-D3B9-43D4-B9DD-F88EC61E35DE}:_39292C9AEE694F0B982DDECDC0233E12"
|
"{1FB2D0AE-D3B9-43D4-B9DD-F88EC61E35DE}:_39292C9AEE694F0B982DDECDC0233E12"
|
||||||
{
|
{
|
||||||
"SourcePath" = "8:D:\\Assets\\Car Manager\\Icon\\CarMgm_Icon.ico"
|
"SourcePath" = "8:D:\\Assets\\Car Manager\\Icon\\CarMgm_Icon.ico"
|
||||||
|
|||||||
@@ -16,6 +16,12 @@
|
|||||||
<setting name="AllowPrerelease" serializeAs="String">
|
<setting name="AllowPrerelease" serializeAs="String">
|
||||||
<value>False</value>
|
<value>False</value>
|
||||||
</setting>
|
</setting>
|
||||||
|
<setting name="LastFetchedAutoCompletions" serializeAs="String">
|
||||||
|
<value>2026-01-01</value>
|
||||||
|
</setting>
|
||||||
|
<setting name="FetchAutoCompletionsInterval" serializeAs="String">
|
||||||
|
<value>24.00:00:00</value>
|
||||||
|
</setting>
|
||||||
</CarManagerV3.Properties.Settings>
|
</CarManagerV3.Properties.Settings>
|
||||||
</userSettings>
|
</userSettings>
|
||||||
</configuration>
|
</configuration>
|
||||||
50
CarManagerV3/Classes/CarManufacturer.cs
Normal file
50
CarManagerV3/Classes/CarManufacturer.cs
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace CarManagerV3.Classes
|
||||||
|
{
|
||||||
|
internal class CarManufacturer
|
||||||
|
{
|
||||||
|
public string Name { get; set; }
|
||||||
|
public List<string> Models { get; set; }
|
||||||
|
|
||||||
|
public CarManufacturer(string name)
|
||||||
|
{
|
||||||
|
Name = name;
|
||||||
|
Models = new List<string>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddModel(string model)
|
||||||
|
{
|
||||||
|
Models.Add(model);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return $"{Name} - Models: {string.Join(", ", Models)}";
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<string> GetModels()
|
||||||
|
{
|
||||||
|
return Models;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RemoveModel(string model)
|
||||||
|
{
|
||||||
|
Models.Remove(model);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ClearModels()
|
||||||
|
{
|
||||||
|
Models.Clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
public string GetModelByQuery(string query)
|
||||||
|
{
|
||||||
|
return Models.FirstOrDefault(m => m.Equals(query, StringComparison.OrdinalIgnoreCase));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Windows.Forms;
|
using System.Windows.Forms;
|
||||||
|
using CarManagerV3.Util;
|
||||||
|
|
||||||
namespace CarManagerV3
|
namespace CarManagerV3
|
||||||
{
|
{
|
||||||
@@ -29,6 +30,39 @@ namespace CarManagerV3
|
|||||||
{
|
{
|
||||||
lblID.Text = $"ID: {car.Id}";
|
lblID.Text = $"ID: {car.Id}";
|
||||||
}
|
}
|
||||||
|
SetAutoCompleteForMake();
|
||||||
|
SetAutoCompleteForModel();
|
||||||
|
SetAutoCompleteForColor();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void SetAutoCompleteForMake()
|
||||||
|
{
|
||||||
|
var makes = CarCompletions.GetCarBrands();
|
||||||
|
var makeAutoComplete = new AutoCompleteStringCollection();
|
||||||
|
makeAutoComplete.AddRange(makes.ToArray());
|
||||||
|
tbxMake.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
|
||||||
|
tbxMake.AutoCompleteSource = AutoCompleteSource.CustomSource;
|
||||||
|
tbxMake.AutoCompleteCustomSource = makeAutoComplete;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void SetAutoCompleteForModel()
|
||||||
|
{
|
||||||
|
var models = CarCompletions.GetCarModels(tbxMake.Text);
|
||||||
|
var modelAutoComplete = new AutoCompleteStringCollection();
|
||||||
|
modelAutoComplete.AddRange(models.ToArray());
|
||||||
|
tbxModel.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
|
||||||
|
tbxModel.AutoCompleteSource = AutoCompleteSource.CustomSource;
|
||||||
|
tbxModel.AutoCompleteCustomSource = modelAutoComplete;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void SetAutoCompleteForColor()
|
||||||
|
{
|
||||||
|
var colors = CarCompletions.CommonColors;
|
||||||
|
var colorAutoComplete = new AutoCompleteStringCollection();
|
||||||
|
colorAutoComplete.AddRange(colors.ToArray());
|
||||||
|
tbxColor.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
|
||||||
|
tbxColor.AutoCompleteSource = AutoCompleteSource.CustomSource;
|
||||||
|
tbxColor.AutoCompleteCustomSource = colorAutoComplete;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -69,6 +103,7 @@ namespace CarManagerV3
|
|||||||
() => car.Make = ValueOrFormer(tbxMake.Text, car.Make),
|
() => car.Make = ValueOrFormer(tbxMake.Text, car.Make),
|
||||||
() => tbxMake.Text = car.Make
|
() => tbxMake.Text = car.Make
|
||||||
);
|
);
|
||||||
|
SetAutoCompleteForModel();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void tbxModel_TextChanged(object sender, EventArgs e)
|
private void tbxModel_TextChanged(object sender, EventArgs e)
|
||||||
@@ -163,7 +198,8 @@ namespace CarManagerV3
|
|||||||
|
|
||||||
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
|
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
|
||||||
{
|
{
|
||||||
if (keyData == Keys.Enter)
|
// Ctrl+S to save, Esc to close, Delete to delete
|
||||||
|
if (keyData == (Keys.Control | Keys.S))
|
||||||
{
|
{
|
||||||
btnSave.PerformClick();
|
btnSave.PerformClick();
|
||||||
return true; // Indicate that the key has been handled
|
return true; // Indicate that the key has been handled
|
||||||
|
|||||||
@@ -86,6 +86,8 @@ namespace CarManagerV3
|
|||||||
Console.Error.WriteLine("Error checking for updates: " + ex.Message);
|
Console.Error.WriteLine("Error checking for updates: " + ex.Message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CarCompletions.UpdateCarCompletionDataAsync();
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
105
CarManagerV3/Forms/SettingsForm.Designer.cs
generated
105
CarManagerV3/Forms/SettingsForm.Designer.cs
generated
@@ -36,9 +36,15 @@
|
|||||||
btnAccept = new System.Windows.Forms.Button();
|
btnAccept = new System.Windows.Forms.Button();
|
||||||
btnDiscard = new System.Windows.Forms.Button();
|
btnDiscard = new System.Windows.Forms.Button();
|
||||||
tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
|
tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
|
||||||
|
lblCompletionsRefreshWindow = new System.Windows.Forms.Label();
|
||||||
lblDataLocation = new System.Windows.Forms.Label();
|
lblDataLocation = new System.Windows.Forms.Label();
|
||||||
tbxDataLocation = new System.Windows.Forms.TextBox();
|
tbxDataLocation = new System.Windows.Forms.TextBox();
|
||||||
cbxPreRelease = new System.Windows.Forms.CheckBox();
|
cbxPreRelease = new System.Windows.Forms.CheckBox();
|
||||||
|
flowLayoutPanel4 = new System.Windows.Forms.FlowLayoutPanel();
|
||||||
|
nudCompletionIntervalHours = new System.Windows.Forms.NumericUpDown();
|
||||||
|
label3 = new System.Windows.Forms.Label();
|
||||||
|
nudCompletionIntervalMinutes = new System.Windows.Forms.NumericUpDown();
|
||||||
|
label4 = new System.Windows.Forms.Label();
|
||||||
tableLayoutPanel2 = new System.Windows.Forms.TableLayoutPanel();
|
tableLayoutPanel2 = new System.Windows.Forms.TableLayoutPanel();
|
||||||
btnReset = new System.Windows.Forms.Button();
|
btnReset = new System.Windows.Forms.Button();
|
||||||
flowLayoutPanel3 = new System.Windows.Forms.FlowLayoutPanel();
|
flowLayoutPanel3 = new System.Windows.Forms.FlowLayoutPanel();
|
||||||
@@ -46,6 +52,9 @@
|
|||||||
flowLayoutPanel1.SuspendLayout();
|
flowLayoutPanel1.SuspendLayout();
|
||||||
flowLayoutPanel2.SuspendLayout();
|
flowLayoutPanel2.SuspendLayout();
|
||||||
tableLayoutPanel1.SuspendLayout();
|
tableLayoutPanel1.SuspendLayout();
|
||||||
|
flowLayoutPanel4.SuspendLayout();
|
||||||
|
((System.ComponentModel.ISupportInitialize)nudCompletionIntervalHours).BeginInit();
|
||||||
|
((System.ComponentModel.ISupportInitialize)nudCompletionIntervalMinutes).BeginInit();
|
||||||
tableLayoutPanel2.SuspendLayout();
|
tableLayoutPanel2.SuspendLayout();
|
||||||
flowLayoutPanel3.SuspendLayout();
|
flowLayoutPanel3.SuspendLayout();
|
||||||
SuspendLayout();
|
SuspendLayout();
|
||||||
@@ -121,27 +130,46 @@
|
|||||||
tableLayoutPanel1.ColumnCount = 2;
|
tableLayoutPanel1.ColumnCount = 2;
|
||||||
tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
|
tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
|
||||||
tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
|
tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
|
||||||
|
tableLayoutPanel1.Controls.Add(lblCompletionsRefreshWindow, 0, 2);
|
||||||
tableLayoutPanel1.Controls.Add(lblDataLocation, 0, 0);
|
tableLayoutPanel1.Controls.Add(lblDataLocation, 0, 0);
|
||||||
tableLayoutPanel1.Controls.Add(tbxDataLocation, 1, 0);
|
tableLayoutPanel1.Controls.Add(tbxDataLocation, 1, 0);
|
||||||
tableLayoutPanel1.Controls.Add(cbxPreRelease, 1, 1);
|
tableLayoutPanel1.Controls.Add(cbxPreRelease, 0, 1);
|
||||||
|
tableLayoutPanel1.Controls.Add(flowLayoutPanel4, 1, 2);
|
||||||
tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
|
tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
|
||||||
tableLayoutPanel1.Location = new System.Drawing.Point(0, 86);
|
tableLayoutPanel1.Location = new System.Drawing.Point(0, 86);
|
||||||
tableLayoutPanel1.Name = "tableLayoutPanel1";
|
tableLayoutPanel1.Name = "tableLayoutPanel1";
|
||||||
tableLayoutPanel1.Padding = new System.Windows.Forms.Padding(10, 0, 10, 0);
|
tableLayoutPanel1.Padding = new System.Windows.Forms.Padding(10, 0, 10, 0);
|
||||||
tableLayoutPanel1.RowCount = 3;
|
tableLayoutPanel1.RowCount = 7;
|
||||||
tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle());
|
|
||||||
tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle());
|
tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle());
|
||||||
tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle());
|
tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle());
|
||||||
|
tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 0F));
|
||||||
|
tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
|
||||||
|
tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
|
||||||
|
tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
|
||||||
|
tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
|
||||||
tableLayoutPanel1.Size = new System.Drawing.Size(499, 531);
|
tableLayoutPanel1.Size = new System.Drawing.Size(499, 531);
|
||||||
tableLayoutPanel1.TabIndex = 3;
|
tableLayoutPanel1.TabIndex = 3;
|
||||||
//
|
//
|
||||||
|
// lblCompletionsRefreshWindow
|
||||||
|
//
|
||||||
|
lblCompletionsRefreshWindow.Anchor = System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right;
|
||||||
|
lblCompletionsRefreshWindow.AutoSize = true;
|
||||||
|
lblCompletionsRefreshWindow.Location = new System.Drawing.Point(13, 66);
|
||||||
|
lblCompletionsRefreshWindow.Margin = new System.Windows.Forms.Padding(3);
|
||||||
|
lblCompletionsRefreshWindow.MaximumSize = new System.Drawing.Size(150, 0);
|
||||||
|
lblCompletionsRefreshWindow.Name = "lblCompletionsRefreshWindow";
|
||||||
|
lblCompletionsRefreshWindow.Size = new System.Drawing.Size(148, 1);
|
||||||
|
lblCompletionsRefreshWindow.TabIndex = 4;
|
||||||
|
lblCompletionsRefreshWindow.Text = "Refresh completions after";
|
||||||
|
lblCompletionsRefreshWindow.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
|
||||||
|
//
|
||||||
// lblDataLocation
|
// lblDataLocation
|
||||||
//
|
//
|
||||||
lblDataLocation.Anchor = System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right;
|
lblDataLocation.Anchor = System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right;
|
||||||
lblDataLocation.AutoSize = true;
|
lblDataLocation.AutoSize = true;
|
||||||
lblDataLocation.Location = new System.Drawing.Point(13, 6);
|
lblDataLocation.Location = new System.Drawing.Point(13, 6);
|
||||||
lblDataLocation.Name = "lblDataLocation";
|
lblDataLocation.Name = "lblDataLocation";
|
||||||
lblDataLocation.Size = new System.Drawing.Size(102, 20);
|
lblDataLocation.Size = new System.Drawing.Size(148, 20);
|
||||||
lblDataLocation.TabIndex = 0;
|
lblDataLocation.TabIndex = 0;
|
||||||
lblDataLocation.Text = "Data Location";
|
lblDataLocation.Text = "Data Location";
|
||||||
lblDataLocation.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
|
lblDataLocation.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
|
||||||
@@ -149,9 +177,9 @@
|
|||||||
// tbxDataLocation
|
// tbxDataLocation
|
||||||
//
|
//
|
||||||
tbxDataLocation.Dock = System.Windows.Forms.DockStyle.Fill;
|
tbxDataLocation.Dock = System.Windows.Forms.DockStyle.Fill;
|
||||||
tbxDataLocation.Location = new System.Drawing.Point(121, 3);
|
tbxDataLocation.Location = new System.Drawing.Point(167, 3);
|
||||||
tbxDataLocation.Name = "tbxDataLocation";
|
tbxDataLocation.Name = "tbxDataLocation";
|
||||||
tbxDataLocation.Size = new System.Drawing.Size(365, 27);
|
tbxDataLocation.Size = new System.Drawing.Size(319, 27);
|
||||||
tbxDataLocation.TabIndex = 1;
|
tbxDataLocation.TabIndex = 1;
|
||||||
//
|
//
|
||||||
// cbxPreRelease
|
// cbxPreRelease
|
||||||
@@ -166,6 +194,61 @@
|
|||||||
cbxPreRelease.Text = "Pre-Release channel";
|
cbxPreRelease.Text = "Pre-Release channel";
|
||||||
cbxPreRelease.UseVisualStyleBackColor = true;
|
cbxPreRelease.UseVisualStyleBackColor = true;
|
||||||
//
|
//
|
||||||
|
// flowLayoutPanel4
|
||||||
|
//
|
||||||
|
flowLayoutPanel4.AutoSize = true;
|
||||||
|
flowLayoutPanel4.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
|
||||||
|
flowLayoutPanel4.Controls.Add(nudCompletionIntervalHours);
|
||||||
|
flowLayoutPanel4.Controls.Add(label3);
|
||||||
|
flowLayoutPanel4.Controls.Add(nudCompletionIntervalMinutes);
|
||||||
|
flowLayoutPanel4.Controls.Add(label4);
|
||||||
|
flowLayoutPanel4.Dock = System.Windows.Forms.DockStyle.Fill;
|
||||||
|
flowLayoutPanel4.Location = new System.Drawing.Point(164, 66);
|
||||||
|
flowLayoutPanel4.Margin = new System.Windows.Forms.Padding(0, 3, 0, 3);
|
||||||
|
flowLayoutPanel4.Name = "flowLayoutPanel4";
|
||||||
|
flowLayoutPanel4.Size = new System.Drawing.Size(325, 1);
|
||||||
|
flowLayoutPanel4.TabIndex = 5;
|
||||||
|
//
|
||||||
|
// nudCompletionIntervalHours
|
||||||
|
//
|
||||||
|
nudCompletionIntervalHours.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
|
||||||
|
nudCompletionIntervalHours.Location = new System.Drawing.Point(3, 3);
|
||||||
|
nudCompletionIntervalHours.Name = "nudCompletionIntervalHours";
|
||||||
|
nudCompletionIntervalHours.Size = new System.Drawing.Size(44, 27);
|
||||||
|
nudCompletionIntervalHours.TabIndex = 0;
|
||||||
|
nudCompletionIntervalHours.UpDownAlign = System.Windows.Forms.LeftRightAlignment.Left;
|
||||||
|
//
|
||||||
|
// label3
|
||||||
|
//
|
||||||
|
label3.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom;
|
||||||
|
label3.AutoSize = true;
|
||||||
|
label3.Location = new System.Drawing.Point(53, 0);
|
||||||
|
label3.Name = "label3";
|
||||||
|
label3.Size = new System.Drawing.Size(24, 33);
|
||||||
|
label3.TabIndex = 1;
|
||||||
|
label3.Text = "h :";
|
||||||
|
label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
|
||||||
|
//
|
||||||
|
// nudCompletionIntervalMinutes
|
||||||
|
//
|
||||||
|
nudCompletionIntervalMinutes.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
|
||||||
|
nudCompletionIntervalMinutes.Location = new System.Drawing.Point(83, 3);
|
||||||
|
nudCompletionIntervalMinutes.Name = "nudCompletionIntervalMinutes";
|
||||||
|
nudCompletionIntervalMinutes.Size = new System.Drawing.Size(44, 27);
|
||||||
|
nudCompletionIntervalMinutes.TabIndex = 2;
|
||||||
|
nudCompletionIntervalMinutes.UpDownAlign = System.Windows.Forms.LeftRightAlignment.Left;
|
||||||
|
//
|
||||||
|
// label4
|
||||||
|
//
|
||||||
|
label4.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom;
|
||||||
|
label4.AutoSize = true;
|
||||||
|
label4.Location = new System.Drawing.Point(133, 0);
|
||||||
|
label4.Name = "label4";
|
||||||
|
label4.Size = new System.Drawing.Size(22, 33);
|
||||||
|
label4.TabIndex = 3;
|
||||||
|
label4.Text = "m";
|
||||||
|
label4.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
|
||||||
|
//
|
||||||
// tableLayoutPanel2
|
// tableLayoutPanel2
|
||||||
//
|
//
|
||||||
tableLayoutPanel2.AutoSize = true;
|
tableLayoutPanel2.AutoSize = true;
|
||||||
@@ -240,6 +323,10 @@
|
|||||||
flowLayoutPanel2.ResumeLayout(false);
|
flowLayoutPanel2.ResumeLayout(false);
|
||||||
tableLayoutPanel1.ResumeLayout(false);
|
tableLayoutPanel1.ResumeLayout(false);
|
||||||
tableLayoutPanel1.PerformLayout();
|
tableLayoutPanel1.PerformLayout();
|
||||||
|
flowLayoutPanel4.ResumeLayout(false);
|
||||||
|
flowLayoutPanel4.PerformLayout();
|
||||||
|
((System.ComponentModel.ISupportInitialize)nudCompletionIntervalHours).EndInit();
|
||||||
|
((System.ComponentModel.ISupportInitialize)nudCompletionIntervalMinutes).EndInit();
|
||||||
tableLayoutPanel2.ResumeLayout(false);
|
tableLayoutPanel2.ResumeLayout(false);
|
||||||
tableLayoutPanel2.PerformLayout();
|
tableLayoutPanel2.PerformLayout();
|
||||||
flowLayoutPanel3.ResumeLayout(false);
|
flowLayoutPanel3.ResumeLayout(false);
|
||||||
@@ -264,5 +351,11 @@
|
|||||||
private System.Windows.Forms.CheckBox cbxPreRelease;
|
private System.Windows.Forms.CheckBox cbxPreRelease;
|
||||||
private System.Windows.Forms.FlowLayoutPanel flowLayoutPanel3;
|
private System.Windows.Forms.FlowLayoutPanel flowLayoutPanel3;
|
||||||
private System.Windows.Forms.Label lblEnv;
|
private System.Windows.Forms.Label lblEnv;
|
||||||
|
private System.Windows.Forms.Label lblCompletionsRefreshWindow;
|
||||||
|
private System.Windows.Forms.FlowLayoutPanel flowLayoutPanel4;
|
||||||
|
private System.Windows.Forms.NumericUpDown nudCompletionIntervalHours;
|
||||||
|
private System.Windows.Forms.Label label3;
|
||||||
|
private System.Windows.Forms.NumericUpDown nudCompletionIntervalMinutes;
|
||||||
|
private System.Windows.Forms.Label label4;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -43,6 +43,9 @@ namespace CarManagerV3.Forms
|
|||||||
read: () => Properties.Settings.Default.AllowPrerelease,
|
read: () => Properties.Settings.Default.AllowPrerelease,
|
||||||
write: v => Properties.Settings.Default.AllowPrerelease = v);
|
write: v => Properties.Settings.Default.AllowPrerelease = v);
|
||||||
|
|
||||||
|
//TODO: implement refresh settings
|
||||||
|
|
||||||
|
|
||||||
lblEnv.Text = lblEnv.Text.Replace("%E%", InstallModeDetector.IsInstalledViaMsi() ? "Installed via MSI" : "Running in portable mode");
|
lblEnv.Text = lblEnv.Text.Replace("%E%", InstallModeDetector.IsInstalledViaMsi() ? "Installed via MSI" : "Running in portable mode");
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -60,10 +63,33 @@ namespace CarManagerV3.Forms
|
|||||||
|
|
||||||
private void saveSettings()
|
private void saveSettings()
|
||||||
{
|
{
|
||||||
|
bool requiresRestart = false;
|
||||||
foreach (var kvp in settingsMap)
|
foreach (var kvp in settingsMap)
|
||||||
|
{
|
||||||
kvp.Value.Save();
|
kvp.Value.Save();
|
||||||
|
if (kvp.Value.RequiresRestart())
|
||||||
|
requiresRestart = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Properties.Settings.Default.Save();
|
Properties.Settings.Default.Save();
|
||||||
|
if(requiresRestart)
|
||||||
|
{
|
||||||
|
DialogResult result = MessageBox.Show(
|
||||||
|
"Some changes you made require a restart to take effect. Do you want to restart now?",
|
||||||
|
"Restart Required",
|
||||||
|
MessageBoxButtons.YesNo,
|
||||||
|
MessageBoxIcon.Information);
|
||||||
|
if (result == DialogResult.Yes)
|
||||||
|
{
|
||||||
|
Application.Restart();
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
MessageBox.Show("Please restart the application as soon as possible to ensure all changes take effect.", "Restart Recommended", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void resetSettings()
|
private void resetSettings()
|
||||||
@@ -106,6 +132,7 @@ namespace CarManagerV3.Forms
|
|||||||
void Load();
|
void Load();
|
||||||
void Save();
|
void Save();
|
||||||
void Reset();
|
void Reset();
|
||||||
|
bool RequiresRestart();
|
||||||
}
|
}
|
||||||
|
|
||||||
internal sealed class SettingBinding<T> : ISettingBinding
|
internal sealed class SettingBinding<T> : ISettingBinding
|
||||||
@@ -115,14 +142,18 @@ namespace CarManagerV3.Forms
|
|||||||
private readonly Func<T> read;
|
private readonly Func<T> read;
|
||||||
private readonly Action<T> write;
|
private readonly Action<T> write;
|
||||||
private readonly Action<T, T>? onChange;
|
private readonly Action<T, T>? onChange;
|
||||||
|
private readonly bool requiresRestart;
|
||||||
|
private bool changeRequiresRestart;
|
||||||
|
|
||||||
public SettingBinding(Control control, T defaultValue, Func<T> read, Action<T> write, Action<T, T>? onChange = null)
|
public SettingBinding(Control control, T defaultValue, Func<T> read, Action<T> write, bool requiresRestart = false, Action<T, T>? onChange = null)
|
||||||
{
|
{
|
||||||
this.control = control;
|
this.control = control;
|
||||||
this.defaultValue = defaultValue;
|
this.defaultValue = defaultValue;
|
||||||
this.read = read;
|
this.read = read;
|
||||||
this.write = write;
|
this.write = write;
|
||||||
this.onChange = onChange;
|
this.onChange = onChange;
|
||||||
|
this.requiresRestart = requiresRestart;
|
||||||
|
this.changeRequiresRestart = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Load()
|
public void Load()
|
||||||
@@ -140,10 +171,15 @@ namespace CarManagerV3.Forms
|
|||||||
|
|
||||||
if (onChange != null && !EqualityComparer<T>.Default.Equals(before, after))
|
if (onChange != null && !EqualityComparer<T>.Default.Equals(before, after))
|
||||||
onChange(before, after);
|
onChange(before, after);
|
||||||
|
|
||||||
|
if (requiresRestart && !EqualityComparer<T>.Default.Equals(before, after))
|
||||||
|
changeRequiresRestart = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Reset() => write(defaultValue);
|
public void Reset() => write(defaultValue);
|
||||||
|
|
||||||
|
public bool RequiresRestart() => changeRequiresRestart;
|
||||||
|
|
||||||
private void ApplyToControl(T value)
|
private void ApplyToControl(T value)
|
||||||
{
|
{
|
||||||
switch (control)
|
switch (control)
|
||||||
|
|||||||
@@ -64,6 +64,26 @@ namespace CarManagerV3
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Ensures the directory exists.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="path">The path.</param>
|
||||||
|
public static void EnsureDirectoryExists(string path)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string directory = Path.GetDirectoryName(path);
|
||||||
|
if (directory != null && !Directory.Exists(directory))
|
||||||
|
{
|
||||||
|
Directory.CreateDirectory(directory);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.Error.WriteLine($"Error ensuring directory exists: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Reads cars from a specified file path.
|
/// Reads cars from a specified file path.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -118,6 +118,9 @@
|
|||||||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||||
</resheader>
|
</resheader>
|
||||||
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
|
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
|
||||||
|
<data name="CarCompleteData" type="System.Resources.ResXFileRef, System.Windows.Forms">
|
||||||
|
<value>..\Resources\merged-cars.json;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||||
|
</data>
|
||||||
<data name="Icon_Add" type="System.Resources.ResXFileRef, System.Windows.Forms">
|
<data name="Icon_Add" type="System.Resources.ResXFileRef, System.Windows.Forms">
|
||||||
<value>..\Resources\Icon_Add.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
|
<value>..\Resources\Icon_Add.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
|
||||||
</data>
|
</data>
|
||||||
|
|||||||
24
CarManagerV3/Properties/Settings.Designer.cs
generated
24
CarManagerV3/Properties/Settings.Designer.cs
generated
@@ -46,5 +46,29 @@ namespace CarManagerV3.Properties {
|
|||||||
this["AllowPrerelease"] = value;
|
this["AllowPrerelease"] = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[global::System.Configuration.UserScopedSettingAttribute()]
|
||||||
|
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
|
||||||
|
[global::System.Configuration.DefaultSettingValueAttribute("2026-01-01")]
|
||||||
|
public global::System.DateTime LastFetchedAutoCompletions {
|
||||||
|
get {
|
||||||
|
return ((global::System.DateTime)(this["LastFetchedAutoCompletions"]));
|
||||||
|
}
|
||||||
|
set {
|
||||||
|
this["LastFetchedAutoCompletions"] = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[global::System.Configuration.UserScopedSettingAttribute()]
|
||||||
|
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
|
||||||
|
[global::System.Configuration.DefaultSettingValueAttribute("24.00:00:00")]
|
||||||
|
public global::System.TimeSpan FetchAutoCompletionsInterval {
|
||||||
|
get {
|
||||||
|
return ((global::System.TimeSpan)(this["FetchAutoCompletionsInterval"]));
|
||||||
|
}
|
||||||
|
set {
|
||||||
|
this["FetchAutoCompletionsInterval"] = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,5 +8,11 @@
|
|||||||
<Setting Name="AllowPrerelease" Type="System.Boolean" Scope="User">
|
<Setting Name="AllowPrerelease" Type="System.Boolean" Scope="User">
|
||||||
<Value Profile="(Default)">False</Value>
|
<Value Profile="(Default)">False</Value>
|
||||||
</Setting>
|
</Setting>
|
||||||
|
<Setting Name="LastFetchedAutoCompletions" Type="System.DateTime" Scope="User">
|
||||||
|
<Value Profile="(Default)">2026-01-01</Value>
|
||||||
|
</Setting>
|
||||||
|
<Setting Name="FetchAutoCompletionsInterval" Type="System.TimeSpan" Scope="User">
|
||||||
|
<Value Profile="(Default)">24.00:00:00</Value>
|
||||||
|
</Setting>
|
||||||
</Settings>
|
</Settings>
|
||||||
</SettingsFile>
|
</SettingsFile>
|
||||||
6573
CarManagerV3/Resources/merged-cars.json
Normal file
6573
CarManagerV3/Resources/merged-cars.json
Normal file
File diff suppressed because it is too large
Load Diff
261
CarManagerV3/Util/CarCompletions.cs
Normal file
261
CarManagerV3/Util/CarCompletions.cs
Normal file
@@ -0,0 +1,261 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System.Windows.Forms;
|
||||||
|
using CarManagerV3.Classes;
|
||||||
|
|
||||||
|
namespace CarManagerV3.Util
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Provides car manufacturer and model autocompletion data for the application.
|
||||||
|
/// Manages fetching, caching, and retrieval of car brand and model information from remote sources and local storage.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// This class maintains a static list of car manufacturers and their models, supports fetching updated data from remote URLs,
|
||||||
|
/// and caches the data locally for offline use. It implements retry logic to prevent excessive network requests.
|
||||||
|
/// </remarks>
|
||||||
|
internal class CarCompletions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// A list of URLs that should be used to fetch the json file with autocompletion data.
|
||||||
|
/// Multiple URLs are provided to ensure that if one source is unavailable, the others can be used as a fallback. The application should attempt to fetch the data from each URL in order until it successfully retrieves the data or exhausts all options.
|
||||||
|
/// </summary>
|
||||||
|
private static readonly string[] carDataFileUrls =
|
||||||
|
{
|
||||||
|
"https://static.clsw.app/carmgm/merged-cars.json"
|
||||||
|
};
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The filename for the cached car completion data in the user's data directory.
|
||||||
|
/// </summary>
|
||||||
|
private static readonly string carCompletionDataFileName = "car_data.json";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The timestamp of the last fetch attempt for car completion data.
|
||||||
|
/// Used to implement retry throttling to prevent excessive network requests.
|
||||||
|
/// </summary>
|
||||||
|
private static DateTime lastFetchAttempt = Properties.Settings.Default.LastFetchedAutoCompletions;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The minimum time interval that must elapse between fetch attempts.
|
||||||
|
/// Prevents excessive retry attempts when the remote source is unavailable.
|
||||||
|
/// </summary>
|
||||||
|
private static readonly TimeSpan fetchRetryInterval = Properties.Settings.Default.FetchAutoCompletionsInterval; // Minimum interval between fetch attempts
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the full file path for the car completion data file.
|
||||||
|
/// Ensures that the user data directory exists before returning the path.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>The full file path to the car completion data file in the user's data directory.</returns>
|
||||||
|
private static string getCarCompletionDataFilePath()
|
||||||
|
{
|
||||||
|
var userDataDir = Properties.Settings.Default.DataLocation;
|
||||||
|
SafeManager.EnsureDirectoryExists(userDataDir);
|
||||||
|
return Path.Combine(userDataDir, carCompletionDataFileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The static list of car manufacturers and their available models.
|
||||||
|
/// This list serves as the default data and is updated when fetching from remote sources or local cache.
|
||||||
|
/// </summary>
|
||||||
|
public static List<CarManufacturer> carBrands = new List<CarManufacturer>
|
||||||
|
{
|
||||||
|
new CarManufacturer("Toyota") { Models = new List<string> { "Camry", "Corolla", "RAV4" } },
|
||||||
|
new CarManufacturer("Honda") { Models = new List<string> { "Civic", "Accord", "CR-V" } },
|
||||||
|
new CarManufacturer("Ford") { Models = new List<string> { "F-150", "Mustang", "Escape" } },
|
||||||
|
new CarManufacturer("Chevrolet") { Models = new List<string> { "Silverado", "Malibu", "Equinox" } },
|
||||||
|
new CarManufacturer("BMW") { Models = new List<string> { "3 Series", "5 Series", "X5" } },
|
||||||
|
new CarManufacturer("Mercedes-Benz") { Models = new List<string> { "C-Class", "E-Class", "GLC" } },
|
||||||
|
new CarManufacturer("Audi") { Models = new List<string> { "A4", "A6", "Q5" } },
|
||||||
|
new CarManufacturer("Volkswagen") { Models = new List<string> { "Golf", "Passat", "Tiguan" } },
|
||||||
|
new CarManufacturer("Nissan") { Models = new List<string> { "Altima", "Sentra", "Rogue" } },
|
||||||
|
new CarManufacturer("Hyundai") { Models = new List<string> { "Elantra", "Sonata", "Tucson" } },
|
||||||
|
new CarManufacturer("SEAT") { Models = new List<string> { "Ibiza", "Leon", "Ateca", "Alhambra" } },
|
||||||
|
new CarManufacturer("Skoda") { Models = new List<string> { "Octavia", "Fabia", "Kodiaq" } },
|
||||||
|
};
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the full list of car brands.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>A list of strings containing all available car manufacturer names.</returns>
|
||||||
|
public static List<string> GetCarBrands()
|
||||||
|
{
|
||||||
|
return carBrands.Select(c => c.Name).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Retrieves the list of car models for a specified brand.
|
||||||
|
/// The search is case-insensitive.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="brand">The name of the car brand to search for.</param>
|
||||||
|
/// <returns>A list of car models for the specified brand, or an empty list if the brand is not found.</returns>
|
||||||
|
public static List<string> GetCarModels(string brand)
|
||||||
|
{
|
||||||
|
var manufacturer = carBrands.FirstOrDefault(c => c.Name.Equals(brand, StringComparison.OrdinalIgnoreCase));
|
||||||
|
return manufacturer != null ? manufacturer.Models : new List<string>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A predefined list of common car colors for autocompletion purposes.
|
||||||
|
/// Provides standard color options that are frequently used in vehicle descriptions.
|
||||||
|
/// </summary>
|
||||||
|
public static List<string> CommonColors = new List<string>
|
||||||
|
{
|
||||||
|
"Black", "White", "Gray", "Silver", "Red", "Blue", "Green", "Yellow", "Brown", "Orange"
|
||||||
|
};
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Reads car completion data from an embedded resource file.
|
||||||
|
/// This method is intended to populate the carBrands list from a JSON resource embedded in the project.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// The expected resource format is a JSON object where keys are car brand names and values are lists of model names.
|
||||||
|
/// Resource name: CarCompleteData (JSON file format).
|
||||||
|
/// Currently not implemented.
|
||||||
|
/// </remarks>
|
||||||
|
public static void ReadFromResourceFile()
|
||||||
|
{
|
||||||
|
// Read the json file from the projects resources and populate the carBrands list
|
||||||
|
// Format is a json object with the key being the car brand and the value being a list of models
|
||||||
|
|
||||||
|
// Get the json string from the resources, Resource name is CarCompleteData, it is a .json file
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Fetches the car data from urls asynchronous and saves it in the users data directory for offline use.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>A task representing the asynchronous operation.</returns>
|
||||||
|
/// <remarks>
|
||||||
|
/// This method implements retry throttling using the <see cref="fetchRetryInterval"/> to prevent excessive network requests.
|
||||||
|
/// It attempts to fetch data from each URL in <see cref="carDataFileUrls"/> in order until successful or all URLs are exhausted.
|
||||||
|
/// The fetched data is saved to the local file system for offline access.
|
||||||
|
/// </remarks>
|
||||||
|
public static async Task FetchCarCompletionDataFromUrlsAsync()
|
||||||
|
{
|
||||||
|
if(DateTime.Now - lastFetchAttempt < fetchRetryInterval)
|
||||||
|
{
|
||||||
|
System.Diagnostics.Debug.WriteLine("Fetch attempt skipped to avoid excessive retries. Last attempt was at " + lastFetchAttempt);
|
||||||
|
Console.WriteLine("Fetch attempt skipped to avoid excessive retries. Last attempt was at " + lastFetchAttempt);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
foreach (var url in carDataFileUrls)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
lastFetchAttempt = DateTime.Now;
|
||||||
|
Properties.Settings.Default.LastFetchedAutoCompletions = lastFetchAttempt;
|
||||||
|
Properties.Settings.Default.Save();
|
||||||
|
using var httpClient = new HttpClient();
|
||||||
|
var response = await httpClient.GetAsync(url);
|
||||||
|
response.EnsureSuccessStatusCode();
|
||||||
|
var jsonData = await response.Content.ReadAsStringAsync();
|
||||||
|
// Saves the json data to a file in the user's data directory for offline use
|
||||||
|
var filePath = getCarCompletionDataFilePath();
|
||||||
|
await File.WriteAllTextAsync(filePath, jsonData);
|
||||||
|
System.Diagnostics.Debug.WriteLine($"Successfully fetched car data from {url} and saved to {filePath}");
|
||||||
|
break; // Exit the loop if data is successfully fetched
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
// Log the error and try the next URL
|
||||||
|
System.Diagnostics.Debug.WriteLine($"Failed to fetch car data from {url}: {ex.Message}");
|
||||||
|
Console.WriteLine($"Failed to fetch car data from {url}: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Fetches the car data from urls and saves it in the users data directory for offline use.
|
||||||
|
/// This is a synchronous wrapper that blocks until the asynchronous fetch operation completes.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>A task representing the synchronous operation.</returns>
|
||||||
|
/// <remarks>
|
||||||
|
/// This method wraps <see cref="FetchCarCompletionDataFromUrlsAsync"/> and runs it synchronously.
|
||||||
|
/// Consider using the async version directly when possible to avoid blocking the thread.
|
||||||
|
/// </remarks>
|
||||||
|
public static Task FetchCarCompletionDataFromUrls()
|
||||||
|
{
|
||||||
|
// Run synchronously
|
||||||
|
return Task.Run(() => FetchCarCompletionDataFromUrlsAsync().Wait());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Loads car manufacturer data from the locally cached file.
|
||||||
|
/// Optionally fetches updated data from remote sources before loading from file.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="skipOnlineUpdate">If true, skips fetching data from remote sources and only reads from the local cache. Default is false.</param>
|
||||||
|
/// <returns>A list of <see cref="CarManufacturer"/> objects populated from the cached file, or an empty list if the file doesn't exist or an error occurs.</returns>
|
||||||
|
/// <remarks>
|
||||||
|
/// The expected file format is a JSON object where keys are car brand names and values are lists of model names.
|
||||||
|
/// If the file doesn't exist or cannot be read, an error message is logged and an empty list is returned.
|
||||||
|
/// </remarks>
|
||||||
|
public static List<CarManufacturer> GetFromFile(bool skipOnlineUpdate = false)
|
||||||
|
{
|
||||||
|
if (!skipOnlineUpdate) FetchCarCompletionDataFromUrls();
|
||||||
|
var filePath = getCarCompletionDataFilePath();
|
||||||
|
if (File.Exists(filePath))
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var jsonData = File.ReadAllText(filePath);
|
||||||
|
// Parse the json data and populate the carBrands list
|
||||||
|
// Format is a json object with the key being the car brand and the value being a list of models
|
||||||
|
var carData = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, List<string>>>(jsonData);
|
||||||
|
List<CarManufacturer> _carBrands = carData.Select(kvp => new CarManufacturer(kvp.Key) { Models = kvp.Value }).ToList();
|
||||||
|
return _carBrands;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
// Silent error.
|
||||||
|
Console.WriteLine($"Failed to read car data from file: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Console.Error.WriteLine("Car data file not found. Please ensure that the data has been fetched successfully at least once.");
|
||||||
|
}
|
||||||
|
return new List<CarManufacturer>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Updates the static <see cref="carBrands"/> list with data from the local cache file.
|
||||||
|
/// Only updates the list if the cached file contains valid data.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="skipOnlineUpdate">If true, skips fetching data from remote sources before updating. Default is false.</param>
|
||||||
|
/// <remarks>
|
||||||
|
/// This method calls <see cref="GetFromFile"/> and updates <see cref="carBrands"/> only if the returned list is not empty.
|
||||||
|
/// If no data is retrieved, the existing <see cref="carBrands"/> list remains unchanged.
|
||||||
|
/// </remarks>
|
||||||
|
public static void UpdateCarCompletionData(bool skipOnlineUpdate = false)
|
||||||
|
{
|
||||||
|
var updatedCarBrands = GetFromFile(skipOnlineUpdate);
|
||||||
|
if (updatedCarBrands.Count > 0)
|
||||||
|
{
|
||||||
|
carBrands = updatedCarBrands;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously updates the car completion data by fetching from remote sources and updating the local cache.
|
||||||
|
/// This is the recommended method for refreshing car data in the application.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>A task representing the asynchronous update operation.</returns>
|
||||||
|
/// <remarks>
|
||||||
|
/// This method first attempts to fetch updated data from remote URLs using <see cref="FetchCarCompletionDataFromUrlsAsync"/>,
|
||||||
|
/// then updates the static <see cref="carBrands"/> list from the local cache using <see cref="UpdateCarCompletionData"/>.
|
||||||
|
/// </remarks>
|
||||||
|
public static async Task UpdateCarCompletionDataAsync()
|
||||||
|
{
|
||||||
|
await FetchCarCompletionDataFromUrlsAsync();
|
||||||
|
System.Diagnostics.Debug.WriteLine("Car completion data fetch attempt completed. Now updating local data.");
|
||||||
|
UpdateCarCompletionData(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user