Beispiel #1
0
        public void DrawLine(int cell1, int cell2, char value)
        {
            if (!IsValidIndex(cell1) || !IsValidIndex(cell2))
            {
                return;
            }

            if (cell1 == cell2)
            {
                this[cell1] = value;
                return;
            }

            var x1 = cell1 % _layout.Size;
            var y1 = cell1 / _layout.Size;
            var x2 = cell2 % _layout.Size;
            var y2 = cell2 / _layout.Size;

            var length = Math.Max(Math.Abs(x2 - x1), Math.Abs(y2 - y1));

            for (var i = 0; i <= length; ++i)
            {
                var x = x1 + (x2 - x1) * (2 * i + 1) / (length * 2);
                var y = y1 + (y2 - y1) * (2 * i + 1) / (length * 2);
                SetValue(x, y, value);
            }

            DataChangedEvent.Invoke();
        }
Beispiel #2
0
 private void OnInitialDataChangedEvent(DataChangedEvent e)
 {
     try
     {
         if (e.LocalAccountId == LocalAccountId)
         {
             Dispatcher.Run(async delegate
             {
                 try
                 {
                     // There's a good reason why we lock here. You initially might think "Well, it's on the UI thread, no reason
                     // to lock when something's on the UI thread, right?"
                     // However, some things like Live Tiles will load the view items group on a background thread, and will
                     // enumerate over the items within the view items group from their background thread.
                     // Therefore, a new change could come in on the UI thread while the background thread is enumerating over
                     // the items in the view group. That would therefore cause an exception in the enumeration.
                     // So far we haven't seen any cases of this lock causing deadlocks, and it's 100% isolated from the main data write
                     // lock, so there shouldn't be any concerns about this causing delays
                     using (await DataChangeLock.LockForWriteAsync(callerName: this.GetType() + ".OnInitialDataChanged"))
                     {
                         OnDataChangedEvent(e);
                     }
                 }
                 catch (Exception ex)
                 {
                     TelemetryExtension.Current?.TrackException(ex);
                 }
             });
         }
     }
     catch (Exception ex)
     {
         TelemetryExtension.Current?.TrackException(ex);
     }
 }
 public void DataChanged(AccountDataStore dataStore, DataChangedEvent e)
 {
     try
     {
         if (LayoutMode != LayoutModes.Normal && e.LocalAccountId == MainScreenViewModel.CurrentLocalAccountId)
         {
             Dispatcher.Run(delegate
             {
                 try
                 {
                     if (LayoutMode != LayoutModes.Normal)
                     {
                         UpdateEditMode();
                     }
                 }
                 catch (Exception ex)
                 {
                     TelemetryExtension.Current?.TrackException(ex);
                 }
             });
         }
     }
     catch (Exception ex)
     {
         TelemetryExtension.Current?.TrackException(ex);
     }
 }
Beispiel #4
0
        private bool HandleModifyingChildrenRecursively(DataChangedEvent e)
        {
            IViewItemChildrenHelper[] childrenHelpers;
            lock (this)
            {
                childrenHelpers = _children.ToArray();
            }

            bool changed = false;

            // Make sure that children are in the right place (and add existing/new)
            foreach (var childrenHandler in childrenHelpers)
            {
                if (childrenHandler.HandleChanges(e))
                {
                    changed = true;
                }
            }

            // And do the same for children recursively
            foreach (var child in childrenHelpers.SelectMany(i => i.GetChildren()))
            {
                if (child.HandleModifyingChildrenRecursively(e))
                {
                    changed = true;
                }
            }

            return(changed);
        }
Beispiel #5
0
        private async void AccountDataStore_DataChangedEvent(object sender, DataChangedEvent e)
        {
            lock (_changedItemListeners)
            {
                for (int i = 0; i < _changedItemListeners.Count; i++)
                {
                    IDataChangeListener listener;
                    _changedItemListeners[i].TryGetTarget(out listener);

                    if (listener == null)
                    {
                        _changedItemListeners.RemoveAt(i);
                        i--;
                    }

                    else
                    {
                        listener.HandleDataChangedEvent(e);
                    }
                }
            }

            // If there's no semester right now, nothing needs changing
            if (CurrentSemesterId == Guid.Empty)
            {
                return;
            }

            // If the changes are for this account
            if (e.LocalAccountId == CurrentLocalAccountId)
            {
                try
                {
                    // We fall back to current thread since the view model should remain correct even
                    // if the view is disconnected.
                    await Dispatcher.RunOrFallbackToCurrentThreadAsync(async delegate
                    {
                        try
                        {
                            // If this semester was deleted
                            if (e.DeletedItems.Contains(CurrentSemesterId))
                            {
                                await SetCurrentSemester(Guid.Empty);
                                return;
                            }
                        }
                        catch (Exception ex)
                        {
                            TelemetryExtension.Current?.TrackException(ex);
                        }
                    });
                }

                catch (Exception ex)
                {
                    TelemetryExtension.Current?.TrackException(ex);
                }
            }
        }
        protected override void OnDataChangedEvent(DataChangedEvent e)
        {
            if (Items != null && _semester != null && _cachedSemesterItems == null)
            {
                Guid[]   classIdentifiers = _semester.Classes.Select(i => i.Identifier).ToArray();
                DateTime todayAsUtc       = DateTime.SpecifyKind(Today, DateTimeKind.Utc);

                // Remove items that were deleted
                Items.RemoveWhere(i => e.DeletedItems.Contains(i.Identifier));

                // Look through edited items
                foreach (var edited in e.EditedItems.OfType <DataItemMegaItem>())
                {
                    var matched = Items.FirstOrDefault(i => i.Identifier == edited.Identifier);

                    // If the edited item should be in our collection
                    if (ShouldIncludeItem(edited, classIdentifiers, todayAsUtc))
                    {
                        // If there was a matching, we need to update it
                        if (matched != null)
                        {
                            matched.PopulateFromDataItem(edited);

                            AssignClass(edited, matched);

                            // And then add/remove (a.k.a. resort)
                            Items.Remove(matched);
                            Add(matched);
                        }

                        // Otherwise we need to create a view item
                        else
                        {
                            Add(edited);
                        }
                    }

                    // Otherwise it shouldn't be in our collection
                    else
                    {
                        // If there was a matching, we need to remove it since it's no longer needed
                        if (matched != null)
                        {
                            Items.Remove(matched);
                        }
                    }
                }

                // Any items that are no longer have a class need to be removed
                Items.RemoveWhere(i => !i.Class.IsNoClassClass && !_semester.Classes.Contains(i.Class));

                // Add new items
                foreach (var newItem in e.NewItems.OfType <DataItemMegaItem>().Where(ShouldIncludeItemFunction(classIdentifiers, todayAsUtc)))
                {
                    Add(newItem);
                }
            }
        }
Beispiel #7
0
 public char this[int index]
 {
     get { return(_layout[index]); }
     set
     {
         SetValue(index % _layout.Size, index / _layout.Size, value);
         DataChangedEvent.Invoke();
     }
 }
Beispiel #8
0
 public bool Remove(string key)
 {
     if (dataDict.TryGetValue(key, out var oldValue) && dataDict.Remove(key))
     {
         DataChangedEvent?.Invoke(key, null, oldValue);
         return(true);
     }
     return(false);
 }
Beispiel #9
0
 public char this[int x, int y]
 {
     get { return(_layout[x, y]); }
     set
     {
         SetValue(x, y, value);
         DataChangedEvent.Invoke();
     }
 }
Beispiel #10
0
 protected static void OnDataChanged(DataEventArgs e)
 {
     if (DataChangedEvent != null)
     {
         foreach (EventHandler <DataEventArgs> dataChangedEvent in DataChangedEvent.GetInvocationList())
         {
             dataChangedEvent(null, e);
         }
     }
 }
Beispiel #11
0
 public void HandleDataChangedEvent(DataChangedEvent e)
 {
     if (e.LocalAccountId == _localAccountId && e.WasLocalChanges)
     {
         var changedItems = e.NewItems.Union(e.EditedItems).OfType <T>().Select(i => i.Identifier).ToArray();
         if (changedItems.Length > 0 && ChangedItems != null)
         {
             ChangedItems.Invoke(this, changedItems);
         }
     }
 }
Beispiel #12
0
        public void Set(string key, string value)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            dataDict.TryGetValue(key, out var oldValue);
            dataDict[key] = value;
            DataChangedEvent?.Invoke(key, value, oldValue);
        }
 protected override void OnDataChangedEvent(DataChangedEvent e)
 {
     if (Semester != null)
     {
         if (Semester.HandleDataChangedEvent(e))
         {
             if (OnChangesOccurred != null)
             {
                 OnChangesOccurred(this, e);
             }
         }
     }
 }
Beispiel #14
0
 public void HandleDataChangedEvent(DataChangedEvent e)
 {
     if (e.LocalAccountId == _localAccountId)
     {
         if (e.DeletedItems.Contains(_identifier))
         {
             _dispatcher.Run(delegate
             {
                 Deleted?.Invoke(this, new EventArgs());
             });
         }
     }
 }
        protected override void OnDataChangedEvent(DataChangedEvent e)
        {
            ViewItemSemester s = Item?.Class?.Semester;

            if (s != null)
            {
                s.HandleDataChangedEvent(e);
                Item.HandleDataChangedEvent(e);

                // And update the class
                var newClass = s.Classes.FirstOrDefault(c => c.Identifier == (Item.DataItem as DataItemMegaItem).UpperIdentifier);
                Item.GetType().GetRuntimeProperty("Class").SetValue(Item, newClass);
            }
        }
Beispiel #16
0
        protected override void OnDataChangedEvent(DataChangedEvent e)
        {
            if (School != null)
            {
                bool changed = School.HandleDataChangedEvent(e);

                if (changed)
                {
                    // Sort years here since changes in semester dates changes year sort
                    School.Years.Sort();
                    School.CalculateEverything();
                }
            }
        }
Beispiel #17
0
        public void DrawEllipse(int cell1, int cell2, char value)
        {
            if (!IsValidIndex(cell1) || !IsValidIndex(cell2))
            {
                return;
            }

            var x1 = cell1 % _layout.Size;
            var x2 = cell2 % _layout.Size;
            var y1 = cell1 / _layout.Size;
            var y2 = cell2 / _layout.Size;

            if (x1 > x2)
            {
                var t = x1;
                x1 = x2;
                x2 = t;
            }

            if (y1 > y2)
            {
                var t = y1;
                y1 = y2;
                y2 = t;
            }

            var d    = (x1 + x2) / 2f;
            var d2   = (y1 + y2) / 2f;
            var num  = (x2 - x1) / 2f + 0.5f;
            var num2 = (y2 - y1) / 2f + 0.5f;

            for (int i = x1; i <= x2; i++)
            {
                for (int j = y1; j <= y2; j++)
                {
                    if ((i - d) * (i - d) / (num * num) + (j - d2) * (j - d2) / (num2 * num2) <= 1f)
                    {
                        SetValue(i, j, value);
                    }
                }
            }

            DataChangedEvent.Invoke();
        }
Beispiel #18
0
        private static void AccountDataStore_DataChangedEvent(object sender, DataChangedEvent e)
        {
            lock (_viewItemsGroups)
            {
                for (int i = 0; i < _viewItemsGroups.Count; i++)
                {
                    BaseAccountViewItemsGroup model;

                    if (_viewItemsGroups[i].TryGetTarget(out model))
                    {
                        model.OnInitialDataChangedEvent(e);
                    }

                    else
                    {
                        _viewItemsGroups.RemoveAt(i);
                        i--;
                    }
                }
            }
        }
Beispiel #19
0
        internal virtual bool HandleDataChangedEvent(DataChangedEvent e)
        {
            bool changed = false;

            // Apply the edits to self and descendants
            if (HandleUpdatingSelfAndDescendants(e))
            {
                changed = true;
            }

            // Modify the children
            if (HandleModifyingChildrenRecursively(e))
            {
                changed = true;
            }

            if (HandleDataChangedEventAfterBase(e))
            {
                changed = true;
            }

            return(changed);
        }
Beispiel #20
0
        public void DrawRectangle(int cell1, int cell2, char value)
        {
            if (!IsValidIndex(cell1) || !IsValidIndex(cell2))
            {
                return;
            }

            var x1 = cell1 % _layout.Size;
            var y1 = cell1 / _layout.Size;
            var x2 = cell2 % _layout.Size;
            var y2 = cell2 / _layout.Size;

            if (x1 > x2)
            {
                var t = x1;
                x1 = x2;
                x2 = t;
            }

            if (y1 > y2)
            {
                var t = y1;
                y1 = y2;
                y2 = t;
            }

            for (var x = x1; x <= x2; ++x)
            {
                for (var y = y1; y <= y2; ++y)
                {
                    SetValue(x, y, value);
                }
            }

            DataChangedEvent.Invoke();
        }
Beispiel #21
0
        private bool HandleUpdatingSelfAndDescendants(DataChangedEvent e)
        {
            bool changed = false;

            // First edit this item itself
            if (this is BaseViewItem && (this as BaseViewItem).HasData)
            {
                BaseViewItem baseViewItem = this as BaseViewItem;

                BaseDataItem editedItem = e.EditedItems.FirstOrDefault(i => i.Identifier == baseViewItem.Identifier);

                if (editedItem != null)
                {
                    baseViewItem.PopulateFromDataItem(editedItem);

                    changed = true;
                }
            }

            IViewItemChildrenHelper[] childrenHelpers;
            lock (this)
            {
                childrenHelpers = _children.ToArray();
            }

            // Then edit all children
            foreach (var child in childrenHelpers.SelectMany(i => i.GetChildren()))
            {
                if (child.HandleUpdatingSelfAndDescendants(e))
                {
                    changed = true;
                }
            }

            return(changed);
        }
Beispiel #22
0
 private void OnTypeChanged()
 {
     _content = CreateContent(Type);
     DataChangedEvent?.Invoke();
     LayoutChangedEvent?.Invoke();
 }
Beispiel #23
0
 private void OnTypeChanged()
 {
     CreateContent(null, null);
     DataChangedEvent?.Invoke();
     LayoutChangedEvent?.Invoke();
 }
Beispiel #24
0
 private void OnTypeChanged()
 {
     Content = LootFactory.CreateLoot(Type);
     DataChangedEvent?.Invoke();
     LayoutChangedEvent?.Invoke();
 }
 public DataChangedEventDictionary(DataChangedEvent dataChangedEvent)
 {
     _dataChangedEvent = dataChangedEvent;
     _dictionary       = new Dictionary <Type, Type>();
 }
 private void OnTypeChanged()
 {
     Content = RequirementFactory.CreateRequirement(Type);
     DataChangedEvent?.Invoke();
     LayoutChangedEvent?.Invoke();
 }
Beispiel #27
0
 private void ViewModelSchedule_OnChangesOccurred(object sender, DataChangedEvent e)
 {
     UpdateAvailableItemsAndTriggerUpdateDisplay();
 }
        public bool HandleChanges(DataChangedEvent e)
        {
            bool changed = false;

            List <V> toRemove = new List <V>();
            List <V> toReSort = new List <V>();

            foreach (V child in Children)
            {
                // If it was deleted, then we mark it for remove
                if (e.DeletedItems.Contains(child.Identifier))
                {
                    toRemove.Add(child);
                }

                else
                {
                    D edited = e.EditedItems.OfType <D>().FirstOrDefault(i => i.Identifier == child.Identifier);

                    // If it was edited
                    if (edited != null)
                    {
                        // If it's still a child, we'll need to re-sort it
                        if (IsChild.Invoke(edited))
                        {
                            toReSort.Add(child);
                        }

                        // Otherwise it's no longer a child, so remove it from this parent
                        else
                        {
                            toRemove.Add(child);
                        }
                    }
                }
            }

            if (toRemove.Count > 0 || toReSort.Count > 0)
            {
                changed = true;
            }

            // Now remove all that need removing
            foreach (V item in toRemove)
            {
                RemoveChildMethod.Invoke(item);
            }

            // And re-sort all that need re-sorting
            foreach (V item in toReSort)
            {
                // First remove
                RemoveChildMethod.Invoke(item);

                // Then re-add
                AddChildMethod.Invoke(item);
            }


            // And also add items that currently aren't children, but were edited and may be new children
            if (FilterAndAddChildren(e.EditedItems.Where(i => !Children.Any(c => c.Identifier == i.Identifier))))
            {
                changed = true;
            }

            // And now add the new items
            if (FilterAndAddChildren(e.NewItems))
            {
                changed = true;
            }

            return(changed);
        }
Beispiel #29
0
 private void ScheduleViewItemsGroup_OnChangesOccurred(object sender, DataChangedEvent e)
 {
     EnsureChangesPropagated();
 }
Beispiel #30
0
        protected override void OnDataChangedEvent(DataChangedEvent e)
        {
            base.OnDataChangedEvent(e);

            if (_semester != null)
            {
                // Look through edited items
                if (Class.HomeworkAndExams != null)
                {
                    foreach (var edited in e.EditedItems.OfType <DataItemMegaItem>())
                    {
                        var matched = Class.HomeworkAndExams.FirstOrDefault(i => i.Identifier == edited.Identifier);

                        // If found matching
                        if (matched != null)
                        {
                            // If no longer under this class, we need to re-assign the class
                            if (matched is ViewItemHomework)
                            {
                                var h = matched as ViewItemHomework;
                                if (h.Class.Identifier != edited.UpperIdentifier)
                                {
                                    if (edited.UpperIdentifier == _semester.NoClassClass.Identifier)
                                    {
                                        h.Class = _semester.NoClassClass;
                                    }
                                    else
                                    {
                                        h.Class = _semester.Classes.FirstOrDefault(i => i.Identifier == edited.UpperIdentifier);
                                    }
                                }
                            }
                            else if (matched is ViewItemExam)
                            {
                                var exam = matched as ViewItemExam;
                                if (exam.Class.Identifier != edited.UpperIdentifier)
                                {
                                    if (edited.UpperIdentifier == _semester.NoClassClass.Identifier)
                                    {
                                        exam.Class = _semester.NoClassClass;
                                    }
                                    else
                                    {
                                        exam.Class = _semester.Classes.FirstOrDefault(i => i.Identifier == edited.UpperIdentifier);
                                    }
                                }
                            }
                        }
                    }
                }

                // Re-assigning classes needs to be done before, otherwise we'll no longer have a reference to the object
                _semester.HandleDataChangedEvent(e);

                // Only calculates if needed
                if (IsGradesLoaded)
                {
                    Class.CalculateEverything();
                }

                // If we previously didn't have old items, see whether we do now
                if (!HasPastCompletedHomework)
                {
                    bool hasPastCompleted = e.EditedItems.Concat(e.NewItems).OfType <DataItemMegaItem>().Any(
                        h => IsPastCompletedHomeworkFunction(_classId, TodayAsUtc).Invoke(h));

                    if (hasPastCompleted)
                    {
                        HasPastCompletedHomework = true;
                    }
                }

                if (!HasPastCompletedExams)
                {
                    bool hasPastCompleted = e.EditedItems.Concat(e.NewItems).OfType <DataItemMegaItem>().Any(
                        exam => IsPastCompletedExamFunction(_classId, TodayAsUtc).Invoke(exam));

                    if (hasPastCompleted)
                    {
                        HasPastCompletedExams = true;
                    }
                }

                if (UnassignedItems != null)
                {
                    // Remove any deleted
                    if (e.DeletedItems.Any())
                    {
                        UnassignedItems.RemoveWhere(i => e.DeletedItems.Contains(i.Identifier));
                    }

                    List <DataItemMegaItem> potentialNew = new List <DataItemMegaItem>(e.NewItems.OfType <DataItemMegaItem>());

                    // Look through edited
                    foreach (var edited in e.EditedItems.OfType <DataItemMegaItem>())
                    {
                        var matching = UnassignedItems.FirstOrDefault(i => i.Identifier == edited.Identifier);
                        if (matching != null)
                        {
                            // If it should be removed
                            if (!IsUnassignedChild(edited))
                            {
                                UnassignedItems.Remove(matching);
                            }

                            // Otherwise it needs to be updated and then re-sorted
                            else
                            {
                                matching.PopulateFromDataItem(edited);
                                UnassignedItems.Remove(matching);
                                UnassignedItems.InsertSorted(matching);
                            }
                        }

                        // New
                        else
                        {
                            potentialNew.Add(edited);
                        }
                    }

                    foreach (var newItem in potentialNew)
                    {
                        if (IsUnassignedChild(newItem))
                        {
                            BaseViewItemHomeworkExam newViewItem;
                            if (newItem.MegaItemType == PowerPlannerSending.MegaItemType.Homework)
                            {
                                newViewItem = new ViewItemHomework(newItem)
                                {
                                    Class          = this.Class,
                                    WeightCategory = ViewItemWeightCategory.UNASSIGNED
                                };
                            }
                            else if (newItem.MegaItemType == PowerPlannerSending.MegaItemType.Exam)
                            {
                                newViewItem = new ViewItemExam(newItem)
                                {
                                    Class          = this.Class,
                                    WeightCategory = ViewItemWeightCategory.UNASSIGNED
                                };
                            }
                            else
                            {
                                continue;
                            }

                            UnassignedItems.InsertSorted(newViewItem);
                        }
                    }

                    HasUnassignedItems = UnassignedItems.Count > 0;
                }
            }
        }