| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using DCL.Chat.Channels; |
| | 4 | | using DCL.Helpers; |
| | 5 | | using TMPro; |
| | 6 | | using UnityEngine; |
| | 7 | | using UnityEngine.UI; |
| | 8 | |
|
| | 9 | | namespace DCL.Chat.HUD |
| | 10 | | { |
| | 11 | | public class SearchChannelsWindowComponentView : BaseComponentView, ISearchChannelsWindowView |
| | 12 | | { |
| | 13 | | [SerializeField] internal CollapsablePublicChannelListComponentView channelList; |
| | 14 | | [SerializeField] internal GameObject resultsHeaderLabelContainer; |
| | 15 | | [SerializeField] internal TMP_Text resultsHeaderLabel; |
| | 16 | | [SerializeField] internal GameObject loadingContainer; |
| | 17 | | [SerializeField] internal ScrollRect scroll; |
| | 18 | | [SerializeField] internal SearchBarComponentView searchBar; |
| | 19 | | [SerializeField] internal Button backButton; |
| | 20 | | [SerializeField] internal Button closeButton; |
| | 21 | | [SerializeField] internal GameObject loadMoreContainer; |
| | 22 | | [SerializeField] internal GameObject loadMoreContent; |
| | 23 | | [SerializeField] internal GameObject loadMoreSpinner; |
| | 24 | | [SerializeField] internal GameObject createChannelOnSearchContainer; |
| | 25 | | [SerializeField] internal GameObject createChannelOnSearchContent; |
| | 26 | | [SerializeField] internal Button[] createChannelButtons; |
| | 27 | |
|
| | 28 | | private bool isLayoutDirty; |
| | 29 | | private bool isSortDirty; |
| | 30 | | private Vector2 lastScrollPosition; |
| | 31 | | private Coroutine requireMoreEntriesRoutine; |
| | 32 | | private bool shouldCreationBeActive; |
| | 33 | | private bool isLoading; |
| | 34 | |
|
| | 35 | | public event Action OnBack; |
| | 36 | | public event Action OnClose; |
| | 37 | | public event Action<string> OnSearchUpdated; |
| | 38 | | public event Action OnRequestMoreChannels; |
| | 39 | | public event Action<string> OnJoinChannel; |
| | 40 | | public event Action<string> OnLeaveChannel; |
| | 41 | | public event Action OnCreateChannel; |
| | 42 | |
|
| 0 | 43 | | public RectTransform Transform => (RectTransform) transform; |
| 0 | 44 | | public int EntryCount => channelList.Count(); |
| 0 | 45 | | public bool IsActive => gameObject.activeInHierarchy; |
| | 46 | |
|
| | 47 | | public override void Awake() |
| | 48 | | { |
| 21 | 49 | | base.Awake(); |
| 21 | 50 | | backButton.onClick.AddListener(() => OnBack?.Invoke()); |
| 21 | 51 | | closeButton.onClick.AddListener(() => OnClose?.Invoke()); |
| 26 | 52 | | searchBar.OnSearchText += s => OnSearchUpdated?.Invoke(s); |
| 21 | 53 | | channelList.SortingMethod = (a, b) => b.Model.memberCount.CompareTo(a.Model.memberCount); |
| 21 | 54 | | scroll.onValueChanged.AddListener(LoadMoreEntries); |
| | 55 | |
|
| 168 | 56 | | foreach (var button in createChannelButtons) |
| 63 | 57 | | button.onClick.AddListener(() => OnCreateChannel?.Invoke()); |
| 21 | 58 | | } |
| | 59 | |
|
| | 60 | | public override void Update() |
| | 61 | | { |
| 363 | 62 | | base.Update(); |
| | 63 | |
|
| 363 | 64 | | if (isLayoutDirty) |
| 1 | 65 | | ((RectTransform) scroll.transform).ForceUpdateLayout(); |
| 363 | 66 | | isLayoutDirty = false; |
| | 67 | |
|
| 363 | 68 | | if (isSortDirty) |
| 0 | 69 | | channelList.Sort(); |
| 363 | 70 | | isSortDirty = false; |
| 363 | 71 | | } |
| | 72 | |
|
| | 73 | | public override void Dispose() |
| | 74 | | { |
| 42 | 75 | | if (!this) return; |
| 42 | 76 | | if (!gameObject) return; |
| 42 | 77 | | base.Dispose(); |
| 42 | 78 | | } |
| | 79 | |
|
| | 80 | | public static SearchChannelsWindowComponentView Create() |
| | 81 | | { |
| 21 | 82 | | return Instantiate(Resources.Load<SearchChannelsWindowComponentView>("SocialBarV1/ChannelSearchHUD")); |
| | 83 | | } |
| | 84 | |
|
| | 85 | | public void ClearAllEntries() |
| | 86 | | { |
| 3 | 87 | | channelList.Clear(); |
| 3 | 88 | | UpdateLayout(); |
| 3 | 89 | | UpdateHeaders(); |
| 3 | 90 | | } |
| | 91 | |
|
| | 92 | | public void ShowLoading() |
| | 93 | | { |
| 2 | 94 | | isLoading = true; |
| 2 | 95 | | loadingContainer.SetActive(true); |
| 2 | 96 | | channelList.gameObject.SetActive(false); |
| 2 | 97 | | resultsHeaderLabel.gameObject.SetActive(false); |
| 2 | 98 | | createChannelOnSearchContent.SetActive(false); |
| 2 | 99 | | loadMoreContent.SetActive(false); |
| 2 | 100 | | } |
| | 101 | |
|
| | 102 | | public void Set(Channel channel) |
| | 103 | | { |
| 13 | 104 | | channelList.Set(channel.ChannelId, |
| | 105 | | new PublicChatEntryModel(channel.ChannelId, channel.Name, channel.Joined, channel.MemberCount, |
| | 106 | | showOnlyOnlineMembers: false, channel.Muted)); |
| | 107 | |
|
| 13 | 108 | | var entry = channelList.Get(channel.ChannelId); |
| 13 | 109 | | entry.OnOpenChat -= HandleJoinRequest; |
| 13 | 110 | | entry.OnOpenChat += HandleJoinRequest; |
| 13 | 111 | | entry.OnLeave -= HandleLeaveRequest; |
| 13 | 112 | | entry.OnLeave += HandleLeaveRequest; |
| | 113 | |
|
| 13 | 114 | | UpdateLayout(); |
| 13 | 115 | | Sort(); |
| 13 | 116 | | UpdateHeaders(); |
| 13 | 117 | | } |
| | 118 | |
|
| | 119 | | public void Show() |
| | 120 | | { |
| 8 | 121 | | gameObject.SetActive(true); |
| 8 | 122 | | searchBar.SetFocus(); |
| 8 | 123 | | } |
| | 124 | |
|
| 2 | 125 | | public void Hide() => gameObject.SetActive(false); |
| | 126 | |
|
| 2 | 127 | | public void ClearSearchInput() => searchBar.ClearSearch(); |
| | 128 | |
|
| | 129 | | public void HideLoading() |
| | 130 | | { |
| 1 | 131 | | isLoading = false; |
| 1 | 132 | | loadingContainer.SetActive(false); |
| 1 | 133 | | channelList.gameObject.SetActive(true); |
| 1 | 134 | | resultsHeaderLabel.gameObject.SetActive(true); |
| 1 | 135 | | createChannelOnSearchContent.SetActive(shouldCreationBeActive); |
| 1 | 136 | | loadMoreContent.SetActive(true); |
| 1 | 137 | | } |
| | 138 | |
|
| 1 | 139 | | public void ShowLoadingMore() => loadMoreContainer.SetActive(true); |
| | 140 | |
|
| 1 | 141 | | public void HideLoadingMore() => loadMoreContainer.SetActive(false); |
| | 142 | |
|
| 1 | 143 | | public void ShowResultsHeader() => resultsHeaderLabelContainer.SetActive(true); |
| | 144 | |
|
| 1 | 145 | | public void HideResultsHeader() => resultsHeaderLabelContainer.SetActive(false); |
| | 146 | |
|
| 1 | 147 | | public void ShowCreateChannelOnSearch() => createChannelOnSearchContainer.SetActive(true); |
| | 148 | |
|
| 1 | 149 | | public void HideCreateChannelOnSearch() => createChannelOnSearchContainer.SetActive(false); |
| | 150 | |
|
| | 151 | | public void SetCreateChannelButtonsActive(bool isActive) |
| | 152 | | { |
| 1 | 153 | | shouldCreationBeActive = isActive; |
| | 154 | |
|
| 8 | 155 | | foreach (var button in createChannelButtons) |
| 3 | 156 | | button.gameObject.SetActive(isActive); |
| | 157 | |
|
| 1 | 158 | | createChannelOnSearchContent.SetActive(isActive && !isLoading); |
| 1 | 159 | | } |
| | 160 | |
|
| | 161 | | public override void RefreshControl() |
| | 162 | | { |
| 0 | 163 | | throw new NotImplementedException(); |
| | 164 | | } |
| | 165 | |
|
| 0 | 166 | | private void UpdateLayout() => isLayoutDirty = true; |
| | 167 | |
|
| 0 | 168 | | private void Sort() => isSortDirty = true; |
| | 169 | |
|
| | 170 | | private void UpdateHeaders() |
| | 171 | | { |
| 16 | 172 | | var text = $"Results ({channelList.Count()})"; |
| | 173 | |
|
| 16 | 174 | | if (!string.IsNullOrEmpty(searchBar.Text)) |
| 4 | 175 | | text = "Did you mean?"; |
| | 176 | |
|
| 16 | 177 | | resultsHeaderLabel.text = text; |
| 16 | 178 | | } |
| | 179 | |
|
| 0 | 180 | | private void HandleJoinRequest(PublicChatEntry entry) => OnJoinChannel?.Invoke(entry.Model.channelId); |
| | 181 | |
|
| 1 | 182 | | private void HandleLeaveRequest(PublicChatEntry entry) => OnLeaveChannel?.Invoke(entry.Model.channelId); |
| | 183 | |
|
| | 184 | | private void LoadMoreEntries(Vector2 scrollPosition) |
| | 185 | | { |
| 2 | 186 | | if (scrollPosition.y < 0.005f && lastScrollPosition.y >= 0.005f) |
| | 187 | | { |
| 1 | 188 | | if (requireMoreEntriesRoutine != null) |
| 0 | 189 | | StopCoroutine(requireMoreEntriesRoutine); |
| | 190 | |
|
| 1 | 191 | | loadMoreSpinner.SetActive(true); |
| 1 | 192 | | requireMoreEntriesRoutine = StartCoroutine(WaitThenRequireMoreEntries()); |
| | 193 | | } |
| | 194 | |
|
| 2 | 195 | | lastScrollPosition = scrollPosition; |
| 2 | 196 | | } |
| | 197 | |
|
| | 198 | | private IEnumerator WaitThenRequireMoreEntries() |
| | 199 | | { |
| 1 | 200 | | yield return new WaitForSeconds(1f); |
| 1 | 201 | | loadMoreSpinner.SetActive(false); |
| 1 | 202 | | OnRequestMoreChannels?.Invoke(); |
| 1 | 203 | | } |
| | 204 | | } |
| | 205 | | } |