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