Skip to content

Commit

Permalink
move the clearing of ViewPool to ItemTemplate Change instead of OnSiz…
Browse files Browse the repository at this point in the history
…eChange
  • Loading branch information
DominicGenetec committed Dec 6, 2024
1 parent 1cb8053 commit bbc316a
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions PanCardView/Common/CardsView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ public class CardsView : Grid

public static readonly BindableProperty ItemTemplateProperty = BindableProperty.Create(nameof(ItemTemplate), typeof(DataTemplate), typeof(CardsView), propertyChanged: (bindable, oldValue, newValue) =>
{
bindable.AsCardsView().ForceRedrawViews();
var cardView = bindable.AsCardsView();
cardView.OnItemTemplateChanged();
cardView.ForceRedrawViews();
});

public static readonly BindableProperty BackViewsDepthProperty = BindableProperty.Create(nameof(BackViewsDepth), typeof(int), typeof(CardsView), defaultValueCreator: b => b.AsCardsView().DefaultBackViewsDepth, propertyChanged: (bindable, oldValue, newValue) =>
Expand Down Expand Up @@ -806,23 +808,12 @@ await Task.WhenAll(

protected virtual async void OnSizeChanged()
{
if (CurrentView != null && ItemTemplate != null)
{
var currentViewPair = _viewsPool.FirstOrDefault(p => p.Value.Contains(CurrentView));
if (!currentViewPair.Equals(default(KeyValuePair<object, List<View>>)))
{
currentViewPair.Value.Clear();
currentViewPair.Value.Add(CurrentView);
_viewsPool.Clear();
_viewsPool.Add(currentViewPair.Key, currentViewPair.Value);
}
}
await Task.Delay(1);// Workaround for https://github.com/AndreiMisiukevich/CardView/issues/194
ForceRedrawViews();
RemoveUnprocessingChildren();
Layout(new Rect(X, Y, Width, Height));
}

protected virtual void SetupBackViews(int? index = null)
{
var realIndex = index ?? SelectedIndex;
Expand Down Expand Up @@ -1766,6 +1757,21 @@ private void SetItemsSource(IEnumerable oldCollection)
OnObservableCollectionChanged(oldCollection, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}

private void OnItemTemplateChanged()
{
if (CurrentView != null)
{
var currentViewPair = _viewsPool.FirstOrDefault(p => p.Value.Contains(CurrentView));
if (!currentViewPair.Equals(default(KeyValuePair<object, List<View>>)))
{
currentViewPair.Value.Clear();
currentViewPair.Value.Add(CurrentView);
_viewsPool.Clear();
_viewsPool.Add(currentViewPair.Key, currentViewPair.Value);
}
}
}

private void SetNewIndex()
{
if (ItemsCount <= 0)
Expand Down

0 comments on commit bbc316a

Please sign in to comment.