Skip to content

Commit

Permalink
Fixed duplicate content additon
Browse files Browse the repository at this point in the history
  • Loading branch information
NizamLZ committed May 10, 2017
1 parent 4072e12 commit d9083b1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 64 deletions.
79 changes: 28 additions & 51 deletions ANAConversationStudio/Controls/ChatContentCollectionControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<object>().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<object>().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();
Expand All @@ -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);
}
}
}
22 changes: 9 additions & 13 deletions ANAConversationStudio/Controls/ChatNodeCollectionEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
});
}

Expand Down

0 comments on commit d9083b1

Please sign in to comment.