Code clean up

This commit is contained in:
retr0volkov
2025-07-10 09:19:06 +03:00 Unverified
parent 2a8f8d2714
commit 915c7a7c5e
36 changed files with 116 additions and 133 deletions
+3 -4
View File
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using CommunityToolkit.Mvvm.ComponentModel;
@@ -43,7 +42,7 @@ public partial class EquipmentViewModel : ViewModelBase
Image = new ImageViewModel(equipment.Image ?? new Image());
Properties = new ObservableCollection<PropertyViewModel>(equipment.Properties.Select(e => new PropertyViewModel(e)));
List<DateTime[]> times = (equipment.Inspections.Select(e => new InspectionViewModel(e)))
var times = equipment.Inspections.Select(e => new InspectionViewModel(e))
.Select(e => e.GetNearestDates())
.ToList();
@@ -62,12 +61,12 @@ public partial class EquipmentViewModel : ViewModelBase
public Equipment GetEquipment()
{
return new Equipment()
return new Equipment
{
Name = Name,
Description = Description,
Image = Image?.GetImage(),
Inspections = Inspections.Select(e => e.GetInspection()).ToList(),
Inspections = Inspections.Select(e => e.GetInspection()).ToList()
};
}
}
+5 -6
View File
@@ -20,7 +20,7 @@ public partial class ErrorViewModel : ViewModelBase
w.Close();
}
private ErrorSeverity severity;
private readonly ErrorSeverity _severity;
// Visual and other properties
[ObservableProperty] private bool _isErrorVisible;
@@ -38,7 +38,7 @@ public partial class ErrorViewModel : ViewModelBase
{
Title = error.Title;
Message = error.Message;
severity = error.Severity;
_severity = error.Severity;
switch (error.Severity)
{
case ErrorSeverity.Error:
@@ -48,21 +48,20 @@ public partial class ErrorViewModel : ViewModelBase
IsWarnVisible = true;
break;
case ErrorSeverity.Message:
IsInfoVisible = true;
break;
default:
IsInfoVisible = true;
break;
}
}
// ReSharper disable once UnusedMember.Global
public Error GetError()
{
Error error = new Error()
var error = new Error
{
Title = Title,
Message = Message,
Severity = severity,
Severity = _severity
};
return error;
}
+3 -3
View File
@@ -35,13 +35,13 @@ public partial class ImageViewModel : ViewModelBase
public Image GetImage()
{
MemoryStream ms = new MemoryStream();
var ms = new MemoryStream();
FileName?.Save(ms);
return new Image()
return new Image
{
Bytes = ms.ToArray(),
Equipment = Equipment ?? [],
Points = Points ?? [],
Points = Points ?? []
};
}
}
+2 -2
View File
@@ -19,7 +19,7 @@ public partial class InspectionViewModel : ViewModelBase
// Visual properties
[ObservableProperty]
private bool _isEditing = false;
private bool _isEditing;
public InspectionViewModel()
{
@@ -39,7 +39,7 @@ public partial class InspectionViewModel : ViewModelBase
public Inspection GetInspection()
{
return new Inspection()
return new Inspection
{
Name = Name,
InspectionDate = InspectionDate,
+8 -7
View File
@@ -26,7 +26,7 @@ public partial class PointViewModel : ViewModelBase
[ObservableProperty]
private List<Property>? _properties;
public PointViewModel(Models.Point point)
public PointViewModel(Point point)
{
Name = point.Name;
Description = point.Description;
@@ -37,14 +37,15 @@ public partial class PointViewModel : ViewModelBase
Properties = point.Properties;
}
public Models.Point GetPoint()
// ReSharper disable once UnusedMember.Global
public Point GetPoint()
{
return new Models.Point()
return new Point
{
Name = this.Name,
Description = this.Description,
Equipment = this.Equipment ?? [],
Properties = this.Properties ?? [],
Name = Name,
Description = Description,
Equipment = Equipment ?? [],
Properties = Properties ?? []
};
}
}
+1 -2
View File
@@ -1,7 +1,6 @@
using System.Collections.ObjectModel;
using System.Linq;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using GazpromApp.Models;
namespace GazpromApp.ViewModels;
@@ -26,7 +25,7 @@ public partial class PropertyViewModel : ViewModelBase
public Property GetProperty(Property? parent = null)
{
Property p = new Property
var p = new Property
{
Name = Name,
Value = Value,
+8 -8
View File
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
@@ -13,6 +12,7 @@ using CommunityToolkit.Mvvm.Messaging;
using GazpromApp.Enums;
using GazpromApp.Messages;
using GazpromApp.Models;
using Image = GazpromApp.Models.Image;
namespace GazpromApp.ViewModels;
@@ -54,26 +54,26 @@ public partial class EditEquipmentDialogViewModel : ViewModelBase
[RelayCommand]
private async Task AddImage(Window w)
{
IReadOnlyList<IStorageFile> files = await w.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions()
var files = await w.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions
{
AllowMultiple = false,
Title = "Выберите картинку",
FileTypeFilter = [FilePickerFileTypes.ImageAll],
FileTypeFilter = [FilePickerFileTypes.ImageAll]
});
IsImageSelected = files.Count > 0;
try
{
Image = IsImageSelected ? new ImageViewModel(new Models.Image()) { FileName = new Bitmap(HttpUtility.UrlDecode(files[0].Path.AbsolutePath)) } : null;
Image = IsImageSelected ? new ImageViewModel(new Image()) { FileName = new Bitmap(HttpUtility.UrlDecode(files[0].Path.AbsolutePath)) } : null;
}
catch (ArgumentException)
{
Error error = new Error()
var error = new Error
{
Title = "Ошибка обработки картинки",
Message = "Картинка не была добавлена",
Severity = ErrorSeverity.Error,
Severity = ErrorSeverity.Error
};
_ = await WeakReferenceMessenger.Default.Send(new ErrorMessage(new ErrorViewModel(error)));
@@ -142,13 +142,13 @@ public partial class EditEquipmentDialogViewModel : ViewModelBase
private void CompleteEdit()
{
IsClosed = true;
Equipment equipment = new Equipment
var equipment = new Equipment
{
Name = Name,
Description = Description,
Image = Image?.GetImage(),
Inspections = Inspections.Select(e => e.GetInspection()).ToList(),
Properties = Properties.Select(e => e.GetProperty()).ToList(),
Properties = Properties.Select(e => e.GetProperty()).ToList()
};
WeakReferenceMessenger.Default.Send(new EditEquipmentDone(new EquipmentViewModel(equipment)));
}
+10 -13
View File
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
@@ -14,7 +12,6 @@ using CommunityToolkit.Mvvm.Messaging;
using GazpromApp.Enums;
using GazpromApp.Messages;
using GazpromApp.Models;
using GazpromApp.Views;
using Image = GazpromApp.Models.Image;
namespace GazpromApp.ViewModels;
@@ -54,11 +51,11 @@ public partial class EditPointDialogViewModel : ViewModelBase
[RelayCommand]
private async Task AddImage(Window w)
{
IReadOnlyList<IStorageFile> files = await w.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions()
var files = await w.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions
{
AllowMultiple = true,
Title = "Выберите одну или несколько картинок",
FileTypeFilter = [FilePickerFileTypes.ImageAll],
FileTypeFilter = [FilePickerFileTypes.ImageAll]
});
foreach (var file in files)
{
@@ -68,11 +65,11 @@ public partial class EditPointDialogViewModel : ViewModelBase
}
catch (ArgumentException)
{
Error error = new Error()
var error = new Error
{
Title = "Ошибка обработки картинки",
Message = $"Картинка не была добавлена: \"{HttpUtility.UrlDecode(file.Name)}\"",
Severity = ErrorSeverity.Error,
Severity = ErrorSeverity.Error
};
_ = await WeakReferenceMessenger.Default.Send(new ErrorMessage(new ErrorViewModel(error)));
}
@@ -96,7 +93,7 @@ public partial class EditPointDialogViewModel : ViewModelBase
[RelayCommand]
private async Task AddEquipment()
{
EquipmentViewModel? equipment = await WeakReferenceMessenger.Default.Send(new NewEquipmentMessage());
var equipment = await WeakReferenceMessenger.Default.Send(new NewEquipmentMessage());
if (equipment != null)
Equipment.Add(equipment);
@@ -107,7 +104,7 @@ public partial class EditPointDialogViewModel : ViewModelBase
[RelayCommand]
private async Task EditEquipment(EquipmentViewModel equipment)
{
EquipmentViewModel e = await WeakReferenceMessenger.Default.Send(new EditEquipmentMessage(equipment)) ?? equipment;
var e = await WeakReferenceMessenger.Default.Send(new EditEquipmentMessage(equipment)) ?? equipment;
Equipment[Equipment.IndexOf(equipment)] = e;
}
@@ -145,13 +142,13 @@ public partial class EditPointDialogViewModel : ViewModelBase
private void CompleteEdit()
{
IsClosed = true;
Point point = new Point()
var point = new Point
{
Name = Name,
Description = Description,
Images = Images.Select(e => e.GetImage()).ToList(),
Equipment = Equipment.Select(e => e.GetEquipment()).ToList(),
Properties = Properties.Select(e => e.GetProperty()).ToList(),
Properties = Properties.Select(e => e.GetProperty()).ToList()
};
WeakReferenceMessenger.Default.Send(new EditPointDone(new PointViewModel(point)));
}
@@ -234,8 +231,8 @@ public partial class EditPointDialogViewModel : ViewModelBase
Value = "Property Value",
Properties = [],
Parent = null
},
],
}
],
Parent = null
},
new PropertyViewModel
+7 -7
View File
@@ -1,10 +1,10 @@
using CommunityToolkit.Mvvm.ComponentModel;
using GazpromApp.Messages;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using System.Collections.ObjectModel;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using Avalonia.Controls;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using GazpromApp.Messages;
using GazpromApp.Models;
namespace GazpromApp.ViewModels;
@@ -16,7 +16,7 @@ public partial class MainWindowViewModel : ViewModelBase
[RelayCommand]
private async Task AddPoint()
{
PointViewModel? point = await WeakReferenceMessenger.Default.Send(new NewPointMessage());
var point = await WeakReferenceMessenger.Default.Send(new NewPointMessage());
if (point != null)
Points.Add(point);
@@ -27,7 +27,7 @@ public partial class MainWindowViewModel : ViewModelBase
[RelayCommand]
private async Task EditPoint(PointViewModel point)
{
PointViewModel p = await WeakReferenceMessenger.Default.Send(new EditPointMessage(point)) ?? point;
var p = await WeakReferenceMessenger.Default.Send(new EditPointMessage(point)) ?? point;
Points[Points.IndexOf(point)] = p;
}
+7 -7
View File
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Web;
using Avalonia.Controls;
@@ -11,6 +10,7 @@ using CommunityToolkit.Mvvm.Messaging;
using GazpromApp.Enums;
using GazpromApp.Messages;
using GazpromApp.Models;
using Image = GazpromApp.Models.Image;
namespace GazpromApp.ViewModels;
@@ -30,26 +30,26 @@ public partial class NewEquipmentDialogViewModel : ViewModelBase
[RelayCommand]
private async Task AddImage(Window w)
{
IReadOnlyList<IStorageFile> files = await w.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions()
var files = await w.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions
{
AllowMultiple = false,
Title = "Выберите картинку",
FileTypeFilter = [FilePickerFileTypes.ImageAll],
FileTypeFilter = [FilePickerFileTypes.ImageAll]
});
IsImageSelected = files.Count > 0;
try
{
Image = IsImageSelected ? new ImageViewModel(new Models.Image()) { FileName = new Bitmap(HttpUtility.UrlDecode(files[0].Path.AbsolutePath)) } : null;
Image = IsImageSelected ? new ImageViewModel(new Image()) { FileName = new Bitmap(HttpUtility.UrlDecode(files[0].Path.AbsolutePath)) } : null;
}
catch (ArgumentException)
{
Error error = new Error()
var error = new Error
{
Title = "Ошибка обработки картинки",
Message = "Картинка не была добавлена",
Severity = ErrorSeverity.Error,
Severity = ErrorSeverity.Error
};
_ = await WeakReferenceMessenger.Default.Send(new ErrorMessage(new ErrorViewModel(error)));
@@ -69,7 +69,7 @@ public partial class NewEquipmentDialogViewModel : ViewModelBase
[RelayCommand (CanExecute = nameof(CanEnterEquipment))]
private void EnterEquipment()
{
Equipment equipment = new Equipment()
var equipment = new Equipment
{
Name = Name,
Description = Description ?? string.Empty,
+5 -7
View File
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
@@ -34,11 +32,11 @@ public partial class NewPointDialogViewModel : ViewModelBase
[RelayCommand]
private async Task AddImage(Window w)
{
IReadOnlyList<IStorageFile> files = await w.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions()
var files = await w.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions
{
AllowMultiple = true,
Title = "Выберите одну или несколько картинок",
FileTypeFilter = [FilePickerFileTypes.ImageAll],
FileTypeFilter = [FilePickerFileTypes.ImageAll]
});
foreach (var file in files)
{
@@ -48,11 +46,11 @@ public partial class NewPointDialogViewModel : ViewModelBase
}
catch (ArgumentException)
{
Error error = new Error()
var error = new Error
{
Title = "Ошибка обработки картинки",
Message = $"Картинка не была добавлена: \"{HttpUtility.UrlDecode(file.Name)}\"",
Severity = ErrorSeverity.Error,
Severity = ErrorSeverity.Error
};
_ = await WeakReferenceMessenger.Default.Send(new ErrorMessage(new ErrorViewModel(error)));
}
@@ -73,7 +71,7 @@ public partial class NewPointDialogViewModel : ViewModelBase
[RelayCommand (CanExecute = nameof(CanEnterPoint))]
private void EnterPoint()
{
Point point = new Point()
var point = new Point
{
Name = Name,
Description = Description,
+1 -3
View File
@@ -2,6 +2,4 @@
namespace GazpromApp.ViewModels;
public class ViewModelBase : ObservableObject
{
}
public class ViewModelBase : ObservableObject;