Profile modules - Added option to provide default profiles

Profiles - Run adaption on freshly imported profiles when activating
Profiles - Run adaption on freshly imported profiles when surface changes
This commit is contained in:
Robert committed 2021-05-15 22:41:15 +02:00
1 parent c7c78dfecc
commit 14f82284ac
7 files changed
+121 -53

No files matched your search

+17 -2
View File
@@ -6,6 +6,7 @@ using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using Artemis.Core.Modules;
using Artemis.Storage.Entities.Profile;
using Artemis.Storage.Repositories.Interfaces;
using Serilog;
using Timer = System.Timers.Timer;
@@ -17,13 +18,15 @@ namespace Artemis.Core.Services
private static readonly SemaphoreSlim ActiveModuleSemaphore = new(1, 1);
private readonly ILogger _logger;
private readonly IModuleRepository _moduleRepository;
private readonly IProfileRepository _profileRepository;
private readonly IPluginManagementService _pluginManagementService;
private readonly IProfileService _profileService;
public ModuleService(ILogger logger, IModuleRepository moduleRepository, IPluginManagementService pluginManagementService, IProfileService profileService)
public ModuleService(ILogger logger, IModuleRepository moduleRepository, IProfileRepository profileRepository, IPluginManagementService pluginManagementService, IProfileService profileService)
{
_logger = logger;
_moduleRepository = moduleRepository;
_profileRepository = profileRepository;
_pluginManagementService = pluginManagementService;
_profileService = profileService;
_pluginManagementService.PluginFeatureEnabled += OnPluginFeatureEnabled;
@@ -45,12 +48,24 @@ namespace Artemis.Core.Services
{
try
{
ProfileModule? profileModule = module as ProfileModule;
if (profileModule != null && profileModule.DefaultProfiles.Any())
{
List<ProfileDescriptor> descriptors = _profileService.GetProfileDescriptors(profileModule);
foreach (ProfileEntity defaultProfile in profileModule.DefaultProfiles)
{
if (descriptors.All(d => d.Id != defaultProfile.Id))
_profileRepository.Add(defaultProfile);
}
}
module.Activate(false);
try
{
// If this is a profile module, activate the last active profile after module activation
if (module is ProfileModule profileModule)
if (profileModule != null)
await _profileService.ActivateLastProfileAnimated(profileModule);
}
catch (Exception e)
@@ -32,8 +32,8 @@ namespace Artemis.Core.Services
_rgbService.LedsChanged += RgbServiceOnLedsChanged;
}
public JsonSerializerSettings MementoSettings { get; set; } = new() {TypeNameHandling = TypeNameHandling.All};
public JsonSerializerSettings ExportSettings { get; set; } = new() {TypeNameHandling = TypeNameHandling.All, Formatting = Formatting.Indented};
public static JsonSerializerSettings MementoSettings { get; set; } = new() {TypeNameHandling = TypeNameHandling.All};
public static JsonSerializerSettings ExportSettings { get; set; } = new() {TypeNameHandling = TypeNameHandling.All, Formatting = Formatting.Indented};
public ProfileDescriptor? GetLastActiveProfile(ProfileModule module)
{
@@ -64,8 +64,16 @@ namespace Artemis.Core.Services
private void ActiveProfilesPopulateLeds()
{
List<ProfileModule> profileModules = _pluginManagementService.GetFeaturesOfType<ProfileModule>();
foreach (ProfileModule profileModule in profileModules.Where(p => p.ActiveProfile != null).ToList())
profileModule.ActiveProfile?.PopulateLeds(_rgbService.EnabledDevices); // Avoid race condition
foreach (ProfileModule profileModule in profileModules)
{
// Avoid race condition, make the check here
if (profileModule.ActiveProfile != null)
{
profileModule.ActiveProfile.PopulateLeds(_rgbService.EnabledDevices);
_logger.Debug("Profile is a fresh import, adapting to surface - {profile}", profileModule.ActiveProfile);
AdaptProfile(profileModule.ActiveProfile);
}
}
}
public List<ProfileDescriptor> GetProfileDescriptors(ProfileModule module)
@@ -107,13 +115,14 @@ namespace Artemis.Core.Services
Profile profile = new(profileDescriptor.ProfileModule, profileEntity);
InstantiateProfile(profile);
if (profileDescriptor.NeedsAdaption)
{
AdaptProfile(profile);
profileDescriptor.NeedsAdaption = false;
}
profileDescriptor.ProfileModule.ChangeActiveProfile(profile, _rgbService.EnabledDevices);
if (profile.IsFreshImport)
{
_logger.Debug("Profile is a fresh import, adapting to surface - {profile}", profile);
AdaptProfile(profile);
}
SaveActiveProfile(profileDescriptor.ProfileModule);
return profile;
@@ -143,7 +152,7 @@ namespace Artemis.Core.Services
Profile profile = new(profileDescriptor.ProfileModule, profileEntity);
InstantiateProfile(profile);
void ActivatingRgbServiceOnLedsChanged(object? sender, EventArgs e)
{
profile.PopulateLeds(_rgbService.EnabledDevices);
@@ -161,6 +170,12 @@ namespace Artemis.Core.Services
_rgbService.LedsChanged += ActivatingRgbServiceOnLedsChanged;
await profileDescriptor.ProfileModule.ChangeActiveProfileAnimated(profile, _rgbService.EnabledDevices);
if (profile.IsFreshImport)
{
_logger.Debug("Profile is a fresh import, adapting to surface - {profile}", profile);
AdaptProfile(profile);
}
SaveActiveProfile(profileDescriptor.ProfileModule);
_pluginManagementService.PluginEnabled -= ActivatingProfilePluginToggle;
@@ -198,6 +213,8 @@ namespace Artemis.Core.Services
public void DeleteProfile(ProfileDescriptor profileDescriptor)
{
ProfileEntity profileEntity = _profileRepository.Get(profileDescriptor.Id);
if (profileEntity == null)
return;
_profileRepository.Remove(profileEntity);
}
@@ -208,6 +225,7 @@ namespace Artemis.Core.Services
profile.RedoStack.Clear();
profile.UndoStack.Push(memento);
profile.IsFreshImport = false;
profile.Save();
if (includeChildren)
{
@@ -294,8 +312,9 @@ namespace Artemis.Core.Services
profileEntity.UpdateGuid(Guid.NewGuid());
profileEntity.Name = $"{profileEntity.Name} - {nameAffix}";
profileEntity.IsFreshImport = true;
_profileRepository.Add(profileEntity);
return new ProfileDescriptor(profileModule, profileEntity) {NeedsAdaption = true};
return new ProfileDescriptor(profileModule, profileEntity);
}
/// <inheritdoc />