예제 #1
0
        protected override void OnBindingContextChanged()
        {
            base.OnBindingContextChanged();

            var eventItem = BindingContext as GenEvent;

            if (eventItem != null)
            {
                Task.Factory.StartNew(async() =>
                {
                    var newWholePage = new StackLayout
                    {
                        Orientation       = StackOrientation.Vertical,
                        Padding           = 5,
                        Spacing           = 0,
                        HorizontalOptions = LayoutOptions.FillAndExpand,
                        VerticalOptions   = LayoutOptions.FillAndExpand
                    };

                    eventChangeLogList = await GlobalVars.db.GetEventChangeLogsForGenEvent(eventItem);
                    if (eventChangeLogList != null && eventChangeLogList.Count > 0)
                    {
                        newWholePage.Children.Add(new Label
                        {
                            Text              = "This event was updated since you last viewed it in your list!",
                            FontSize          = 18,
                            Margin            = new Thickness(0, 20, 0, 0),
                            HorizontalOptions = LayoutOptions.FillAndExpand
                        });

                        var changeLogs = eventChangeLogList.OrderBy(d => d.ChangeTime).ToList();

                        var changeTime = DateTime.MinValue;

                        for (var i = 0; i < changeLogs.Count; i++)
                        {
                            if (changeTime != changeLogs[i].ChangeTime)
                            {
                                changeTime = changeLogs[i].ChangeTime;

                                newWholePage.Children.Add(new Label
                                {
                                    Text              = "Changes from " + changeTime.ToShortDateString() + "@" + changeTime.ToShortTimeString() + ":",
                                    FontSize          = 12,
                                    FontAttributes    = FontAttributes.Bold,
                                    Margin            = new Thickness(5, 5, 0, 0),
                                    HorizontalOptions = LayoutOptions.FillAndExpand
                                });
                            }

                            newWholePage.Children.Add(new Label
                            {
                                Text              = GenEventHelpers.GetFriendlyColumnName(changeLogs[i].Property) + " changed FROM",
                                FontSize          = 12,
                                Margin            = new Thickness(10, i > 0 ? 15 : 5, 0, 0),
                                HorizontalOptions = LayoutOptions.FillAndExpand
                            });

                            newWholePage.Children.Add(new Label
                            {
                                Text              = changeLogs[i].OldValue,
                                FontSize          = 12,
                                Margin            = new Thickness(15, 0, 0, 0),
                                TextColor         = Color.DarkRed,
                                HorizontalOptions = LayoutOptions.FillAndExpand
                            });

                            newWholePage.Children.Add(new Label
                            {
                                Text              = "--TO--",
                                FontSize          = 12,
                                Margin            = new Thickness(20, 0, 0, 0),
                                HorizontalOptions = LayoutOptions.FillAndExpand
                            });

                            newWholePage.Children.Add(new Label
                            {
                                Text              = changeLogs[i].NewValue,
                                FontSize          = 12,
                                Margin            = new Thickness(15, 0, 0, 0),
                                TextColor         = Color.DarkGreen,
                                HorizontalOptions = LayoutOptions.FillAndExpand
                            });
                        }
                    }
                    else
                    {
                        newWholePage.Children.Add(new Label
                        {
                            Text = "There are no new changes detected for this event.",
                            HorizontalOptions = LayoutOptions.FillAndExpand
                        });
                    }

                    var closeButton = new Button
                    {
                        Text = "OK",
                        HorizontalOptions = LayoutOptions.FillAndExpand
                    };

                    closeButton.Clicked += CloseButton_Clicked;

                    newWholePage.Children.Add(closeButton);

                    wholePageScroller.Content = newWholePage;
                });
            }
        }
        /// <summary>
        /// Saves the given GenEvents to database.
        /// </summary>
        /// <param name="items"></param>
        /// <returns>A phrase designed to be placed into a yellow popup if notemptyornull</returns>
        public async Task <string> SaveItemsAsync(List <GenEvent> items)
        {
            bool LoggedUpdatesWereFoundInLists = false;

            List <string> oldListEventIDs = new List <string>();

            // get all user list events
            var userLists = await database.Table <UserEventList>().Where(d => d.ID > -1).ToListAsync();

            foreach (var userList in userLists)
            {
                //var userListWithChildren = await database.GetWithChildrenAsync<UserEventList>(userList);
                var userListChildrenEvents = await database.Table <GenEventUserEventList>().Where(d => d.UserEventListID == userList.ID).ToListAsync();

                foreach (var foundEvent in userListChildrenEvents)
                {
                    if (items.FirstOrDefault(d => d.ID == foundEvent.GenEventID) != null)
                    {
                        oldListEventIDs.Add(foundEvent.GenEventID);
                    }
                }
            }

            oldListEventIDs = oldListEventIDs.Distinct().ToList();

            List <GenEvent> oldEvents = new List <GenEvent>();

            if (oldListEventIDs.Count > 0)
            {
                oldEvents = await database.Table <GenEvent>().Where(d => oldListEventIDs.Contains(d.ID)).ToListAsync();
            }

            List <EventChangeLog> newChangeLogs = new List <EventChangeLog>();

            DateTime changeTime = DateTime.Now;

            foreach (var oldEventID in oldListEventIDs)
            {
                var newEvent = items.FirstOrDefault(d => d.ID == oldEventID);
                if (newEvent != null)
                {
                    var oldEvent = oldEvents.FirstOrDefault(d => d.ID == oldEventID);
                    if (oldEvent != null)
                    {
                        var detectedChanges = GenEventHelpers.GetChanges(oldEvent, newEvent, changeTime);
                        if (detectedChanges.Count > 0)
                        {
                            newChangeLogs.AddRange(detectedChanges);
                        }
                    }
                }
            }

            if (newChangeLogs.Count > 0)
            {
                await database.InsertAllAsync(newChangeLogs);

                LoggedUpdatesWereFoundInLists = true;
            }

            // start of manual insert-or-replace of items (does not affect many-many or one-many relationships)
            var columnNameQueryResult = await database.QueryAsync <table_info_record>("PRAGMA table_info(GenEvent);");

            List <string> columnNames = new List <string>();

            columnNames.Add("docid");
            columnNameQueryResult.ForEach(d => columnNames.Add(d.name));

            string insertStub = "INSERT OR REPLACE INTO GenEvent (";

            for (var c = 0; c < columnNames.Count; c++)
            {
                if (c != columnNames.Count - 1)
                {
                    insertStub += columnNames[c] + ", ";
                }
                else
                {
                    insertStub += columnNames[c];
                }
            }

            insertStub += ") values (";

            for (var c = 0; c < columnNames.Count; c++)
            {
                if (c != columnNames.Count - 1)
                {
                    insertStub += "?, ";
                }
                else
                {
                    insertStub += "?";
                }
            }

            insertStub += ")";

            var args = new object[columnNames.Count];
            List <PropertyInfo> reflectionProperties = new List <PropertyInfo>();

            if (items.Count > 0)
            {
                for (var c = 0; c < columnNames.Count; c++)
                {
                    reflectionProperties.Add(items[0].GetType().GetRuntimeProperties().FirstOrDefault(d => string.Equals(d.Name, columnNames[c], StringComparison.OrdinalIgnoreCase)));
                }
            }

            await database.RunInTransactionAsync(delegate(SQLiteConnection transaction) {
                foreach (var item in items)
                {
                    for (var c = 0; c < columnNames.Count; c++)
                    {
                        if (columnNames[c] == "Title")
                        {
                            args[c] = ((string)(reflectionProperties[c]?.GetValue(item, null))).Trim();
                        }
                        else
                        {
                            args[c] = reflectionProperties[c]?.GetValue(item, null);
                        }

                        if (args[c].GetType() == typeof(System.DateTime))
                        {
                            args[c] = (long)(((DateTime)args[c]).Ticks);
                        }
                    }

                    transaction.Execute(insertStub, args);
                }
            });

            if (LoggedUpdatesWereFoundInLists)
            {
                List <string> finalUpdatedIDs = newChangeLogs.Select(d => d.GenEventID).Distinct().ToList();

                var notificationUpdateEvents = await database.Table <GenEvent>().Where(d => finalUpdatedIDs.Contains(d.ID)).ToListAsync();

                foreach (var notifyEvent in notificationUpdateEvents)
                {
                    notifyEvent.HasUpdateNotifications = true;
                    await database.UpdateAsync(notifyEvent);
                }
            }

            GlobalVars.eventCount = await InternalEventCountAsync();

            var returnMe = "";

            if (LoggedUpdatesWereFoundInLists)
            {
                returnMe = "Some events in your saved lists were updated! They have been marked in yellow.";
            }

            GlobalVars.View_GenUserListView.IsUpdateRequested = true;

            return(returnMe);
        }