using System.ComponentModel;
using System.Windows.Input;
namespace TestProject;
/// <summary>
/// 키패드 뷰 모델
/// </summary>
public class KeypadViewModel : INotifyPropertyChanged
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Event
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 속성 변경시 - PropertyChanged
/// <summary>
/// 속성 변경시
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 입력 문자열
/// </summary>
private string inputString = string.Empty;
/// <summary>
/// 디스플레이 텍스트
/// </summary>
private string displayText = string.Empty;
/// <summary>
/// 특수 문자 배열
/// </summary>
private char[] specialCharacterArray = { '*', '#' };
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Property
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 문자 추가 명령 - AddCharacterCommand
/// <summary>
/// 문자 추가 명령
/// </summary>
public ICommand AddCharacterCommand { get; private set; }
#endregion
#region 문자 삭제 명령 - DeleteCharacterCommand
/// <summary>
/// 문자 삭제 명령
/// </summary>
public ICommand DeleteCharacterCommand { get; private set; }
#endregion
#region 입력 문자열 - InputString
/// <summary>
/// 입력 문자열
/// </summary>
public string InputString
{
get
{
return this.inputString;
}
private set
{
if(this.inputString != value)
{
this.inputString = value;
FirePropertyChangedEvent("InputString");
DisplayText = GetFormatText(this.inputString);
((Command)DeleteCharacterCommand).ChangeCanExecute();
}
}
}
#endregion
#region 디스플레이 텍스트 - DisplayText
/// <summary>
/// 디스플레이 텍스트
/// </summary>
public string DisplayText
{
get
{
return this.displayText;
}
private set
{
if(this.displayText != value)
{
this.displayText = value;
FirePropertyChangedEvent("DisplayText");
}
}
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - KeypadViewModel()
/// <summary>
/// 생성자
/// </summary>
public KeypadViewModel()
{
AddCharacterCommand = new Command<string>
(
(key) =>
{
InputString += key;
}
);
DeleteCharacterCommand = new Command
(
() =>
{
InputString = InputString.Substring(0, InputString.Length - 1);
},
() =>
{
return InputString.Length > 0;
}
);
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Protected
#region 속성 변경시 이벤트 발생시키기 - FirePropertyChangedEvent(propertyName)
/// <summary>
/// 속성 변경시 이벤트 발생시키기
/// </summary>
/// <param name="propertyName">속성명</param>
protected void FirePropertyChangedEvent(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
////////////////////////////////////////////////////////////////////////////////////////// Private
#region 포맷 텍스트 구하기 - GetFormatText(source)
/// <summary>
/// 포맷 텍스트 구하기
/// </summary>
/// <param name="source">소스 문자열</param>
/// <returns>포맷 텍스트</returns>
private string GetFormatText(string source)
{
bool hasNonNumbers = source.IndexOfAny(this.specialCharacterArray) != -1;
string target = source;
if(hasNonNumbers || source.Length < 4 || source.Length > 10)
{
}
else if(source.Length < 8)
{
target = string.Format
(
"{0}-{1}",
source.Substring(0, 3),
source.Substring(3)
);
}
else
{
target = string.Format
(
"({0}) {1}-{2}",
source.Substring(0, 3),
source.Substring(3, 3),
source.Substring(6)
);
}
return target;
}
#endregion
}