Exemplo n.º 1
0
        private async void AddItem(AddHomeworkViewModel.ItemType type)
        {
            if (MainScreenViewModel.Classes == null || MainScreenViewModel.Classes.Count == 0)
            {
                await new PortableMessageDialog("You don't have any classes. Make sure you've opened a semester that has classes.", "No classes").ShowAsync();
                return;
            }

            MainScreenViewModel.ShowPopup(AddHomeworkViewModel.CreateForAdd(MainScreenViewModel, new AddHomeworkViewModel.AddParameter()
            {
                SemesterIdentifier = MainScreenViewModel.CurrentSemesterId,
                Classes            = MainScreenViewModel.Classes,
                SelectedClass      = null,
                Type    = type,
                DueDate = DateTime.Today
            }));

            RemoveViewModel();
        }
        public static AddHomeworkViewModel CreateForEdit(BaseViewModel parent, EditParameter editParams)
        {
            AccountDataItem account = parent.FindAncestor <MainWindowViewModel>()?.CurrentAccount;

            if (account == null)
            {
                throw new NullReferenceException("CurrentAccount was null");
            }
            ViewItemClass c = null;
            ItemType      type;

            if (editParams.Item is ViewItemHomework)
            {
                var h = editParams.Item as ViewItemHomework;

                c    = h.Class;
                type = ItemType.Homework;
            }

            else if (editParams.Item is ViewItemExam)
            {
                var e = editParams.Item as ViewItemExam;

                c    = e.Class;
                type = ItemType.Exam;
            }

            else
            {
                throw new NotImplementedException("editParams.Item wasn't of any of the expected types");
            }

            if (type == ItemType.Homework || type == ItemType.Exam)
            {
                if (c == null)
                {
                    throw new NullReferenceException("Class of the item was null. Item id " + editParams.Item.Identifier);
                }

                if (c.Semester == null)
                {
                    throw new NullReferenceException("Semester of the class was null. Item id " + editParams.Item.Identifier);
                }

                if (c.Semester.Classes == null)
                {
                    throw new NullReferenceException("Classes of the semester was null. Item id " + editParams.Item.Identifier);
                }
            }

            var model = new AddHomeworkViewModel(parent)
            {
                Account    = account,
                State      = OperationState.Editing,
                EditParams = editParams,
                Name       = editParams.Item.Name,
                Classes    = GetClassesWithNoClassClass(c.Semester.Classes),
                Date       = editParams.Item.Date.Date,
                Details    = editParams.Item.Details,
                Type       = type,
                ImageNames = editParams.Item.ImageNames.ToArray(),
                Class      = c // Assign class last, since it also assigns weight categories
            };

            // Assign existing image attachments
            model.ImageAttachments = new ObservableCollection <BaseEditingImageAttachmentViewModel>(editParams.Item.ImageNames.Select(i => new EditingExistingImageAttachmentViewModel(model, i)));

            switch (editParams.Item.GetActualTimeOption())
            {
            case DataItemMegaItem.TimeOptions.AllDay:
                model.SelectedTimeOption = model.TimeOption_AllDay;
                break;

            case DataItemMegaItem.TimeOptions.BeforeClass:
                model.SelectedTimeOption = model.TimeOption_BeforeClass;
                break;

            case DataItemMegaItem.TimeOptions.Custom:
                model._startTime         = new TimeSpan(editParams.Item.Date.Hour, editParams.Item.Date.Minute, 0);
                model._endTime           = editParams.Item.EndTime.TimeOfDay;
                model.SelectedTimeOption = model.TimeOption_Custom;
                break;

            case DataItemMegaItem.TimeOptions.DuringClass:
                model.SelectedTimeOption = model.TimeOption_DuringClass;
                break;

            case DataItemMegaItem.TimeOptions.EndOfClass:
                model.SelectedTimeOption = model.TimeOption_EndOfClass;
                break;

            case DataItemMegaItem.TimeOptions.StartOfClass:
                model.SelectedTimeOption = model.TimeOption_StartOfClass;
                break;
            }

            // We don't want to consider setting the initial time option as the user configuring the time option
            model._userChangedSelectedTimeOption = false;

            return(model);
        }