private static SemesterItemsViewGroup CreateInstance(Guid localAccountId, ViewItemSemester semester, bool trackChanges = true)
        {
            var answer = new SemesterItemsViewGroup(localAccountId, semester, trackChanges);

            answer.LoadingTask = Task.Run(answer.LoadBlocking);
            return(answer);
        }
        ///// <summary>
        ///// Eliminates any items that have become expired (like events that have expired)
        ///// </summary>
        //private void Refilter()
        //{
        //    // Beware - can't do this from a background thread or else it would crash things
        //    // Hence we actually need to only do this from a UI thread and also ensure to put it in the
        //    // lock so that background threads using cached item don't get messed up
        //    Guid[] classIdentifiers = Classes.Select(i => i.Identifier).ToArray();
        //    DateTime todayAsUtc = DateTime.SpecifyKind(DateTime.Today, DateTimeKind.Utc);

        //    this.Items.RemoveWhere(i => !ShouldIncludeItem(i.DataItem as DataItemMegaItem, classIdentifiers, todayAsUtc);
        //}

        private static async Task <AgendaViewItemsGroup> CreateLoadTask(Guid localAccountId, ViewItemSemester semester, DateTime today)
        {
            var answer = new AgendaViewItemsGroup(localAccountId, semester, today, trackChanges: true);

            SemesterItemsViewGroup cached = SemesterItemsViewGroup.GetCached(semester.Identifier);

            if (cached != null)
            {
                // Can perform this without locks since we already know we're on UI thread right now,
                // and any modifications to the SemesterItems list would need to be on UI thread to occur
                await cached.LoadingTask;
                answer.PrepareFromCached(cached);
            }
            else
            {
                await Task.Run(answer.LoadBlocking);
            }
            return(answer);
        }
 private void PrepareFromCached(SemesterItemsViewGroup semesterItems)
 {
     // Hold onto a strong reference of the cached items so they continue updating
     _cachedSemesterItems = semesterItems;
     this.Items           = new AgendaItemsList(Today, semesterItems.Items);
 }
Example #4
0
        public async void LoadHomeworkAndExams()
        {
            try
            {
                if (_hasHomeworkAndExamsBeenRequested)
                {
                    return;
                }

                _hasHomeworkAndExamsBeenRequested = true;

                bool hasPastCompletedHomework = false;
                bool hasPastCompletedExams    = false;

                SemesterItemsViewGroup cached = null;
                if (this.Class.Semester != null)
                {
                    cached = SemesterItemsViewGroup.GetCached(this.Class.Semester.Identifier);
                }

                if (cached != null)
                {
                    await cached.LoadingTask;
                    DataItemMegaItem[] dataMegaItems = cached.Items
                                                       .OfType <BaseViewItemHomeworkExam>()
                                                       .Select(i => i.DataItem)
                                                       .OfType <DataItemMegaItem>()
                                                       .ToArray();

                    this.Class.AddHomeworkAndExamChildrenHelper(CreateHomeworkOrExam, ShouldIncludeHomeworkOrExamFunction(_classId, TodayAsUtc));
                    this.Class.FilterAndAddChildren(dataMegaItems);

                    hasPastCompletedHomework = dataMegaItems.Any(IsPastCompletedHomeworkFunction(_classId, TodayAsUtc));
                    hasPastCompletedExams    = dataMegaItems.Any(IsPastCompletedExamFunction(_classId, TodayAsUtc));
                }
                else
                {
                    await Task.Run(async delegate
                    {
                        var dataStore = await GetDataStore();

                        DataItemMegaItem[] dataHomeworks;

                        using (await Locks.LockDataForReadAsync())
                        {
                            dataHomeworks = dataStore.TableMegaItems.Where(ShouldIncludeHomeworkOrExamFunction(_classId, TodayAsUtc)).ToArray();

                            this.Class.AddHomeworkAndExamChildrenHelper(CreateHomeworkOrExam, ShouldIncludeHomeworkOrExamFunction(_classId, TodayAsUtc));
                            this.Class.FilterAndAddChildren(dataHomeworks);

                            hasPastCompletedHomework = dataStore.TableMegaItems.Any(IsPastCompletedHomeworkFunction(_classId, TodayAsUtc));
                            hasPastCompletedExams    = dataStore.TableMegaItems.Any(IsPastCompletedExamFunction(_classId, TodayAsUtc));
                        }
                    });
                }

                HasPastCompletedHomework = hasPastCompletedHomework;
                HasPastCompletedExams    = hasPastCompletedExams;
                Homework = this.Class.HomeworkAndExams.Sublist(ShouldIncludeInNormalHomeworkFunction(TodayAsUtc)).Cast <ViewItemHomework>();
                Exams    = new MyObservableList <ViewItemExam>();
                (Exams as MyObservableList <ViewItemExam>).InsertSorted(
                    this.Class.HomeworkAndExams.Sublist(ShouldIncludeInNormalExamsFunction()).Cast <ViewItemExam>());
                OnPropertyChanged(nameof(Homework));
                OnPropertyChanged(nameof(Exams));

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