Files
GazpromApp/ViewModels/MainWindowViewModel.cs
T
2025-07-10 10:57:42 +03:00

80 lines
2.2 KiB
C#

using System;
using System.IO;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.Web;
using Avalonia.Controls;
using Avalonia.Platform.Storage;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using GazpromApp.Messages;
using GazpromApp.Models;
using GazpromApp.Utils;
namespace GazpromApp.ViewModels;
public partial class MainWindowViewModel : ViewModelBase
{
public ObservableCollection<PointViewModel> Points { get; }
[RelayCommand]
private async Task AddPoint()
{
var point = await WeakReferenceMessenger.Default.Send(new NewPointMessage());
if (point != null)
Points.Add(point);
IsLineVisible = Points.Count > 0;
}
[RelayCommand]
private async Task EditPoint(PointViewModel point)
{
var p = await WeakReferenceMessenger.Default.Send(new EditPointMessage(point)) ?? point;
Points[Points.IndexOf(point)] = p;
}
[RelayCommand]
private void RemovePoint(PointViewModel point)
{
Points.Remove(point);
IsLineVisible = Points.Count > 0;
}
[RelayCommand]
private void ImportPoint(Window w)
{
}
[RelayCommand]
private async Task ExportPoint(Window w)
{
var folder = await w.StorageProvider.OpenFolderPickerAsync(new FolderPickerOpenOptions()
{
AllowMultiple = false,
SuggestedStartLocation =
await w.StorageProvider.TryGetFolderFromPathAsync(
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments))
});
foreach (var point in Points)
{
DataSerialization.SavePoint(point.GetPoint(), Path.Combine(HttpUtility.HtmlDecode(folder[0].Path.AbsolutePath), point.Name + ".json") );
}
}
// Visual properties
[ObservableProperty]
private bool _isLineVisible;
public MainWindowViewModel()
{
if (Design.IsDesignMode)
Points = [new PointViewModel(new Point()) { Name = "Name test", Description = "Description test" }];
else Points = [];
}
}