fix: Update instead of re-fill
This commit is contained in:
@@ -54,7 +54,12 @@ namespace CarManagerV2
|
||||
|
||||
public bool IsChanged(Car other)
|
||||
{
|
||||
return make != other.make || model != other.model || year != other.year || color != other.color || mileage != other.mileage || price != other.price;
|
||||
return make != other.make || model != other.model || year != other.year || color != other.color || mileage != other.mileage || price != other.price || other.color != color;
|
||||
}
|
||||
|
||||
public Car Clone()
|
||||
{
|
||||
return new Car(id, make, model, year, color, mileage, price);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,8 @@ namespace CarManagerV2
|
||||
{
|
||||
public partial class CarCard : UserControl
|
||||
{
|
||||
public Car Car;
|
||||
|
||||
public string CarName
|
||||
{
|
||||
get { return lblCarName.Text; }
|
||||
@@ -58,5 +60,16 @@ namespace CarManagerV2
|
||||
this.CardClicked(this, EventArgs.Empty);
|
||||
}
|
||||
}
|
||||
|
||||
public void ClearCardClickedHandlers()
|
||||
{
|
||||
if (this.CardClicked != null)
|
||||
{
|
||||
foreach (Delegate d in this.CardClicked.GetInvocationList())
|
||||
{
|
||||
this.CardClicked -= (EventHandler)d;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,15 +67,17 @@ namespace CarManagerV2
|
||||
btnDelete.Enabled = false;
|
||||
var msgbox = new PleaseWait();
|
||||
msgbox.Show();
|
||||
Application.DoEvents();
|
||||
await Task.Run(() =>
|
||||
{
|
||||
StateManager.UpdateCar(car);
|
||||
Console.WriteLine("Saved car: " + car.Id);
|
||||
});
|
||||
await Task.Run(() =>
|
||||
{
|
||||
ImageManager.GetImage(car);
|
||||
Console.WriteLine("Updated image for car: " + car.Id);
|
||||
});
|
||||
Console.WriteLine("Car saved. " + car.Id);
|
||||
btnSave.Enabled = true;
|
||||
btnDelete.Enabled = true;
|
||||
this.Close();
|
||||
|
||||
@@ -13,13 +13,16 @@ namespace CarManagerV2
|
||||
public partial class MainForm : Form
|
||||
{
|
||||
List<Car> cars = new List<Car>();
|
||||
List<Car> lastCars = new List<Car>();
|
||||
|
||||
public MainForm()
|
||||
{
|
||||
InitializeComponent();
|
||||
SafeManager.InitializeFile("cars.csv");
|
||||
List<Car> _cars = SafeManager.ReadCars("cars.csv");
|
||||
|
||||
refreshCars(_cars);
|
||||
|
||||
}
|
||||
|
||||
private async void refreshCars(List<Car> _cars)
|
||||
@@ -81,31 +84,87 @@ namespace CarManagerV2
|
||||
// Console.WriteLine($"Added cars: {addedCars.Count}, Removed cars: {removedCars.Count}, Modified cars: {modifiedCars.Count}");
|
||||
//}
|
||||
|
||||
flpCars.Controls.Clear();
|
||||
// Get the cars currently in the FlowLayoutPanel to compare with _cars
|
||||
|
||||
|
||||
//flpCars.Controls.Clear();
|
||||
foreach (Car car in _cars)
|
||||
{
|
||||
// not in list? add it
|
||||
bool isNew = flpCars.Controls.OfType<CarCard>().All(c => c.Car.Id != car.Id);
|
||||
bool isUpdated = false;
|
||||
// existing but changed? update it
|
||||
CarCard card = new CarCard();
|
||||
if (!isNew)
|
||||
{
|
||||
CarCard existing = flpCars.Controls.OfType<CarCard>().First(c => c.Car.Id == car.Id);
|
||||
Car existingCar = existing.Car;
|
||||
if (existing == null)
|
||||
{
|
||||
Console.Error.WriteLine($"[L] Error: Existing car card not found for car ID: {car.Id}");
|
||||
continue;
|
||||
}
|
||||
// compare details
|
||||
Console.WriteLine($"[L] Checking car: {car.Id} | Car Color: {car.Color} | Ex Color: {existingCar.Color}");
|
||||
if (existingCar.IsChanged(car))
|
||||
{
|
||||
|
||||
isUpdated = true;
|
||||
Console.WriteLine($"[L] Updating car: {car.Id}");
|
||||
// changes
|
||||
card = existing;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
// no changes
|
||||
Console.WriteLine($"[L] No changes for car: {car.Id}");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
card.CarName = $"{car.Make} {car.Model}";
|
||||
card.CarDetails = $"{car.Year}, {car.Mileage} km, ${car.Price}";
|
||||
card.Car = car.Clone();
|
||||
card.CarImage = await Task.Run(() => ImageManager.GetImage(car));
|
||||
// clear existing event handlers to prevent multiple subscriptions
|
||||
card.ClearCardClickedHandlers();
|
||||
card.CardClicked += (s, e) =>
|
||||
{
|
||||
Console.WriteLine($"Card clicked: {car.Id}");
|
||||
CarDetailsForm detailsForm = new CarDetailsForm(car);
|
||||
detailsForm.FormClosed += async (s2, e2) =>
|
||||
{
|
||||
Console.WriteLine("Car details form closed.");
|
||||
// refresh cars
|
||||
Application.DoEvents();
|
||||
Console.WriteLine("Refreshing cars...");
|
||||
List<Car> __cars = await Task.Run(() => SafeManager.ReadCars("cars.csv"));
|
||||
refreshCars(__cars);
|
||||
};
|
||||
detailsForm.ShowDialog();
|
||||
};
|
||||
if (isNew)
|
||||
{
|
||||
flpCars.Controls.Add(card);
|
||||
}
|
||||
}
|
||||
|
||||
// Remove cards that are no longer in _cars
|
||||
var carIds = _cars.Select(c => c.Id).ToList();
|
||||
var cardsToRemove = flpCars.Controls.OfType<CarCard>().Where(c => !carIds.Contains(c.Car.Id)).ToList();
|
||||
foreach (var card in cardsToRemove)
|
||||
{
|
||||
flpCars.Controls.Remove(card);
|
||||
}
|
||||
flpCars.Invalidate();
|
||||
|
||||
flpCars.Refresh();
|
||||
|
||||
lastCars = new List<Car>(cars);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
private void btnNewCar_Click(object sender, EventArgs e)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user