Skip to content

Commit

Permalink
Merge pull request #15 from muak/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
muak authored Aug 16, 2019
2 parents 5d5f8f9 + 50085fd commit b902957
Show file tree
Hide file tree
Showing 30 changed files with 576 additions and 100 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
## files generated by popular Visual Studio add-ons.

.vs/
.idea/

# User-specific files
*.suo
Expand Down
4 changes: 2 additions & 2 deletions CollectionView.Droid/CollectionView.Droid.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
<AndroidResgenClass>Resource</AndroidResgenClass>
<MonoAndroidResourcePrefix>Resources</MonoAndroidResourcePrefix>
<MonoAndroidAssetsPrefix>Assets</MonoAndroidAssetsPrefix>
<ReleaseVersion>1.1.2</ReleaseVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down Expand Up @@ -132,6 +131,7 @@
<Compile Include="SelectableSmoothScroller.cs" />
<Compile Include="CollectionViewRenderer.cs" />
<Compile Include="DrawableUtility.cs" />
<Compile Include="CollectionViewScrollListener.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Resources\AboutResources.txt" />
Expand All @@ -142,7 +142,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CollectionView\CollectionView.csproj">
<Project>{9D6D76D0-B250-4262-B970-52DAE6764B88}</Project>
<Project>{D5C93D88-A5D6-4843-935B-CC72F073B54E}</Project>
<Name>CollectionView</Name>
</ProjectReference>
</ItemGroup>
Expand Down
10 changes: 10 additions & 0 deletions CollectionView.Droid/CollectionViewRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using AView = Android.Views.View;
using Android.Views;

namespace AiForms.Renderers.Droid
{
Expand All @@ -22,6 +23,7 @@ public abstract class CollectionViewRenderer : ViewRenderer<CollectionView, AVie
protected bool IsAttached;
protected ITemplatedItemsView<Cell> TemplatedItemsView => Element;

CollectionViewScrollListener _scrollListener;
SelectableSmoothScroller _scroller;
ScrollToRequestedEventArgs _pendingScrollTo;

Expand All @@ -44,6 +46,10 @@ protected override void Dispose(bool disposing)
_scroller?.Dispose();
_scroller = null;

RecyclerView.RemoveOnScrollListener(_scrollListener);
_scrollListener?.Dispose();
_scrollListener = null;

Adapter?.Dispose();
Adapter = null;

Expand All @@ -69,12 +75,16 @@ protected override void OnElementChanged(ElementChangedEventArgs<CollectionView>
Adapter?.Dispose();
Adapter = null;
}
e.OldElement.SetLoadMoreCompletionAction = null;
}

if (e.NewElement != null)
{
((IListViewController)e.NewElement).ScrollToRequested += OnScrollToRequested;
_scroller = new SelectableSmoothScroller(Context);
_scrollListener = new CollectionViewScrollListener(e.NewElement);
RecyclerView.AddOnScrollListener(_scrollListener);
e.NewElement.SetLoadMoreCompletionAction = (isEnd) => _scrollListener.IsReachedBottom = isEnd;
}
}

Expand Down
50 changes: 50 additions & 0 deletions CollectionView.Droid/CollectionViewScrollListener.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using AiForms.Renderers;
using Android.Support.V7.Widget;

namespace AiForms.Renderers.Droid
{
public class CollectionViewScrollListener:RecyclerView.OnScrollListener
{
public bool IsReachedBottom { get; set; }

CollectionView _collectionView;


public CollectionViewScrollListener(CollectionView collectionView)
{
_collectionView = collectionView;
}

protected override void Dispose(bool disposing)
{
if(disposing)
{
_collectionView = null;
}
base.Dispose(disposing);
}

public override void OnScrolled(RecyclerView recyclerView, int dx, int dy)
{
base.OnScrolled(recyclerView, dx, dy);

if(dx < 0 || dy < 0 || IsReachedBottom || _collectionView.LoadMoreCommand == null)
{
return;
}

var layoutManager = recyclerView.GetLayoutManager() as LinearLayoutManager;

var visibleItemCount = recyclerView.ChildCount;
var totalItemCount = layoutManager.ItemCount;
var firstVisibleItem = layoutManager.FindFirstVisibleItemPosition();

if(totalItemCount - visibleItemCount - _collectionView.LoadMoreMargin <= firstVisibleItem)
{
IsReachedBottom = true;
_collectionView.LoadMoreCommand?.Execute(null);
}
}
}
}
14 changes: 10 additions & 4 deletions CollectionView.Droid/GridCollectionViewRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ protected override void Dispose(bool disposing)

protected override void OnElementChanged(ElementChangedEventArgs<CollectionView> e)
{
base.OnElementChanged(e);

if (e.NewElement != null)
{
if (RecyclerView == null)
Expand Down Expand Up @@ -103,6 +101,8 @@ protected override void OnElementChanged(ElementChangedEventArgs<CollectionView>
UpdatePullToRefreshEnabled();
UpdatePullToRefreshColor();
}

base.OnElementChanged(e);
}

protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
Expand Down Expand Up @@ -468,8 +468,14 @@ public override void GetItemOffsets(Android.Graphics.Rect outRect, Android.Views
outRect.Right = _parentRenderer.ColumnSpacing - (spanIndex + 1) * _parentRenderer.ColumnSpacing / _spanCount; // spacing - (column + 1) * ((1f / spanCount) * spacing)
}

// Group bottom spacing is applied at the last cell.
if (_parentRenderer.Element.IsGroupingEnabled && position >= _parentRenderer.Adapter.ItemCount - _spanCount)
// Disabled grouping top spacing is applied at the first row cells.
if (!_parentRenderer.Element.IsGroupingEnabled && position < _spanCount)
{
outRect.Top = _parentRenderer._firstSpacing;
}

// Group bottom or single bottom spacing is applied at the last row cells.
if (position >= _parentRenderer.Adapter.ItemCount - _spanCount)
{
outRect.Bottom = _parentRenderer._lastSpacing;
}
Expand Down
16 changes: 14 additions & 2 deletions CollectionView.Droid/HCollectionViewRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ protected override void Dispose(bool disposing)

protected override void OnElementChanged(ElementChangedEventArgs<CollectionView> e)
{
base.OnElementChanged(e);

if (e.NewElement != null)
{
if (Control == null)
Expand Down Expand Up @@ -76,6 +74,8 @@ protected override void OnElementChanged(ElementChangedEventArgs<CollectionView>
UpdateSpacing();
}
}

base.OnElementChanged(e);
}

protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
Expand Down Expand Up @@ -221,6 +221,18 @@ public override void GetItemOffsets(Rect outRect, Android.Views.View view, Recyc
return;
}

// Disabled grouping first spacing is applied at the first cell.
if (!_renderer._hCollectionView.IsGroupingEnabled && position == 0)
{
outRect.Left = _renderer._firstSpacing;
}

// Group last or single last spacing is applied at the last cell.
if (position == _renderer.Adapter.ItemCount - 1)
{
outRect.Right = _renderer._lastSpacing;
}

if (position == 0 || _renderer.Adapter.FirstSectionItems.Contains(realPosition))
{
return;
Expand Down
3 changes: 1 addition & 2 deletions CollectionView.iOS/CollectionView.iOS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
<RootNamespace>AiForms.Renderers.iOS</RootNamespace>
<AssemblyName>CollectionView.iOS</AssemblyName>
<IPhoneResourcePrefix>Resources</IPhoneResourcePrefix>
<ReleaseVersion>1.1.2</ReleaseVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down Expand Up @@ -86,7 +85,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CollectionView\CollectionView.csproj">
<Project>{9D6D76D0-B250-4262-B970-52DAE6764B88}</Project>
<Project>{D5C93D88-A5D6-4843-935B-CC72F073B54E}</Project>
<Name>CollectionView</Name>
</ProjectReference>
</ItemGroup>
Expand Down
13 changes: 8 additions & 5 deletions CollectionView.iOS/CollectionViewRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public class CollectionViewRenderer : ViewRenderer<CollectionView, UICollectionV
protected CollectionViewSource DataSource;
protected UICollectionViewFlowLayout ViewLayout;
protected ITemplatedItemsView<Cell> TemplatedItemsView => Element;
ScrollToRequestedEventArgs _requestedScroll;
bool _disposed;

protected override void OnElementChanged(ElementChangedEventArgs<CollectionView> e)
Expand All @@ -32,6 +31,7 @@ protected override void OnElementChanged(ElementChangedEventArgs<CollectionView>
templatedItems.CollectionChanged -= OnCollectionChanged;
templatedItems.GroupedCollectionChanged -= OnGroupedCollectionChanged;
e.OldElement.ScrollToRequested -= OnScrollToRequested;
e.OldElement.SetLoadMoreCompletionAction = null;
}

if (e.NewElement != null)
Expand All @@ -42,6 +42,10 @@ protected override void OnElementChanged(ElementChangedEventArgs<CollectionView>
templatedItems.CollectionChanged += OnCollectionChanged;
templatedItems.GroupedCollectionChanged += OnGroupedCollectionChanged;
e.NewElement.ScrollToRequested += OnScrollToRequested;
e.NewElement.SetLoadMoreCompletionAction = (isEnd) =>
{
DataSource.IsReachedBottom = isEnd;
};

UpdateBackgroundColor();
}
Expand All @@ -55,7 +59,7 @@ protected override void Dispose(bool disposing)
}

if (disposing)
{
{
ViewLayout?.Dispose();
ViewLayout = null;

Expand All @@ -65,11 +69,12 @@ protected override void Dispose(bool disposing)
}

if (Element != null)
{
{
var templatedItems = TemplatedItemsView.TemplatedItems;
templatedItems.CollectionChanged -= OnCollectionChanged;
templatedItems.GroupedCollectionChanged -= OnGroupedCollectionChanged;
Element.ScrollToRequested -= OnScrollToRequested;
Element.SetLoadMoreCompletion = null;
}
}

Expand All @@ -89,14 +94,12 @@ protected override void OnElementPropertyChanged(object sender, PropertyChangedE
{
UpdateBackgroundColor();
}

}

protected virtual async void OnScrollToRequested(object sender, ScrollToRequestedEventArgs e)
{
if (Superview == null)
{
_requestedScroll = e;
return;
}

Expand Down
15 changes: 14 additions & 1 deletion CollectionView.iOS/CollectionViewSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@
namespace AiForms.Renderers.iOS
{
[Foundation.Preserve(AllMembers = true)]
public class CollectionViewSource : UICollectionViewSource, IUICollectionViewDelegateFlowLayout
public class CollectionViewSource : UICollectionViewSource, IUICollectionViewDelegateFlowLayout,IUIScrollViewDelegate
{
static int s_dataTemplateIncrementer = 2; // lets start at not 0 because

public CGSize CellSize { get; set; }
public Dictionary<int, int> Counts { get; set; }
public bool IsReachedBottom { get; set; }
public float LoadMoreMargin { get; set; }

protected CollectionView CollectionView;
protected ITemplatedItemsView<Cell> TemplatedItemsView => CollectionView;


const int DefaultItemTemplateId = 1;
bool _isLongTap;
bool _disposed;
Expand Down Expand Up @@ -57,6 +60,16 @@ protected override void Dispose(bool disposing)
base.Dispose(disposing);
}

public override void Scrolled(UIScrollView scrollView)
{
}

protected void RaiseReachedBottom()
{
IsReachedBottom = true;
CollectionView?.LoadMoreCommand?.Execute(null);
}

public override nint NumberOfSections(UICollectionView collectionView)
{
if (TemplatedItemsView.TemplatedItems.Count == 0)
Expand Down
11 changes: 8 additions & 3 deletions CollectionView.iOS/GridCollectionViewRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public class GridCollectionViewRenderer : CollectionViewRenderer
bool _disposed;
GridCollectionView _gridCollectionView => (GridCollectionView)Element;
GridCollectionViewSource _gridSource => DataSource as GridCollectionViewSource;
float _firstSpacing => _gridCollectionView.IsGroupingEnabled ? (float)_gridCollectionView.GroupFirstSpacing : 0;
float _lastSpacing => _gridCollectionView.IsGroupingEnabled ? (float)_gridCollectionView.GroupLastSpacing : 0;
float _firstSpacing => (float)_gridCollectionView.GroupFirstSpacing;
float _lastSpacing => (float)_gridCollectionView.GroupLastSpacing;
bool _isRatioHeight => _gridCollectionView.ColumnHeight <= 5.0;

protected override void OnElementChanged(ElementChangedEventArgs<CollectionView> e)
Expand Down Expand Up @@ -275,11 +275,12 @@ protected virtual void UpdateGridType()
case UIInterfaceOrientation.PortraitUpsideDown:
case UIInterfaceOrientation.Unknown:
itemSize = GetUniformItemSize(_gridCollectionView.PortraitColumns);

DataSource.LoadMoreMargin = Element.LoadMoreMargin / _gridCollectionView.PortraitColumns * (float)itemSize.Height;
break;
case UIInterfaceOrientation.LandscapeLeft:
case UIInterfaceOrientation.LandscapeRight:
itemSize = GetUniformItemSize(_gridCollectionView.LandscapeColumns);
DataSource.LoadMoreMargin = Element.LoadMoreMargin / _gridCollectionView.LandscapeColumns * (float)itemSize.Height;
break;
}
ViewLayout.MinimumInteritemSpacing = (System.nfloat)_gridCollectionView.ColumnSpacing;
Expand Down Expand Up @@ -317,6 +318,7 @@ protected virtual CGSize GetUniformItemSize(int columns)

var itemWidth = Math.Floor((float)(width / (float)columns));
var itemHeight = CalcurateColumnHeight(itemWidth);

return new CGSize(itemWidth, itemHeight);
}

Expand All @@ -326,6 +328,7 @@ protected virtual CGSize GetAutoSpacingItemSize()
var itemHeight = CalcurateColumnHeight(itemWidth);
if (_gridCollectionView.SpacingType == SpacingType.Between)
{
DataSource.LoadMoreMargin = Element.LoadMoreMargin / ((float)Frame.Width / itemWidth) * (float)itemHeight;
return new CGSize(itemWidth, itemHeight);
}

Expand All @@ -348,6 +351,8 @@ protected virtual CGSize GetAutoSpacingItemSize()
leftSize -= spacing;
} while (true);

DataSource.LoadMoreMargin = Element.LoadMoreMargin / (float)columnCount * (float)itemHeight;

var contentWidth = itemWidth * columnCount + spacing * (columnCount - 1f);

var insetSum = Frame.Width - contentWidth;
Expand Down
Loading

0 comments on commit b902957

Please sign in to comment.