1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00

Implemented profile creation, deletion and activation

This commit is contained in:
Robert 2019-11-20 19:25:29 +01:00
parent 66f7dc94c8
commit 97907c97eb
17 changed files with 217 additions and 49 deletions

View File

@ -8,7 +8,7 @@ using Artemis.Storage.Entities.Profile;
namespace Artemis.Core.Models.Profile
{
public class Folder : ProfileElement
public sealed class Folder : ProfileElement
{
public Folder(Profile profile, Folder folder, string name)
{

View File

@ -8,7 +8,7 @@ using Artemis.Storage.Entities.Profile;
namespace Artemis.Core.Models.Profile
{
public class Layer : ProfileElement
public sealed class Layer : ProfileElement
{
internal Layer(Profile profile, Folder folder, string name)
{

View File

@ -10,7 +10,7 @@ using Artemis.Storage.Entities.Profile;
namespace Artemis.Core.Models.Profile
{
public class Profile : ProfileElement
public sealed class Profile : ProfileElement
{
internal Profile(PluginInfo pluginInfo, string name)
{
@ -21,6 +21,8 @@ namespace Artemis.Core.Models.Profile
Name = name;
Children = new List<ProfileElement> {new Folder(this, null, "Root folder")};
ApplyToEntity();
}
internal Profile(PluginInfo pluginInfo, ProfileEntity profileEntity, IPluginService pluginService)

View File

@ -71,8 +71,7 @@ namespace Artemis.Core.Models.Surface
internal void ApplyToEntity()
{
// Other properties are mapped computed
// Other properties are computed
DeviceEntity.DeviceHashCode = RgbDevice.GetDeviceHashCode();
}
@ -102,13 +101,6 @@ namespace Artemis.Core.Models.Surface
RenderPath = path;
}
internal void Destroy()
{
DeviceEntity = null;
RgbDevice = null;
Surface = null;
}
public override string ToString()
{
return $"[{RgbDevice.DeviceInfo.DeviceType}] {RgbDevice.DeviceInfo.DeviceName} - {X}.{Y}.{ZIndex}";

View File

@ -61,15 +61,6 @@ namespace Artemis.Core.Models.Surface
}
}
internal void Destroy()
{
SurfaceEntity = null;
foreach (var deviceConfiguration in Devices)
deviceConfiguration.Destroy();
Devices.Clear();
}
public void UpdateScale(double value)
{
Scale = value;

View File

@ -38,13 +38,13 @@ namespace Artemis.Core.Plugins.Abstract
{
lock (this)
{
if (profile == null)
throw new ArgumentNullException(nameof(profile));
if (profile == ActiveProfile)
return;
ActiveProfile?.Deactivate();
ActiveProfile = profile;
ActiveProfile.Activate();
ActiveProfile?.Activate();
}
OnActiveProfileChanged();

View File

@ -7,7 +7,10 @@ namespace Artemis.Core.Services.Storage.Interfaces
{
public interface IProfileService : IArtemisService
{
Profile CreateProfile(ProfileModule module, string name);
List<Profile> GetProfiles(ProfileModule module);
Profile GetActiveProfile(ProfileModule module);
void UpdateProfile(Profile profile, bool includeChildren);
void DeleteProfile(Profile profile);
}
}

View File

@ -1,11 +1,9 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Artemis.Core.Models.Profile;
using Artemis.Core.Plugins.Abstract;
using Artemis.Core.Services.Interfaces;
using Artemis.Core.Services.Storage.Interfaces;
using Artemis.Storage.Repositories;
using Artemis.Storage.Repositories.Interfaces;
namespace Artemis.Core.Services.Storage
@ -29,13 +27,22 @@ namespace Artemis.Core.Services.Storage
var profileEntities = _profileRepository.GetByPluginGuid(module.PluginInfo.Guid);
var profiles = new List<Profile>();
foreach (var profileEntity in profileEntities)
profiles.Add(new Profile(module.PluginInfo, profileEntity, _pluginService));
{
// 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, _pluginService));
}
return profiles;
}
public Profile GetActiveProfile(ProfileModule module)
{
if (module.ActiveProfile != null)
return module.ActiveProfile;
var profileEntity = _profileRepository.GetByPluginGuid(module.PluginInfo.Guid).FirstOrDefault(p => p.IsActive);
if (profileEntity == null)
return null;
@ -46,20 +53,26 @@ namespace Artemis.Core.Services.Storage
public Profile CreateProfile(ProfileModule module, string name)
{
var profile = new Profile(module.PluginInfo, name);
_profileRepository.Add(profile.ProfileEntity);
return profile;
}
public void DeleteProfile(Profile profile)
{
_profileRepository.Remove(profile.ProfileEntity);
}
public void UpdateProfile(Profile profile, bool includeChildren)
{
profile.ApplyToEntity();
if (includeChildren)
{
foreach (var profileElement in profile.Children)
{
profileElement.ApplyToEntity();
}
}
_profileRepository.Save(profile.ProfileEntity);
}
}
}

View File

@ -123,8 +123,6 @@ namespace Artemis.Core.Services.Storage
lock (_surfaceConfigurations)
{
var entity = surface.SurfaceEntity;
surface.Destroy();
_surfaceConfigurations.Remove(surface);
_surfaceRepository.Remove(entity);
}

View File

@ -5,6 +5,12 @@ namespace Artemis.Storage.Entities.Profile
{
public class ProfileEntity
{
public ProfileEntity()
{
Folders = new List<FolderEntity>();
Layers = new List<LayerEntity>();
}
public Guid Id { get; set; }
public Guid PluginGuid { get; set; }

View File

@ -159,6 +159,8 @@
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="Screens\Module\ProfileEditor\Dialogs\ProfileCreateViewModel.cs" />
<Compile Include="Screens\Module\ProfileEditor\Dialogs\SurfaceCreateViewModelValidator.cs" />
<Compile Include="Screens\Module\ProfileEditor\DisplayConditions\DisplayConditionsViewModel.cs" />
<Compile Include="Screens\Module\ProfileEditor\DisplayConditions\DisplayConditionViewModel.cs" />
<Compile Include="Screens\Module\ProfileEditor\ElementProperties\ElementPropertiesViewModel.cs" />
@ -200,6 +202,10 @@
<SubType>Code</SubType>
</Compile>
<Compile Include="Screens\Settings\SettingsViewModel.cs" />
<Page Include="Screens\Module\ProfileEditor\Dialogs\ProfileCreateView.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="Screens\Module\ProfileEditor\DisplayConditions\DisplayConditionsView.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>

View File

@ -0,0 +1,37 @@
<UserControl x:Class="Artemis.UI.Screens.Module.ProfileEditor.Dialogs.ProfileCreateView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:s="https://github.com/canton7/Stylet"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
mc:Ignorable="d"
d:DesignHeight="213.053" d:DesignWidth="254.425">
<StackPanel Margin="16">
<TextBlock Style="{StaticResource MaterialDesignTitleTextBlock}">
Add a new profile
</TextBlock>
<TextBox materialDesign:HintAssist.Hint="Profile name" Margin="0 8 0 16"
Style="{StaticResource MaterialDesignFloatingHintTextBox}" Text="{Binding ProfileName}" />
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<Button Style="{StaticResource MaterialDesignFlatButton}" IsCancel="True" Margin="0 8 8 0" Command="{s:Action Cancel}">
<Button.CommandParameter>
<system:Boolean xmlns:system="clr-namespace:System;assembly=mscorlib">
False
</system:Boolean>
</Button.CommandParameter>
CANCEL
</Button>
<Button Style="{StaticResource MaterialDesignFlatButton}" IsDefault="True" Margin="0 8 8 0" Command="{s:Action Accept}">
<Button.CommandParameter>
<system:Boolean xmlns:system="clr-namespace:System;assembly=mscorlib">
True
</system:Boolean>
</Button.CommandParameter>
ACCEPT
</Button>
</StackPanel>
</StackPanel>
</UserControl>

View File

@ -0,0 +1,30 @@
using System.Threading.Tasks;
using Artemis.UI.ViewModels.Dialogs;
using Stylet;
namespace Artemis.UI.Screens.Module.ProfileEditor.Dialogs
{
public class ProfileCreateViewModel : DialogViewModelBase
{
public ProfileCreateViewModel(IModelValidator<ProfileCreateViewModel> validator) : base(validator)
{
}
public string ProfileName { get; set; }
public async Task Accept()
{
await ValidateAsync();
if (HasErrors)
return;
Session.Close(ProfileName);
}
public void Cancel()
{
Session.Close();
}
}
}

View File

@ -0,0 +1,12 @@
using FluentValidation;
namespace Artemis.UI.Screens.Module.ProfileEditor.Dialogs
{
public class ProfileCreateViewModelValidator : AbstractValidator<ProfileCreateViewModel>
{
public ProfileCreateViewModelValidator()
{
RuleFor(m => m.ProfileName).NotEmpty().WithMessage("Profile name may not be empty");
}
}
}

View File

@ -5,11 +5,13 @@ using System.Threading.Tasks;
using Artemis.Core.Models.Profile;
using Artemis.Core.Plugins.Abstract;
using Artemis.Core.Services.Storage.Interfaces;
using Artemis.UI.Screens.Module.ProfileEditor.Dialogs;
using Artemis.UI.Screens.Module.ProfileEditor.DisplayConditions;
using Artemis.UI.Screens.Module.ProfileEditor.ElementProperties;
using Artemis.UI.Screens.Module.ProfileEditor.LayerElements;
using Artemis.UI.Screens.Module.ProfileEditor.Layers;
using Artemis.UI.Screens.Module.ProfileEditor.Visualization;
using Artemis.UI.Services.Interfaces;
using Stylet;
namespace Artemis.UI.Screens.Module.ProfileEditor
@ -17,10 +19,13 @@ namespace Artemis.UI.Screens.Module.ProfileEditor
public class ProfileEditorViewModel : Conductor<ProfileEditorPanelViewModel>.Collection.AllActive
{
private readonly IProfileService _profileService;
private readonly IDialogService _dialogService;
private Profile _selectedProfile;
public ProfileEditorViewModel(ProfileModule module, ICollection<ProfileEditorPanelViewModel> viewModels, IProfileService profileService)
public ProfileEditorViewModel(ProfileModule module, ICollection<ProfileEditorPanelViewModel> viewModels, IProfileService profileService, IDialogService dialogService)
{
_profileService = profileService;
_dialogService = dialogService;
DisplayName = "Profile editor";
Module = module;
@ -30,13 +35,14 @@ namespace Artemis.UI.Screens.Module.ProfileEditor
LayerElementsViewModel = (LayerElementsViewModel) viewModels.First(vm => vm is LayerElementsViewModel);
LayersViewModel = (LayersViewModel) viewModels.First(vm => vm is LayersViewModel);
ProfileViewModel = (ProfileViewModel) viewModels.First(vm => vm is ProfileViewModel);
Profiles = new BindableCollection<Profile>();
Items.AddRange(viewModels);
module.ActiveProfileChanged += ModuleOnActiveProfileChanged;
}
public Core.Plugins.Abstract.Module Module { get; }
public ProfileModule Module { get; }
public DisplayConditionsViewModel DisplayConditionsViewModel { get; }
public ElementPropertiesViewModel ElementPropertiesViewModel { get; }
public LayerElementsViewModel LayerElementsViewModel { get; }
@ -44,20 +50,73 @@ namespace Artemis.UI.Screens.Module.ProfileEditor
public ProfileViewModel ProfileViewModel { get; }
public BindableCollection<Profile> Profiles { get; set; }
public Profile SelectedProfile { get; set; }
public bool CanDeleteActiveProfile => SelectedProfile != null;
public Profile SelectedProfile
{
get => _selectedProfile;
set
{
if (_selectedProfile == value)
return;
var old = _selectedProfile;
_selectedProfile = value;
ChangeActiveProfile(old);
}
}
private void ChangeActiveProfile(Profile oldProfile)
{
Module.ChangeActiveProfile(_selectedProfile);
if (_selectedProfile != null)
_profileService.UpdateProfile(_selectedProfile, false);
if (oldProfile != null)
_profileService.UpdateProfile(oldProfile, false);
}
public bool CanDeleteActiveProfile => SelectedProfile != null && Profiles.Count > 1;
public Profile CreateProfile(string name)
{
var profile = _profileService.CreateProfile(Module, name);
Profiles.Add(profile);
return profile;
}
public async Task AddProfile()
{
var result = await _dialogService.ShowDialog<ProfileCreateViewModel>();
if (result is string name)
CreateProfile(name);
}
public async Task DeleteActiveProfile()
{
var result = await _dialogService.ShowConfirmDialog(
"Delete active profile",
"Are you sure you want to delete your currently active profile? This cannot be undone."
);
if (!result || !CanDeleteActiveProfile)
return;
var profile = SelectedProfile;
var index = Profiles.IndexOf(profile);
// Get a new active profile
var newActiveProfile = index - 1 > -1 ? Profiles[index - 1] : Profiles[index + 1];
// Activate the new active profile
Module.ChangeActiveProfile(newActiveProfile);
// Remove the old one
Profiles.Remove(profile);
_profileService.DeleteProfile(profile);
}
private void ModuleOnActiveProfileChanged(object sender, EventArgs e)
{
SelectedProfile = ((ProfileModule) Module).ActiveProfile;
SelectedProfile = Profiles.FirstOrDefault(p => p == Module.ActiveProfile);
}
protected override void OnActivate()
@ -68,14 +127,30 @@ namespace Artemis.UI.Screens.Module.ProfileEditor
private void LoadProfiles()
{
var profiles = _profileService.GetProfiles((ProfileModule) Module);
Profiles.Clear();
Profiles.AddRange(profiles);
// Get all profiles from the database
var profiles = _profileService.GetProfiles(Module);
var activeProfile = _profileService.GetActiveProfile(Module);
if (activeProfile == null)
{
activeProfile = CreateProfile("Default");
profiles.Add(activeProfile);
}
// GetActiveProfile can return a duplicate because inactive profiles aren't kept in memory, make sure it's unique in the profiles list
profiles = profiles.Where(p => p.EntityId != activeProfile.EntityId).ToList();
profiles.Add(activeProfile);
Execute.OnUIThread(() =>
{
// Populate the UI collection
Profiles.Clear();
Profiles.AddRange(profiles.OrderBy(p => p.Name));
// if (!profiles.Any())
// {
// var profile = new Profile(Module.PluginInfo, "Default");
// }
SelectedProfile = activeProfile;
});
if (!activeProfile.IsActivated)
Module.ChangeActiveProfile(activeProfile);
}
}
}

View File

@ -91,11 +91,14 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization
private void UpdateLeds(object sender, CustomUpdateData customUpdateData)
{
if (IsInitializing)
lock (Devices)
{
if (IsInitializing)
IsInitializing = Devices.Any(d => !d.AddedLeds);
foreach (var profileDeviceViewModel in Devices)
profileDeviceViewModel.Update();
foreach (var profileDeviceViewModel in Devices)
profileDeviceViewModel.Update();
}
}
protected override void OnActivate()

View File

@ -64,7 +64,7 @@ namespace Artemis.UI.Services
_viewManager.BindViewToModel(view, viewModel);
if (identifier == null)
return await DialogHost.Show(view, viewModel.OnDialogClosed);
return await DialogHost.Show(view, viewModel.OnDialogOpened, viewModel.OnDialogClosed);
return await DialogHost.Show(view, identifier, viewModel.OnDialogOpened, viewModel.OnDialogClosed);
}
}