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

SuggestionManager.cs « Managers « UVtools.Core - github.com/sn4k3/UVtools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cd6b272ae2ba48317a0c221a0975b9d737b4b0b0 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 *                     GNU AFFERO GENERAL PUBLIC LICENSE
 *                       Version 3, 19 November 2007
 *  Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
 *  Everyone is permitted to copy and distribute verbatim copies
 *  of this license document, but changing it is not allowed.
 */
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Xml.Serialization;
using UVtools.Core;
using UVtools.Core.Extensions;
using UVtools.Core.Suggestions;

namespace UVtools.WPF.Structures;

[Serializable]
public class SuggestionManager
{
    #region Properties
    /// <summary>
    /// Default filepath for store
    /// </summary>
    public static string FilePath => Path.Combine(CoreSettings.DefaultSettingsFolderAndEnsureCreation, "suggestions.xml");

    public SuggestionBottomLayerCount BottomLayerCount { get; set; } = new();
    public SuggestionTransitionLayerCount TransitionLayerCount { get; set; } = new();
    public SuggestionWaitTimeBeforeCure WaitTimeBeforeCure { get; set; } = new();
    public SuggestionWaitTimeAfterCure WaitTimeAfterCure { get; set; } = new();
    public SuggestionLayerHeight LayerHeight { get; set; } = new();
    public SuggestionModelPosition ModelPosition { get; set; } = new();

    /// <summary>
    /// Gets all suggestions
    /// </summary>
    [XmlIgnore]
    public Suggestion[] Suggestions
    {
        get
        {
            return new Suggestion[]
            {
                BottomLayerCount,
                TransitionLayerCount,
                WaitTimeBeforeCure,
                WaitTimeAfterCure,
                LayerHeight,
                ModelPosition,
            };
        }
        set
        {
            foreach (var suggestion in value)
            {
                Set(suggestion);
            }

            Save();
        }
    }

    public void Set(Suggestion suggestion)
    {
        switch (suggestion)
        {
            case SuggestionBottomLayerCount bottomLayerCount:
                BottomLayerCount = bottomLayerCount;
                break;
            case SuggestionTransitionLayerCount transitionLayerCount:
                TransitionLayerCount = transitionLayerCount;
                break;
            case SuggestionWaitTimeBeforeCure waitTimeBeforeCure:
                WaitTimeBeforeCure = waitTimeBeforeCure;
                break;
            case SuggestionWaitTimeAfterCure waitTimeAfterCure:
                WaitTimeAfterCure = waitTimeAfterCure;
                break;
            case SuggestionLayerHeight layerHeight:
                LayerHeight = layerHeight;
                break;
            case SuggestionModelPosition modelPosition:
                ModelPosition = modelPosition;
                break;
            default: throw new ArgumentOutOfRangeException(nameof(suggestion));
        }
    }

    public Suggestion? Get(Type type)
    {
        return Suggestions.FirstOrDefault(suggestion => suggestion.GetType() == type);
    }

    #endregion

    #region Singleton

    private static Lazy<SuggestionManager> _instanceHolder =
        new(() => new SuggestionManager());

    /// <summary>
    /// Instance (singleton)
    /// </summary>
    public static SuggestionManager Instance => _instanceHolder.Value;

    //public static List<Operation> Operations => _instance.Operations;
    #endregion

    #region Constructor

    private SuggestionManager()
    { }

    #endregion

    #region Static Methods

    public static void SetSuggestion(Suggestion suggestion, bool save = false)
    {
        Instance.Set(suggestion);
        if(save) Save();
    }

    public static Suggestion? GetSuggestion(Type type)
    {
        return Instance.Get(type);
    }

    /// <summary>
    /// Reset to defaults
    /// </summary>
    /// <param name="save">True to save settings on file, otherwise false</param>
    public static void Reset(bool save = true)
    {
        _instanceHolder = new Lazy<SuggestionManager>(() => new SuggestionManager());
        if (save) Save();
    }


    /// <summary>
    /// Load settings from file
    /// </summary>
    public static void Load()
    {
        if (!File.Exists(FilePath))
        {
            return;
        }

        try
        {
            var instance = XmlExtensions.DeserializeFromFile<SuggestionManager>(FilePath);
            _instanceHolder = new Lazy<SuggestionManager>(() => instance);
        }
        catch (Exception e)
        {
            Debug.WriteLine(e.ToString());
        }
    }

    /// <summary>
    /// Save settings to file
    /// </summary>
    public static void Save()
    {
        try
        {
            XmlExtensions.SerializeToFile(Instance, FilePath, XmlExtensions.SettingsIndent);
        }
        catch (Exception e)
        {
            Debug.WriteLine(e.ToString());
        }
    }

    #endregion
}