Example #1
0
        private async void SynchronizationAsync()
        {
            var request = await Service.CalendarList.List().ExecuteAsync();

            var calendars = request.Items;

            calendars.Remove(calendars.First(calendar => calendar.Id == "addressbook#[email protected]"));

            foreach (var scheduler in Schedulers)
            {
                if (calendars.Select(calendar => calendar.Id).Contains(scheduler.Calendar.Id))
                {
                    if (scheduler.Calendar.AccessRole == "reader")
                    {
                        continue;
                    }
                    var events = Service.Events.List(scheduler.Calendar.Id).ExecuteAsync().Result.Items;
                    /// Сравнение текущих ивентов с гугловскими
                    for (var i = 0; i < scheduler.Events.Count; i++)
                    {
                        var thisEvent = scheduler.Events[i];

                        if (events.Select(evt => evt.Id).Contains(thisEvent.Id))
                        {
                            var googleEvent = events.First(@event => @event.Id == thisEvent.Id);

                            if (googleEvent.Updated >= thisEvent.Updated)
                            {
                                lock (scheduler.Events)
                                {
                                    thisEvent = googleEvent;
                                }
                            }
                            else
                            {
                                await Service.Events.Patch(thisEvent, scheduler.Calendar.Id, thisEvent.Id).ExecuteAsync();
                            }
                        }
                        else if (thisEvent.Id != null)
                        {
                            lock (scheduler.Events)
                            {
                                scheduler.Events.Remove(thisEvent);
                            }
                        }
                        else
                        {
                            await Service.Events.Insert(thisEvent, scheduler.Calendar.Id).ExecuteAsync();
                        }
                    }

                    ///Сравниваем гугловские с текущими
                    foreach (var googleEvent in events)
                    {
                        if (!scheduler.Events.Select(e => e.Id).Contains(googleEvent.Id))
                        {
                            scheduler.Events.Add(googleEvent);
                        }
                    }

                    var googleCalendar = calendars.First(calendar => calendar.Id == scheduler.Calendar.Id);

                    if (googleCalendar.BackgroundColor != scheduler.Calendar.BackgroundColor)
                    {
                        scheduler.ChangeColor(ColorTranslator.FromHtml(googleCalendar.BackgroundColor));
                        ChangePanelBoxColor(ColorTranslator.FromHtml(googleCalendar.BackgroundColor), scheduler.Calendar.Id);
                    }
                }
                else
                {
                    var newCalendar = new Google.Apis.Calendar.v3.Data.Calendar
                    {
                        Summary     = scheduler.Calendar.Summary,
                        Description = scheduler.Calendar.Description
                    };

                    scheduler.Calendar.Id       = Service.Calendars.Insert(newCalendar).ExecuteAsync().Result.Id;
                    scheduler.Calendar.Selected = true;
                    scheduler.Calendar.ETag     = null;
                    var req = Service.CalendarList.Update(scheduler.Calendar, scheduler.Calendar.Id);
                    req.ColorRgbFormat = true;
                    await req.ExecuteAsync();

                    foreach (var @event in scheduler.Events)
                    {
                        @event.Id = Service.Events.Insert(@event, scheduler.Calendar.Id).ExecuteAsync().Result.Id;
                    }
                }
            }

            foreach (var googleCalendar in calendars)
            {
                if (!Schedulers.Select(scheduler => scheduler.Calendar.Id).Contains(googleCalendar.Id))
                {
                    lock (Schedulers)
                    {
                        var scheduler = new Scheduler(googleCalendar, Service.Events.List(googleCalendar.Id).ExecuteAsync().Result.Items.ToArray());
                        Schedulers.Add(scheduler);
                        AddToPanel(scheduler);
                    }
                }
            }

            Invalidate();
            UpdateTable(null, null);
        }