Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possiblity to edit already stored functions #22

Merged
merged 2 commits into from
Sep 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ namespace ModularToolManager.Models.Messages;
internal class DeleteFunctionMessage : RequestMessage<bool>
{
/// <summary>
/// The function which got deleted
/// The function Identifier to delete
/// </summary>
public FunctionModel Function { get; init; }
public string Identifier { get; init; }

/// <summary>
/// Create a new instance of this message
/// </summary>
/// <param name="functionModel">The function to delete</param>
public DeleteFunctionMessage(FunctionModel functionModel)
{
Function = functionModel;
Identifier = functionModel.UniqueIdentifier;
}
}
23 changes: 23 additions & 0 deletions src/ModularToolManager/Models/Messages/EditFunctionMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using CommunityToolkit.Mvvm.Messaging.Messages;

namespace ModularToolManager.Models.Messages;

/// <summary>
/// Class to inform that a function should be edited
/// </summary>
internal class EditFunctionMessage : AsyncRequestMessage<bool>
{
/// <summary>
/// The unique identifier for the function to edit
/// </summary>
public string Identifier;

/// <summary>
/// Create a new instance of this class
/// </summary>
/// <param name="functionModel">The function model to edit</param>
public EditFunctionMessage(FunctionModel functionModel)
{
Identifier = functionModel.UniqueIdentifier;
}
}
18 changes: 18 additions & 0 deletions src/ModularToolManager/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/ModularToolManager/Properties/Resources.de.resx
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@
<data name="Default_Delete" xml:space="preserve">
<value>Löschen</value>
</data>
<data name="Default_Edit" xml:space="preserve">
<value>Editieren</value>
</data>
<data name="FunctionSelection_SearchForFunction" xml:space="preserve">
<value>Nach Funktion suchen</value>
</data>
Expand Down Expand Up @@ -177,4 +180,7 @@
<data name="Tooltip_Show" xml:space="preserve">
<value>Application anzeigen</value>
</data>
<data name="Window_EditFunction" xml:space="preserve">
<value>Funktion anpassen</value>
</data>
</root>
6 changes: 6 additions & 0 deletions src/ModularToolManager/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@
<data name="Default_Delete" xml:space="preserve">
<value>Delete</value>
</data>
<data name="Default_Edit" xml:space="preserve">
<value>Edit</value>
</data>
<data name="FunctionSelection_SearchForFunction" xml:space="preserve">
<value>Search for function</value>
</data>
Expand Down Expand Up @@ -183,4 +186,7 @@
<data name="Tooltip_Show" xml:space="preserve">
<value>Show the application</value>
</data>
<data name="Window_EditFunction" xml:space="preserve">
<value>Edit function</value>
</data>
</root>
68 changes: 59 additions & 9 deletions src/ModularToolManager/ViewModels/AddFunctionViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Input;

namespace ModularToolManager.ViewModels;

Expand Down Expand Up @@ -79,6 +76,11 @@ internal partial class AddFunctionViewModel : ObservableValidator
[NotifyCanExecuteChangedFor(nameof(OkCommand))]
private string? selectedPath;

/// <summary>
/// The identifier to use for the function
/// </summary>
private string? identifier;

/// <summary>
/// Create a new instance of this class
/// </summary>
Expand All @@ -98,6 +100,28 @@ public AddFunctionViewModel(IPluginService? pluginService, IFunctionService? fun
InitialValueSet();
}

/// <summary>
/// Load on a function by the identifer to allow editing of the function
/// </summary>
/// <param name="identifier">The identifier of the function to edit</param>
/// <returns>True if editing was successful</returns>
public bool LoadInFunction(string identifier)
{
var function = functionService?.GetFunction(identifier);
if (function is null)
{
return false;
}
this.identifier = identifier;
DisplayName = function.DisplayName;
Description = function.Description;
SelectedFunctionPlugin = FunctionPlugins.FirstOrDefault(plugin => plugin.Plugin == function.Plugin);
FunctionParameters = function.Parameters;
SelectedPath = function.Path;

return true;
}

/// <summary>
/// The method will set some values as initialization, this is required to kick off the field validation
/// </summary>
Expand All @@ -124,21 +148,47 @@ private void Abort()
[RelayCommand(CanExecute = nameof(CanExecuteOk))]
private void Ok()
{
var functionModel = new FunctionModel
var functionModel = identifier is null ? CreateNewFunctionModel() : CreateEditedFunctionModel();
bool success = identifier is null ? functionService?.AddFunction(functionModel) ?? false : functionService?.ReplaceFunction(functionModel) ?? false;
if (success)
{
AbortCommand.Execute(null);
}
}

/// <summary>
/// Create a new function model with a new identifier
/// </summary>
/// <returns></returns>
private FunctionModel CreateNewFunctionModel()
{
return new FunctionModel
{
DisplayName = DisplayName!,
Description = Description,
Plugin = SelectedFunctionPlugin!.Plugin,
Parameters = FunctionParameters!,
Path = SelectedPath!,
SortOrder = 0

};
bool success = functionService?.AddFunction(functionModel) ?? false;
if (success)
}

/// <summary>
/// Create a edited function model with given identifier
/// </summary>
/// <returns>The edited function model</returns>
private FunctionModel CreateEditedFunctionModel()
{
return new FunctionModel
{
AbortCommand.Execute(null);
}
UniqueIdentifier = identifier!,
DisplayName = DisplayName!,
Description = Description,
Plugin = SelectedFunctionPlugin!.Plugin,
Parameters = FunctionParameters!,
Path = SelectedPath!,
SortOrder = 0
};
}

/// <summary>
Expand Down
72 changes: 45 additions & 27 deletions src/ModularToolManager/ViewModels/FunctionButtonViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using ModularToolManager.Models;
using ModularToolManager.Models.Messages;
using System.Threading.Tasks;
using System.Windows.Input;

namespace ModularToolManager.ViewModels;

Expand All @@ -13,28 +12,30 @@ namespace ModularToolManager.ViewModels;
/// </summary>
public partial class FunctionButtonViewModel : ObservableObject
{
/// <summary>
/// The function model to display
/// </summary>
private readonly FunctionModel functionModel;

/// <summary>
/// The identifier for this function button
/// </summary>
public string Identifier { get; }
public string Identifier => functionModel.UniqueIdentifier;

/// <summary>
/// The display name of the function
/// </summary>
[ObservableProperty]
private string? displayName;
public string? DisplayName => functionModel.DisplayName;

/// <summary>
/// The sort id to use for this function button
/// </summary>
[ObservableProperty]
private int sortId;
public int SortId => functionModel.SortOrder;

/// <summary>
/// The description of the function
/// </summary>
[ObservableProperty]
private string? description;
public string? Description => functionModel.Description;

/// <summary>
/// The tool tip delay to use, a really high value is returned if no description is present
Expand All @@ -47,34 +48,51 @@ public partial class FunctionButtonViewModel : ObservableObject
[ObservableProperty]
private bool isActive;

/// <summary>
/// The command to execute if function should run
/// </summary>
public ICommand ExecuteFunctionCommand { get; }

/// <summary>
/// Command to remove the entry
/// </summary>
public ICommand DeleteFunctionCommand { get; }

/// <summary>
/// Create a new instance of this class
/// </summary>
/// <param name="functionModel">The function model to use</param>
public FunctionButtonViewModel(FunctionModel functionModel)
{
IsActive = true;
Identifier = functionModel.UniqueIdentifier;
DisplayName = functionModel.DisplayName;
Description = functionModel.Description;
SortId = functionModel.SortOrder;
this.functionModel = functionModel;
}

ExecuteFunctionCommand = new AsyncRelayCommand(async () => await Task.Run(() => functionModel?.Plugin?.Execute(functionModel.Parameters, functionModel.Path)));
/// <summary>
/// Command to exetute the current function
/// </summary>
/// <returns>A empty task to await until execution is complete</returns>
[RelayCommand]
private async Task ExecuteFunction()
{
await Task.Run(() => functionModel?.Plugin?.Execute(functionModel.Parameters, functionModel.Path));
}

DeleteFunctionCommand = new RelayCommand(() =>
/// <summary>
/// Command to edit the current function
/// </summary>
/// <returns>A waitable task</returns>
[RelayCommand]
private async Task EditFunction()
{
bool result = false;
try
{
IsActive = false;
WeakReferenceMessenger.Default.Send(new DeleteFunctionMessage(functionModel));
});
result = await WeakReferenceMessenger.Default.Send(new EditFunctionMessage(functionModel));
}
catch (System.Exception)
{
//No result from the request returned!
}
}

/// <summary>
/// Command to delete the current function
/// </summary>
[RelayCommand]
private void DeleteFunction()
{
IsActive = false;
WeakReferenceMessenger.Default.Send(new DeleteFunctionMessage(functionModel));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public FunctionSelectionViewModel(IFunctionService? functionService)

WeakReferenceMessenger.Default.Register<DeleteFunctionMessage>(this, (_, e) =>
{
functionService?.DeleteFunction(e.Function.UniqueIdentifier);
functionService?.DeleteFunction(e.Identifier);
ReloadFunctions();
});

Expand Down
35 changes: 34 additions & 1 deletion src/ModularToolManager/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,19 @@ public MainWindowViewModel(
OpenSettingsCommand = new AsyncRelayCommand(async () => await OpenModalWindow(Properties.Resources.SubMenu_Settings, "settings_regular", nameof(SettingsViewModel)));
HideApplicationCommand = new RelayCommand(() => WeakReferenceMessenger.Default.Send(new ToggleApplicationVisibilityMessage(true)));
ShowApplicationCommand = new RelayCommand(() => WeakReferenceMessenger.Default.Send(new ToggleApplicationVisibilityMessage(false)));

WeakReferenceMessenger.Default.Register<EditFunctionMessage>(this, async (_, e) =>
{
var editModal = viewModelLocator.GetViewModel(nameof(AddFunctionViewModel)) as AddFunctionViewModel;
if (editModal is not null && editModal.LoadInFunction(e.Identifier))
{
await OpenModalWindow(Properties.Resources.Window_EditFunction, "settings_regular", editModal);
e.Reply(true);
WeakReferenceMessenger.Default.Send(new ReloadFunctionsMessage());
return;
}
e.Reply(false);
});
}

[RelayCommand]
Expand All @@ -118,7 +131,27 @@ private async Task OpenModalWindow(string title, string imagePath, string modalN
{
return;
}
ShowWindowModel modalWindowData = new ShowWindowModel(title, imagePath, viewModelLocator.GetViewModel(modalName), WindowStartupLocation.CenterScreen);
ShowWindowModel modalWindowData = new ShowWindowModel(title, imagePath, modalContent, WindowStartupLocation.CenterScreen);
if (windowManagementService is not null)
{
await windowManagementService.ShowModalWindowAsync(modalWindowData, windowManagementService?.GetMainWindow());
}
}

/// <summary>
/// Method to open a modal window
/// </summary>
/// <param name="title">The title to use</param>
/// <param name="imagePath">The image path to show</param>
/// <param name="modal">The modal to show</param>
/// <returns></returns>
private async Task OpenModalWindow(string title, string imagePath, ObservableObject? modal)
{
if (modal is null)
{
return;
}
ShowWindowModel modalWindowData = new ShowWindowModel(title, imagePath, modal, WindowStartupLocation.CenterScreen);
if (windowManagementService is not null)
{
await windowManagementService.ShowModalWindowAsync(modalWindowData, windowManagementService?.GetMainWindow());
Expand Down
Loading