Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MaterialManagerWindow.axaml.cs « Windows « UVtools.WPF - github.com/sn4k3/UVtools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a519d34b8f30156f42a57514a71ecb121799ae45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System;
using System.Linq;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using DynamicData;
using MessageBox.Avalonia.Enums;
using UVtools.Core.Managers;
using UVtools.Core.Objects;
using UVtools.WPF.Controls;
using UVtools.WPF.Extensions;

namespace UVtools.WPF.Windows
{
    public class MaterialManagerWindow : WindowEx
    {
        private Material _material = new();
        private readonly DataGrid _grid;
        public MaterialManager Manager => MaterialManager.Instance;

        public Material Material
        {
            get => _material;
            set => RaiseAndSetIfChanged(ref _material, value);
        }

        public MaterialManagerWindow()
        {
            InitializeComponent();
#if DEBUG
            this.AttachDevTools();
#endif

            _grid = this.FindControl<DataGrid>("MaterialsTable");

            MaterialManager.Load(); // Reload

            DataContext = this;
        }

        private void InitializeComponent()
        {
            AvaloniaXamlLoader.Load(this);
        }

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
            MaterialManager.Save(); // Apply changes
        }

        public void RefreshStatistics()
        {
            Manager.RaisePropertiesChanged();
        }

        public async void AddNewMaterial()
        {
            if (string.IsNullOrWhiteSpace(Material.Name))
            {
                await this.MessageBoxError("Material name can't be empty");
                return;
            }

            if (Manager.Contains(Material))
            {
                await this.MessageBoxError("A material with same name already exists.");
                return;
            }

            Material.BottleRemainingVolume = Material.BottleVolume;

            if (await this.MessageBoxQuestion("Are you sure you want to add the following material:\n" +
                                             $"{Material}") != ButtonResult.Yes) return;

            Manager.Add(Material);
            Manager.SortByName();
            MaterialManager.Save();
            Material = new();
        }

        public async void RemoveSelectedMaterials()
        {
            if (_grid.SelectedItems.Count <= 0) return;
            if (await this.MessageBoxQuestion($"Are you sure you want to remove {_grid.SelectedItems.Count} materials?") != ButtonResult.Yes) return;
            Manager.RemoveMany(_grid.SelectedItems.Cast<Material>());
            MaterialManager.Save();
        }

        public async void ClearMaterials()
        {
            if (Manager.Count == 0) return;
            if (await this.MessageBoxQuestion($"Are you sure you want to clear {Manager.Count} materials?") != ButtonResult.Yes) return;
            Manager.Clear(true);
        }
    }
}