| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using DCL.Helpers; |
| | 5 | | using TMPro; |
| | 6 | | using UnityEngine; |
| | 7 | | using UnityEngine.UI; |
| | 8 | |
|
| | 9 | | namespace DCL.Chat.HUD |
| | 10 | | { |
| | 11 | | public class ChannelMembersComponentView : BaseComponentView, IChannelMembersComponentView |
| | 12 | | { |
| | 13 | | private const float REQUEST_MORE_ENTRIES_SCROLL_THRESHOLD = 0.005f; |
| | 14 | | private const int ENTRIES_THROTTLING = 30; |
| | 15 | |
|
| | 16 | | [SerializeField] internal CollapsableChannelMemberListComponentView memberList; |
| | 17 | | [SerializeField] internal GameObject resultsHeaderLabelContainer; |
| | 18 | | [SerializeField] internal TMP_Text resultsHeaderLabel; |
| | 19 | | [SerializeField] internal GameObject loadingContainer; |
| | 20 | | [SerializeField] internal ScrollRect scroll; |
| | 21 | | [SerializeField] internal SearchBarComponentView searchBar; |
| | 22 | | [SerializeField] internal GameObject loadMoreContainer; |
| | 23 | | [SerializeField] internal GameObject loadMoreSpinner; |
| | 24 | |
|
| 16 | 25 | | private readonly Queue<ChannelMemberEntryModel> queuedEntries = new Queue<ChannelMemberEntryModel>(); |
| | 26 | | private bool isLayoutDirty; |
| | 27 | | private bool isSortDirty; |
| | 28 | | private Vector2 lastScrollPosition; |
| | 29 | | private Coroutine requireMoreEntriesRoutine; |
| | 30 | |
|
| | 31 | | public event Action<string> OnSearchUpdated; |
| | 32 | | public event Action OnRequestMoreMembers; |
| | 33 | |
|
| 0 | 34 | | public int EntryCount => memberList.Count() + queuedEntries.Count; |
| | 35 | |
|
| | 36 | | public override void Awake() |
| | 37 | | { |
| 11 | 38 | | base.Awake(); |
| | 39 | |
|
| 12 | 40 | | searchBar.OnSearchText += s => OnSearchUpdated?.Invoke(s); |
| 11 | 41 | | memberList.SortingMethod = (a, b) => a.Model.userName.CompareTo(b.Model.userName); |
| 11 | 42 | | scroll.onValueChanged.AddListener(LoadMoreEntries); |
| 11 | 43 | | } |
| | 44 | |
|
| | 45 | | public override void Update() |
| | 46 | | { |
| 3 | 47 | | base.Update(); |
| | 48 | |
|
| 3 | 49 | | if (isLayoutDirty) |
| 0 | 50 | | ((RectTransform)scroll.transform).ForceUpdateLayout(); |
| | 51 | |
|
| 3 | 52 | | isLayoutDirty = false; |
| | 53 | |
|
| 3 | 54 | | SetQueuedEntries(); |
| | 55 | |
|
| 3 | 56 | | if (isSortDirty) |
| 1 | 57 | | memberList.Sort(); |
| | 58 | |
|
| 3 | 59 | | isSortDirty = false; |
| 3 | 60 | | } |
| | 61 | |
|
| | 62 | | public override void Dispose() |
| | 63 | | { |
| 23 | 64 | | if (!this) return; |
| 23 | 65 | | if (!gameObject) return; |
| 23 | 66 | | base.Dispose(); |
| 23 | 67 | | } |
| | 68 | |
|
| | 69 | | public void ClearAllEntries() |
| | 70 | | { |
| 1 | 71 | | memberList.Clear(); |
| 1 | 72 | | queuedEntries.Clear(); |
| 1 | 73 | | UpdateLayout(); |
| 1 | 74 | | UpdateHeaders(); |
| 1 | 75 | | } |
| | 76 | |
|
| | 77 | | public void ShowLoading() |
| | 78 | | { |
| 1 | 79 | | loadingContainer.SetActive(true); |
| 1 | 80 | | memberList.gameObject.SetActive(false); |
| 1 | 81 | | resultsHeaderLabel.gameObject.SetActive(false); |
| 1 | 82 | | } |
| | 83 | |
|
| 1 | 84 | | public void Set(ChannelMemberEntryModel user) => queuedEntries.Enqueue(user); |
| | 85 | |
|
| 1 | 86 | | public override void Show(bool instant = false) => gameObject.SetActive(true); |
| | 87 | |
|
| 1 | 88 | | public override void Hide(bool instant = false) => gameObject.SetActive(false); |
| | 89 | |
|
| 1 | 90 | | public void ClearSearchInput() => searchBar.ClearSearch(); |
| | 91 | |
|
| | 92 | | public void HideLoading() |
| | 93 | | { |
| 1 | 94 | | loadingContainer.SetActive(false); |
| 1 | 95 | | memberList.gameObject.SetActive(true); |
| 1 | 96 | | resultsHeaderLabel.gameObject.SetActive(true); |
| 1 | 97 | | } |
| | 98 | |
|
| 1 | 99 | | public void ShowLoadingMore() => loadMoreContainer.SetActive(true); |
| | 100 | |
|
| 1 | 101 | | public void HideLoadingMore() => loadMoreContainer.SetActive(false); |
| | 102 | |
|
| 1 | 103 | | public void ShowResultsHeader() => resultsHeaderLabelContainer.SetActive(true); |
| | 104 | |
|
| 1 | 105 | | public void HideResultsHeader() => resultsHeaderLabelContainer.SetActive(false); |
| | 106 | |
|
| 0 | 107 | | public override void RefreshControl() { } |
| | 108 | |
|
| 0 | 109 | | private void UpdateLayout() => isLayoutDirty = true; |
| | 110 | |
|
| 0 | 111 | | private void Sort() => isSortDirty = true; |
| | 112 | |
|
| | 113 | | private void UpdateHeaders() |
| | 114 | | { |
| 2 | 115 | | var text = $"Results ({memberList.Count()})"; |
| | 116 | |
|
| 2 | 117 | | if (!string.IsNullOrEmpty(searchBar.Text)) |
| 0 | 118 | | text = "Did you mean?"; |
| | 119 | |
|
| 2 | 120 | | resultsHeaderLabel.text = text; |
| 2 | 121 | | } |
| | 122 | |
|
| | 123 | | private void LoadMoreEntries(Vector2 scrollPosition) |
| | 124 | | { |
| 0 | 125 | | if (scrollPosition.y < REQUEST_MORE_ENTRIES_SCROLL_THRESHOLD && |
| | 126 | | lastScrollPosition.y >= REQUEST_MORE_ENTRIES_SCROLL_THRESHOLD) |
| | 127 | | { |
| 0 | 128 | | if (requireMoreEntriesRoutine != null) |
| 0 | 129 | | StopCoroutine(requireMoreEntriesRoutine); |
| | 130 | |
|
| 0 | 131 | | loadMoreSpinner.SetActive(true); |
| 0 | 132 | | requireMoreEntriesRoutine = StartCoroutine(WaitThenRequireMoreEntries()); |
| | 133 | | } |
| | 134 | |
|
| 0 | 135 | | lastScrollPosition = scrollPosition; |
| 0 | 136 | | } |
| | 137 | |
|
| | 138 | | private IEnumerator WaitThenRequireMoreEntries() |
| | 139 | | { |
| 0 | 140 | | yield return new WaitForSeconds(1f); |
| 0 | 141 | | loadMoreSpinner.SetActive(false); |
| 0 | 142 | | OnRequestMoreMembers?.Invoke(); |
| 0 | 143 | | } |
| | 144 | |
|
| | 145 | | private void SetQueuedEntries() |
| | 146 | | { |
| 5 | 147 | | if (queuedEntries.Count <= 0) return; |
| | 148 | |
|
| 4 | 149 | | for (var i = 0; i < ENTRIES_THROTTLING && queuedEntries.Count > 0; i++) |
| | 150 | | { |
| 1 | 151 | | var user = queuedEntries.Dequeue(); |
| 1 | 152 | | memberList.Set(user.userId, user); |
| | 153 | | } |
| | 154 | |
|
| 1 | 155 | | UpdateLayout(); |
| 1 | 156 | | Sort(); |
| 1 | 157 | | UpdateHeaders(); |
| 1 | 158 | | } |
| | 159 | |
|
| | 160 | | public static ChannelMembersComponentView Create() |
| | 161 | | { |
| 0 | 162 | | return Instantiate(Resources.Load<ChannelMembersComponentView>("SocialBarV1/ChannelMembersHUD")); |
| | 163 | | } |
| | 164 | | } |
| | 165 | | } |