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