Beispiel #1
0
        public OperEditorViewModel(ICachedService cachedService, IMapper mapper, IShell shell)
        {
            Shell = shell as CachedServiceShell;
            this.cachedService = cachedService;
            this.mapper        = mapper;
            IsValid            = true;
            validator          = new OperEditorValidator();

            var operTemplates = new SourceList <string>();

            var canSave = this.WhenAnyValue(tvm => tvm.IsDirty,
                                            isd => isd == true).Concat(Shell.CanEdit);

            SaveChangesCommand = ReactiveCommand.CreateFromTask(async() => await Save(),
                                                                canSave);

            CancelCommand = ReactiveCommand.CreateFromTask(async() =>
            {
                if (IsDirty)
                {
                    if (!await Shell.ShowWarningAffirmativeDialogAsync
                            ("All unsaved changes will be lost. Close window?"))
                    {
                        return;
                    }
                }

                Close();
            });

            this.ObservableForProperty(s => s.Mode)
            .Subscribe(mode =>
            {
                var templates = mode.Value == OperMode.Exporter
                        ? DataExporters.Select(pair => pair.Key)
                        : DataImporters.Select(pair => pair.Key);

                operTemplates.ClearAndAddRange(templates);

                OperTemplates = operTemplates.SpawnCollection();

                ImplementationType = operTemplates.First();
            });

            this.ObservableForProperty(s => s.ImplementationType)
            .Where(type => type.Value != null)
            .Subscribe(type =>
            {
                var operType = Mode == OperMode.Exporter
                        ? DataExporters[type.Value]
                        : DataImporters[type.Value];
                if (operType == null)
                {
                    return;
                }

                Configuration = Activator.CreateInstance(operType);
                mapper.Map(cachedService, Configuration);
            });

            this.WhenAnyObservable(s => s.AllErrors.CountChanged)
            .Subscribe(_ => IsValid = !AllErrors.Items.Any());
        }