edit
This commit is contained in:
138
XLAB2/AccountingBookEditWindowViewModel.cs
Normal file
138
XLAB2/AccountingBookEditWindowViewModel.cs
Normal file
@@ -0,0 +1,138 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace XLAB2
|
||||
{
|
||||
internal sealed class AccountingBookEditWindowViewModel : ObservableObject
|
||||
{
|
||||
private readonly IReadOnlyList<GroupOption> _existingItems;
|
||||
private readonly string _originalKey;
|
||||
private string _bookNumber;
|
||||
private string _key;
|
||||
private string _validationMessage;
|
||||
|
||||
public AccountingBookEditWindowViewModel(GroupOption seed, bool isNew, IReadOnlyList<GroupOption> existingItems)
|
||||
{
|
||||
var source = seed ?? new GroupOption();
|
||||
_existingItems = existingItems ?? Array.Empty<GroupOption>();
|
||||
_originalKey = Normalize(source.Key);
|
||||
IsNew = isNew;
|
||||
BookNumber = source.Title ?? string.Empty;
|
||||
Key = source.Key ?? string.Empty;
|
||||
|
||||
ConfirmCommand = new RelayCommand(Confirm);
|
||||
CancelCommand = new RelayCommand(Cancel);
|
||||
}
|
||||
|
||||
public event EventHandler<bool?> CloseRequested;
|
||||
|
||||
public string BookNumber
|
||||
{
|
||||
get { return _bookNumber; }
|
||||
set { SetProperty(ref _bookNumber, value); }
|
||||
}
|
||||
|
||||
public ICommand CancelCommand { get; private set; }
|
||||
|
||||
public ICommand ConfirmCommand { get; private set; }
|
||||
|
||||
public bool IsNew { get; private set; }
|
||||
|
||||
public string Key
|
||||
{
|
||||
get { return _key; }
|
||||
set { SetProperty(ref _key, value); }
|
||||
}
|
||||
|
||||
public string Title
|
||||
{
|
||||
get { return IsNew ? "Новая книга учета" : "Редактирование книги учета"; }
|
||||
}
|
||||
|
||||
public string ValidationMessage
|
||||
{
|
||||
get { return _validationMessage; }
|
||||
private set { SetProperty(ref _validationMessage, value); }
|
||||
}
|
||||
|
||||
public GroupOption ToResult()
|
||||
{
|
||||
return new GroupOption
|
||||
{
|
||||
Key = Normalize(Key) ?? string.Empty,
|
||||
Title = Normalize(BookNumber) ?? string.Empty
|
||||
};
|
||||
}
|
||||
|
||||
private void Cancel(object parameter)
|
||||
{
|
||||
RaiseCloseRequested(false);
|
||||
}
|
||||
|
||||
private void Confirm(object parameter)
|
||||
{
|
||||
var normalizedKey = Normalize(Key);
|
||||
if (normalizedKey == null)
|
||||
{
|
||||
ValidationMessage = "Укажите ключ книги учета.";
|
||||
return;
|
||||
}
|
||||
|
||||
var normalizedBookNumber = Normalize(BookNumber);
|
||||
if (normalizedBookNumber == null)
|
||||
{
|
||||
ValidationMessage = "Укажите номер книги учета.";
|
||||
return;
|
||||
}
|
||||
|
||||
var duplicateKey = _existingItems.FirstOrDefault(delegate(GroupOption item)
|
||||
{
|
||||
return item != null
|
||||
&& !IsCurrentItem(item)
|
||||
&& string.Equals(Normalize(item.Key), normalizedKey, StringComparison.OrdinalIgnoreCase);
|
||||
});
|
||||
if (duplicateKey != null)
|
||||
{
|
||||
ValidationMessage = string.Format("Ключ книги учета \"{0}\" уже существует.", normalizedKey);
|
||||
return;
|
||||
}
|
||||
|
||||
var duplicateBookNumber = _existingItems.FirstOrDefault(delegate(GroupOption item)
|
||||
{
|
||||
return item != null
|
||||
&& !IsCurrentItem(item)
|
||||
&& string.Equals(Normalize(item.Title), normalizedBookNumber, StringComparison.OrdinalIgnoreCase);
|
||||
});
|
||||
if (duplicateBookNumber != null)
|
||||
{
|
||||
ValidationMessage = string.Format("Книга учета \"{0}\" уже существует.", normalizedBookNumber);
|
||||
return;
|
||||
}
|
||||
|
||||
ValidationMessage = string.Empty;
|
||||
RaiseCloseRequested(true);
|
||||
}
|
||||
|
||||
private bool IsCurrentItem(GroupOption item)
|
||||
{
|
||||
return !IsNew
|
||||
&& string.Equals(Normalize(item == null ? null : item.Key), _originalKey, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
private static string Normalize(string value)
|
||||
{
|
||||
return string.IsNullOrWhiteSpace(value) ? null : value.Trim();
|
||||
}
|
||||
|
||||
private void RaiseCloseRequested(bool? dialogResult)
|
||||
{
|
||||
var handler = CloseRequested;
|
||||
if (handler != null)
|
||||
{
|
||||
handler(this, dialogResult);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user