mirror of
https://github.com/Artemis-RGB/Artemis
synced 2026-11-12 11:56:30 +00:00
Profiles - Dispose WIP
This commit is contained in:
1 parent
1f70c65651
commit
c0bdd8cf26
26 files changed
+246
-105
No files matched your search
@@ -7,19 +7,50 @@ namespace Artemis.Core.Services.Storage.Interfaces
|
||||
{
|
||||
public interface IProfileService : IArtemisService
|
||||
{
|
||||
void ActivateDefaultProfiles();
|
||||
Profile CreateProfile(ProfileModule module, string name);
|
||||
List<Profile> GetProfiles(ProfileModule module);
|
||||
Profile GetActiveProfile(ProfileModule module);
|
||||
/// <summary>
|
||||
/// Activates the last profile for each module
|
||||
/// </summary>
|
||||
void ActivateLastActiveProfiles();
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new profile for the given module and returns a descriptor pointing to it
|
||||
/// </summary>
|
||||
/// <param name="module">The profile module to create the profile for</param>
|
||||
/// <param name="name">The name of the new profile</param>
|
||||
/// <returns></returns>
|
||||
ProfileDescriptor CreateProfile(ProfileModule module, string name);
|
||||
|
||||
/// <summary>
|
||||
/// Gets a descriptor for each profile stored for the given <see cref="ProfileModule" />
|
||||
/// </summary>
|
||||
/// <param name="module">The module to return profile descriptors for</param>
|
||||
/// <returns></returns>
|
||||
List<ProfileDescriptor> GetProfiles(ProfileModule module);
|
||||
|
||||
/// <summary>
|
||||
/// Writes the profile to persistent storage
|
||||
/// </summary>
|
||||
/// <param name="profile"></param>
|
||||
/// <param name="includeChildren"></param>
|
||||
void UpdateProfile(Profile profile, bool includeChildren);
|
||||
|
||||
/// <summary>
|
||||
/// Disposes and permanently deletes the provided profile
|
||||
/// </summary>
|
||||
/// <param name="profile">The profile to delete</param>
|
||||
void DeleteProfile(Profile profile);
|
||||
|
||||
/// <summary>
|
||||
/// Activates the profile for the given <see cref="ProfileModule" /> with the currently active surface
|
||||
/// Activates the profile described in the given <see cref="ProfileDescriptor" /> with the currently active surface
|
||||
/// </summary>
|
||||
/// <param name="module">The module to activate the profile for</param>
|
||||
/// <param name="profile">The profile to activate</param>
|
||||
void ActivateProfile(ProfileModule module, Profile profile);
|
||||
/// <param name="profileDescriptor">The descriptor describing the profile to activate</param>
|
||||
Profile ActivateProfile(ProfileDescriptor profileDescriptor);
|
||||
|
||||
/// <summary>
|
||||
/// Clears the active profile on the given <see cref="ProfileModule" />
|
||||
/// </summary>
|
||||
/// <param name="module">The profile module to deactivate the active profile on</param>
|
||||
void ClearActiveProfile(ProfileModule module);
|
||||
|
||||
/// <summary>
|
||||
/// Attempts to restore the profile to the state it had before the last <see cref="UpdateProfile" /> call.
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Artemis.Core.Events;
|
||||
using Artemis.Core.Models.Profile;
|
||||
@@ -41,70 +42,58 @@ namespace Artemis.Core.Services.Storage
|
||||
|
||||
public JsonSerializerSettings MementoSettings { get; set; } = new JsonSerializerSettings {TypeNameHandling = TypeNameHandling.All};
|
||||
|
||||
public void ActivateDefaultProfiles()
|
||||
public void ActivateLastActiveProfiles()
|
||||
{
|
||||
foreach (var profileModule in _pluginService.GetPluginsOfType<ProfileModule>())
|
||||
{
|
||||
var activeProfile = GetActiveProfile(profileModule);
|
||||
ActivateProfile(profileModule, activeProfile);
|
||||
var activeProfile = GetLastActiveProfile(profileModule);
|
||||
ActivateProfile(activeProfile);
|
||||
}
|
||||
}
|
||||
|
||||
public List<Profile> GetProfiles(ProfileModule module)
|
||||
public List<ProfileDescriptor> GetProfiles(ProfileModule module)
|
||||
{
|
||||
var profileEntities = _profileRepository.GetByPluginGuid(module.PluginInfo.Guid);
|
||||
var profiles = new List<Profile>();
|
||||
foreach (var profileEntity in profileEntities)
|
||||
{
|
||||
// If the profile entity matches the module's currently active profile, use that instead
|
||||
if (module.ActiveProfile != null && module.ActiveProfile.EntityId == profileEntity.Id)
|
||||
profiles.Add(module.ActiveProfile);
|
||||
else
|
||||
profiles.Add(new Profile(module.PluginInfo, profileEntity));
|
||||
}
|
||||
|
||||
return profiles;
|
||||
return profileEntities.Select(e => new ProfileDescriptor(module, e)).ToList();
|
||||
}
|
||||
|
||||
public Profile GetActiveProfile(ProfileModule module)
|
||||
public ProfileDescriptor GetLastActiveProfile(ProfileModule module)
|
||||
{
|
||||
if (module.ActiveProfile != null)
|
||||
return module.ActiveProfile;
|
||||
|
||||
var moduleProfiles = _profileRepository.GetByPluginGuid(module.PluginInfo.Guid);
|
||||
var profileEntity = moduleProfiles.FirstOrDefault(p => p.IsActive) ?? moduleProfiles.FirstOrDefault();
|
||||
if (profileEntity == null)
|
||||
return null;
|
||||
|
||||
return new Profile(module.PluginInfo, profileEntity);
|
||||
return profileEntity == null ? null : new ProfileDescriptor(module, profileEntity);
|
||||
}
|
||||
|
||||
public Profile CreateProfile(ProfileModule module, string name)
|
||||
public ProfileDescriptor CreateProfile(ProfileModule module, string name)
|
||||
{
|
||||
var profile = new Profile(module.PluginInfo, name);
|
||||
_profileRepository.Add(profile.ProfileEntity);
|
||||
var profileEntity = new ProfileEntity {Id = Guid.NewGuid(), Name = name, PluginGuid = module.PluginInfo.Guid};
|
||||
return new ProfileDescriptor(module, profileEntity);
|
||||
}
|
||||
|
||||
if (_surfaceService.ActiveSurface != null)
|
||||
profile.PopulateLeds(_surfaceService.ActiveSurface);
|
||||
public Profile ActivateProfile(ProfileDescriptor profileDescriptor)
|
||||
{
|
||||
if (profileDescriptor.ProfileModule.ActiveProfile.EntityId == profileDescriptor.Id)
|
||||
return profileDescriptor.ProfileModule.ActiveProfile;
|
||||
|
||||
var profile = new Profile(profileDescriptor.ProfileModule, profileDescriptor.ProfileEntity);
|
||||
InitializeLayerProperties(profile);
|
||||
InstantiateLayers(profile);
|
||||
InstantiateFolders(profile);
|
||||
|
||||
profileDescriptor.ProfileModule.ChangeActiveProfile(profile, _surfaceService.ActiveSurface);
|
||||
return profile;
|
||||
}
|
||||
|
||||
|
||||
public void ActivateProfile(ProfileModule module, Profile profile)
|
||||
public void ClearActiveProfile(ProfileModule module)
|
||||
{
|
||||
module.ChangeActiveProfile(profile, _surfaceService.ActiveSurface);
|
||||
if (profile != null)
|
||||
{
|
||||
InitializeLayerProperties(profile);
|
||||
InstantiateLayers(profile);
|
||||
InstantiateFolders(profile);
|
||||
}
|
||||
module.ChangeActiveProfile(null, _surfaceService.ActiveSurface);
|
||||
}
|
||||
|
||||
public void DeleteProfile(Profile profile)
|
||||
{
|
||||
_logger.Debug("Removing profile " + profile);
|
||||
|
||||
|
||||
_profileRepository.Remove(profile.ProfileEntity);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user