mirror of
https://github.com/Artemis-RGB/Artemis
synced 2026-11-11 19:36:28 +00:00
Cleaned up setting service, lol old approach was dumb
Made PluginSetting<T> act as a singleton
This commit is contained in:
1 parent
05eed5ba00
commit
54fcbdb732
6 files changed
+70
-55
No files matched your search
@@ -8,12 +8,13 @@ using Stylet;
|
||||
|
||||
namespace Artemis.Core.Plugins.Models
|
||||
{
|
||||
public class PluginSetting<T> : PropertyChangedBase
|
||||
public class PluginSetting<T>
|
||||
{
|
||||
// ReSharper disable once NotAccessedField.Local
|
||||
private readonly PluginInfo _pluginInfo;
|
||||
private readonly PluginSettingEntity _pluginSettingEntity;
|
||||
private readonly IPluginSettingRepository _pluginSettingRepository;
|
||||
private T _value;
|
||||
|
||||
internal PluginSetting(PluginInfo pluginInfo, IPluginSettingRepository pluginSettingRepository, PluginSettingEntity pluginSettingEntity)
|
||||
{
|
||||
@@ -23,15 +24,6 @@ namespace Artemis.Core.Plugins.Models
|
||||
|
||||
Name = pluginSettingEntity.Name;
|
||||
Value = JsonConvert.DeserializeObject<T>(pluginSettingEntity.Value);
|
||||
|
||||
// PropertyChanged is for bindings, but we can use it here to create a easy to use SettingsChanged event
|
||||
PropertyChanged += OnPropertyChanged;
|
||||
}
|
||||
|
||||
private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
|
||||
{
|
||||
if (e.PropertyName == nameof(Value))
|
||||
OnSettingChanged();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -42,7 +34,15 @@ namespace Artemis.Core.Plugins.Models
|
||||
/// <summary>
|
||||
/// The value of the setting
|
||||
/// </summary>
|
||||
public T Value { get; set; }
|
||||
public T Value
|
||||
{
|
||||
get => _value;
|
||||
set
|
||||
{
|
||||
_value = value;
|
||||
OnSettingChanged();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether the setting has been changed
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Artemis.Storage.Entities;
|
||||
using Artemis.Storage.Repositories.Interfaces;
|
||||
using Newtonsoft.Json;
|
||||
@@ -14,13 +13,13 @@ namespace Artemis.Core.Plugins.Models
|
||||
{
|
||||
private readonly PluginInfo _pluginInfo;
|
||||
private readonly IPluginSettingRepository _pluginSettingRepository;
|
||||
private readonly Dictionary<string, PluginSettingEntity> _settingEntities;
|
||||
private readonly Dictionary<string, object> _settingEntities;
|
||||
|
||||
internal PluginSettings(PluginInfo pluginInfo, IPluginSettingRepository pluginSettingRepository)
|
||||
{
|
||||
_pluginInfo = pluginInfo;
|
||||
_pluginSettingRepository = pluginSettingRepository;
|
||||
_settingEntities = pluginSettingRepository.GetByPluginGuid(_pluginInfo.Guid).ToDictionary(se => se.Name);
|
||||
_settingEntities = new Dictionary<string, object>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -34,15 +33,22 @@ namespace Artemis.Core.Plugins.Models
|
||||
{
|
||||
lock (_settingEntities)
|
||||
{
|
||||
// Return cached value if available
|
||||
if (_settingEntities.ContainsKey(name))
|
||||
return new PluginSetting<T>(_pluginInfo, _pluginSettingRepository, _settingEntities[name]);
|
||||
return (PluginSetting<T>) _settingEntities[name];
|
||||
// Try to find in database
|
||||
var settingEntity = _pluginSettingRepository.GetByNameAndPluginGuid(name, _pluginInfo.Guid);
|
||||
// If not found, create a new one
|
||||
if (settingEntity == null)
|
||||
{
|
||||
settingEntity = new PluginSettingEntity {Name = name, PluginGuid = _pluginInfo.Guid, Value = JsonConvert.SerializeObject(defaultValue)};
|
||||
_pluginSettingRepository.Add(settingEntity);
|
||||
_pluginSettingRepository.Save();
|
||||
}
|
||||
|
||||
var settingEntity = new PluginSettingEntity {Name = name, PluginGuid = _pluginInfo.Guid, Value = JsonConvert.SerializeObject(defaultValue)};
|
||||
_pluginSettingRepository.Add(settingEntity);
|
||||
_pluginSettingRepository.Save();
|
||||
|
||||
_settingEntities.Add(name, settingEntity);
|
||||
return new PluginSetting<T>(_pluginInfo, _pluginSettingRepository, _settingEntities[name]);
|
||||
var pluginSetting = new PluginSetting<T>(_pluginInfo, _pluginSettingRepository, settingEntity);
|
||||
_settingEntities.Add(name, pluginSetting);
|
||||
return pluginSetting;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user