Initial commit

This commit is contained in:
2026-01-23 12:09:56 +01:00
parent d17e1183f9
commit e511610052
18 changed files with 407 additions and 45 deletions

View File

@@ -8,25 +8,36 @@ using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CarManagerV2
namespace CarManagerV3
{
public partial class MainForm : Form
{
List<Car> cars = new List<Car>();
string filepath = "cars.csv";
public MainForm()
{
InitializeComponent();
SafeManager.InitializeFile("cars.csv");
List<Car> _cars = SafeManager.ReadCars("cars.csv");
List<string> recentFiles = SafeManager.GetRecentPaths();
if(recentFiles.Count > 0)
{
filepath = recentFiles[0];
}
SafeManager.InitializeFile(filepath);
List<Car> _cars = SafeManager.ReadCars(filepath);
refreshCars(_cars);
refreshRecents();
}
private async void refreshCars(List<Car> _cars, bool updateGlobal = true)
{
this.Text = "Car Manager - " + System.IO.Path.GetFileName(filepath);
// Sort by Car.Order. If equal, sort by ID
_cars = _cars.Count > 0 ? _cars.OrderBy(c => c.Order).ThenBy(c => c.Id).ToList() : _cars;
@@ -83,8 +94,8 @@ namespace CarManagerV2
Console.WriteLine("Car details form closed.");
// refresh cars
Console.WriteLine("Refreshing cars...");
List<Car> __cars = await Task.Run(() => SafeManager.ReadCars("cars.csv"));
if(tbxSearch.Text.Length > 0)
List<Car> __cars = await Task.Run(() => SafeManager.ReadCars(filepath));
if (tbxSearch.Text.Length > 0)
{
Console.WriteLine("Search box has text, applying search filter.");
cars = __cars;
@@ -92,11 +103,11 @@ namespace CarManagerV2
return;
}
refreshCars(__cars);
};
detailsForm.ShowDialog();
};
if (isNew)
{
flpCars.Controls.Add(card);
@@ -126,7 +137,7 @@ namespace CarManagerV2
{
// refresh cars
Console.WriteLine("Refreshing cars...");
List<Car> cars_ = SafeManager.ReadCars("cars.csv");
List<Car> cars_ = SafeManager.ReadCars(filepath);
refreshCars(cars_, false);
};
detailsForm.ShowDialog();
@@ -155,7 +166,7 @@ namespace CarManagerV2
{
string query = tbxSearch.Text;
await Task.Delay(100); // debounce
if(query != tbxSearch.Text) return; // text changed during delay
if (query != tbxSearch.Text) return; // text changed during delay
//flpCars.Controls.Clear();
if (string.IsNullOrWhiteSpace(query))
{
@@ -166,5 +177,207 @@ namespace CarManagerV2
searchList(query);
}
}
private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
{
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog dlgOpen = new OpenFileDialog();
dlgOpen.Filter = "CSV Files (*.csv)|*.csv|All Files (*.*)|*.*";
dlgOpen.Title = "Open Car Data File";
// Default to users documents
dlgOpen.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
DialogResult result = dlgOpen.ShowDialog();
if (result == DialogResult.OK)
{
try
{
List<Car> importedCars = SafeManager.ReadCars(dlgOpen.FileName);
if(importedCars.Count == 0)
{
throw new Exception("File doesn't contain valid Cars.");
}
filepath = dlgOpen.FileName;
cars = importedCars;
StateManager.setFilePath(filepath);
// Refresh display
refreshCars(cars);
MessageBox.Show("File loaded successfully.", "Load File", MessageBoxButtons.OK, MessageBoxIcon.Information);
SafeManager.AddRecentPath(filepath);
refreshRecents();
}
catch (Exception ex)
{
MessageBox.Show("Error loading file: " + ex.Message);
}
}
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
refreshCars(cars);
SafeManager.SaveCars(filepath, cars);
MessageBox.Show("File saved successfully.", "Save File", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog dlgSave = new SaveFileDialog();
dlgSave.Filter = "CSV Files (*.csv)|*.csv|All Files (*.*)|*.*";
dlgSave.Title = "Save Car Data File As";
// Default to users documents
dlgSave.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
DialogResult result = dlgSave.ShowDialog();
if (result == DialogResult.OK)
{
// does file already exist?
/*if (System.IO.File.Exists(dlgSave.FileName))
{
var overwriteResult = MessageBox.Show("File already exists. Overwrite?", "Overwrite File", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (overwriteResult != DialogResult.Yes)
{
return;
}
}*/
// Windows already handles this lmao
filepath = dlgSave.FileName;
this.Text = "Car Manager - " + System.IO.Path.GetFileName(filepath);
StateManager.setFilePath(filepath);
SafeManager.SaveCars(filepath, cars);
SafeManager.AddRecentPath(filepath);
}
refreshRecents();
}
private void importToolStripMenuItem_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("Importing will add cars from another file to this file. This action cannot be undone. Continue?", "Import Cars", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (result == DialogResult.Yes)
{
OpenFileDialog dlgOpen = new OpenFileDialog();
dlgOpen.Filter = "CSV Files (*.csv)|*.csv|All Files (*.*)|*.*";
dlgOpen.Title = "Import Car Data File";
// Default to users documents
dlgOpen.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
DialogResult dlgResult = dlgOpen.ShowDialog();
if (dlgResult == DialogResult.OK)
{
try
{
Console.WriteLine("Starting merge...");
List<Car> importedCars = SafeManager.ReadCars(dlgOpen.FileName);
if(importedCars.Count == 0)
{
throw new Exception("File doesn't contain valid Cars.");
}
// merge cars
foreach (Car car in importedCars)
{
// check if car with same ID exists
if (cars.Any(c => c.Id == car.Id))
{
// assign new ID
int newId = cars.Count > 0 ? cars.Max(c => c.Id) + 1 : 1;
car.Id = newId;
}
cars.Add(car);
}
DialogResult mergeAsNewFileResult = MessageBox.Show("Do you want to save the merged cars as a new file?", "Save As New File", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (mergeAsNewFileResult == DialogResult.Yes)
{
SaveFileDialog dlgSave = new SaveFileDialog();
dlgSave.Filter = "CSV Files (*.csv)|*.csv|All Files (*.*)|*.*";
dlgSave.Title = "Save Merged Car Data File As";
// Default to users documents
dlgSave.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
DialogResult saveResult = dlgSave.ShowDialog();
if (saveResult == DialogResult.OK)
{
filepath = dlgSave.FileName;
StateManager.setFilePath(filepath);
SafeManager.SaveCars(filepath, cars);
SafeManager.AddRecentPath(filepath);
refreshRecents();
}
}
else
{
// save to current file
SafeManager.SaveCars(filepath, cars);
}
// Refresh display
refreshCars(cars);
MessageBox.Show("File imported successfully.", "Import File", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show("Error importing file: " + ex.Message);
}
}
else
{
Console.WriteLine("Import cancelled.");
}
}
}
private void recentFilesToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void refreshRecents()
{
recentFilesToolStripMenuItem.DropDownItems.Clear();
List<string> recentFiles = SafeManager.GetRecentPaths();
recentFilesToolStripMenuItem.Enabled = recentFiles.Count > 0;
recentFilesToolStripMenuItem.ToolTipText = recentFiles.Count > 0 ? "" : "No recent files.";
foreach (string path in recentFiles)
{
ToolStripMenuItem item = new ToolStripMenuItem(path);
item.Click += (s, e2) =>
{
try
{
List<Car> importedCars = SafeManager.ReadCars(path);
if(importedCars.Count == 0)
{
throw new Exception("File doesn't contain valid Cars.");
}
filepath = path;
cars = importedCars;
StateManager.setFilePath(filepath);
// Refresh display
refreshCars(cars);
MessageBox.Show("File loaded successfully.", "Load File", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show("Error loading file: " + ex.Message);
}
};
recentFilesToolStripMenuItem.DropDownItems.Add(item);
}
}
private void revealInFileExplorerToolStripMenuItem_Click(object sender, EventArgs e)
{
// Open File Explorer at the location of the current filepath
if (System.IO.File.Exists(filepath))
{
System.Diagnostics.Process.Start("explorer.exe", "/select,\"" + filepath + "\"");
}
else
{
MessageBox.Show("File does not exist.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}