GtaVUsersInfo/MainForm.cs

147 lines
4.6 KiB
C#
Raw Normal View History

2024-10-06 12:32:33 +03:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.AccessControl;
2024-10-06 12:32:33 +03:00
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using GtaVUsersInfo.Helpers;
using GtaVUsersInfo.Controls;
using Newtonsoft.Json;
using System.IO;
using System.Data.Common;
using GtaVUsersInfo.Forms;
2024-10-06 12:32:33 +03:00
namespace GtaVUsersInfo
{
public partial class MainForm : Form
{
private List<Control> debugControlsList;
private List<Car> carList;
private List<Control> parameters;
2024-10-06 12:32:33 +03:00
public MainForm()
{
InitializeComponent();
debugControlsList = new List<Control> { parserButton, readJsonButton };
parameters = new List<Control> { new ParameterControl("Деньги"), new ParameterControl("Имя") };
this.visible(debugControlsList, false);
carList = LoadCarsFromJson(Resources.carJsonPath);
}
private void MainForm_Load(object sender, EventArgs e)
{
foreach (Control param in parameters)
{
param.Dock = DockStyle.Top;
parametersPanel.Controls.Add(param);
}
}
public static List<Car> LoadCarsFromJson(string filePath)
{
try
{
string json = File.ReadAllText(filePath);
List<Car> cars = JsonConvert.DeserializeObject<List<Car>>(json);
return cars;
}
catch (FileNotFoundException)
{
string message = $"Файл с автомобилями не найден: {filePath}";
Console.WriteLine(message);
MessageBox.Show(message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return new List<Car>();
}
catch (JsonException ex)
{
ErrorsShow.ShowErrorMessage($"Ошибка при десериализации JSON автомобилей: {ex.Message}");
return new List<Car>();
}
catch (Exception ex)
{
ErrorsShow.ShowErrorMessage($"Произошла неожиданная ошибка при открытии файла с автомобилями: {ex.Message}");
return new List<Car>();
}
2024-10-06 12:32:33 +03:00
}
#region Methods
private void visible(List<Control> controls, bool isVisible)
{
controls.ForEach(control => { control.Visible = isVisible; control.Enabled = isVisible; });
}
2024-10-06 12:32:33 +03:00
#endregion
async private void parserButton_Click(object sender, EventArgs e)
{
using (var parser = new CarParser())
{
var cars = await parser.ParseCarsFromPages(25);
parser.SaveToJson(cars, Resources.carJsonPath);
ErrorsShow.ShowInfoMessage($"Данные сохранены: {Resources.carJsonPath}");
}
carList = LoadCarsFromJson(Resources.carJsonPath);
}
private void readJsonButton_Click(object sender, EventArgs e)
{
carList = LoadCarsFromJson(Resources.carJsonPath);
}
private void debugCheck_CheckedChanged(object sender, EventArgs e)
{
this.visible(debugControlsList, debugCheck.Checked);
}
private void addCar_Click(object sender, EventArgs e)
{
if (carList.Count <= 0)
{
ErrorsShow.ShowErrorMessage($"Список автомобилей пуст, ошибка чтения файла: {Resources.carJsonPath}");
return;
}
Car car = new Car();
using (AddCars form = new AddCars(carList, car))
{
form.ShowDialog();
}
if (car.Name != null)
{
CarItem item = new CarItem(car);
carsItems.Controls.Add(item);
}
2024-10-06 12:32:33 +03:00
}
private void addParameter_Click(object sender, EventArgs e)
{
ParameterControl parameter = new ParameterControl();
using (AddParameter form = new AddParameter(parameters, parameter))
{
form.ShowDialog();
}
if (!string.IsNullOrEmpty(parameter.ParamName))
{
parameter.Dock = DockStyle.Top;
parameters.Add(parameter);
parametersPanel.Controls.Add(parameter);
}
}
2024-10-06 12:32:33 +03:00
}
}