| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | | using DCL.Helpers; |
| | 6 | | using TMPro; |
| | 7 | | using UnityEngine; |
| | 8 | | using UnityEngine.UI; |
| | 9 | |
|
| | 10 | | public class WorldChatWindowComponentView : BaseComponentView, IWorldChatWindowView, IComponentModelConfig<WorldChatWind |
| | 11 | | { |
| | 12 | | private const int CREATION_AMOUNT_PER_FRAME = 5; |
| | 13 | | private const int AVATAR_SNAPSHOTS_PER_FRAME = 5; |
| | 14 | | private const float REQUEST_MORE_ENTRIES_SCROLL_THRESHOLD = 0.005f; |
| | 15 | | private const float MIN_TIME_TO_REQUIRE_MORE_ENTRIES = 0.5f; |
| | 16 | |
|
| | 17 | | [SerializeField] internal CollapsablePublicChannelListComponentView publicChannelList; |
| | 18 | | [SerializeField] internal CollapsableDirectChatListComponentView directChatList; |
| | 19 | | [SerializeField] internal CollapsableChatSearchListComponentView searchResultsList; |
| | 20 | | [SerializeField] internal GameObject searchLoading; |
| | 21 | | [SerializeField] internal Button closeButton; |
| | 22 | | [SerializeField] internal GameObject directChatsLoadingContainer; |
| | 23 | | [SerializeField] internal GameObject directChannelHeader; |
| | 24 | | [SerializeField] internal GameObject searchResultsHeader; |
| | 25 | | [SerializeField] internal TMP_Text directChatsHeaderLabel; |
| | 26 | | [SerializeField] internal TMP_Text searchResultsHeaderLabel; |
| | 27 | | [SerializeField] internal ScrollRect scroll; |
| | 28 | | [SerializeField] internal SearchBarComponentView searchBar; |
| | 29 | | [SerializeField] private WorldChatWindowModel model; |
| | 30 | |
|
| | 31 | | [Header("Load More Entries")] |
| | 32 | | [SerializeField] internal GameObject loadMoreEntriesContainer; |
| | 33 | | [SerializeField] internal TMP_Text loadMoreEntriesLabel; |
| | 34 | | [SerializeField] internal GameObject loadMoreEntriesLoading; |
| | 35 | |
|
| 29 | 36 | | private readonly Dictionary<string, PrivateChatModel> creationQueue = new Dictionary<string, PrivateChatModel>(); |
| | 37 | | private bool isSortingDirty; |
| | 38 | | private bool isLayoutDirty; |
| | 39 | | private int currentAvatarSnapshotIndex; |
| | 40 | | private bool isLoadingPrivateChannels; |
| | 41 | | private bool isSearchMode; |
| | 42 | | private Vector2 lastScrollPosition; |
| | 43 | | private float loadMoreEntriesRestrictionTime; |
| | 44 | | private Coroutine requireMoreEntriesRoutine; |
| | 45 | |
|
| | 46 | | public event Action OnClose; |
| | 47 | | public event Action<string> OnOpenPrivateChat; |
| | 48 | | public event Action<string> OnOpenPublicChannel; |
| | 49 | |
|
| | 50 | | public event Action<string> OnSearchChannelRequested; |
| | 51 | | public event Action OnRequireMorePrivateChats; |
| | 52 | |
|
| 0 | 53 | | public RectTransform Transform => (RectTransform) transform; |
| 0 | 54 | | public bool IsActive => gameObject.activeInHierarchy; |
| | 55 | |
|
| | 56 | | public static WorldChatWindowComponentView Create() |
| | 57 | | { |
| 27 | 58 | | return Instantiate(Resources.Load<WorldChatWindowComponentView>("SocialBarV1/ConversationListHUD")); |
| | 59 | | } |
| | 60 | |
|
| | 61 | | public override void Awake() |
| | 62 | | { |
| 27 | 63 | | base.Awake(); |
| 28 | 64 | | closeButton.onClick.AddListener(() => OnClose?.Invoke()); |
| 73 | 65 | | directChatList.SortingMethod = (a, b) => b.Model.lastMessageTimestamp.CompareTo(a.Model.lastMessageTimestamp); |
| 28 | 66 | | directChatList.OnOpenChat += entry => OnOpenPrivateChat?.Invoke(entry.Model.userId); |
| 27 | 67 | | searchResultsList.OnOpenPrivateChat += entry => OnOpenPrivateChat?.Invoke(entry.Model.userId); |
| 27 | 68 | | searchResultsList.OnOpenPublicChat += entry => OnOpenPublicChannel?.Invoke(entry.Model.channelId); |
| 28 | 69 | | publicChannelList.OnOpenChat += entry => OnOpenPublicChannel?.Invoke(entry.Model.channelId); |
| 28 | 70 | | searchBar.OnSearchText += text => OnSearchChannelRequested?.Invoke(text); |
| 27 | 71 | | scroll.onValueChanged.AddListener(RequestMorePrivateChats); |
| 27 | 72 | | UpdateHeaders(); |
| 27 | 73 | | } |
| | 74 | |
|
| | 75 | | public override void OnEnable() |
| | 76 | | { |
| 28 | 77 | | base.OnEnable(); |
| 28 | 78 | | UpdateLayout(); |
| 28 | 79 | | } |
| | 80 | |
|
| | 81 | | public void Initialize(IChatController chatController) |
| | 82 | | { |
| 27 | 83 | | directChatList.Initialize(chatController); |
| 27 | 84 | | publicChannelList.Initialize(chatController); |
| 27 | 85 | | searchResultsList.Initialize(chatController); |
| 27 | 86 | | } |
| | 87 | |
|
| | 88 | | public override void Update() |
| | 89 | | { |
| 279 | 90 | | base.Update(); |
| | 91 | |
|
| 279 | 92 | | SetQueuedEntries(); |
| | 93 | |
|
| 279 | 94 | | if (isSortingDirty) |
| | 95 | | { |
| 18 | 96 | | directChatList.Sort(); |
| 18 | 97 | | searchResultsList.Sort(); |
| 18 | 98 | | publicChannelList.Sort(); |
| | 99 | | } |
| | 100 | |
|
| 279 | 101 | | isSortingDirty = false; |
| | 102 | |
|
| 279 | 103 | | if (isLayoutDirty) |
| 33 | 104 | | ((RectTransform) scroll.transform).ForceUpdateLayout(); |
| 279 | 105 | | isLayoutDirty = false; |
| | 106 | |
|
| 279 | 107 | | FetchProfilePicturesForVisibleEntries(); |
| 279 | 108 | | } |
| | 109 | |
|
| 2 | 110 | | public void Show() => gameObject.SetActive(true); |
| | 111 | |
|
| 2 | 112 | | public void Hide() => gameObject.SetActive(false); |
| | 113 | |
|
| 38 | 114 | | public void SetPrivateChat(PrivateChatModel model) => creationQueue[model.user.userId] = model; |
| | 115 | |
|
| | 116 | | public void RemovePrivateChat(string userId) |
| | 117 | | { |
| 1 | 118 | | directChatList.Remove(userId); |
| 1 | 119 | | searchResultsList.Remove(userId); |
| 1 | 120 | | UpdateHeaders(); |
| 1 | 121 | | UpdateLayout(); |
| 1 | 122 | | } |
| | 123 | |
|
| | 124 | | public void SetPublicChannel(PublicChatChannelModel model) |
| | 125 | | { |
| 20 | 126 | | var entryModel = new PublicChannelEntry.PublicChannelEntryModel(model.channelId, name = model.name); |
| | 127 | |
|
| 20 | 128 | | if (isSearchMode) |
| 8 | 129 | | searchResultsList.Set(entryModel); |
| | 130 | | else |
| 12 | 131 | | publicChannelList.Set(model.channelId, entryModel); |
| | 132 | |
|
| 20 | 133 | | UpdateLayout(); |
| 20 | 134 | | } |
| | 135 | |
|
| | 136 | | public void Configure(WorldChatWindowModel newModel) |
| | 137 | | { |
| 0 | 138 | | model = newModel; |
| 0 | 139 | | RefreshControl(); |
| 0 | 140 | | } |
| | 141 | |
|
| 2 | 142 | | public void ShowPrivateChatsLoading() => SetPrivateChatLoadingVisibility(true); |
| | 143 | |
|
| 1 | 144 | | public void HidePrivateChatsLoading() => SetPrivateChatLoadingVisibility(false); |
| | 145 | |
|
| | 146 | | public void RefreshBlockedDirectMessages(List<string> blockedUsers) |
| | 147 | | { |
| 0 | 148 | | if (blockedUsers == null) return; |
| 0 | 149 | | directChatList.RefreshBlockedEntries(blockedUsers); |
| 0 | 150 | | } |
| | 151 | |
|
| | 152 | | public void HideMoreChatsToLoadHint() |
| | 153 | | { |
| 1 | 154 | | loadMoreEntriesContainer.SetActive(false); |
| 1 | 155 | | UpdateLayout(); |
| 1 | 156 | | } |
| | 157 | |
|
| | 158 | | public void ShowMoreChatsToLoadHint(int count) |
| | 159 | | { |
| 2 | 160 | | loadMoreEntriesLabel.SetText($"{count} chats hidden. Use the search bar to find them or click below to show more |
| 2 | 161 | | loadMoreEntriesContainer.SetActive(true); |
| 2 | 162 | | UpdateLayout(); |
| 2 | 163 | | } |
| | 164 | |
|
| | 165 | | public void HideMoreChatsLoading() |
| | 166 | | { |
| 20 | 167 | | loadMoreEntriesLoading.SetActive(false); |
| 20 | 168 | | } |
| | 169 | |
|
| | 170 | | public void ShowMoreChatsLoading() |
| | 171 | | { |
| 2 | 172 | | loadMoreEntriesLoading.SetActive(true); |
| 2 | 173 | | } |
| | 174 | |
|
| | 175 | | public void HideSearchLoading() |
| | 176 | | { |
| 1 | 177 | | searchLoading.SetActive(false); |
| 1 | 178 | | } |
| | 179 | |
|
| | 180 | | public void ShowSearchLoading() |
| | 181 | | { |
| 1 | 182 | | searchLoading.SetActive(true); |
| 1 | 183 | | searchLoading.transform.SetAsLastSibling(); |
| 1 | 184 | | } |
| | 185 | |
|
| | 186 | | public void DisableSearchMode() |
| | 187 | | { |
| 3 | 188 | | isSearchMode = false; |
| 3 | 189 | | searchResultsList.Hide(); |
| 3 | 190 | | publicChannelList.Show(); |
| 3 | 191 | | publicChannelList.Sort(); |
| | 192 | |
|
| 3 | 193 | | if (!isLoadingPrivateChannels) |
| | 194 | | { |
| 3 | 195 | | directChatList.Show(); |
| 3 | 196 | | directChatList.Sort(); |
| 3 | 197 | | directChannelHeader.SetActive(true); |
| | 198 | | } |
| | 199 | |
|
| 3 | 200 | | searchResultsHeader.SetActive(false); |
| 3 | 201 | | searchResultsList.Clear(); |
| | 202 | |
|
| 3 | 203 | | UpdateHeaders(); |
| 3 | 204 | | } |
| | 205 | |
|
| | 206 | | public void EnableSearchMode() |
| | 207 | | { |
| 6 | 208 | | isSearchMode = true; |
| | 209 | |
|
| 6 | 210 | | searchResultsList.Clear(); |
| 6 | 211 | | searchResultsList.Show(); |
| 6 | 212 | | searchResultsList.Sort(); |
| 6 | 213 | | publicChannelList.Hide(); |
| 6 | 214 | | directChatList.Hide(); |
| 6 | 215 | | directChannelHeader.SetActive(false); |
| 6 | 216 | | searchResultsHeader.SetActive(true); |
| | 217 | |
|
| 6 | 218 | | UpdateHeaders(); |
| 6 | 219 | | searchLoading.transform.SetAsLastSibling(); |
| 6 | 220 | | } |
| | 221 | |
|
| 0 | 222 | | public bool ContainsPrivateChannel(string userId) => creationQueue.ContainsKey(userId) |
| | 223 | | || directChatList.Get(userId) != null; |
| | 224 | |
|
| | 225 | | public override void RefreshControl() |
| | 226 | | { |
| 0 | 227 | | publicChannelList.Clear(); |
| 0 | 228 | | foreach (var entry in model.publicChannels) |
| 0 | 229 | | publicChannelList.Set(entry.channelId, entry); |
| 0 | 230 | | directChatList.Clear(); |
| 0 | 231 | | foreach (var entry in model.privateChats) |
| 0 | 232 | | directChatList.Set(entry.userId, entry); |
| 0 | 233 | | SetPrivateChatLoadingVisibility(model.isLoadingDirectChats); |
| 0 | 234 | | } |
| | 235 | |
|
| | 236 | | private void Set(PrivateChatModel model) |
| | 237 | | { |
| 37 | 238 | | var user = model.user; |
| 37 | 239 | | var userId = user.userId; |
| | 240 | |
|
| 37 | 241 | | var entry = new PrivateChatEntry.PrivateChatEntryModel( |
| | 242 | | user.userId, |
| | 243 | | user.userName, |
| | 244 | | model.recentMessage != null ? model.recentMessage.body : string.Empty, |
| | 245 | | user.face256SnapshotURL, |
| | 246 | | model.isBlocked, |
| | 247 | | model.isOnline, |
| | 248 | | model.recentMessage != null ? model.recentMessage.timestamp : 0); |
| | 249 | |
|
| 37 | 250 | | if (isSearchMode) |
| 18 | 251 | | searchResultsList.Set(entry); |
| | 252 | | else |
| 19 | 253 | | directChatList.Set(userId, entry); |
| | 254 | |
|
| 37 | 255 | | UpdateHeaders(); |
| 37 | 256 | | UpdateLayout(); |
| 37 | 257 | | SortLists(); |
| 37 | 258 | | } |
| | 259 | |
|
| 0 | 260 | | private void SortLists() => isSortingDirty = true; |
| | 261 | |
|
| | 262 | | private void SetPrivateChatLoadingVisibility(bool visible) |
| | 263 | | { |
| 3 | 264 | | model.isLoadingDirectChats = visible; |
| 3 | 265 | | directChatsLoadingContainer.SetActive(visible); |
| | 266 | |
|
| 3 | 267 | | if (visible) |
| 2 | 268 | | directChatList.Hide(); |
| 1 | 269 | | else if (!isSearchMode) |
| 1 | 270 | | directChatList.Show(); |
| | 271 | |
|
| 3 | 272 | | scroll.enabled = !visible; |
| 3 | 273 | | isLoadingPrivateChannels = visible; |
| 3 | 274 | | } |
| | 275 | |
|
| | 276 | | private void UpdateHeaders() |
| | 277 | | { |
| 74 | 278 | | directChatsHeaderLabel.text = $"Direct Messages ({directChatList.Count()})"; |
| 74 | 279 | | searchResultsHeaderLabel.text = $"Results ({searchResultsList.Count()})"; |
| 74 | 280 | | } |
| | 281 | |
|
| 0 | 282 | | private void UpdateLayout() => isLayoutDirty = true; |
| | 283 | |
|
| | 284 | | private void SetQueuedEntries() |
| | 285 | | { |
| 540 | 286 | | if (creationQueue.Count == 0) return; |
| | 287 | |
|
| 110 | 288 | | for (var i = 0; i < CREATION_AMOUNT_PER_FRAME && creationQueue.Count > 0; i++) |
| | 289 | | { |
| 37 | 290 | | var (userId, model) = creationQueue.First(); |
| 37 | 291 | | creationQueue.Remove(userId); |
| 37 | 292 | | Set(model); |
| | 293 | | } |
| | 294 | |
|
| 18 | 295 | | HideMoreChatsLoading(); |
| 18 | 296 | | } |
| | 297 | |
|
| | 298 | | private void FetchProfilePicturesForVisibleEntries() |
| | 299 | | { |
| 287 | 300 | | if (isSearchMode) return; |
| | 301 | |
|
| 580 | 302 | | foreach (var entry in directChatList.Entries.Values.Skip(currentAvatarSnapshotIndex) |
| | 303 | | .Take(AVATAR_SNAPSHOTS_PER_FRAME)) |
| | 304 | | { |
| 19 | 305 | | if (entry.IsVisible((RectTransform) scroll.transform)) |
| 19 | 306 | | entry.EnableAvatarSnapshotFetching(); |
| | 307 | | else |
| 0 | 308 | | entry.DisableAvatarSnapshotFetching(); |
| | 309 | | } |
| | 310 | |
|
| 271 | 311 | | currentAvatarSnapshotIndex += AVATAR_SNAPSHOTS_PER_FRAME; |
| | 312 | |
|
| 271 | 313 | | if (currentAvatarSnapshotIndex >= directChatList.Entries.Count) |
| 271 | 314 | | currentAvatarSnapshotIndex = 0; |
| 271 | 315 | | } |
| | 316 | |
|
| | 317 | | private void RequestMorePrivateChats(Vector2 position) |
| | 318 | | { |
| 2 | 319 | | if (!loadMoreEntriesContainer.activeInHierarchy || |
| | 320 | | loadMoreEntriesLoading.activeInHierarchy || |
| 0 | 321 | | Time.realtimeSinceStartup - loadMoreEntriesRestrictionTime < MIN_TIME_TO_REQUIRE_MORE_ENTRIES) return; |
| | 322 | |
|
| 2 | 323 | | if (position.y < REQUEST_MORE_ENTRIES_SCROLL_THRESHOLD && lastScrollPosition.y >= REQUEST_MORE_ENTRIES_SCROLL_TH |
| | 324 | | { |
| 1 | 325 | | if (requireMoreEntriesRoutine != null) |
| 0 | 326 | | StopCoroutine(requireMoreEntriesRoutine); |
| | 327 | |
|
| 1 | 328 | | ShowMoreChatsLoading(); |
| 1 | 329 | | requireMoreEntriesRoutine = StartCoroutine(WaitThenRequireMoreEntries()); |
| | 330 | |
|
| 1 | 331 | | loadMoreEntriesRestrictionTime = Time.realtimeSinceStartup; |
| | 332 | | } |
| | 333 | |
|
| 2 | 334 | | lastScrollPosition = position; |
| 2 | 335 | | } |
| | 336 | |
|
| | 337 | | private IEnumerator WaitThenRequireMoreEntries() |
| | 338 | | { |
| 1 | 339 | | yield return new WaitForSeconds(1f); |
| 1 | 340 | | OnRequireMorePrivateChats?.Invoke(); |
| 1 | 341 | | } |
| | 342 | | } |