1
0
Fork 0
mirror of https://github.com/hawkeye-stan/msfs-popout-panel-manager.git synced 2024-11-22 22:00:11 +00:00
msfs-popout-panel-manager/MainApp/ViewModel/AddProfileViewModel.cs

66 lines
2.4 KiB
C#
Raw Normal View History

2023-07-12 22:41:31 +00:00
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
{
2024-02-28 02:44:21 +00:00
private readonly ProfileOrchestrator _profileOrchestrator;
private readonly PanelSourceOrchestrator _panelSourceOrchestrator;
2023-07-12 22:41:31 +00:00
public UserProfile Profile { get; set; }
public UserProfile CopiedProfile { get; set; }
2024-02-28 02:44:21 +00:00
public AddProfileViewModel(SharedStorage sharedStorage, ProfileOrchestrator profileOrchestrator, PanelSourceOrchestrator panelSourceOrchestrator) : base(sharedStorage)
2023-07-12 22:41:31 +00:00
{
2024-02-28 02:44:21 +00:00
_profileOrchestrator = profileOrchestrator;
_panelSourceOrchestrator = panelSourceOrchestrator;
2023-07-12 22:41:31 +00:00
Profile = new UserProfile();
}
public void ClosingEventHandler(object sender, DialogClosingEventArgs eventArgs)
{
2024-02-28 02:44:21 +00:00
if (eventArgs.Parameter == null || !eventArgs.Parameter.Equals("ADD"))
return;
if (string.IsNullOrEmpty(Profile.Name.Trim()))
2023-07-12 22:41:31 +00:00
{
2024-02-28 02:44:21 +00:00
Profile.Name = null;
eventArgs.Cancel();
return;
2023-07-12 22:41:31 +00:00
}
2024-02-28 02:44:21 +00:00
// Unload the current profile
ProfileData.ResetActiveProfile();
_panelSourceOrchestrator.CloseAllPanelSource();
_profileOrchestrator.AddProfile(Profile.Name, CopiedProfile);
2023-07-12 22:41:31 +00:00
}
}
2024-02-28 02:44:21 +00:00
public class ProfileNameValidationRule : ValidationRule
2023-07-12 22:41:31 +00:00
{
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;
2024-02-28 02:44:21 +00:00
if (viewModel.ProfileData != null && viewModel.ProfileData.Profiles.Any(p => p.Name.ToLower().Equals(((string)value).ToLower())))
2023-07-12 22:41:31 +00:00
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;
}
}
}