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