Exemplo n.º 1
0
        public async void Save()
        {
            try
            {
                string name = Name.Trim();
                if (string.IsNullOrWhiteSpace(name))
                {
                    await new PortableMessageDialog(PowerPlannerResources.GetStringNoNameMessageBody(), PowerPlannerResources.GetStringNoNameMessageHeader()).ShowAsync();
                    return;
                }

                if (Class == null)
                {
                    await new PortableMessageDialog(PowerPlannerResources.GetStringNoClassMessageBody(), PowerPlannerResources.GetStringNoClassMessageHeader()).ShowAsync();
                    return;
                }

                if (IsEndTimePickerVisible && EndTime <= StartTime)
                {
                    new PortableMessageDialog(PowerPlannerResources.GetString("EditingClassScheduleItemView_LowEndTime.Content"), PowerPlannerResources.GetString("EditingClassScheduleItemView_InvalidEndTime.Title")).Show();
                    return;
                }

                List <DataItemMegaItem> bulkEntry = null;

                if (Repeats && IsRepeatingEntryEnabled)
                {
                    if (RecurrenceControlViewModel.ShowErrorIfInvalid())
                    {
                        return;
                    }

                    if (RecurrenceControlViewModel.EndDate.Date <= Date.Date)
                    {
                        TelemetryExtension.Current?.TrackEvent("UserError_InvalidRecurrence", new Dictionary <string, string>()
                        {
                            { "Error", "EndDateNotGreaterThanStart" },
                            { "UserData", "Start: " + Date.ToString("d") + " End: " + RecurrenceControlViewModel.EndDate.ToString("d") }
                        });

                        await new PortableMessageDialog("Your end date must be greater than the date that this series starts on.", "Repeating occurrence invalid").ShowAsync();
                        return;
                    }

                    bulkEntry = new List <DataItemMegaItem>();

                    foreach (var currDate in RecurrenceControlViewModel.GetEnumerableDates(Date.Date))
                    {
                        if (bulkEntry.Count >= 50)
                        {
                            TelemetryExtension.Current?.TrackEvent("UserError_TooManyOccurrences", new Dictionary <string, string>()
                            {
                                { "Error", "EndDateTooFarOut" },
                                { "UserData", $"Date: {Date.ToString("d")} {RecurrenceControlViewModel}" }
                            });

                            await new PortableMessageDialog("The repeating end date you selected is too far into the future such that greater than 50 occurrences would be created. Please select a closer end date.", "Repeating occurrence invalid").ShowAsync();
                            return;
                        }

                        bulkEntry.Add(CreateDataItem(currDate));
                    }
                }

                DataChanges changes = new DataChanges();

                // If we're adding
                if (AddParams != null)
                {
                    try
                    {
                        if (Class.IsNoClassClass)
                        {
                            // Save into a single shared storage object (the semester overrides the data item and facilitates storing these properly)
                            PopulateClassSavedInfo(Class.DataItem);
                        }
                        else
                        {
                            // Save into the class itself
                            changes.Add(CreateClassSavedInfoDataItem());

                            // And ignore updating calendar integration on that class edit, since it doesn't affect calendar integration
                            changes.IgnoreEditedClassIdentifierFromCalendarIntegration(Class.Identifier);
                        }
                    }
                    catch (Exception ex)
                    {
                        TelemetryExtension.Current?.TrackException(ex);
                    }
                }

                DataItemMegaItem dataItem;
                bool             repeating = false;
                if (bulkEntry != null && bulkEntry.Count > 0)
                {
                    dataItem = bulkEntry[0];

                    foreach (var item in bulkEntry)
                    {
                        changes.Add(item);
                    }

                    repeating = true;
                }

                // Otherwise not repeating, create item as normal
                else
                {
                    dataItem = CreateDataItem(Date.Date);
                    changes.Add(dataItem);
                }

                TryStartDataOperationAndThenNavigate(async delegate
                {
                    if (!repeating)
                    {
                        string[] updatedImageNames = await SaveImageAttachmentsAsync();
                        if (updatedImageNames != null)
                        {
                            dataItem.ImageNames = updatedImageNames;
                        }
                    }

                    await PowerPlannerApp.Current.SaveChanges(changes);

                    // Non-critical code
                    try
                    {
                        // Perfect time to ask for permission to send notifications
                        RemindersExtension.Current?.RequestReminderPermission();

                        NavigationManager.SetPreviousAddItemClass(dataItem.UpperIdentifier);

                        if (!Class.IsNoClassClass)
                        {
                            NavigationManager.SelectedWeightCategoryIdentifier = dataItem.WeightCategoryIdentifier;
                        }

                        NavigationManager.SetPreviousAddItemDate(Date.Date);

                        if (SelectedTimeOption == TimeOption_AllDay)
                        {
                            TrackTimeOption("AllDay");
                        }
                        else if (SelectedTimeOption == TimeOption_BeforeClass)
                        {
                            TrackTimeOption("BeforeClass");
                        }
                        else if (SelectedTimeOption == TimeOption_Custom)
                        {
                            TrackTimeOption("Custom");
                        }
                        else if (SelectedTimeOption == TimeOption_DuringClass)
                        {
                            TrackTimeOption("DuringClass");
                        }
                        else if (SelectedTimeOption == TimeOption_EndOfClass)
                        {
                            TrackTimeOption("EndOfClass");
                        }
                        else if (SelectedTimeOption == TimeOption_StartOfClass)
                        {
                            TrackTimeOption("StartOfClass");
                        }

                        if (_userChangedSelectedTimeOption)
                        {
                            TelemetryExtension.Current?.TrackEvent("Action_CustomizedTimeOption");
                        }

                        if (bulkEntry != null && RecurrenceControlViewModel != null)
                        {
                            if (!Account.HasAddedRepeating)
                            {
                                Account.HasAddedRepeating = true;
                                await AccountsManager.Save(Account);
                            }

                            TelemetryExtension.Current?.TrackEvent("Action_RecurringBulkEntry", new Dictionary <string, string>()
                            {
                                { "RepeatInterval", RecurrenceControlViewModel.GetRepeatIntervalAsNumber().ToString() },
                                { "RepeatType", RecurrenceControlViewModel.SelectedRepeatOption.ToString() },
                                { "EndType", RecurrenceControlViewModel.SelectedEndOption.ToString() },
                                { "Occurrences", bulkEntry.Count.ToString() }
                            });
                        }
                    }
                    catch (Exception ex)
                    {
                        // Non-critical exception
                        TelemetryExtension.Current?.TrackException(ex);
                    }
                }, delegate
                {
                    this.RemoveViewModel();
                });
            }

            catch (Exception ex)
            {
                TelemetryExtension.Current?.TrackException(ex);
            }
        }