From d9083b148d8629d5b34bde8e2abe76739cb1ec7c Mon Sep 17 00:00:00 2001 From: Khaja Nizamuddin Date: Wed, 10 May 2017 20:53:01 +0530 Subject: [PATCH] Fixed duplicate content additon --- .../Controls/ChatContentCollectionControl.cs | 79 +++++++------------ .../Controls/ChatNodeCollectionEditor.cs | 22 +++--- 2 files changed, 37 insertions(+), 64 deletions(-) diff --git a/ANAConversationStudio/Controls/ChatContentCollectionControl.cs b/ANAConversationStudio/Controls/ChatContentCollectionControl.cs index d31c77a..db3e257 100644 --- a/ANAConversationStudio/Controls/ChatContentCollectionControl.cs +++ b/ANAConversationStudio/Controls/ChatContentCollectionControl.cs @@ -25,13 +25,11 @@ public ChatNode ParentChatNode else control.Visibility = Visibility.Visible; })); - public object ChatContentOwner { get { return (object)GetValue(ChatContentOwnerProperty); } set { SetValue(ChatContentOwnerProperty, value); } } - public static readonly DependencyProperty ChatContentOwnerProperty = DependencyProperty.Register(nameof(ChatContentOwner), typeof(object), typeof(ChatContentCollectionControl), new PropertyMetadata((s, e) => { var control = s as ChatContentCollectionControl; @@ -61,59 +59,40 @@ public object ChatContentOwner else control.Visibility = Visibility.Collapsed; })); - public ChatContentCollectionControl() { ItemAdding += ItemAddingEventHandler; - //ItemAdded += ItemAddedEventHandler; - //ItemDeleted += ItemDeletedEventHandler; - Items.CollectionChanged += Items_CollectionChanged; + ItemAdded += ItemAddedEventHandler; + ItemDeleted += ItemDeletedEventHandler; } - - private void Items_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) + public void ContentItemAddExternal(BaseContent item) { - switch (e.Action) - { - case System.Collections.Specialized.NotifyCollectionChangedAction.Add: - if (e.NewItems?.Count > 0) - { - var contentItem = e.NewItems.Cast().First(); - MongoHelper.Current.Contents.Add(contentItem as BaseContent); - } - break; - case System.Collections.Specialized.NotifyCollectionChangedAction.Remove: - if (e.OldItems?.Count > 0) - { - var c = e.OldItems.Cast().First() as BaseContent; - var content = MongoHelper.Current.Contents.FirstOrDefault(x => x._id == c._id); - if (content != null) - MongoHelper.Current.Contents.Remove(content); - } - break; - default: - break; - } + if (item == null) return; + PreProccessAddingItem(item); + Items.Add(item); + ContentItemAdded(item); + } + public void ContentItemDeleted(BaseContent item) + { + if (item == null) return; + var content = MongoHelper.Current.Contents.FirstOrDefault(x => x._id == item._id); + if (content != null) + MongoHelper.Current.Contents.Remove(content); } - //private void ItemDeletedEventHandler(object sender, ItemEventArgs e) - //{ - // var c = e.Item as BaseContent; - // var content = MongoHelper.Current.Contents.FirstOrDefault(x => x._id == c._id); - // if (content != null) - // MongoHelper.Current.Contents.Remove(content); - //} - - //private void ItemAddedEventHandler(object sender, ItemEventArgs e) - //{ - // MongoHelper.Current.Contents.Add(e.Item as BaseContent); - //} - + private void ItemDeletedEventHandler(object sender, ItemEventArgs e) + { + ContentItemDeleted(e.Item as BaseContent); + } + private void ItemAddedEventHandler(object sender, ItemEventArgs e) + { + ContentItemAdded(e.Item as BaseContent); + } private void ItemAddingEventHandler(object sender, ItemAddingEventArgs e) { PreProccessAddingItem(e.Item); } - - public void PreProccessAddingItem(object item) + private void PreProccessAddingItem(object item) { if (item is BaseIdEntity) (item as BaseIdEntity)._id = ObjectId.GenerateNewId().ToString(); @@ -132,17 +111,15 @@ public void PreProccessAddingItem(object item) if ((item is SectionContent || item is ButtonContent) && ChatContentOwner != null) { if (ChatContentOwner is Section && item is SectionContent) - { (item as SectionContent).SectionId = (ChatContentOwner as Section)._id; - //if (item is TextSectionContent tsItem) - // tsItem.SectionText = (ChatContentOwner as Section).Alias; - } else if (ChatContentOwner is Button && item is ButtonContent) - { (item as ButtonContent).ButtonId = (ChatContentOwner as Button)._id; - //(item as ButtonContent).ButtonText = (ChatContentOwner as Button).Alias; - } } } + private void ContentItemAdded(BaseContent item) + { + if (item == null) return; + MongoHelper.Current.Contents.Add(item as BaseContent); + } } } diff --git a/ANAConversationStudio/Controls/ChatNodeCollectionEditor.cs b/ANAConversationStudio/Controls/ChatNodeCollectionEditor.cs index d4a427c..1a8f8db 100644 --- a/ANAConversationStudio/Controls/ChatNodeCollectionEditor.cs +++ b/ANAConversationStudio/Controls/ChatNodeCollectionEditor.cs @@ -46,7 +46,7 @@ protected override void ResolveValueBinding(PropertyItem propertyItem) _collectionControl.Loaded += _collectionControl_Loaded; _collectionControl.ItemAdding += ItemAdding; - _collectionControl.ItemAdded += ItemAdded; + _collectionControl.ItemAdded += ItemAddedAsync; _collectionControl.ItemDeleted += ItemCollectionChanged; _collectionControl.ItemMovedDown += ItemCollectionChanged; _collectionControl.ItemMovedUp += ItemCollectionChanged; @@ -62,22 +62,18 @@ protected override void ResolveValueBinding(PropertyItem propertyItem) base.ResolveValueBinding(_propertyItem); } - private void ItemAdded(object sender, ItemEventArgs e) + private async void ItemAddedAsync(object sender, ItemEventArgs e) { InvalidateSource(); - Task.Delay(1000).ContinueWith(async (s) => + await Application.Current.Dispatcher.InvokeAsync(() => { - await App.Current.Dispatcher.InvokeAsync(() => + var editor = MainWindow.Current.ChatContentCollectionEditor; + if (editor != null && editor.NewItemTypes?.Count > 0 && editor.Items.Count == 0) { - var editor = MainWindow.Current.ChatContentCollectionEditor; - if (editor != null && editor.NewItemTypes?.Count > 0 && editor.Items.Count == 0) - { - var newItem = Activator.CreateInstance(editor.NewItemTypes.First()); - editor.PreProccessAddingItem(newItem); - editor.Items.Add(newItem); - editor.SelectedItem = newItem; - } - }); + var newItem = Activator.CreateInstance(editor.NewItemTypes.First()); + editor.ContentItemAddExternal(newItem as BaseContent); + editor.SelectedItem = newItem; + } }); }