1
0
Fork 0
mirror of https://github.com/hawkeye-stan/msfs-popout-panel-manager.git synced 2024-10-16 14:10:45 +00:00
msfs-popout-panel-manager/MainApp/ViewModel/AddProfileViewModel.cs
2024-02-27 21:44:21 -05:00

65 lines
2.4 KiB
C#

using MaterialDesignThemes.Wpf;
using MSFSPopoutPanelManager.DomainModel.Profile;
using MSFSPopoutPanelManager.Orchestration;
using System.Globalization;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Data;
namespace MSFSPopoutPanelManager.MainApp.ViewModel
{
public class AddProfileViewModel : BaseViewModel
{
private readonly ProfileOrchestrator _profileOrchestrator;
private readonly PanelSourceOrchestrator _panelSourceOrchestrator;
public UserProfile Profile { get; set; }
public UserProfile CopiedProfile { get; set; }
public AddProfileViewModel(SharedStorage sharedStorage, ProfileOrchestrator profileOrchestrator, PanelSourceOrchestrator panelSourceOrchestrator) : base(sharedStorage)
{
_profileOrchestrator = profileOrchestrator;
_panelSourceOrchestrator = panelSourceOrchestrator;
Profile = new UserProfile();
}
public void ClosingEventHandler(object sender, DialogClosingEventArgs eventArgs)
{
if (eventArgs.Parameter == null || !eventArgs.Parameter.Equals("ADD"))
return;
if (string.IsNullOrEmpty(Profile.Name.Trim()))
{
Profile.Name = null;
eventArgs.Cancel();
return;
}
// Unload the current profile
ProfileData.ResetActiveProfile();
_panelSourceOrchestrator.CloseAllPanelSource();
_profileOrchestrator.AddProfile(Profile.Name, CopiedProfile);
}
}
public class ProfileNameValidationRule : ValidationRule
{
public override ValidationResult Validate(object value, CultureInfo cultureInfo) => ValidationResult.ValidResult; // not used
public override ValidationResult Validate(object value, CultureInfo cultureInfo, BindingExpressionBase owner)
{
var viewModel = (AddProfileViewModel)((BindingExpression)owner).DataItem;
if (viewModel.ProfileData != null && viewModel.ProfileData.Profiles.Any(p => p.Name.ToLower().Equals(((string)value).ToLower())))
return new ValidationResult(false, "Profile name already exist");
if (string.IsNullOrEmpty(((string)value).Trim()))
return new ValidationResult(false, "Profile name is required");
return ValidationResult.ValidResult;
}
}
}