Skip to content
This repository was archived by the owner on Jan 17, 2024. It is now read-only.

Commit

Permalink
updated to v1.5.1
Browse files Browse the repository at this point in the history
  • Loading branch information
michael811125 committed Nov 7, 2023
1 parent 193a05a commit dd1056b
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 54 deletions.
10 changes: 10 additions & 0 deletions Assets/InfiniteScrollView/Scripts/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
## CHANGELOG

## [1.5.1] - 2023-11-08
- Added OnSnap method in InfiniteCell.
```C#
public virtual void OnSnap()
```
- Modified OnClick can override in InfiniteCell.
```C#
ublic virtual void OnClick()
```

## [1.5.0] - 2023-11-07
- Added DataOrder.
```C#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,11 @@ public class #SCRIPTNAME# : InfiniteCell
* Do Somethings On Cell Recycle In Here
*/
}

public override void OnSnap()
{
/**
* Do Somethings On Cell Snap In Here
*/
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,10 @@ public override void Snap(int index, float duration)
width += this._dataList[i * this.rowCount].cellSize.x + this.spacing.x;
}

width = this.CalculateSnapPos(ScrollType.Horizontal, this.snapAlign, width, _dataList[index]);

if (this.scrollRect.content.anchoredPosition.x != width)
{
// Check content direction pivot
this.DoSnapping(new Vector2(width * this._contentDirCoeff, 0), duration);
}
var cellData = this._dataList[index];
width = this.CalculateSnapPos(ScrollType.Horizontal, this.snapAlign, width, cellData);
// Check content direction pivot
this.DoSnapping(index, new Vector2(width * this._contentDirCoeff, 0), duration);
}
#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,10 @@ public override void Snap(int index, float duration)
width += this._dataList[i].cellSize.x + this.spacing;
}

width = this.CalculateSnapPos(ScrollType.Horizontal, this.snapAlign, width, this._dataList[index]);

if (this.scrollRect.content.anchoredPosition.x != width)
{
// Check content direction pivot
this.DoSnapping(new Vector2(width * this._contentDirCoeff, 0), duration);
}
var cellData = this._dataList[index];
width = this.CalculateSnapPos(ScrollType.Horizontal, this.snapAlign, width, cellData);
// Check content direction pivot
this.DoSnapping(index, new Vector2(width * this._contentDirCoeff, 0), duration);
}

public override bool Remove(int index, bool withRefresh = true)
Expand Down
25 changes: 13 additions & 12 deletions Assets/InfiniteScrollView/Scripts/Runtime/InfiniteCell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,30 @@ namespace InfiniteScrollViews
{
public class InfiniteCell : MonoBehaviour
{
public event Action<InfiniteCell> onSelected;
internal event Action<InfiniteCell> onSelected;

private RectTransform rectTransform;
private RectTransform _rectTransform;
public RectTransform RectTransform
{
get
{
if (rectTransform == null)
rectTransform = transform as RectTransform;
return rectTransform;
if (_rectTransform == null)
this._rectTransform = transform as RectTransform;
return this._rectTransform;
}
}

private InfiniteCellData cellData;
private InfiniteCellData _cellData;
public InfiniteCellData CellData
{
set
{
cellData = value;
OnRefresh();
this._cellData = value;
this.OnRefresh();
}
get
{
return cellData;
return this._cellData;
}
}

Expand All @@ -39,18 +39,19 @@ public virtual void OnRefresh() { }

public virtual void OnRecycle() { }

public virtual void OnSnap() { }

/// <summary>
/// Button event
/// </summary>
public void OnClick()
public virtual void OnClick()
{
this.InvokeSelected();
}

protected void InvokeSelected()
{
if (onSelected != null)
onSelected.Invoke(this);
this.onSelected?.Invoke(this);
}
}
}
Expand Down
31 changes: 18 additions & 13 deletions Assets/InfiniteScrollView/Scripts/Runtime/InfiniteScrollView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public class Padding
public Action<Vector2> onValueChanged;
public Action onRectTransformDimensionsChanged;
public Action onRefreshed;
public event Action<InfiniteCell> onCellSelected;
public Action<InfiniteCell> onCellSelected;

// Task cancellation
private CancellationTokenSource _cts;
Expand Down Expand Up @@ -221,7 +221,7 @@ public virtual void Add(InfiniteCellData data, bool autoRefresh = false)

this._dataList.Add(data);
this._cellList.Add(null);
this.RefreshCellDataIndex(this._dataList.Count - 1);
this._RefreshCellDataIndex(this._dataList.Count - 1);
if (autoRefresh) this.Refresh();
}

Expand All @@ -242,7 +242,7 @@ public virtual bool Insert(int index, InfiniteCellData data)

this._dataList.Insert(index, data);
this._cellList.Insert(index, null);
this.RefreshCellDataIndex(index);
this._RefreshCellDataIndex(index);
return true;
}

Expand All @@ -262,7 +262,7 @@ public virtual bool Remove(int index, bool autoRefresh = true)

this._dataList[index].Dispose();
this._dataList.RemoveAt(index);
this.RefreshCellDataIndex(index);
this._RefreshCellDataIndex(index);
this.RecycleCell(index);
this._cellList.RemoveAt(index);
if (autoRefresh) this.Refresh();
Expand Down Expand Up @@ -430,14 +430,14 @@ public void SnapLast(float duration)
this.Snap(index, duration);
}

protected void DoSnapping(Vector2 target, float duration)
protected void DoSnapping(int index, Vector2 target, float duration)
{
this.StopSnapping();
this._StopSnapping();
this._cts = new CancellationTokenSource();
this.ProcessSnapping(target, duration).Forget();
this._ProcessSnapping(index, target, duration).Forget();
}

private void StopSnapping()
private void _StopSnapping()
{
if (this._cts != null)
{
Expand All @@ -447,7 +447,7 @@ private void StopSnapping()
}
}

private async UniTask ProcessSnapping(Vector2 target, float duration)
private async UniTask _ProcessSnapping(int index, Vector2 target, float duration)
{
this.scrollRect.velocity = Vector2.zero;

Expand Down Expand Up @@ -482,6 +482,11 @@ private async UniTask ProcessSnapping(Vector2 target, float duration)
*/
}

if (index < 0) index = 0;
else if (index >= this._dataList.Count) index = this._dataList.Count - 1;
var cell = this._cellList[index];
if (cell != null) cell.OnSnap();

// After snap end to release cts
this._cts.Cancel();
this._cts.Dispose();
Expand Down Expand Up @@ -542,7 +547,7 @@ protected void SetupCell(InfiniteCell cell, int index, Vector2 pos)
this._cellList[index] = cell;
cell.CellData = this._dataList[index];
cell.RectTransform.anchoredPosition = pos;
cell.onSelected += this.OnCellSelected;
cell.onSelected += this._OnCellSelected;
cell.gameObject.SetActive(true);
}
}
Expand All @@ -553,19 +558,19 @@ protected void RecycleCell(int index)
{
var cell = this._cellList[index];
this._cellList[index] = null;
cell.onSelected -= this.OnCellSelected;
cell.onSelected -= this._OnCellSelected;
cell.gameObject.SetActive(false);
cell.OnRecycle();
this._cellPool.Enqueue(cell);
}
}

private void OnCellSelected(InfiniteCell selectedCell)
private void _OnCellSelected(InfiniteCell selectedCell)
{
this.onCellSelected?.Invoke(selectedCell);
}

private void RefreshCellDataIndex(int beginIndex)
private void _RefreshCellDataIndex(int beginIndex)
{
// Optimized refresh efficiency
for (int i = beginIndex; i < this._dataList.Count; i++)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,10 @@ public override void Snap(int index, float duration)
height += this._dataList[i * this.columeCount].cellSize.y + this.spacing.y;
}

height = this.CalculateSnapPos(ScrollType.Vertical, this.snapAlign, height, this._dataList[index]);

if (this.scrollRect.content.anchoredPosition.y != height)
{
// Check content direction pivot
this.DoSnapping(new Vector2(0, height * this._contentDirCoeff), duration);
}
var cellData = this._dataList[index];
height = this.CalculateSnapPos(ScrollType.Vertical, this.snapAlign, height, cellData);
// Check content direction pivot
this.DoSnapping(index, new Vector2(0, height * this._contentDirCoeff), duration);
}
#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,10 @@ public override void Snap(int index, float duration)
height += this._dataList[i].cellSize.y + this.spacing;
}

height = this.CalculateSnapPos(ScrollType.Vertical, this.snapAlign, height, this._dataList[index]);

if (this.scrollRect.content.anchoredPosition.y != height)
{
// Check content direction pivot
this.DoSnapping(new Vector2(0, height * this._contentDirCoeff), duration);
}
var cellData = this._dataList[index];
height = this.CalculateSnapPos(ScrollType.Vertical, this.snapAlign, height, cellData);
// Check content direction pivot
this.DoSnapping(index, new Vector2(0, height * this._contentDirCoeff), duration);
}

public override bool Remove(int index, bool withRefresh = true)
Expand Down
2 changes: 1 addition & 1 deletion Assets/InfiniteScrollView/Scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "com.michaelo.infinitescrollview.unitask",
"displayName": "InfiniteScrollView with UniTask",
"description": "InfiniteScrollView is made for Unity extension, that support use as less as possible gameObject count to achieve large infinite scrolling content. Developed by native UGUI system, no any magical code inside, so you can easily modify and extend by yourself.",
"version": "1.5.0",
"version": "1.5.1",
"unity": "2021.3",
"license": "MIT",
"samples": [
Expand Down

0 comments on commit dd1056b

Please sign in to comment.