mirror of
https://github.com/Artemis-RGB/Artemis
synced 2025-12-13 05:48:35 +00:00
Fixed build
Added PluginSetting, settings service needs some more thought
This commit is contained in:
parent
bfc320c0ac
commit
99d15ad6ac
66
src/Artemis.Core/Plugins/Models/PluginSetting.cs
Normal file
66
src/Artemis.Core/Plugins/Models/PluginSetting.cs
Normal file
@ -0,0 +1,66 @@
|
||||
using System.Threading.Tasks;
|
||||
using Artemis.Storage.Entities;
|
||||
using Artemis.Storage.Repositories;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Artemis.Core.Plugins.Models
|
||||
{
|
||||
public class PluginSetting<T>
|
||||
{
|
||||
private readonly PluginInfo _pluginInfo;
|
||||
private readonly SettingEntity _settingEntity;
|
||||
private readonly SettingRepository _settingRepository;
|
||||
|
||||
internal PluginSetting(PluginInfo pluginInfo, SettingRepository settingRepository, SettingEntity settingEntity)
|
||||
{
|
||||
_pluginInfo = pluginInfo;
|
||||
_settingRepository = settingRepository;
|
||||
_settingEntity = settingEntity;
|
||||
|
||||
Name = settingEntity.Name;
|
||||
Value = JsonConvert.DeserializeObject<T>(settingEntity.Value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The name of the setting, unique to this plugin
|
||||
/// </summary>
|
||||
public string Name { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The value of the setting
|
||||
/// </summary>
|
||||
public T Value { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether the setting has been changed
|
||||
/// </summary>
|
||||
public bool HasChanged => JsonConvert.SerializeObject(Value) != _settingEntity.Value;
|
||||
|
||||
/// <summary>
|
||||
/// Resets the setting to the last saved value
|
||||
/// </summary>
|
||||
public void RejectChanges()
|
||||
{
|
||||
Value = JsonConvert.DeserializeObject<T>(_settingEntity.Value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves the setting
|
||||
/// </summary>
|
||||
public void Save()
|
||||
{
|
||||
_settingEntity.Value = JsonConvert.SerializeObject(Value);
|
||||
_settingRepository.Save();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves the setting asynchronously
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task SaveAsync()
|
||||
{
|
||||
_settingEntity.Value = JsonConvert.SerializeObject(Value);
|
||||
await _settingRepository.SaveAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,26 +0,0 @@
|
||||
using Artemis.Storage.Repositories;
|
||||
|
||||
namespace Artemis.Core.Plugins.Models
|
||||
{
|
||||
public class PluginSettings
|
||||
{
|
||||
private readonly PluginInfo _pluginInfo;
|
||||
private readonly SettingRepository _settingRepository;
|
||||
|
||||
internal PluginSettings(PluginInfo pluginInfo, SettingRepository settingRepository)
|
||||
{
|
||||
_pluginInfo = pluginInfo;
|
||||
_settingRepository = settingRepository;
|
||||
}
|
||||
|
||||
public bool HasSettingChanged(string settingName)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool HasAnySettingChanged()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
90
src/Artemis.Core/Plugins/Models/PluginSettingsContainer.cs
Normal file
90
src/Artemis.Core/Plugins/Models/PluginSettingsContainer.cs
Normal file
@ -0,0 +1,90 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Artemis.Storage.Entities;
|
||||
using Artemis.Storage.Repositories;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Artemis.Core.Plugins.Models
|
||||
{
|
||||
public class PluginSettingsContainer
|
||||
{
|
||||
private readonly PluginInfo _pluginInfo;
|
||||
private readonly SettingRepository _settingRepository;
|
||||
private Task<List<SettingEntity>> _settings;
|
||||
|
||||
internal PluginSettingsContainer(PluginInfo pluginInfo, SettingRepository settingRepository)
|
||||
{
|
||||
_pluginInfo = pluginInfo;
|
||||
_settingRepository = settingRepository;
|
||||
}
|
||||
|
||||
public bool HasSettingChanged(string settingName)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool HasAnySettingChanged()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public class PluginSetting<T>
|
||||
{
|
||||
private readonly PluginInfo _pluginInfo;
|
||||
private readonly SettingEntity _settingEntity;
|
||||
private readonly SettingRepository _settingRepository;
|
||||
|
||||
internal PluginSetting(PluginInfo pluginInfo, SettingRepository settingRepository, SettingEntity settingEntity)
|
||||
{
|
||||
_pluginInfo = pluginInfo;
|
||||
_settingRepository = settingRepository;
|
||||
_settingEntity = settingEntity;
|
||||
|
||||
Name = settingEntity.Name;
|
||||
Value = JsonConvert.DeserializeObject<T>(settingEntity.Value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The name of the setting, unique to this plugin
|
||||
/// </summary>
|
||||
public string Name { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The value of the setting
|
||||
/// </summary>
|
||||
public T Value { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether the setting has been changed
|
||||
/// </summary>
|
||||
public bool HasChanged => JsonConvert.SerializeObject(Value) != _settingEntity.Value;
|
||||
|
||||
/// <summary>
|
||||
/// Resets the setting to the last saved value
|
||||
/// </summary>
|
||||
public void RejectChanges()
|
||||
{
|
||||
Value = JsonConvert.DeserializeObject<T>(_settingEntity.Value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves the setting
|
||||
/// </summary>
|
||||
public void Save()
|
||||
{
|
||||
_settingEntity.Value = JsonConvert.SerializeObject(Value);
|
||||
_settingRepository.Save();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves the setting asynchronously
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task SaveAsync()
|
||||
{
|
||||
_settingEntity.Value = JsonConvert.SerializeObject(Value);
|
||||
await _settingRepository.SaveAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -147,7 +147,7 @@ namespace Artemis.Core.Services
|
||||
try
|
||||
{
|
||||
var constructorArguments = new ConstructorArgument("pluginInfo", pluginInfo);
|
||||
pluginInfo.Instance = (Plugin) _childKernel.Get(pluginType, constraint: null, constructorArguments);
|
||||
pluginInfo.Instance = (Plugin) _childKernel.Get(pluginType, constraint: null, parameters: constructorArguments);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
||||
@ -4,6 +4,7 @@ using Artemis.Storage.Repositories;
|
||||
|
||||
namespace Artemis.Core.Services
|
||||
{
|
||||
// TODO: Rethink this :')
|
||||
public class SettingsService : ISettingsService
|
||||
{
|
||||
private SettingRepository _settingRepository;
|
||||
@ -13,9 +14,9 @@ namespace Artemis.Core.Services
|
||||
_settingRepository = new SettingRepository();
|
||||
}
|
||||
|
||||
public PluginSettings GetPluginSettings(PluginInfo pluginInfo)
|
||||
public PluginSettingsContainer GetPluginSettings(PluginInfo pluginInfo)
|
||||
{
|
||||
return new PluginSettings(pluginInfo, _settingRepository);
|
||||
return new PluginSettingsContainer(pluginInfo, _settingRepository);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -21,7 +21,12 @@ namespace Artemis.Storage.Repositories
|
||||
return _dbContext.Settings;
|
||||
}
|
||||
|
||||
public async Task<List<SettingEntity>> GetByPluginGuid(Guid pluginGuid)
|
||||
public List<SettingEntity> GetByPluginGuid(Guid pluginGuid)
|
||||
{
|
||||
return _dbContext.Settings.Where(p => p.PluginGuid == pluginGuid).ToList();
|
||||
}
|
||||
|
||||
public async Task<List<SettingEntity>> GetByPluginGuidAsync(Guid pluginGuid)
|
||||
{
|
||||
return await _dbContext.Settings.Where(p => p.PluginGuid == pluginGuid).ToListAsync();
|
||||
}
|
||||
@ -36,6 +41,11 @@ namespace Artemis.Storage.Repositories
|
||||
return await _dbContext.Settings.FirstOrDefaultAsync(p => p.Name == name);
|
||||
}
|
||||
|
||||
public void Save()
|
||||
{
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
|
||||
public async Task SaveAsync()
|
||||
{
|
||||
await _dbContext.SaveChangesAsync();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user