Implemented profile creation, deletion and activation

This commit is contained in:
Robert committed 2019-11-20 17:30:18 +01:00
1 parent 2fb2e5444c
commit 57a2be778d
17 files changed
+217 -49

No files matched your search

@@ -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>
@@ -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();
}
}
}
@@ -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");
}
}
}
@@ -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);
}
}
}
@@ -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()