Skip to content

Commit

Permalink
https://github.com/AndreiMisiukevich/CardView/issues/1
Browse files Browse the repository at this point in the history
updated samples and fixed issue with recycling
  • Loading branch information
AndreiMisiukevich committed Jan 31, 2018
1 parent a3ae8a4 commit 16eef73
Show file tree
Hide file tree
Showing 18 changed files with 191 additions and 56 deletions.
53 changes: 49 additions & 4 deletions PanCardView/CardsView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class CardsView : AbsoluteLayout

public static readonly BindableProperty CurrentIndexProperty = BindableProperty.Create(nameof(CurrentIndex), typeof(int), typeof(CardsView), 0, BindingMode.TwoWay, propertyChanged: (bindable, oldValue, newValue) => {
var view = bindable.AsCardView();
view.OldIndex = (int)oldValue;
if(view.ShouldIgnoreSetCurrentView)
{
view.ShouldIgnoreSetCurrentView = false;
Expand Down Expand Up @@ -61,10 +62,12 @@ public class CardsView : AbsoluteLayout

public static readonly BindableProperty PanDelayProperty = BindableProperty.Create(nameof(PanDelay), typeof(int), typeof(CardsView), 200);

public static readonly BindableProperty IsPanInCourseProperty = BindableProperty.Create(nameof(IsPanInCourse), typeof(bool), typeof(CardsView), false);
public static readonly BindableProperty IsPanInCourseProperty = BindableProperty.Create(nameof(IsPanInCourse), typeof(bool), typeof(CardsView), true);

public static readonly BindableProperty IsRecycledProperty = BindableProperty.Create(nameof(IsRecycled), typeof(bool), typeof(CardsView), false);

public static readonly BindableProperty IsAutoNavigatingProperty = BindableProperty.Create(nameof(IsAutoNavigating), typeof(bool), typeof(CardsView), false, BindingMode.OneWayToSource);

public static readonly BindableProperty MaxChildrenCountProperty = BindableProperty.Create(nameof(MaxChildrenCount), typeof(int), typeof(CardsView), 18);

public static readonly BindableProperty DesiredMaxChildrenCountProperty = BindableProperty.Create(nameof(DesiredMaxChildrenCount), typeof(int), typeof(CardsView), 9);
Expand Down Expand Up @@ -119,6 +122,8 @@ public CardsView(ICardProcessor frontViewProcessor, ICardProcessor backViewProce

public double CurrentDiff { get; private set; }

public int OldIndex { get; private set; } = -1;

public ICardProcessor FrontViewProcessor { get; }

public ICardProcessor BackViewProcessor { get; }
Expand Down Expand Up @@ -199,6 +204,12 @@ public bool IsRecycled
set => SetValue(IsRecycledProperty, value);
}

public bool IsAutoNavigating
{
get => (bool)GetValue(IsAutoNavigatingProperty);
set => SetValue(IsAutoNavigatingProperty, value);
}

public int MaxChildrenCount
{
get => (int)GetValue(MaxChildrenCountProperty);
Expand Down Expand Up @@ -285,6 +296,7 @@ public void AutoNavigatingStarted(View view)
{
if (view != null)
{
IsAutoNavigating = true;
if(IsPanInCourse)
{
_inCoursePanDelay = int.MaxValue;
Expand All @@ -308,6 +320,7 @@ public void AutoNavigatingEnded(View view)
ClearBindingContext(view);
}
}
IsAutoNavigating = false;
}

protected virtual void SetupBackViews(bool? isOnStart = null)
Expand Down Expand Up @@ -425,9 +438,27 @@ private PanItemPosition GetAutoNavigatePanPosition()
: PanItemPosition.Next;
}

return CurrentIndex < Items.IndexOf(_currentView.BindingContext)
if(!IsRecycled)
{
return CurrentIndex < Items.IndexOf(_currentView.BindingContext)
? PanItemPosition.Prev
: PanItemPosition.Next;
}

var recIndex = GetRecycledIndex(CurrentIndex);
var oldRecIndex = GetRecycledIndex(OldIndex);

var deltaIndex = recIndex - oldRecIndex;
if(Math.Abs(deltaIndex) == 1)
{
return deltaIndex > 0
? PanItemPosition.Next
: PanItemPosition.Prev;
}

return deltaIndex > 0
? PanItemPosition.Prev
: PanItemPosition.Next;
}

private void OnTouchStarted()
Expand Down Expand Up @@ -913,9 +944,23 @@ private void RemoveRangeViewsInUse(Guid gestureId)

private int GetRecycledIndex(int index)
{
while (index < 0 || index >= _itemsCount)
if(_itemsCount < 0)
{
return -1;
}

if(index < 0)
{
while(index < 0)
{
index += _itemsCount;
}
return index;
}

while(index >= _itemsCount)
{
index = Math.Abs(_itemsCount - Math.Abs(index));
index -= _itemsCount;
}
return index;
}
Expand Down
1 change: 0 additions & 1 deletion PanCardView/CarouselView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public CarouselView(ICardProcessor frontViewProcessor, ICardProcessor backViewPr
{
IsClippedToBounds = true;
IsRecycled = true;
IsPanInCourse = true;
}
}
}
2 changes: 1 addition & 1 deletion PanCardView/Processors/BaseCardBackViewProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class BaseCardBackViewProcessor : ICardProcessor

protected uint ResetAnimationLength { get; set; } = 150;

protected uint AutoNavigateAnimationLength { get; set; } = 100;
protected uint AutoNavigateAnimationLength { get; set; } = 150;

protected Easing ApplyEasing { get; set; } = Easing.SinOut;

Expand Down
2 changes: 1 addition & 1 deletion PanCardView/Processors/BaseCardFrontViewProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class BaseCardFrontViewProcessor : ICardProcessor

protected uint ResetAnimationLength { get; set; } = 150;

protected uint AutoNavigateAnimationLength { get; set; } = 100;
protected uint AutoNavigateAnimationLength { get; set; } = 150;

protected Easing ResetEasing { get; set; } = Easing.SinIn;

Expand Down
14 changes: 13 additions & 1 deletion PanCardView/Processors/BaseCarouselBackViewProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,19 @@ public virtual void InitView(View view, CardsView cardsView, PanItemPosition pan

public virtual void AutoNavigate(View view, CardsView cardsView, PanItemPosition panItemPosition)
{

if (view != null)
{
var destinationPos = panItemPosition == PanItemPosition.Prev
? cardsView.Width
: -cardsView.Width;

cardsView.AutoNavigatingStarted(view);
new Animation(v => view.TranslationX = v, 0, destinationPos)
.Commit(view, nameof(AutoNavigate), 16, AnimationLength, AnimEasing, (v, t) =>
{
cardsView.AutoNavigatingEnded(view);
});
}
}

public virtual void HandlePanChanged(View view, CardsView cardsView, double xPos, PanItemPosition panItemPosition)
Expand Down
7 changes: 6 additions & 1 deletion PanCardView/Processors/BaseCarouselFrontViewProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ public virtual void InitView(View view, CardsView cardsView, PanItemPosition pan

public virtual void AutoNavigate(View view, CardsView cardsView, PanItemPosition panItemPosition)
{

if (view != null)
{
view.IsVisible = true;
new Animation(v => view.TranslationX = v, view.TranslationX, 0)
.Commit(view, nameof(AutoNavigate), 16, AnimationLength, AnimEasing);
}
}

public virtual void HandlePanChanged(View view, CardsView cardsView, double xPos, PanItemPosition panItemPosition)
Expand Down
2 changes: 2 additions & 0 deletions PanCardViewSample/Droid/PanCardViewSample.Droid.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@
<AndroidResource Include="Resources\drawable-hdpi\icon.png" />
<AndroidResource Include="Resources\drawable-xhdpi\icon.png" />
<AndroidResource Include="Resources\drawable-xxhdpi\icon.png" />
<AndroidResource Include="Resources\drawable-xxhdpi\next.png" />
<AndroidResource Include="Resources\drawable-xxhdpi\prev.png" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets" />
<Import Project="..\packages\Xamarin.Android.Support.Annotations.26.1.0.1\build\MonoAndroid80\Xamarin.Android.Support.Annotations.targets" Condition="Exists('..\packages\Xamarin.Android.Support.Annotations.26.1.0.1\build\MonoAndroid80\Xamarin.Android.Support.Annotations.targets')" />
Expand Down
64 changes: 35 additions & 29 deletions PanCardViewSample/Droid/Resources/Resource.designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.ComponentModel;
using Xamarin.Forms;
using System;
using System.Windows.Input;

namespace PanCardViewSample.ViewModels
{
Expand All @@ -22,24 +23,27 @@ public SharedSampleViewModel()
new { Source = CreateSource(), Ind = _ImageCount++, Color = Color.Silver },
new { Source = CreateSource(), Ind = _ImageCount++, Color = Color.Blue }
};

PanPositionChangedCommand = new Command(v =>
{
var val = (bool)v;
if (val)
{
CurrentIndex += 1;
return;
}
CurrentIndex -= 1;
});
}

public ICommand PanPositionChangedCommand { get; }

public int CurrentIndex
{
get => _currentIndex;
set
{
//var rnd = new Random();
//if (value + 2 >= Items.Count)
//{
// Items.Add(new
// {
// Source = CreateSource(),
// Ind = _ImageCount++,
// Color = Color.FromRgb(rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255))
// });
// Items.RemoveAt(0);
//}
_currentIndex = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(CurrentIndex)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public SharedSampleCustomViewModel()
}
else
{
if(!_contextStack.Any())
{
return;
}
--_ImageCount;
NextContext = CurrentContext;
CurrentContext = PrevContext;
Expand Down
23 changes: 18 additions & 5 deletions PanCardViewSample/PanCardViewSample/Views/CardsSampleCustomView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,31 @@ public class CardsSampleCustomView : ContentPage
{
public CardsSampleCustomView()
{
var cardsView = new CardsView()
var cardsView = new CardsView
{
ItemViewFactory = new CardViewItemFactory(RuleHolder.Rule),
BackgroundColor = Color.Black.MultiplyAlpha(.9),
IsPanInCourse = true
};

var tapGesture = new TapGestureRecognizer();
tapGesture.SetBinding(TapGestureRecognizer.CommandProperty, nameof(SharedSampleCustomViewModel.PanPositionChangedCommand));
tapGesture.CommandParameter = true;
cardsView.GestureRecognizers.Add(tapGesture);
var prevItem = new ToolbarItem
{
Text = "**Prev**",
Icon = "prev",
CommandParameter = false
};
prevItem.SetBinding(MenuItem.CommandProperty, nameof(SharedSampleCustomViewModel.PanPositionChangedCommand));

var nextItem = new ToolbarItem
{
Text = "**Next**",
Icon = "next",
CommandParameter = true
};
nextItem.SetBinding(MenuItem.CommandProperty, nameof(SharedSampleCustomViewModel.PanPositionChangedCommand));

ToolbarItems.Add(prevItem);
ToolbarItems.Add(nextItem);

cardsView.SetBinding(CardsView.CurrentContextProperty, nameof(SharedSampleCustomViewModel.CurrentContext));
cardsView.SetBinding(CardsView.NextContextProperty, nameof(SharedSampleCustomViewModel.NextContext));
Expand Down
Loading

0 comments on commit 16eef73

Please sign in to comment.