Newer
Older
Correlator / Correlator / ViewModel / SoundSpeedViewModel.cs
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using Correlator.Dialog;
using Correlator.Model;
using Correlator.Service;
using Correlator.Util;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using MessageBox = HandyControl.Controls.MessageBox;

namespace Correlator.ViewModel
{
    public class SoundSpeedViewModel : ViewModelBase
    {
        private readonly ISoundSpeedDataService _dataService;
        private string _materialName;
        private MaterialVelocity _materialVelocity;

        private ObservableCollection<MaterialVelocity> _velocityCollection;

        public ObservableCollection<MaterialVelocity> VelocityCollection
        {
            get => _velocityCollection;
            private set
            {
                _velocityCollection = value;
                RaisePropertyChanged(() => VelocityCollection);
            }
        }

        public RelayCommand DeleteSpeedCommand { get; set; }
        public RelayCommand<SoundSpeedDialog> AddSpeedCommand { get; set; }
        public RelayCommand<SoundSpeedDialog> EditSpeedCommand { get; set; }
        public RelayCommand<ListView> ItemSelectedCommand { get; set; }
        public RelayCommand<SoundSpeedDialog> NextStepCommand { get; set; }

        public SoundSpeedViewModel(ISoundSpeedDataService dataService)
        {
            _dataService = dataService;
            Messenger.Default.Register<string>(this, MessengerToken.SoundSpeed, it =>
            {
                _materialName = it;
                //查询数据库
                VelocityCollection = _dataService.GetVelocityCollection(_materialName);
            });

            Messenger.Default.Register<string>(this, MessengerToken.UpdateSoundSpeed,
                it => { VelocityCollection = _dataService.GetVelocityCollection(_materialName); });

            ItemSelectedCommand = new RelayCommand<ListView>(ItemSelected);

            DeleteSpeedCommand = new RelayCommand(DeleteSpeed);

            AddSpeedCommand = new RelayCommand<SoundSpeedDialog>(AddSpeed);

            EditSpeedCommand = new RelayCommand<SoundSpeedDialog>(EditSpeed);

            NextStepCommand = new RelayCommand<SoundSpeedDialog>(UpdatePipeMaterial);
        }

        private void ItemSelected(object sender)
        {
            var listView = (ListView)sender;
            _materialVelocity = (MaterialVelocity)listView.SelectedItem;
        }

        private void DeleteSpeed()
        {
            if (_materialVelocity == null)
            {
                MessageBox.Show("未选择任何项,无法删除",
                    "温馨提示", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            var result = MessageBox.Show("是否删除?删除后无法恢复",
                "温馨提示", MessageBoxButton.OKCancel, MessageBoxImage.Question);
            if (result == MessageBoxResult.Cancel)
            {
                return;
            }

            _dataService.DeleteVelocity(_materialVelocity);
            VelocityCollection = _dataService.GetVelocityCollection(_materialName);
        }

        private void AddSpeed(SoundSpeedDialog it)
        {
            var addSoundSpeedDialog = new AddSoundSpeedDialog(_materialName)
            {
                Owner = Window.GetWindow(it)
            };
            addSoundSpeedDialog.ShowDialog();
        }

        private void EditSpeed(SoundSpeedDialog it)
        {
            if (_materialVelocity == null)
            {
                MessageBox.Show("未选择任何项,无法编辑",
                    "温馨提示", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            var editSoundSpeedDialog = new EditSoundSpeedDialog(_materialVelocity)
            {
                Owner = Window.GetWindow(it)
            };
            editSoundSpeedDialog.ShowDialog();
        }

        private void UpdatePipeMaterial(SoundSpeedDialog it)
        {
            if (_materialVelocity == null)
            {
                MessageBox.Show("还未选择任何材料,无法进行下一步操作",
                    "温馨提示", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            _dataService.UpdatePipeMaterial(_materialName, _materialVelocity);

            //更新界面数据
            Messenger.Default.Send("", MessengerToken.UpdateMaterialSoundSpeed);

            //返回选择材质界面
            it.Close();
        }
    }
}