Newer
Older
Correlator / Correlator / ViewModels / AddSoundSpeedDialogViewModel.cs
using System;
using System.Text.RegularExpressions;
using Correlator.DataService;
using Correlator.Util;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;

namespace Correlator.ViewModels
{
    public class AddSoundSpeedDialogViewModel : BindableBase, IDialogAware
    {
        private string _materialName;
        public event Action<IDialogResult> RequestClose;

        #region VM

        public string Title { get; private set; } = string.Empty;
        private string _minDiameter = string.Empty;

        public string MinDiameter
        {
            get => _minDiameter;
            set
            {
                _minDiameter = value;
                RaisePropertyChanged();
            }
        }

        private string _maxDiameter = string.Empty;

        public string MaxDiameter
        {
            get => _maxDiameter;
            set
            {
                _maxDiameter = value;
                RaisePropertyChanged();
            }
        }

        private string _soundSpeed = string.Empty;

        public string SoundSpeed
        {
            get => _soundSpeed;
            set
            {
                _soundSpeed = value;
                RaisePropertyChanged();
            }
        }

        #endregion

        #region DelegateCommand

        public DelegateCommand CancelCommand { get; set; }
        public DelegateCommand DetermineParamCommand { get; set; }

        #endregion

        public AddSoundSpeedDialogViewModel(IApplicationDataService dataService, IDialogService dialogService)
        {
            CancelCommand = new DelegateCommand(delegate
            {
                RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel));
            });

            DetermineParamCommand = new DelegateCommand(delegate
            {
                if (string.IsNullOrEmpty(_minDiameter) || string.IsNullOrEmpty(_maxDiameter) ||
                    string.IsNullOrEmpty(_soundSpeed))
                {
                    dialogService.ShowDialog(
                        "AlertMessageDialog",
                        new DialogParameters
                        {
                            { "AlertType", AlertType.Warning }, { "Title", "温馨提示" }, { "Message", "不允许为空" }
                        },
                        delegate { }
                    );
                    return;
                }

                if (!Regex.IsMatch(_minDiameter, "^(([1-9]\\d{0,3})|10000|0)$"))
                {
                    dialogService.ShowDialog(
                        "AlertMessageDialog",
                        new DialogParameters
                        {
                            { "AlertType", AlertType.Error }, { "Title", "温馨提示" }, { "Message", "管径输入错误" }
                        },
                        delegate { }
                    );
                    return;
                }

                if (!Regex.IsMatch(_maxDiameter, "^(([1-9]\\d{0,3})|10000|0)$"))
                {
                    dialogService.ShowDialog(
                        "AlertMessageDialog",
                        new DialogParameters
                        {
                            { "AlertType", AlertType.Error }, { "Title", "温馨提示" }, { "Message", "管径输入错误" }
                        },
                        delegate { }
                    );
                    return;
                }

                if (int.Parse(_minDiameter) > int.Parse(_maxDiameter))
                {
                    dialogService.ShowDialog(
                        "AlertMessageDialog",
                        new DialogParameters
                        {
                            { "AlertType", AlertType.Error }, { "Title", "温馨提示" }, { "Message", "管径下限不能大于管径上限" }
                        },
                        delegate { }
                    );
                    return;
                }

                if (!Regex.IsMatch(_soundSpeed, "^(([1-9]\\d{0,3})|10000|0)$"))
                {
                    dialogService.ShowDialog(
                        "AlertMessageDialog",
                        new DialogParameters
                        {
                            { "AlertType", AlertType.Error }, { "Title", "温馨提示" }, { "Message", "速度输入错误" }
                        },
                        delegate { }
                    );
                    return;
                }

                dataService.InsertVelocity(_materialName, _minDiameter, _maxDiameter, _soundSpeed);
                RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
            });
        }

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            _materialName = parameters.GetValue<string>("MaterialName");
        }
    }
}