defautl setttings generating fix

This commit is contained in:
Alex Z 2020-03-16 01:11:09 +03:00
parent cf4f1e9281
commit ca9bda7a57
4 changed files with 79 additions and 44 deletions

View File

@ -5,7 +5,6 @@ namespace UniversalValveToolbox.Model.Dto {
private string lastSelectedProject;
private string[] availableLanguages;
private string language;
private string theme;
public string LastSelectedProject {
get => lastSelectedProject;

View File

@ -1,6 +1,8 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Runtime.CompilerServices;
using System.Windows.Forms;
@ -11,27 +13,71 @@ using UniversalValveToolbox.Utils;
namespace UniversalValveToolbox.Model.Provider {
class DataProvider {
public static readonly string SettingsPath = Path.Combine(Application.StartupPath, "json", "settings.json");
public static readonly string EnginesPath = Path.Combine(Application.StartupPath, "json", "engines");
public static readonly string ProjectsPath = Path.Combine(Application.StartupPath, "json", "projects");
public static readonly string AddonsPath = Path.Combine(Application.StartupPath, "json", "addons");
public static readonly string JsonRootPath = Path.Combine(Application.StartupPath, "json");
public static readonly string SettingsPath = Path.Combine(JsonRootPath, "settings.json");
public static readonly string EnginesPath = Path.Combine(JsonRootPath, "engines");
public static readonly string ProjectsPath = Path.Combine(JsonRootPath, "projects");
public static readonly string AddonsPath = Path.Combine(JsonRootPath, "addons");
public static readonly SettingsDtoModel DefaultSettings = GenerateDefaultSettings();
public SettingsDtoModel Settings {
get => JsonFileUtil.ReadValue<SettingsDtoModel>(SettingsPath);
set => JsonFileUtil.WriteValue(SettingsPath, value);
get {
validateJsonFolder();
return JsonFileUtil.ReadValue<SettingsDtoModel>(SettingsPath, DefaultSettings);
}
set {
validateJsonFolder();
JsonFileUtil.WriteValue(SettingsPath, value);
}
}
public EngineDtoModel[] Engines {
get => JsonFileUtil.ReadValues<EngineDtoModel>(EnginesPath);
get {
validateJsonFolder();
return JsonFileUtil.ReadValues<EngineDtoModel>(EnginesPath);
}
}
public AddonDtoModel[] Addons {
get => JsonFileUtil.ReadValues<AddonDtoModel>(AddonsPath);
get {
validateJsonFolder();
return JsonFileUtil.ReadValues<AddonDtoModel>(AddonsPath);
}
}
public ProjectDtoModel[] Projects {
get => JsonFileUtil.ReadValues<ProjectDtoModel>(ProjectsPath);
get {
validateJsonFolder();
return JsonFileUtil.ReadValues<ProjectDtoModel>(ProjectsPath);
}
}
private void validateJsonFolder() {
if (!Directory.Exists(JsonRootPath)) {
Directory.CreateDirectory(JsonRootPath);
}
}
private static SettingsDtoModel GenerateDefaultSettings() {
var result = new SettingsDtoModel();
result.Language = "en-US"; //may be fix in future
var listLang = new List<string>();
var resourceSet = Properties.translations.LangDict.ResourceManager.GetResourceSet(new CultureInfo("en-US"), true, true);
foreach (DictionaryEntry entry in resourceSet) {
listLang.Add(entry.Key.ToString());
}
result.AvailableLanguages = listLang.ToArray();
return result;
}
}
}

View File

@ -29,8 +29,6 @@
this.buttonOK = new System.Windows.Forms.Button();
this.label5 = new System.Windows.Forms.Label();
this.comboBoxLang = new System.Windows.Forms.ComboBox();
this.comboBoxTheme = new System.Windows.Forms.ComboBox();
this.label6 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// labelDivider
@ -38,7 +36,7 @@
this.labelDivider.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.labelDivider.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.labelDivider.Location = new System.Drawing.Point(-21, 272);
this.labelDivider.Location = new System.Drawing.Point(-21, 114);
this.labelDivider.Name = "labelDivider";
this.labelDivider.Size = new System.Drawing.Size(385, 2);
this.labelDivider.TabIndex = 26;
@ -46,7 +44,7 @@
// buttonCancel
//
this.buttonCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonCancel.Location = new System.Drawing.Point(224, 284);
this.buttonCancel.Location = new System.Drawing.Point(224, 126);
this.buttonCancel.Name = "buttonCancel";
this.buttonCancel.Size = new System.Drawing.Size(75, 23);
this.buttonCancel.TabIndex = 25;
@ -58,7 +56,7 @@
//
this.buttonOK.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonOK.DialogResult = System.Windows.Forms.DialogResult.OK;
this.buttonOK.Location = new System.Drawing.Point(143, 284);
this.buttonOK.Location = new System.Drawing.Point(143, 126);
this.buttonOK.Name = "buttonOK";
this.buttonOK.Size = new System.Drawing.Size(75, 23);
this.buttonOK.TabIndex = 24;
@ -87,35 +85,11 @@
this.comboBoxLang.Size = new System.Drawing.Size(287, 21);
this.comboBoxLang.TabIndex = 30;
//
// comboBoxTheme
//
this.comboBoxTheme.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.comboBoxTheme.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBoxTheme.FormattingEnabled = true;
this.comboBoxTheme.Items.AddRange(new object[] {
"None"});
this.comboBoxTheme.Location = new System.Drawing.Point(12, 65);
this.comboBoxTheme.Name = "comboBoxTheme";
this.comboBoxTheme.Size = new System.Drawing.Size(287, 21);
this.comboBoxTheme.TabIndex = 32;
//
// label6
//
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(12, 49);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(43, 13);
this.label6.TabIndex = 31;
this.label6.Text = "Theme:";
//
// FormSettings
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(311, 319);
this.Controls.Add(this.comboBoxTheme);
this.Controls.Add(this.label6);
this.ClientSize = new System.Drawing.Size(311, 161);
this.Controls.Add(this.comboBoxLang);
this.Controls.Add(this.label5);
this.Controls.Add(this.labelDivider);
@ -141,7 +115,5 @@
private System.Windows.Forms.Button buttonOK;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.ComboBox comboBoxLang;
private System.Windows.Forms.ComboBox comboBoxTheme;
private System.Windows.Forms.Label label6;
}
}

View File

@ -8,14 +8,32 @@ using System.Threading.Tasks;
namespace UniversalValveToolbox.Utils {
static class JsonFileUtil {
public static T ReadValue<T>(string path) => JsonConvert.DeserializeObject<T>(File.ReadAllText(path));
public static T ReadValue<T>(string path) {
try {
var result = JsonConvert.DeserializeObject<T>(File.ReadAllText(path));
return result;
} catch (Exception) {
return default(T);
}
}
public static T ReadValue<T>(string path, T fileDefaultValue) {
try {
var result = JsonConvert.DeserializeObject<T>(File.ReadAllText(path));
return result;
} catch (Exception) {
WriteValue(path, fileDefaultValue);
return fileDefaultValue;
}
}
public static T[] ReadValues<T>(string directoryPath) {
if (!Directory.Exists(directoryPath)) {
Directory.CreateDirectory(directoryPath);
}
return Directory.GetFiles(directoryPath, "*").Select(path => ReadValue<T>(path)).ToArray();
return Directory.GetFiles(directoryPath, "*").Select(path => ReadValue<T>(path)).Where(value => value != null).ToArray();
}
public static List<T> ReadListValues<T>(string directoryPath) => new List<T>(ReadValues<T>(directoryPath));