Skip to content

Commit

Permalink
#407 Check new setting when doing property notifcations
Browse files Browse the repository at this point in the history
  • Loading branch information
Nigel Sampson committed Jul 18, 2019
1 parent b9effb8 commit 1b4dcba
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 12 deletions.
78 changes: 67 additions & 11 deletions src/Caliburn.Micro.Core/BindableCollection.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
Expand Down Expand Up @@ -42,7 +43,14 @@ public virtual void NotifyOfPropertyChange(string propertyName)
{
if (IsNotifying)
{
OnUIThread(() => OnPropertyChanged(new PropertyChangedEventArgs(propertyName)));
if (PlatformProvider.Current.PropertyChangeNotificationsOnUIThread)
{
OnUIThread(() => OnPropertyChanged(new PropertyChangedEventArgs(propertyName)));
}
else
{
OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}
}
}

Expand All @@ -51,12 +59,21 @@ public virtual void NotifyOfPropertyChange(string propertyName)
/// </summary>
public void Refresh()
{
OnUIThread(() =>
if (PlatformProvider.Current.PropertyChangeNotificationsOnUIThread)
{
OnUIThread(() =>
{
OnPropertyChanged(new PropertyChangedEventArgs("Count"));
OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
});
}
else
{
OnPropertyChanged(new PropertyChangedEventArgs("Count"));
OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
});
}
}

/// <summary>
Expand All @@ -66,7 +83,14 @@ public void Refresh()
/// <param name = "item">The item to be inserted.</param>
protected override sealed void InsertItem(int index, T item)
{
OnUIThread(() => InsertItemBase(index, item));
if (PlatformProvider.Current.PropertyChangeNotificationsOnUIThread)
{
OnUIThread(() => InsertItemBase(index, item));
}
else
{
InsertItemBase(index, item);
}
}

/// <summary>
Expand All @@ -89,7 +113,14 @@ protected virtual void InsertItemBase(int index, T item)
/// <param name = "item">The item to set.</param>
protected override sealed void SetItem(int index, T item)
{
OnUIThread(() => SetItemBase(index, item));
if (PlatformProvider.Current.PropertyChangeNotificationsOnUIThread)
{
OnUIThread(() => SetItemBase(index, item));
}
else
{
SetItemBase(index, item);
}
}

/// <summary>
Expand All @@ -111,7 +142,14 @@ protected virtual void SetItemBase(int index, T item)
/// <param name = "index">The position used to identify the item to remove.</param>
protected override sealed void RemoveItem(int index)
{
Execute.OnUIThread(() => RemoveItemBase(index));
if (PlatformProvider.Current.PropertyChangeNotificationsOnUIThread)
{
OnUIThread(() => RemoveItemBase(index));
}
else
{
RemoveItemBase(index);
}
}

/// <summary>
Expand Down Expand Up @@ -175,7 +213,7 @@ protected override void OnPropertyChanged(PropertyChangedEventArgs e)
/// <param name = "items">The items.</param>
public virtual void AddRange(IEnumerable<T> items)
{
OnUIThread(() =>
void AddRange()
{
var previousNotificationSetting = IsNotifying;
IsNotifying = false;
Expand All @@ -190,7 +228,16 @@ public virtual void AddRange(IEnumerable<T> items)
OnPropertyChanged(new PropertyChangedEventArgs("Count"));
OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
});
}

if (PlatformProvider.Current.PropertyChangeNotificationsOnUIThread)
{
OnUIThread(AddRange);
}
else
{
AddRange();
}
}

/// <summary>
Expand All @@ -199,7 +246,7 @@ public virtual void AddRange(IEnumerable<T> items)
/// <param name = "items">The items.</param>
public virtual void RemoveRange(IEnumerable<T> items)
{
OnUIThread(() =>
void RemoveRange()
{
var previousNotificationSetting = IsNotifying;
IsNotifying = false;
Expand All @@ -216,7 +263,16 @@ public virtual void RemoveRange(IEnumerable<T> items)
OnPropertyChanged(new PropertyChangedEventArgs("Count"));
OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
});
}

if (PlatformProvider.Current.PropertyChangeNotificationsOnUIThread)
{
OnUIThread(RemoveRange);
}
else
{
RemoveRange();
}
}

/// <summary>
Expand Down
9 changes: 8 additions & 1 deletion src/Caliburn.Micro.Core/PropertyChangedBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,14 @@ public virtual void NotifyOfPropertyChange([System.Runtime.CompilerServices.Call
{
if (IsNotifying && PropertyChanged != null)
{
OnUIThread(() => OnPropertyChanged(new PropertyChangedEventArgs(propertyName)));
if (PlatformProvider.Current.PropertyChangeNotificationsOnUIThread)
{
OnUIThread(() => OnPropertyChanged(new PropertyChangedEventArgs(propertyName)));
}
else
{
OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}
}
}

Expand Down

0 comments on commit 1b4dcba

Please sign in to comment.