Post by mystigan on Feb 19, 2020 3:10:30 GMT
Hello, I am trying to add pagination to a list and I have these two scripts - UILeaderboards (handling the UI and scroller), GetLeaderboards for getting the leaderboards.
also there are two types of leaderboards which I am handling in one script hence the boolean m_IsVampire.
However I am getting Scroller.reloadData as null ref - which is kind of confusing as the two lists are never null, these should be empty under all cases.
Here is an example of the UI
using System.Collections;
using System.Collections.Generic;
using Newtonsoft.Json;
using UnityEngine;
using UnityEngine.UI;
using EnhancedUI.EnhancedScroller;
namespace Raincrow.Leaderboards.UI
{
public class UILeaderboards : UIAnimateUpPanel, IEnhancedScrollerDelegate
{
private static UILeaderboards m_Instance;
public static bool IsShowing
{
get
{
if (m_Instance == null)
return false;
else
return m_Instance.m_IsShowing;
}
}
[Header("Child")]
[SerializeField] private Button m_CloseBtn;
[SerializeField] private EnhancedScroller m_Scroller;
[SerializeField] private UIDrudgeLeaderboardItem m_DrudgePrefab;
[SerializeField] private UILoadingLeaderboardItem m_LoadingPrefab;
[SerializeField] private UIVampireLeaderboardItem m_VampirePrefab;
private List<DrudgeLeader> m_DrudgeLeaders;
private List<VampireLeader> m_VampireLeaders;
private bool m_IsVampire;
private bool m_IsLoading;
private System.Action m_OnClose;
protected override void Awake()
{
m_CloseBtn.onClick.AddListener(() =>
{
_Close();
});
m_VampireLeaders = new List<VampireLeader>();
m_DrudgeLeaders = new List<DrudgeLeader>();
m_Scroller.Delegate = this;
m_Scroller.scrollerScrolled = ScrollerScrolled;
base.Awake();
}
public static void Show(System.Action onClose = null)
{
if (m_Instance == null)
{
var prefab = Resources.Load<UILeaderboards>("UILeaderboards");
m_Instance = Instantiate(prefab, Vector3.zero, Quaternion.identity) as UILeaderboards;
}
m_Instance._Show(onClose);
}
private void _Show(System.Action onClose)
{
base.Show();
GetLeaderboards.Initialize();
GetLeaderboards.NextDrudges(list =>
{
m_DrudgeLeaders = list;
Debug.Log(m_DrudgeLeaders.Count + " COUNTT");
Debug.Log(m_VampireLeaders.Count + " COUNTT");
m_Scroller.ReloadData();
});
}
private void _Close()
{
m_OnClose?.Invoke();
base.Close();
}
public int GetNumberOfCells(EnhancedScroller scroller)
{
return (m_IsVampire ? m_VampireLeaders.Count : m_DrudgeLeaders.Count) + 1;
}
public float GetCellViewSize(EnhancedScroller scroller, int dataIndex)
{
float size = 0;
if (m_IsVampire)
{
size = dataIndex == m_VampireLeaders.Count ? 60 : 202;
}
else
{
size = dataIndex == m_DrudgeLeaders.Count ? 60 : 202;
}
return size;
}
public EnhancedScrollerCellView GetCellView(EnhancedScroller scroller, int dataIndex, int cellIndex)
{
int count = m_IsVampire ? m_VampireLeaders.Count : m_DrudgeLeaders.Count;
if (dataIndex == count)
{
UILoadingLeaderboardItem cellView = scroller.GetCellView(m_LoadingPrefab) as UILoadingLeaderboardItem;
return cellView;
}
else if (m_IsVampire)
{
UIVampireLeaderboardItem cellView = scroller.GetCellView(m_VampirePrefab) as UIVampireLeaderboardItem;
cellView.Setup(m_VampireLeaders[dataIndex], dataIndex);
return cellView;
}
else
{
try
{
UIDrudgeLeaderboardItem cellView = scroller.GetCellView(m_DrudgePrefab) as UIDrudgeLeaderboardItem;
cellView.Setup(m_DrudgeLeaders[dataIndex], dataIndex);
return cellView;
}
catch (System.Exception)
{
Debug.Log(dataIndex);
}
}
return null;
}
private void ScrollerScrolled(EnhancedScroller scroller, Vector2 val, float scrollPosition)
{
// if the scroller is at the end of the list and not already loading
if (scroller.NormalizedScrollPosition >= 1f && !m_IsLoading)
{
Debug.Log("Load More");
int count = m_IsVampire ? m_VampireLeaders.Count : m_DrudgeLeaders.Count;
m_IsLoading = true;
if (m_IsVampire)
{
GetLeaderboards.NextVampires(list =>
{
m_VampireLeaders = list;
RefreshScroller(count);
});
}
else
{
GetLeaderboards.NextDrudges(list =>
{
m_DrudgeLeaders = list;
RefreshScroller(count);
});
}
}
}
private void RefreshScroller(int previousLastIndex)
{
m_Scroller.ReloadData();
m_Scroller.JumpToDataIndex(previousLastIndex, 1, 1);
m_IsLoading = false;
}
}
}
using System.Collections.Generic;
using Newtonsoft.Json;
using UnityEngine;
namespace Raincrow.Leaderboards
{
public class GetLeaderboards
{
private static List<DrudgeLeader> m_DrudgeLeaders;
private static List<VampireLeader> m_VampireLeaders;
public static int m_Skip => 20;
private static int m_Max = 100;
private static int m_DrudgePointer = 0;
private static int m_VampirePointer = 0;
public static void Initialize()
{
m_DrudgePointer = m_VampirePointer = 0;
}
public static void NextDrudges(System.Action<List<DrudgeLeader>> callback)
{
if (m_DrudgeLeaders == null)
{
m_DrudgeLeaders = new List<DrudgeLeader>();
Get(true, 0, s =>
{
m_DrudgeLeaders = JsonConvert.DeserializeObject<List<DrudgeLeader>>(s);
callback?.Invoke(m_DrudgeLeaders);
});
}
else
{
if (m_DrudgeLeaders.Count > m_DrudgePointer + m_Skip)
{
callback?.Invoke(m_DrudgeLeaders.GetRange(m_DrudgePointer, m_DrudgePointer + m_Skip));
}
else
{
Get(true, m_DrudgePointer, s =>
{
m_DrudgeLeaders.AddRange(JsonConvert.DeserializeObject<List<DrudgeLeader>>(s));
callback?.Invoke(m_DrudgeLeaders);
});
}
m_DrudgePointer += m_Skip;
}
}
public static void NextVampires(System.Action<List<VampireLeader>> callback)
{
if (m_VampireLeaders == null)
{
m_VampireLeaders = new List<VampireLeader>();
Get(false, 0, s =>
{
m_VampireLeaders = JsonConvert.DeserializeObject<List<VampireLeader>>(s);
callback?.Invoke(m_VampireLeaders);
});
}
else
{
if (m_VampireLeaders.Count > m_VampirePointer + m_Skip)
{
callback?.Invoke(m_VampireLeaders.GetRange(m_VampirePointer, m_VampirePointer + m_Skip));
}
else
{
Get(false, m_DrudgePointer, s =>
{
m_VampireLeaders.AddRange(JsonConvert.DeserializeObject<List<VampireLeader>>(s));
callback?.Invoke(m_VampireLeaders);
});
}
m_DrudgePointer += m_Skip;
}
}
private static void Get(bool isDrudge, int skip, System.Action<string> callback)
{
var data = new { drudge = isDrudge, skip = skip, count = m_Skip };
ApiManager.Post("get-leaderboards", JsonConvert.SerializeObject(data), (s, r) =>
{
if (r == 200)
{
Debug.Log(s);
callback?.Invoke(s);
}
});
}
}
public class DrudgeLeader
{
public string vampire;
public string name;
public double networth;
public int prestige;
public string dba;
}
public class VampireLeader
{
public string drudge;
public string name;
public int power;
public int country;
public int lair;
public int age;
}
}