mirror of
https://github.com/Artemis-RGB/Artemis
synced 2026-11-12 11:56:30 +00:00
Core - Nullable refactoring
Core - Nullable refactoring Core - Nullable refactoring (finish)
This commit is contained in:
1 parent
b185b28645
commit
fb3466e102
61 files changed
+320
-272
No files matched your search
@@ -36,6 +36,40 @@ namespace Artemis.Core.Services
|
||||
public JsonSerializerSettings MementoSettings { get; set; } = new JsonSerializerSettings {TypeNameHandling = TypeNameHandling.All};
|
||||
public JsonSerializerSettings ExportSettings { get; set; } = new JsonSerializerSettings {TypeNameHandling = TypeNameHandling.All, Formatting = Formatting.Indented};
|
||||
|
||||
public ProfileDescriptor? GetLastActiveProfile(ProfileModule module)
|
||||
{
|
||||
List<ProfileEntity> moduleProfiles = _profileRepository.GetByModuleId(module.Id);
|
||||
if (!moduleProfiles.Any())
|
||||
return CreateProfileDescriptor(module, "Default");
|
||||
|
||||
ProfileEntity? profileEntity = moduleProfiles.FirstOrDefault(p => p.IsActive) ?? moduleProfiles.FirstOrDefault();
|
||||
return profileEntity == null ? null : new ProfileDescriptor(module, profileEntity);
|
||||
}
|
||||
|
||||
private void SaveActiveProfile(ProfileModule module)
|
||||
{
|
||||
if (module.ActiveProfile == null)
|
||||
return;
|
||||
|
||||
List<ProfileEntity> profileEntities = _profileRepository.GetByModuleId(module.Id);
|
||||
foreach (ProfileEntity profileEntity in profileEntities)
|
||||
{
|
||||
profileEntity.IsActive = module.ActiveProfile.EntityId == profileEntity.Id;
|
||||
_profileRepository.Save(profileEntity);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Populates all missing LEDs on all currently active profiles
|
||||
/// </summary>
|
||||
/// <param name="surface"></param>
|
||||
private void ActiveProfilesPopulateLeds(ArtemisSurface surface)
|
||||
{
|
||||
List<ProfileModule> profileModules = _pluginManagementService.GetFeaturesOfType<ProfileModule>();
|
||||
foreach (ProfileModule profileModule in profileModules.Where(p => p.ActiveProfile != null).ToList())
|
||||
profileModule.ActiveProfile?.PopulateLeds(surface); // Avoid race condition
|
||||
}
|
||||
|
||||
public List<ProfileDescriptor> GetProfileDescriptors(ProfileModule module)
|
||||
{
|
||||
List<ProfileEntity> profileEntities = _profileRepository.GetByModuleId(module.Id);
|
||||
@@ -52,14 +86,14 @@ namespace Artemis.Core.Services
|
||||
|
||||
public void ActivateLastProfile(ProfileModule profileModule)
|
||||
{
|
||||
ProfileDescriptor activeProfile = GetLastActiveProfile(profileModule);
|
||||
ProfileDescriptor? activeProfile = GetLastActiveProfile(profileModule);
|
||||
if (activeProfile != null)
|
||||
ActivateProfile(activeProfile);
|
||||
}
|
||||
|
||||
public async Task ActivateLastProfileAnimated(ProfileModule profileModule)
|
||||
{
|
||||
ProfileDescriptor activeProfile = GetLastActiveProfile(profileModule);
|
||||
ProfileDescriptor? activeProfile = GetLastActiveProfile(profileModule);
|
||||
if (activeProfile != null)
|
||||
await ActivateProfileAnimated(activeProfile);
|
||||
}
|
||||
@@ -196,7 +230,8 @@ namespace Artemis.Core.Services
|
||||
string top = profile.UndoStack.Pop();
|
||||
string memento = JsonConvert.SerializeObject(profile.ProfileEntity, MementoSettings);
|
||||
profile.RedoStack.Push(memento);
|
||||
profile.ProfileEntity = JsonConvert.DeserializeObject<ProfileEntity>(top, MementoSettings);
|
||||
profile.ProfileEntity = JsonConvert.DeserializeObject<ProfileEntity>(top, MementoSettings)
|
||||
?? throw new InvalidOperationException("Failed to deserialize memento");
|
||||
|
||||
profile.Load();
|
||||
InstantiateProfile(profile);
|
||||
@@ -220,7 +255,8 @@ namespace Artemis.Core.Services
|
||||
string top = profile.RedoStack.Pop();
|
||||
string memento = JsonConvert.SerializeObject(profile.ProfileEntity, MementoSettings);
|
||||
profile.UndoStack.Push(memento);
|
||||
profile.ProfileEntity = JsonConvert.DeserializeObject<ProfileEntity>(top, MementoSettings);
|
||||
profile.ProfileEntity = JsonConvert.DeserializeObject<ProfileEntity>(top, MementoSettings)
|
||||
?? throw new InvalidOperationException("Failed to deserialize memento");
|
||||
|
||||
profile.Load();
|
||||
InstantiateProfile(profile);
|
||||
@@ -246,7 +282,9 @@ namespace Artemis.Core.Services
|
||||
|
||||
public ProfileDescriptor ImportProfile(string json, ProfileModule profileModule)
|
||||
{
|
||||
ProfileEntity profileEntity = JsonConvert.DeserializeObject<ProfileEntity>(json, ExportSettings);
|
||||
ProfileEntity? profileEntity = JsonConvert.DeserializeObject<ProfileEntity>(json, ExportSettings);
|
||||
if (profileEntity == null)
|
||||
throw new ArtemisCoreException("Failed to import profile but JSON.NET threw no error :(");
|
||||
|
||||
// Assign a new GUID to make sure it is unique in case of a previous import of the same content
|
||||
profileEntity.UpdateGuid(Guid.NewGuid());
|
||||
@@ -256,40 +294,6 @@ namespace Artemis.Core.Services
|
||||
return new ProfileDescriptor(profileModule, profileEntity);
|
||||
}
|
||||
|
||||
public ProfileDescriptor GetLastActiveProfile(ProfileModule module)
|
||||
{
|
||||
List<ProfileEntity> moduleProfiles = _profileRepository.GetByModuleId(module.Id);
|
||||
if (!moduleProfiles.Any())
|
||||
return CreateProfileDescriptor(module, "Default");
|
||||
|
||||
ProfileEntity profileEntity = moduleProfiles.FirstOrDefault(p => p.IsActive) ?? moduleProfiles.FirstOrDefault();
|
||||
return profileEntity == null ? null : new ProfileDescriptor(module, profileEntity);
|
||||
}
|
||||
|
||||
private void SaveActiveProfile(ProfileModule module)
|
||||
{
|
||||
if (module.ActiveProfile == null)
|
||||
return;
|
||||
|
||||
List<ProfileEntity> profileEntities = _profileRepository.GetByModuleId(module.Id);
|
||||
foreach (ProfileEntity profileEntity in profileEntities)
|
||||
{
|
||||
profileEntity.IsActive = module.ActiveProfile.EntityId == profileEntity.Id;
|
||||
_profileRepository.Save(profileEntity);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Populates all missing LEDs on all currently active profiles
|
||||
/// </summary>
|
||||
/// <param name="surface"></param>
|
||||
private void ActiveProfilesPopulateLeds(ArtemisSurface surface)
|
||||
{
|
||||
List<ProfileModule> profileModules = _pluginManagementService.GetFeaturesOfType<ProfileModule>();
|
||||
foreach (ProfileModule profileModule in profileModules.Where(p => p.ActiveProfile != null).ToList())
|
||||
profileModule.ActiveProfile.PopulateLeds(surface);
|
||||
}
|
||||
|
||||
#region Event handlers
|
||||
|
||||
private void OnActiveSurfaceConfigurationSelected(object? sender, SurfaceConfigurationEventArgs e)
|
||||
|
||||
@@ -28,6 +28,8 @@ namespace Artemis.Core.Services
|
||||
_surfaceConfigurations = new List<ArtemisSurface>();
|
||||
_renderScaleSetting = settingsService.GetSetting("Core.RenderScale", 0.5);
|
||||
|
||||
// LoadFromRepository is guaranteed to set the ActiveSurface
|
||||
ActiveSurface = null!;
|
||||
LoadFromRepository();
|
||||
|
||||
_rgbService.DeviceLoaded += RgbServiceOnDeviceLoaded;
|
||||
@@ -61,6 +63,7 @@ namespace Artemis.Core.Services
|
||||
|
||||
public void SetActiveSurfaceConfiguration(ArtemisSurface surface)
|
||||
{
|
||||
if (surface == null) throw new ArgumentNullException(nameof(surface));
|
||||
if (ActiveSurface == surface)
|
||||
return;
|
||||
|
||||
@@ -81,11 +84,8 @@ namespace Artemis.Core.Services
|
||||
}
|
||||
|
||||
// Apply the active surface entity to the devices
|
||||
if (ActiveSurface != null)
|
||||
{
|
||||
foreach (ArtemisDevice device in ActiveSurface.Devices)
|
||||
device.ApplyToRgbDevice();
|
||||
}
|
||||
foreach (ArtemisDevice device in ActiveSurface.Devices)
|
||||
device.ApplyToRgbDevice();
|
||||
|
||||
// Update the RGB service's graphics decorator to work with the new surface entity
|
||||
_rgbService.UpdateSurfaceLedGroup();
|
||||
@@ -134,7 +134,7 @@ namespace Artemis.Core.Services
|
||||
ArtemisSurface surfaceConfiguration = new ArtemisSurface(_rgbService.Surface, surfaceEntity, _renderScaleSetting.Value);
|
||||
foreach (DeviceEntity position in surfaceEntity.DeviceEntities)
|
||||
{
|
||||
IRGBDevice device = _rgbService.Surface.Devices.FirstOrDefault(d => d.GetDeviceIdentifier() == position.DeviceIdentifier);
|
||||
IRGBDevice? device = _rgbService.Surface.Devices.FirstOrDefault(d => d.GetDeviceIdentifier() == position.DeviceIdentifier);
|
||||
if (device != null)
|
||||
{
|
||||
DeviceProvider deviceProvider = _pluginManagementService.GetDeviceProviderByDevice(device);
|
||||
@@ -150,7 +150,7 @@ namespace Artemis.Core.Services
|
||||
}
|
||||
|
||||
// When all surface configs are loaded, apply the active surface config
|
||||
ArtemisSurface active = SurfaceConfigurations.FirstOrDefault(c => c.IsActive);
|
||||
ArtemisSurface? active = SurfaceConfigurations.FirstOrDefault(c => c.IsActive);
|
||||
if (active != null)
|
||||
SetActiveSurfaceConfiguration(active);
|
||||
else
|
||||
@@ -170,13 +170,13 @@ namespace Artemis.Core.Services
|
||||
private void AddDeviceIfMissing(IRGBDevice rgbDevice, ArtemisSurface surface)
|
||||
{
|
||||
string deviceIdentifier = rgbDevice.GetDeviceIdentifier();
|
||||
ArtemisDevice device = surface.Devices.FirstOrDefault(d => d.DeviceEntity.DeviceIdentifier == deviceIdentifier);
|
||||
ArtemisDevice? device = surface.Devices.FirstOrDefault(d => d.DeviceEntity.DeviceIdentifier == deviceIdentifier);
|
||||
|
||||
if (device != null)
|
||||
return;
|
||||
|
||||
// Find an existing device config and use that
|
||||
DeviceEntity existingDeviceConfig = surface.SurfaceEntity.DeviceEntities.FirstOrDefault(d => d.DeviceIdentifier == deviceIdentifier);
|
||||
DeviceEntity? existingDeviceConfig = surface.SurfaceEntity.DeviceEntities.FirstOrDefault(d => d.DeviceIdentifier == deviceIdentifier);
|
||||
if (existingDeviceConfig != null)
|
||||
{
|
||||
DeviceProvider deviceProvider = _pluginManagementService.GetDeviceProviderByDevice(rgbDevice);
|
||||
@@ -225,8 +225,8 @@ namespace Artemis.Core.Services
|
||||
|
||||
#region Events
|
||||
|
||||
public event EventHandler<SurfaceConfigurationEventArgs> ActiveSurfaceConfigurationSelected;
|
||||
public event EventHandler<SurfaceConfigurationEventArgs> SurfaceConfigurationUpdated;
|
||||
public event EventHandler<SurfaceConfigurationEventArgs>? ActiveSurfaceConfigurationSelected;
|
||||
public event EventHandler<SurfaceConfigurationEventArgs>? SurfaceConfigurationUpdated;
|
||||
|
||||
protected virtual void OnActiveSurfaceConfigurationChanged(SurfaceConfigurationEventArgs e)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user