Exemple #1
0
        public async Task <string> CalendarService_SucessfullyAddEvent(GoogleCalendarService service)
        {
            var    dummyEvent = CreateDummyEvent().ToEvent();
            string dummyId    = "";

            try
            {
                dummyId = await service.AddEvent(dummyEvent);
            }
            catch (Exception ex)
            {
                Assert.Fail("Failed to add event: " + ex.Message);
            }
            return(dummyId);
        }
        // Save received event (already validated by edit view model) to db
        // If SaveEventMsg.SaveChangeOnline true, then also save event to / delete event from google calendar
        private async void OnModifyEventReceived(ModifyEventMsg m)
        {
            var eventToModify = m.Event;
            var service       = new GoogleCalendarService();

            try
            {
                // Modify local db and possiblely google calendar based on action
                switch (m.Action)
                {
                case EventModifyAction.Add:
                    string googleCalendarEventId = null;
                    var    targetAccountId       = eventToModify.AID;
                    // Apply changes online first so any failure will prevent changes from being saved locally
                    if (LoggedIn && m.SaveChangeOnline)
                    {
                        googleCalendarEventId = await service.AddEvent(eventToModify.ToEvent());
                    }
                    // Event was successfully persisted on Google Calendar, update account and id
                    if (!googleCalendarEventId.IsNullOrEmpty())
                    {
                        var accounts        = _dbs.GetAvailableAccounts();
                        var lastSyncAccount = accounts.Where(a => a.AccId > 1)
                                              .OrderByDescending(a => a.LastSync)
                                              .FirstOrDefault();
                        if (lastSyncAccount == null)
                        {
                            throw new DataException("Couldn't find target Google Calendar account");
                        }
                        targetAccountId   = lastSyncAccount.AccId;
                        eventToModify.GID = googleCalendarEventId;
                    }
                    // Save changes locally
                    await _dbs.AddEvent(eventToModify, targetAccountId);

                    // Switch view to edit just created event
                    Messenger.Base.Send(eventToModify);
                    break;

                case EventModifyAction.Update:
                    if (LoggedIn && m.SaveChangeOnline)
                    {
                        await service.UpdateEvent(eventToModify.ToEvent());
                    }
                    await _dbs.UpdateEvent(eventToModify, eventToModify.EID);

                    break;

                case EventModifyAction.Delete:
                    if (LoggedIn && m.SaveChangeOnline)
                    {
                        await service.DeleteEvent(eventToModify.GID);
                    }
                    _dbs.DeleteEvent(eventToModify.EID);
                    break;

                default:
                    SendModifyEventStatus(success: false, msg: "Unknown action received");
                    return;
                }
            }
            catch (DataException ex)
            {
                SendModifyEventStatus(success: false, msg: ex.Message);
                return;
            }
            catch (Google.GoogleApiException)
            {
                SendModifyEventStatus(success: false, msg: "Unable to apply changes online. If error persists, consider modifying event locally");
                return;
            }
            SendModifyEventStatus(success: true, deleted: m.Action == EventModifyAction.Delete);
            RefreshEvents(viewableEventsNum);

            // Update tree view if opened
            Messenger.Base.Send(new UpdateTreeViewMsg
            {
                ModifiedEvent = m.Event,
                Action        = m.Action
            });
        }