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