| | 1 | | using DCL.Interface; |
| | 2 | | using Newtonsoft.Json; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | | using DCL; |
| | 6 | | using DCL.Helpers; |
| | 7 | | using UnityEngine; |
| | 8 | |
|
| | 9 | | public class PrivateChatWindowHUDController : IHUD |
| | 10 | | { |
| | 11 | | internal const string PLAYER_PREFS_LAST_READ_CHAT_MESSAGES = "LastReadChatMessages"; |
| | 12 | |
|
| | 13 | | public PrivateChatWindowHUDView view; |
| 8 | 14 | | public bool resetInputFieldOnSubmit = true; |
| | 15 | |
|
| | 16 | | ChatHUDController chatHudController; |
| | 17 | | IChatController chatController; |
| 8 | 18 | | public string conversationUserId { get; private set; } = string.Empty; |
| 8 | 19 | | public string conversationUserName { get; private set; } = string.Empty; |
| | 20 | |
|
| | 21 | | private ILazyTextureObserver conversationSnapshot; |
| | 22 | |
|
| | 23 | | public event System.Action OnPressBack; |
| | 24 | |
|
| | 25 | | public void Initialize(IChatController chatController) |
| | 26 | | { |
| 8 | 27 | | view = PrivateChatWindowHUDView.Create(this); |
| 8 | 28 | | view.OnPressBack -= View_OnPressBack; |
| 8 | 29 | | view.OnPressBack += View_OnPressBack; |
| | 30 | |
|
| 8 | 31 | | view.chatHudView.inputField.onSelect.RemoveListener(ChatHUDViewInputField_OnSelect); |
| 8 | 32 | | view.chatHudView.inputField.onSelect.AddListener(ChatHUDViewInputField_OnSelect); |
| | 33 | |
|
| 8 | 34 | | chatHudController = new ChatHUDController(DataStore.i); |
| 8 | 35 | | chatHudController.Initialize(view.chatHudView); |
| 8 | 36 | | LoadLatestReadChatMessagesStatus(); |
| | 37 | |
|
| 8 | 38 | | view.OnSendMessage += SendChatMessage; |
| | 39 | |
|
| 8 | 40 | | this.chatController = chatController; |
| | 41 | |
|
| 8 | 42 | | if (chatController != null) |
| | 43 | | { |
| 7 | 44 | | chatController.OnAddMessage -= OnAddMessage; |
| 7 | 45 | | chatController.OnAddMessage += OnAddMessage; |
| | 46 | | } |
| | 47 | |
|
| 8 | 48 | | SetVisibility(false); |
| 8 | 49 | | } |
| | 50 | |
|
| 2 | 51 | | void View_OnPressBack() { OnPressBack?.Invoke(); } |
| | 52 | |
|
| | 53 | | public void Configure(string newConversationUserId) |
| | 54 | | { |
| 7 | 55 | | if (string.IsNullOrEmpty(newConversationUserId) || newConversationUserId == conversationUserId) |
| 0 | 56 | | return; |
| | 57 | |
|
| 7 | 58 | | UserProfile newConversationUserProfile = UserProfileController.userProfilesCatalog.Get(newConversationUserId); |
| | 59 | |
|
| 7 | 60 | | if ( conversationSnapshot != null ) |
| 0 | 61 | | conversationSnapshot.RemoveListener(view.ConfigureAvatarSnapshot); |
| | 62 | |
|
| 7 | 63 | | conversationUserId = newConversationUserId; |
| 7 | 64 | | conversationUserName = newConversationUserProfile.userName; |
| 7 | 65 | | conversationSnapshot = newConversationUserProfile.snapshotObserver; |
| | 66 | |
|
| 7 | 67 | | view.ConfigureTitle(conversationUserName); |
| 7 | 68 | | view.ConfigureUserId(newConversationUserProfile.userId); |
| 7 | 69 | | conversationSnapshot.AddListener(view.ConfigureAvatarSnapshot); |
| | 70 | |
|
| 7 | 71 | | view.chatHudView.CleanAllEntries(); |
| | 72 | |
|
| 11 | 73 | | var messageEntries = chatController.GetEntries().Where((x) => IsMessageFomCurrentConversation(x)).ToList(); |
| 22 | 74 | | foreach (var v in messageEntries) |
| | 75 | | { |
| 4 | 76 | | OnAddMessage(v); |
| | 77 | | } |
| 7 | 78 | | } |
| | 79 | |
|
| | 80 | | public void SendChatMessage(ChatMessage message) |
| | 81 | | { |
| 1 | 82 | | if (string.IsNullOrEmpty(conversationUserName)) |
| 0 | 83 | | return; |
| | 84 | |
|
| 1 | 85 | | bool isValidMessage = !string.IsNullOrEmpty(message.body) && !string.IsNullOrWhiteSpace(message.body) && !string |
| | 86 | |
|
| 1 | 87 | | if (!isValidMessage || resetInputFieldOnSubmit) |
| | 88 | | { |
| 0 | 89 | | view.chatHudView.ResetInputField(); |
| 0 | 90 | | view.chatHudView.FocusInputField(); |
| | 91 | | } |
| | 92 | |
|
| 1 | 93 | | if (!isValidMessage) |
| 0 | 94 | | return; |
| | 95 | |
|
| | 96 | | // If Kernel allowed for private messages without the whisper param we could avoid this line |
| 1 | 97 | | message.body = $"/w {message.recipient} {message.body}"; |
| | 98 | |
|
| 1 | 99 | | WebInterface.SendChatMessage(message); |
| 1 | 100 | | } |
| | 101 | |
|
| | 102 | | public void SetVisibility(bool visible) |
| | 103 | | { |
| 22 | 104 | | if (view.gameObject.activeSelf == visible) |
| 4 | 105 | | return; |
| | 106 | |
|
| 18 | 107 | | view.gameObject.SetActive(visible); |
| | 108 | |
|
| 18 | 109 | | if (visible) |
| | 110 | | { |
| | 111 | | // The messages from 'conversationUserId' are marked as read once the private chat is opened |
| 8 | 112 | | MarkUserChatMessagesAsRead(conversationUserId); |
| 8 | 113 | | view.chatHudView.scrollRect.verticalNormalizedPosition = 0; |
| 8 | 114 | | conversationSnapshot?.AddListener(view.ConfigureAvatarSnapshot); |
| | 115 | |
|
| 8 | 116 | | AudioScriptableObjects.dialogOpen.Play(true); |
| 8 | 117 | | } |
| | 118 | | else |
| | 119 | | { |
| 10 | 120 | | conversationSnapshot?.RemoveListener(view.ConfigureAvatarSnapshot); |
| 10 | 121 | | AudioScriptableObjects.dialogClose.Play(true); |
| | 122 | | } |
| 10 | 123 | | } |
| | 124 | |
|
| | 125 | | public void Dispose() |
| | 126 | | { |
| 8 | 127 | | conversationSnapshot?.RemoveListener(view.ConfigureAvatarSnapshot); |
| | 128 | |
|
| 8 | 129 | | view.chatHudView.inputField.onSelect.RemoveListener(ChatHUDViewInputField_OnSelect); |
| | 130 | |
|
| 8 | 131 | | view.OnPressBack -= View_OnPressBack; |
| | 132 | |
|
| 8 | 133 | | if (chatController != null) |
| 7 | 134 | | chatController.OnAddMessage -= OnAddMessage; |
| | 135 | |
|
| 8 | 136 | | if (view != null) |
| 8 | 137 | | UnityEngine.Object.Destroy(view.gameObject); |
| 8 | 138 | | } |
| | 139 | |
|
| | 140 | | void OnAddMessage(ChatMessage message) |
| | 141 | | { |
| 8 | 142 | | if (!IsMessageFomCurrentConversation(message)) |
| 1 | 143 | | return; |
| | 144 | |
|
| 7 | 145 | | view.chatHudView.controller.AddChatMessage(ChatHUDController.ChatMessageToChatEntry(message)); |
| | 146 | |
|
| 7 | 147 | | if (view.userId == conversationUserId) |
| | 148 | | { |
| | 149 | | // The messages from 'conversationUserId' are marked as read if his private chat window is currently open |
| 7 | 150 | | MarkUserChatMessagesAsRead(conversationUserId, (long) message.timestamp); |
| | 151 | | } |
| 7 | 152 | | } |
| | 153 | |
|
| 12 | 154 | | bool IsMessageFomCurrentConversation(ChatMessage message) { return message.messageType == ChatMessage.Type.PRIVATE & |
| | 155 | |
|
| | 156 | | public void ForceFocus() |
| | 157 | | { |
| 1 | 158 | | SetVisibility(true); |
| 1 | 159 | | view.chatHudView.FocusInputField(); |
| 1 | 160 | | } |
| | 161 | |
|
| | 162 | | private void MarkUserChatMessagesAsRead(string userId, long? timestamp = null) |
| | 163 | | { |
| 15 | 164 | | long timeMark = System.DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); |
| 15 | 165 | | if (timestamp != null && timestamp.Value > timeMark) |
| 0 | 166 | | timeMark = timestamp.Value; |
| | 167 | |
|
| 15 | 168 | | CommonScriptableObjects.lastReadChatMessages.Remove(userId); |
| 15 | 169 | | CommonScriptableObjects.lastReadChatMessages.Add(userId, timeMark); |
| 15 | 170 | | SaveLatestReadChatMessagesStatus(); |
| 15 | 171 | | } |
| | 172 | |
|
| | 173 | | private void SaveLatestReadChatMessagesStatus() |
| | 174 | | { |
| 15 | 175 | | List<KeyValuePair<string, long>> lastReadChatMessagesList = new List<KeyValuePair<string, long>>(); |
| 15 | 176 | | using (var iterator = CommonScriptableObjects.lastReadChatMessages.GetEnumerator()) |
| | 177 | | { |
| 46 | 178 | | while (iterator.MoveNext()) |
| | 179 | | { |
| 31 | 180 | | lastReadChatMessagesList.Add(new KeyValuePair<string, long>(iterator.Current.Key, iterator.Current.Value |
| | 181 | | } |
| 15 | 182 | | } |
| | 183 | |
|
| 15 | 184 | | PlayerPrefsUtils.SetString(PLAYER_PREFS_LAST_READ_CHAT_MESSAGES, JsonConvert.SerializeObject(lastReadChatMessage |
| 15 | 185 | | PlayerPrefsUtils.Save(); |
| 15 | 186 | | } |
| | 187 | |
|
| | 188 | | private void LoadLatestReadChatMessagesStatus() |
| | 189 | | { |
| 8 | 190 | | CommonScriptableObjects.lastReadChatMessages.Clear(); |
| | 191 | |
|
| 8 | 192 | | List<KeyValuePair<string, long>> lastReadChatMessagesList = JsonConvert.DeserializeObject<List<KeyValuePair<stri |
| 8 | 193 | | if (lastReadChatMessagesList != null) |
| | 194 | | { |
| 40 | 195 | | foreach (var item in lastReadChatMessagesList) |
| | 196 | | { |
| 13 | 197 | | CommonScriptableObjects.lastReadChatMessages.Add(item.Key, item.Value); |
| | 198 | | } |
| | 199 | | } |
| 8 | 200 | | } |
| | 201 | |
|
| | 202 | | private void ChatHUDViewInputField_OnSelect(string message) |
| | 203 | | { |
| | 204 | | // The messages from 'conversationUserId' are marked as read if the player clicks on the input field of the priv |
| 0 | 205 | | MarkUserChatMessagesAsRead(conversationUserId); |
| 0 | 206 | | } |
| | 207 | | } |