Exemple #1
0
        public ExchangeViewModel(Exchange model, IDataClient client, object parentVm, IDialogCoordinator dialogCoordinator)
            : base(model, new ExchangeValidator())
        {
            var canExecuteSave = this.WhenAnyValue(x => x.HasErrors, x => !x);

            Save = ReactiveCommand.CreateFromTask(async _ =>
            {
                var result = await client.UpdateExchange(Model).ConfigureAwait(true);
                await result.DisplayErrors(parentVm, dialogCoordinator).ConfigureAwait(true);
            },
                                                  canExecuteSave);

            var canRemove = this.WhenAnyValue(x => x.SelectedSession).Select(x => x != null);

            RemoveSession = ReactiveCommand.Create <SessionViewModel>(viewModel =>
            {
                Sessions.Remove(viewModel);
                Model.Sessions.Remove((ExchangeSession)viewModel.Model);
            },
                                                                      canRemove);

            AddSession = ReactiveCommand.Create(() =>
            {
                var toAdd = new ExchangeSession {
                    IsSessionEnd = true
                };

                if (Sessions.Count == 0)
                {
                    toAdd.OpeningDay = DayOfTheWeek.Monday;
                    toAdd.ClosingDay = DayOfTheWeek.Monday;
                }
                else
                {
                    DayOfTheWeek maxDay = (DayOfTheWeek)Math.Min(6, Sessions.Max(x => (int)x.OpeningDay) + 1);
                    toAdd.OpeningDay    = maxDay;
                    toAdd.ClosingDay    = maxDay;
                }
                Sessions.Add(new SessionViewModel(toAdd));
                Model.Sessions.Add(toAdd);
            });

            //populate session collection
            if (model.Sessions == null)
            {
                model.Sessions = new List <ExchangeSession>();
            }

            foreach (var session in model.Sessions)
            {
                Sessions.Add(new SessionViewModel(session));
            }
        }
        private void AddSessionBtn_Click(object sender, RoutedEventArgs e)
        {
            var toAdd = new ExchangeSession();

            toAdd.IsSessionEnd = true;

            if (TheExchange.Sessions.Count == 0)
            {
                toAdd.OpeningDay = DayOfTheWeek.Monday;
                toAdd.ClosingDay = DayOfTheWeek.Monday;
            }
            else
            {
                DayOfTheWeek maxDay = (DayOfTheWeek)Math.Min(6, TheExchange.Sessions.Max(x => (int)x.OpeningDay) + 1);
                toAdd.OpeningDay = maxDay;
                toAdd.ClosingDay = maxDay;
            }
            TheExchange.Sessions.Add(toAdd);
            SessionsGrid.ItemsSource = null;
            SessionsGrid.ItemsSource = TheExchange.Sessions;
        }