protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            this.Event = e.Parameter as EventViewModel;

            using (this.TokenSource = new CancellationTokenSource())
            {
                try
                {
                    this.IsRefreshButtonEnabled = false;
                    await this.Event.Refresh(this.TokenSource.Token, false);
                }
                catch (OperationCanceledException ex)
                {
                    Debug.WriteLine("Operation canceled " + ex.Message);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
                finally
                {
                    this.IsRefreshButtonEnabled = true;
                }
            }
        }
Example #2
0
        public async Task Refresh(CancellationToken token, bool forceRefresh = false)
        {
            //this.Events.Clear();

            EventsProgressBarr.Visibility = Visibility.Visible;
            EventsProgressBarr.IsIndeterminate = true;

            this.SetTitle("refreshing next meetings ...");

            var needEvents = this.Events.Count == 0;

            if (needEvents)
            {
                StackPanelLoader.Visibility = Visibility.Visible;
                StackPanelLoader.Opacity = 1.0d;

                ProgressRingLoader.Visibility = Visibility.Visible;
                EventViewList.Visibility = Visibility.Collapsed;

                ProgressRingLoader.IsActive = true;

            }

            var calenderViewQuery = this.Graph.Me.CalendarView;
            var dsq = (DataServiceQuery<Microsoft.Graph.Event>)calenderViewQuery.Query;

            // Dont start with UtcNow to not go on yesterday
            var start = DateTime.UtcNow.Date.ToString("o");
            var end = DateTime.UtcNow.Date.AddDays(7).ToString("o");

            dsq = dsq.AddQueryOption("startDateTime", start);
            dsq = dsq.AddQueryOption("endDateTime", end);

            List<Microsoft.Graph.IEvent> allEvents = null;

            if (forceRefresh)
                this.cacheEventDays.Clear();

            try
            {
                var events = await this.Graph.Context.ExecuteAsync<Microsoft.Graph.Event, Microsoft.Graph.IEvent>(dsq);
                allEvents = events.CurrentPage.ToList();

                while (events.MorePagesAvailable)
                {
                    events = await events.GetNextPageAsync();
                    allEvents.AddRange(events.CurrentPage.ToList());
                }

                if (allEvents != null)
                {
                    // some events could start yesterday and finish today
                    allEvents = allEvents.Where(ev => DateTime.Parse(ev.Start.DateTime).ToLocalTime() > DateTime.Now.ToLocalTime()).ToList();

                    var allEventsOrdered = allEvents.OrderBy(ev => DateTime.Parse(ev.Start.DateTime)).OrderBy(ev => ev.IsAllDay).ToList();
                    foreach (var ev in allEventsOrdered)
                    {
                        var evm = cacheEventDays.Values.FirstOrDefault(cEvent => cEvent.Id == ev.Id);
                        if (evm == null)
                        {
                            evm = new EventViewModel(ev);
                            cacheEventDays.Values.Add(evm);
                        }

                        evm.Index = allEventsOrdered.IndexOf(ev);

                    }
                    // check if some events are deleted
                    foreach (var cEvent in cacheEventDays.Values.Select(c => c).ToList())
                    {
                        var ev = allEvents.FirstOrDefault(e => e.Id == cEvent.Id);
                        if (ev == null)
                            cacheEventDays.Values.Remove(cEvent);
                    }
                    foreach (var dayEvents in this.Events)
                    {
                        foreach (var cEvent in dayEvents.Events.Select(c => c).ToList())
                        {
                            var ev = allEvents.FirstOrDefault(e => e.Id == cEvent.Id);
                            if (ev == null)
                                dayEvents.Events.Remove(cEvent);
                        }
                    }


                    // get the dates for each group
                    var allDates = cacheEventDays.Values.Select(ced => ced.StartingDate.Date).Distinct().OrderBy(d => d.Date).ToList();

                    foreach (var aDate in allDates)
                    {
                        List<EventViewModel> lstEventViewModel;
                        if (allDates.IndexOf(aDate) == 0)
                            lstEventViewModel = cacheEventDays.Values.Where(ev => ev.StartingDate.Date == aDate).OrderBy(ev => ev.StartingDate).OrderBy(ev => ev.IsAllDay).ToList();
                        else
                            lstEventViewModel = cacheEventDays.Values.Where(ev => ev.StartingDate.Date == aDate).OrderBy(ev => ev.StartingDate).ToList();

                        // get or create the group
                        var edvm = this.Events.FirstOrDefault(ev => ev.DateTime != null && ev.DateTime.Date == aDate);
                        if (edvm == null)
                        {
                            edvm = new EventDayViewModel(aDate, allDates.IndexOf(aDate));

                            var lastIndexOf = this.Events.IndexOf(this.Events.FirstOrDefault(e => e.DateTime.Date >= edvm.DateTime.Date));

                            if (lastIndexOf <= 0 || this.Events.Count == 0)
                                this.Events.Add(edvm);
                            else
                                this.Events.Insert(lastIndexOf, edvm);
                        }

                        foreach (var item in lstEventViewModel)
                        {
                            // if event not exist || i want to force refresh
                            if (!edvm.Events.Any(e => e.Id == item.Id))
                            {
                                var lastIndexOf = edvm.Events.IndexOf(edvm.Events.FirstOrDefault(e => e.StartingDate >= item.StartingDate));

                                if (lastIndexOf <= 0 || this.Events.Count == 0)
                                    edvm.Events.Add(item);
                                else
                                    edvm.Events.Insert(lastIndexOf, item);

                            }
                            else if (edvm.Events.Any(e => e.Id == item.Id) && forceRefresh)
                            {
                                var ev = edvm.Events.First(e => e.Id == item.Id);
                                ev.FillItem(item.InternalEvent);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            if (needEvents)
            {
                var sb = GetStoryboard();

                sb.Completed += async (s, o) =>
                {

                    ProgressRingLoader.IsActive = false;
                    ProgressRingLoader.Visibility = Visibility.Collapsed;
                    StackPanelLoader.Visibility = Visibility.Collapsed;
                    EventViewList.Visibility = Visibility.Visible;

                    AppShell.Current.SetTitle("next meetings");

                    await UpdateUsers(forceRefresh, token);

                };

                sb.Begin();
            }
            else
            {
                await UpdateUsers(forceRefresh, token);
            }
           
            EventsProgressBarr.Visibility = Visibility.Collapsed;
            EventsProgressBarr.IsIndeterminate = false;


        }