Skip to content

Commit 06728bb

Browse files
committed
💈 Infinite scrolling for Unity TSTableView
1 parent ea0a9ae commit 06728bb

File tree

3 files changed

+85
-12
lines changed

3 files changed

+85
-12
lines changed

Assets/Scenes/HighscoresDemo.unity

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3254,7 +3254,7 @@ MonoBehaviour:
32543254
m_HandleRect: {fileID: 1064423869}
32553255
m_Direction: 0
32563256
m_Value: 0
3257-
m_Size: 1
3257+
m_Size: 0.9999999
32583258
m_NumberOfSteps: 0
32593259
m_OnValueChanged:
32603260
m_PersistentCalls:

Assets/Scripts/HighscoresDemo.cs

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ public class HighscoresDemo : MonoBehaviour, ITableViewDataSource
4646
private ScoreCell _cellPrefab;
4747
bool HasNewData = false; // to reload table view when data has changed
4848

49+
// infinite scroll vars
50+
private const float _infiniteScrollSize = 0.2f;
51+
private bool _isLoadingNextPage = false; // load once when scroll buffer is hit
52+
private bool _isInfiniteScroll = true; // enable inifine scrolling
53+
private const uint _noPageResults = 50;
54+
private uint _skip = 0; // no of records to skip
55+
private uint _total = _noPageResults; // default is no more records than requested
56+
4957
[Space(10)]
5058
[SerializeField]
5159
private ModalAlert _modalAlert;
@@ -79,7 +87,9 @@ void Update ()
7987
// Only update table when there is new data
8088
if (HasNewData) {
8189
Debug.Log ("Refresh Table Data");
90+
SetInteractableScrollbars (false);
8291
_tableView.ReloadData ();
92+
SetInteractableScrollbars (true);
8393
HasNewData = false;
8494
}
8595
// Display new score details
@@ -96,6 +106,56 @@ void Update ()
96106
}
97107
}
98108

109+
#region Infinite Scroll private methods for Highscores
110+
111+
void OnEnable()
112+
{
113+
_tableView.GetComponent<ScrollRect>().onValueChanged.AddListener(OnScrollValueChanged);
114+
}
115+
116+
void OnDisable()
117+
{
118+
_tableView.GetComponent<ScrollRect>().onValueChanged.RemoveListener(OnScrollValueChanged);
119+
}
120+
121+
private void OnScrollValueChanged(Vector2 newScrollValue)
122+
{
123+
// skip if not infinite scroll or if still loading next page of results
124+
if (!_isInfiniteScroll || _isLoadingNextPage)
125+
{
126+
return;
127+
}
128+
//Debug.Log (string.Format("Scroll y:{0} table view scroll: {1}/{2}", newScrollValue.y, _tableView.scrollY, _tableView.scrollableHeight));
129+
float scrollY = _tableView.scrollableHeight - _tableView.scrollY;
130+
float scrollBuffer = _infiniteScrollSize * _tableView.scrollableHeight;
131+
// scrollY is still at 'top' and so no need to load anything at this point
132+
if (scrollY > scrollBuffer)
133+
{
134+
return;
135+
}
136+
// scrollY has reached 'bottom' minus buffer size
137+
// only trigger request if there are more records to load
138+
if (_skip < _total)
139+
{
140+
_isLoadingNextPage = true;
141+
_skip += _noPageResults;
142+
Debug.Log (string.Format("Load next page @{0} scroll: {1}<{2}", _skip, scrollY, scrollBuffer));
143+
GetPageHighscores ();
144+
}
145+
}
146+
147+
// Tip: When infinite scrolling and using TSTableView's ReloadData() method I prefer to wrap "disable and enable scrollbar" calls around it to help prevent jumpy behaviour when continously dragging the scrollbar thumb.
148+
private void SetInteractableScrollbars(bool isInteractable)
149+
{
150+
Scrollbar[] scrollbars = _tableView.GetComponentsInChildren<Scrollbar> ();
151+
foreach (Scrollbar scrollbar in scrollbars) {
152+
//Debug.Log (string.Format("Scrollbar {0} is {1}",scrollbar.name, isInteractable));
153+
scrollbar.interactable = isInteractable;
154+
}
155+
}
156+
157+
#endregion
158+
99159
public void Login()
100160
{
101161
_client.Login(MobileServiceAuthenticationProvider.Facebook, _facebookAccessToken, OnLoginCompleted);
@@ -208,22 +268,35 @@ private void OnReadNestedResultsCompleted(IRestResponse<NestedResults<Highscore>
208268
{
209269
Debug.Log("OnReadNestedResultsCompleted: " + response.ResponseUri +" data: "+ response.Content);
210270
List<Highscore> items = response.Data.results;
271+
_total = response.Data.count;
211272
Debug.Log("Read items count: " + items.Count + "/" + response.Data.count);
212-
_scores = items;
273+
// append results if paginated, otherwise set
274+
if (_skip != 0) {
275+
_scores.AddRange (items);
276+
} else {
277+
_scores = items;
278+
}
213279
HasNewData = true;
214280
}
215281
else
216282
{
217283
Debug.Log("Read Nested Results Error Status:" + response.StatusCode + " Uri: "+response.ResponseUri );
218284
}
285+
_isLoadingNextPage = false; // allows next page to be loaded
219286
}
220287

221288
public void GetAllHighscores()
222289
{
223-
CustomQuery query = new CustomQuery ("", "score desc", 50, 0, "id,username,score"); //CustomQuery.OrderBy ("score desc");
224-
_table.Query<NestedResults<Highscore>>(query, OnReadNestedResultsCompleted); //Query(query);
290+
// reset
291+
_skip = 0;
292+
GetPageHighscores();
225293
}
226294

295+
private void GetPageHighscores()
296+
{
297+
CustomQuery query = new CustomQuery ("", "score desc", _noPageResults, _skip, "id,username,score"); //CustomQuery.OrderBy ("score desc");
298+
_table.Query<NestedResults<Highscore>>(query, OnReadNestedResultsCompleted); //Query(query);
299+
}
227300

228301
public void GetTopHighscores()
229302
{

ProjectSettings/GraphicsSettings.asset

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ GraphicsSettings:
4444
useReflectionProbeBoxProjection: 0
4545
useReflectionProbeBlending: 0
4646
m_ShaderSettings_Tier2:
47-
useCascadedShadowMaps: 1
48-
standardShaderQuality: 2
49-
useReflectionProbeBoxProjection: 1
50-
useReflectionProbeBlending: 1
47+
useCascadedShadowMaps: 0
48+
standardShaderQuality: 1
49+
useReflectionProbeBoxProjection: 0
50+
useReflectionProbeBlending: 0
5151
m_ShaderSettings_Tier3:
52-
useCascadedShadowMaps: 1
53-
standardShaderQuality: 2
54-
useReflectionProbeBoxProjection: 1
55-
useReflectionProbeBlending: 1
52+
useCascadedShadowMaps: 0
53+
standardShaderQuality: 1
54+
useReflectionProbeBoxProjection: 0
55+
useReflectionProbeBlending: 0
5656
m_BuildTargetShaderSettings: []
5757
m_LightmapStripping: 0
5858
m_FogStripping: 0

0 commit comments

Comments
 (0)