mirror of
https://github.com/Artemis-RGB/Artemis
synced 2025-12-13 05:48:35 +00:00
Made SurfaceService solely responsible for keeping the surface and the config in sync and saved
This commit is contained in:
parent
49e6dbf09b
commit
4add877156
@ -64,7 +64,7 @@ namespace Artemis.Core.Models.Surface
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Applies the configuration to the device
|
/// Applies the configuration to the device
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void ApplyToDevice()
|
internal void ApplyToDevice()
|
||||||
{
|
{
|
||||||
if (Device != null)
|
if (Device != null)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -1,5 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
using Artemis.Core.Events;
|
using Artemis.Core.Events;
|
||||||
using Artemis.Core.Models.Surface;
|
using Artemis.Core.Models.Surface;
|
||||||
@ -10,9 +9,9 @@ namespace Artemis.Core.Services.Storage
|
|||||||
public interface ISurfaceService : IArtemisService
|
public interface ISurfaceService : IArtemisService
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the currently active surface configuration
|
/// Gets the currently active surface configuration, to change config use <see cref="SetActiveSurfaceConfiguration" />
|
||||||
/// </summary>
|
/// </summary>
|
||||||
SurfaceConfiguration ActiveSurfaceConfiguration { get; set; }
|
SurfaceConfiguration ActiveSurfaceConfiguration { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets a read-only list of all surface configurations
|
/// Gets a read-only list of all surface configurations
|
||||||
@ -26,26 +25,25 @@ namespace Artemis.Core.Services.Storage
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
SurfaceConfiguration CreateSurfaceConfiguration(string name);
|
SurfaceConfiguration CreateSurfaceConfiguration(string name);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sets the provided configuration as active and applies it to the surface
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="surfaceConfiguration">The configuration to activate and apply</param>
|
||||||
|
void SetActiveSurfaceConfiguration(SurfaceConfiguration surfaceConfiguration);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Saves the provided surface configuration to permanent storage and if config is active, applies it to the surface
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="surfaceConfiguration">The configuration to save (and apply if active)</param>
|
||||||
|
/// <param name="includeDevices">Whether to also save devices. If false, devices changes won't be applied either</param>
|
||||||
|
void UpdateSurfaceConfiguration(SurfaceConfiguration surfaceConfiguration, bool includeDevices);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Deletes the supplied surface configuration, surface configuration may not be the active surface configuration
|
/// Deletes the supplied surface configuration, surface configuration may not be the active surface configuration
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="surfaceConfiguration">The surface configuration to delete, may not be the active surface configuration</param>
|
/// <param name="surfaceConfiguration">The surface configuration to delete, may not be the active surface configuration</param>
|
||||||
void DeleteSurfaceConfiguration(SurfaceConfiguration surfaceConfiguration);
|
void DeleteSurfaceConfiguration(SurfaceConfiguration surfaceConfiguration);
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Saves the provided surface configurations to permanent storage
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="surfaceConfigurations">The configurations to save</param>
|
|
||||||
/// <param name="includeDevices">Whether to also save devices</param>
|
|
||||||
void SaveToRepository(List<SurfaceConfiguration> surfaceConfigurations, bool includeDevices);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Saves the provided surface configuration to permanent storage
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="surfaceConfiguration">The configuration to save</param>
|
|
||||||
/// <param name="includeDevices">Whether to also save devices</param>
|
|
||||||
void SaveToRepository(SurfaceConfiguration surfaceConfiguration, bool includeDevices);
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Occurs when the active device configuration has been changed
|
/// Occurs when the active device configuration has been changed
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@ -18,7 +18,6 @@ namespace Artemis.Core.Services.Storage
|
|||||||
private readonly IRgbService _rgbService;
|
private readonly IRgbService _rgbService;
|
||||||
private readonly List<SurfaceConfiguration> _surfaceConfigurations;
|
private readonly List<SurfaceConfiguration> _surfaceConfigurations;
|
||||||
private readonly ISurfaceRepository _surfaceRepository;
|
private readonly ISurfaceRepository _surfaceRepository;
|
||||||
private SurfaceConfiguration _activeSurfaceConfiguration;
|
|
||||||
|
|
||||||
public SurfaceService(ILogger logger, ISurfaceRepository surfaceRepository, IRgbService rgbService)
|
public SurfaceService(ILogger logger, ISurfaceRepository surfaceRepository, IRgbService rgbService)
|
||||||
{
|
{
|
||||||
@ -32,37 +31,7 @@ namespace Artemis.Core.Services.Storage
|
|||||||
_rgbService.DeviceLoaded += RgbServiceOnDeviceLoaded;
|
_rgbService.DeviceLoaded += RgbServiceOnDeviceLoaded;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SurfaceConfiguration ActiveSurfaceConfiguration
|
public SurfaceConfiguration ActiveSurfaceConfiguration { get; private set; }
|
||||||
{
|
|
||||||
get => _activeSurfaceConfiguration;
|
|
||||||
set
|
|
||||||
{
|
|
||||||
if (_activeSurfaceConfiguration == value)
|
|
||||||
return;
|
|
||||||
|
|
||||||
_activeSurfaceConfiguration = value;
|
|
||||||
lock (_surfaceConfigurations)
|
|
||||||
{
|
|
||||||
// Mark only the new value as active
|
|
||||||
foreach (var surfaceConfiguration in _surfaceConfigurations)
|
|
||||||
surfaceConfiguration.IsActive = false;
|
|
||||||
_activeSurfaceConfiguration.IsActive = true;
|
|
||||||
|
|
||||||
SaveToRepository(_surfaceConfigurations, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Apply the active surface configuration to the devices
|
|
||||||
if (ActiveSurfaceConfiguration != null)
|
|
||||||
{
|
|
||||||
foreach (var deviceConfiguration in ActiveSurfaceConfiguration.DeviceConfigurations)
|
|
||||||
deviceConfiguration.ApplyToDevice();
|
|
||||||
}
|
|
||||||
// Update the RGB service's graphics decorator to work with the new surface configuration
|
|
||||||
_rgbService.UpdateGraphicsDecorator();
|
|
||||||
|
|
||||||
OnActiveSurfaceConfigurationChanged(new SurfaceConfigurationEventArgs(_activeSurfaceConfiguration));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public ReadOnlyCollection<SurfaceConfiguration> SurfaceConfigurations
|
public ReadOnlyCollection<SurfaceConfiguration> SurfaceConfigurations
|
||||||
{
|
{
|
||||||
@ -90,11 +59,62 @@ namespace Artemis.Core.Services.Storage
|
|||||||
lock (_surfaceConfigurations)
|
lock (_surfaceConfigurations)
|
||||||
{
|
{
|
||||||
_surfaceRepository.Add(configuration.SurfaceEntity);
|
_surfaceRepository.Add(configuration.SurfaceEntity);
|
||||||
SaveToRepository(configuration, true);
|
UpdateSurfaceConfiguration(configuration, true);
|
||||||
return configuration;
|
return configuration;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void SetActiveSurfaceConfiguration(SurfaceConfiguration surfaceConfiguration)
|
||||||
|
{
|
||||||
|
if (ActiveSurfaceConfiguration == surfaceConfiguration)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Set the new configuration
|
||||||
|
ActiveSurfaceConfiguration = surfaceConfiguration;
|
||||||
|
|
||||||
|
// Ensure only the new configuration is marked as active
|
||||||
|
lock (_surfaceConfigurations)
|
||||||
|
{
|
||||||
|
// Mark only the new surfaceConfiguration as active
|
||||||
|
foreach (var configuration in _surfaceConfigurations)
|
||||||
|
{
|
||||||
|
configuration.IsActive = configuration == ActiveSurfaceConfiguration;
|
||||||
|
configuration.ApplyToEntity();
|
||||||
|
}
|
||||||
|
|
||||||
|
_surfaceRepository.Save();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply the active surface configuration to the devices
|
||||||
|
if (ActiveSurfaceConfiguration != null)
|
||||||
|
{
|
||||||
|
foreach (var deviceConfiguration in ActiveSurfaceConfiguration.DeviceConfigurations)
|
||||||
|
deviceConfiguration.ApplyToDevice();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update the RGB service's graphics decorator to work with the new surface configuration
|
||||||
|
_rgbService.UpdateGraphicsDecorator();
|
||||||
|
OnActiveSurfaceConfigurationChanged(new SurfaceConfigurationEventArgs(ActiveSurfaceConfiguration));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UpdateSurfaceConfiguration(SurfaceConfiguration surfaceConfiguration, bool includeDevices)
|
||||||
|
{
|
||||||
|
surfaceConfiguration.ApplyToEntity();
|
||||||
|
if (includeDevices)
|
||||||
|
{
|
||||||
|
foreach (var deviceConfiguration in surfaceConfiguration.DeviceConfigurations)
|
||||||
|
{
|
||||||
|
deviceConfiguration.ApplyToEntity();
|
||||||
|
if (surfaceConfiguration.IsActive)
|
||||||
|
deviceConfiguration.ApplyToDevice();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_surfaceRepository.Save();
|
||||||
|
_rgbService.UpdateGraphicsDecorator();
|
||||||
|
OnSurfaceConfigurationUpdated(new SurfaceConfigurationEventArgs(surfaceConfiguration));
|
||||||
|
}
|
||||||
|
|
||||||
public void DeleteSurfaceConfiguration(SurfaceConfiguration surfaceConfiguration)
|
public void DeleteSurfaceConfiguration(SurfaceConfiguration surfaceConfiguration)
|
||||||
{
|
{
|
||||||
if (surfaceConfiguration == ActiveSurfaceConfiguration)
|
if (surfaceConfiguration == ActiveSurfaceConfiguration)
|
||||||
@ -152,33 +172,6 @@ namespace Artemis.Core.Services.Storage
|
|||||||
ActiveSurfaceConfiguration = active;
|
ActiveSurfaceConfiguration = active;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SaveToRepository(List<SurfaceConfiguration> surfaceConfigurations, bool includeDevices)
|
|
||||||
{
|
|
||||||
foreach (var surfaceConfiguration in surfaceConfigurations)
|
|
||||||
{
|
|
||||||
surfaceConfiguration.ApplyToEntity();
|
|
||||||
if (!includeDevices)
|
|
||||||
{
|
|
||||||
foreach (var deviceConfiguration in surfaceConfiguration.DeviceConfigurations)
|
|
||||||
deviceConfiguration.ApplyToEntity();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_surfaceRepository.Save();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void SaveToRepository(SurfaceConfiguration surfaceConfiguration, bool includeDevices)
|
|
||||||
{
|
|
||||||
surfaceConfiguration.ApplyToEntity();
|
|
||||||
if (includeDevices)
|
|
||||||
{
|
|
||||||
foreach (var deviceConfiguration in surfaceConfiguration.DeviceConfigurations)
|
|
||||||
deviceConfiguration.ApplyToEntity();
|
|
||||||
}
|
|
||||||
|
|
||||||
_surfaceRepository.Save();
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Utilities
|
#region Utilities
|
||||||
@ -217,12 +210,18 @@ namespace Artemis.Core.Services.Storage
|
|||||||
#region Events
|
#region Events
|
||||||
|
|
||||||
public event EventHandler<SurfaceConfigurationEventArgs> ActiveSurfaceConfigurationChanged;
|
public event EventHandler<SurfaceConfigurationEventArgs> ActiveSurfaceConfigurationChanged;
|
||||||
|
public event EventHandler<SurfaceConfigurationEventArgs> SurfaceConfigurationUpdated;
|
||||||
|
|
||||||
protected virtual void OnActiveSurfaceConfigurationChanged(SurfaceConfigurationEventArgs e)
|
protected virtual void OnActiveSurfaceConfigurationChanged(SurfaceConfigurationEventArgs e)
|
||||||
{
|
{
|
||||||
ActiveSurfaceConfigurationChanged?.Invoke(this, e);
|
ActiveSurfaceConfigurationChanged?.Invoke(this, e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected virtual void OnSurfaceConfigurationUpdated(SurfaceConfigurationEventArgs e)
|
||||||
|
{
|
||||||
|
SurfaceConfigurationUpdated?.Invoke(this, e);
|
||||||
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -152,6 +152,11 @@
|
|||||||
<Compile Include="Extensions\RgbColorExtensions.cs" />
|
<Compile Include="Extensions\RgbColorExtensions.cs" />
|
||||||
<Compile Include="Extensions\RgbRectangleExtensions.cs" />
|
<Compile Include="Extensions\RgbRectangleExtensions.cs" />
|
||||||
<Compile Include="Ninject\UIModule.cs" />
|
<Compile Include="Ninject\UIModule.cs" />
|
||||||
|
<Compile Include="Properties\Resources.Designer.cs">
|
||||||
|
<AutoGen>True</AutoGen>
|
||||||
|
<DesignTime>True</DesignTime>
|
||||||
|
<DependentUpon>Resources.resx</DependentUpon>
|
||||||
|
</Compile>
|
||||||
<Compile Include="Services\Interfaces\IArtemisUIService.cs" />
|
<Compile Include="Services\Interfaces\IArtemisUIService.cs" />
|
||||||
<Compile Include="Stylet\ArtemisViewManager.cs" />
|
<Compile Include="Stylet\ArtemisViewManager.cs" />
|
||||||
<Compile Include="Stylet\NinjectBootstrapper.cs" />
|
<Compile Include="Stylet\NinjectBootstrapper.cs" />
|
||||||
@ -214,11 +219,6 @@
|
|||||||
<Compile Include="Properties\AssemblyInfo.cs">
|
<Compile Include="Properties\AssemblyInfo.cs">
|
||||||
<SubType>Code</SubType>
|
<SubType>Code</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="Properties\Resources.Designer.cs">
|
|
||||||
<AutoGen>True</AutoGen>
|
|
||||||
<DesignTime>True</DesignTime>
|
|
||||||
<DependentUpon>Resources.resx</DependentUpon>
|
|
||||||
</Compile>
|
|
||||||
<Compile Include="Properties\Settings.Designer.cs">
|
<Compile Include="Properties\Settings.Designer.cs">
|
||||||
<AutoGen>True</AutoGen>
|
<AutoGen>True</AutoGen>
|
||||||
<DependentUpon>Settings.settings</DependentUpon>
|
<DependentUpon>Settings.settings</DependentUpon>
|
||||||
@ -226,6 +226,7 @@
|
|||||||
</Compile>
|
</Compile>
|
||||||
<EmbeddedResource Include="Properties\Resources.resx">
|
<EmbeddedResource Include="Properties\Resources.resx">
|
||||||
<Generator>ResXFileCodeGenerator</Generator>
|
<Generator>ResXFileCodeGenerator</Generator>
|
||||||
|
<SubType>Designer</SubType>
|
||||||
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
|
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
|
||||||
</EmbeddedResource>
|
</EmbeddedResource>
|
||||||
<None Include="packages.config" />
|
<None Include="packages.config" />
|
||||||
|
|||||||
@ -70,7 +70,7 @@ namespace Artemis.UI.ViewModels.Screens
|
|||||||
if (activeConfig == null)
|
if (activeConfig == null)
|
||||||
{
|
{
|
||||||
activeConfig = AddSurfaceConfiguration("Default");
|
activeConfig = AddSurfaceConfiguration("Default");
|
||||||
_surfaceService.ActiveSurfaceConfiguration = activeConfig;
|
_surfaceService.SetActiveSurfaceConfiguration(activeConfig);
|
||||||
}
|
}
|
||||||
|
|
||||||
Execute.OnUIThread(() =>
|
Execute.OnUIThread(() =>
|
||||||
@ -106,7 +106,7 @@ namespace Artemis.UI.ViewModels.Screens
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
_surfaceService.ActiveSurfaceConfiguration = SelectedSurfaceConfiguration;
|
_surfaceService.SetActiveSurfaceConfiguration(SelectedSurfaceConfiguration);
|
||||||
}
|
}
|
||||||
|
|
||||||
#region Overrides of Screen
|
#region Overrides of Screen
|
||||||
@ -275,9 +275,7 @@ namespace Artemis.UI.ViewModels.Screens
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
foreach (var device in Devices)
|
_surfaceService.UpdateSurfaceConfiguration(SelectedSurfaceConfiguration, true);
|
||||||
device.DeviceConfiguration.ApplyToDevice();
|
|
||||||
_surfaceService.SaveToRepository(SelectedSurfaceConfiguration, true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Mouse.OverrideCursor = null;
|
Mouse.OverrideCursor = null;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user