[ColorPicker]Store color history in a separated file (#23146)

* [ColorPicker] Store color history in a separated file

* ColorPicker] Separated file for color history: use the list from the settings.json if there is no ColorHistory.json file (preventing loss of existing color history)

* Fix case when there is no history saved or no settings file at all.
This commit is contained in:
Laszlo Nemeth
2023-06-13 11:10:03 +02:00
committed by GitHub
parent 0adda35b4b
commit 06e4518742
3 changed files with 25 additions and 11 deletions

View File

@@ -3,11 +3,12 @@
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.Composition;
using System.IO;
using System.IO.Abstractions;
using System.Linq;
using System.Text.Json;
using System.Threading;
using ColorPicker.Common;
using ManagedCommon;
@@ -23,6 +24,7 @@ namespace ColorPicker.Settings
{
private readonly ISettingsUtils _settingsUtils;
private const string ColorPickerModuleName = "ColorPicker";
private const string ColorPickerHistoryFilename = "colorHistory.json";
private const string DefaultActivationShortcut = "Ctrl + Break";
private const int MaxNumberOfRetry = 5;
private const int SettingsReadOnChangeDelayInMs = 300;
@@ -56,11 +58,7 @@ namespace ColorPicker.Settings
{
if (!_loadingColorsHistory)
{
var settings = _settingsUtils.GetSettingsOrDefault<ColorPickerSettings, ColorPickerSettingsVersion1>(ColorPickerModuleName, settingsUpgrader: ColorPickerSettings.UpgradeSettings);
ColorHistory.CollectionChanged -= ColorHistory_CollectionChanged;
settings.Properties.ColorHistory = ColorHistory.ToList();
ColorHistory.CollectionChanged += ColorHistory_CollectionChanged;
settings.Save(_settingsUtils);
_settingsUtils.SaveSettings(JsonSerializer.Serialize(ColorHistory, new JsonSerializerOptions { WriteIndented = true }), ColorPickerModuleName, ColorPickerHistoryFilename);
}
}
@@ -120,14 +118,31 @@ namespace ColorPicker.Settings
ColorHistoryLimit.Value = settings.Properties.ColorHistoryLimit;
ShowColorName.Value = settings.Properties.ShowColorName;
if (settings.Properties.ColorHistory == null)
List<string> savedColorHistory = new List<string>();
try
{
settings.Properties.ColorHistory = new System.Collections.Generic.List<string>();
string filePath = _settingsUtils.GetSettingsFilePath(ColorPickerModuleName, ColorPickerHistoryFilename);
if (!File.Exists(filePath))
{
if (settings.Properties.ColorHistory != null)
{
savedColorHistory = settings.Properties.ColorHistory;
}
}
else
{
string jsonSettingsString = System.IO.File.ReadAllText(filePath).Trim('\0');
savedColorHistory = JsonSerializer.Deserialize<List<string>>(jsonSettingsString);
}
}
catch (Exception)
{
Logger.LogInfo("ColorPicker colorHistory.json was missing or corrupt");
}
_loadingColorsHistory = true;
ColorHistory.Clear();
foreach (var item in settings.Properties.ColorHistory)
foreach (var item in savedColorHistory)
{
ColorHistory.Add(item);
}