Skip to content

Commit

Permalink
Merge pull request #39 from ZeroGachis/fix/expander-remove-item
Browse files Browse the repository at this point in the history
🐛 Remove item from display list should not be async
  • Loading branch information
valentinmagrez authored Dec 22, 2020
2 parents cd21dee + 9e065bf commit a6013b8
Showing 1 changed file with 32 additions and 41 deletions.
73 changes: 32 additions & 41 deletions Smartway.UiComponent/CardLists/ExpanderCardList.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,15 @@
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Input;
using Smartway.UiComponent.Cards;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace Smartway.UiComponent.CardLists
{
internal class ObservableCollectionAction<T>
{
public ObservableCollectionAction(NotifyCollectionChangedAction action, T item)
{
Action = action;
Item = item;
}

public NotifyCollectionChangedAction Action { get; }

public T Item { get; }

public void ApplyAction(ObservableCollection<T> collection)
{
switch (Action)
{
case NotifyCollectionChangedAction.Add:
collection.Add(Item);
break;
case NotifyCollectionChangedAction.Remove:
collection.Remove(Item);
break;
}
}
}

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ExpanderCardList : ContentView
{
private readonly Queue<ObservableCollectionAction<object>> _actionToExecute = new Queue<ObservableCollectionAction<object>>();
private readonly Queue<object> _articleToAdd = new Queue<object>();

public static readonly BindableProperty StatusProperty = BindableProperty.Create(nameof(Status), typeof(string), typeof(ExpanderCardList));
public static readonly BindableProperty TitleProperty = BindableProperty.Create(nameof(Title), typeof(string), typeof(ExpanderCardList));
Expand Down Expand Up @@ -118,18 +91,18 @@ public void OnNewSourceListValueSet(ObservableCollection<object> oldValue, Obser
else
{
ItemsLoadedList = new ObservableCollection<object>();
AddActions(NotifyCollectionChangedAction.Add, SourceList);
AddArticleToLoadAsync(SourceList);
}

SourceList.CollectionChanged += OnSourceListChanged;
OnPropertyChanged(nameof(Counter));
}

private void AddActions(NotifyCollectionChangedAction action, IEnumerable<object> articles)
private void AddArticleToLoadAsync(IEnumerable<object> articles)
{
foreach (var article in articles)
{
_actionToExecute.Enqueue(new ObservableCollectionAction<object>(action, article));
_articleToAdd.Enqueue(article);
}
}

Expand All @@ -142,18 +115,37 @@ public async Task ForceExpanderResize()
private async void OnSourceListChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (LoadAsync)
SaveAction(e);
await ApplyChanges(e);

OnPropertyChanged(nameof(Counter));
await DisplayData();
}

private void SaveAction(NotifyCollectionChangedEventArgs e)
private async Task ApplyChanges(NotifyCollectionChangedEventArgs e)
{
if (e.NewItems?.Count > 0)
AddActions(e.Action, e.NewItems.Cast<object>());
if (e.OldItems?.Count > 0)
AddActions(e.Action, e.OldItems.Cast<object>());
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
AddArticleToLoadAsync(e.NewItems.Cast<object>());
break;
case NotifyCollectionChangedAction.Remove:
await RemoveArticleFromDisplayedList(e.OldItems.Cast<object>().First());
break;
default:
return;
}
}

private async Task RemoveArticleFromDisplayedList(object item)
{
ItemsLoadedList.Remove(item);
await ForceExpanderResize();
}

private async Task AddArticleToDisplayedList(object item)
{
ItemsLoadedList.Add(item);
await ForceExpanderResize();
}

private async Task OnExpanderClick()
Expand All @@ -168,14 +160,13 @@ private async Task OnExpanderClick()

private async Task DisplayData()
{
while (_actionToExecute.Count>0)
while (_articleToAdd.Count>0)
{
if (!IsExpanded)
return;

var action = _actionToExecute.Dequeue();
action.ApplyAction(ItemsLoadedList);
await ForceExpanderResize();
var article = _articleToAdd.Dequeue();
await AddArticleToDisplayedList(article);
}

await ForceExpanderResize();
Expand Down

0 comments on commit a6013b8

Please sign in to comment.