-
Notifications
You must be signed in to change notification settings - Fork 0
/
ViewHolder.cs
86 lines (68 loc) · 2.18 KB
/
ViewHolder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using System.Linq;
using JetBrains.Annotations;
using UnityEngine;
namespace Pinch.RecyclerView
{
public abstract class ViewHolder<T> : IViewHolderInfo
{
private GameObject _itemView;
private RectTransform _rectTransform;
private int _currentIndex;
private long _timeStamp;
int IViewHolderInfo.LastIndex { get; set; }
int IViewHolderInfo.CurrentIndex
{
get => _currentIndex;
set => _currentIndex = value;
}
GameObject IViewHolderInfo.ItemView
{
get => _itemView;
set => _itemView = value;
}
RectTransform IViewHolderInfo.RectTransform
{
get => _rectTransform;
set => _rectTransform = value;
}
Status IViewHolderInfo.Status { get; set; }
protected ViewHolder(GameObject itemView)
{
_itemView = itemView;
_rectTransform = itemView.GetComponent<RectTransform>();
}
[UsedImplicitly]
protected void Start()
{
}
[UsedImplicitly]
public int GetAdapterPosition()
{
return _currentIndex;
}
private void Destroy()
{
Object.Destroy(_itemView);
}
private bool IsHidden()
{
return !IsVisibleFrom(_itemView.GetComponent<RectTransform>());
}
private static bool IsVisibleFrom(RectTransform rectTransform)
{
return CountCornersVisibleFrom(rectTransform) > 0;
}
private static int CountCornersVisibleFrom(RectTransform rectTransform)
{
var screenBounds = new Rect(0f, 0f, Screen.width, Screen.height);
var objectCorners = new Vector3[4];
rectTransform.GetWorldCorners(objectCorners);
return objectCorners.Count(t => screenBounds.Contains(t));
}
[UsedImplicitly]
private int CompareTo(ViewHolder<T> vh) =>
vh._currentIndex > _currentIndex ? -1 : vh._currentIndex > _currentIndex ? 1 : 0;
void IViewHolderInfo.Destroy() => Destroy();
bool IViewHolderInfo.IsHidden() => IsHidden();
}
}