< Summary

Class:PrivateChatWindowHUDController
Assembly:PrivateChatWindowHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/PrivateChatWindow/PrivateChatWindowHUDController.cs
Covered lines:90
Uncovered lines:9
Coverable lines:99
Total lines:207
Line coverage:90.9% (90 of 99)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
PrivateChatWindowHUDController()0%110100%
Initialize(...)0%220100%
View_OnPressBack()0%2.52050%
Configure(...)0%5.035088.89%
SendChatMessage(...)0%9.367063.64%
SetVisibility(...)0%550100%
Dispose()0%440100%
OnAddMessage(...)0%330100%
IsMessageFomCurrentConversation(...)0%330100%
ForceFocus()0%110100%
MarkUserChatMessagesAsRead(...)0%3.033085.71%
SaveLatestReadChatMessagesStatus()0%220100%
LoadLatestReadChatMessagesStatus()0%330100%
ChatHUDViewInputField_OnSelect(...)0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/PrivateChatWindow/PrivateChatWindowHUDController.cs

#LineLine coverage
 1using DCL.Interface;
 2using Newtonsoft.Json;
 3using System.Collections.Generic;
 4using System.Linq;
 5using DCL;
 6using DCL.Helpers;
 7using UnityEngine;
 8
 9public class PrivateChatWindowHUDController : IHUD
 10{
 11    internal const string PLAYER_PREFS_LAST_READ_CHAT_MESSAGES = "LastReadChatMessages";
 12
 13    public PrivateChatWindowHUDView view;
 814    public bool resetInputFieldOnSubmit = true;
 15
 16    ChatHUDController chatHudController;
 17    IChatController chatController;
 818    public string conversationUserId { get; private set; } = string.Empty;
 819    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    {
 827        view = PrivateChatWindowHUDView.Create(this);
 828        view.OnPressBack -= View_OnPressBack;
 829        view.OnPressBack += View_OnPressBack;
 30
 831        view.chatHudView.inputField.onSelect.RemoveListener(ChatHUDViewInputField_OnSelect);
 832        view.chatHudView.inputField.onSelect.AddListener(ChatHUDViewInputField_OnSelect);
 33
 834        chatHudController = new ChatHUDController(DataStore.i);
 835        chatHudController.Initialize(view.chatHudView);
 836        LoadLatestReadChatMessagesStatus();
 37
 838        view.OnSendMessage += SendChatMessage;
 39
 840        this.chatController = chatController;
 41
 842        if (chatController != null)
 43        {
 744            chatController.OnAddMessage -= OnAddMessage;
 745            chatController.OnAddMessage += OnAddMessage;
 46        }
 47
 848        SetVisibility(false);
 849    }
 50
 251    void View_OnPressBack() { OnPressBack?.Invoke(); }
 52
 53    public void Configure(string newConversationUserId)
 54    {
 755        if (string.IsNullOrEmpty(newConversationUserId) || newConversationUserId == conversationUserId)
 056            return;
 57
 758        UserProfile newConversationUserProfile = UserProfileController.userProfilesCatalog.Get(newConversationUserId);
 59
 760        if ( conversationSnapshot != null )
 061            conversationSnapshot.RemoveListener(view.ConfigureAvatarSnapshot);
 62
 763        conversationUserId = newConversationUserId;
 764        conversationUserName = newConversationUserProfile.userName;
 765        conversationSnapshot = newConversationUserProfile.snapshotObserver;
 66
 767        view.ConfigureTitle(conversationUserName);
 768        view.ConfigureUserId(newConversationUserProfile.userId);
 769        conversationSnapshot.AddListener(view.ConfigureAvatarSnapshot);
 70
 771        view.chatHudView.CleanAllEntries();
 72
 1173        var messageEntries = chatController.GetEntries().Where((x) => IsMessageFomCurrentConversation(x)).ToList();
 2274        foreach (var v in messageEntries)
 75        {
 476            OnAddMessage(v);
 77        }
 778    }
 79
 80    public void SendChatMessage(ChatMessage message)
 81    {
 182        if (string.IsNullOrEmpty(conversationUserName))
 083            return;
 84
 185        bool isValidMessage = !string.IsNullOrEmpty(message.body) && !string.IsNullOrWhiteSpace(message.body) && !string
 86
 187        if (!isValidMessage || resetInputFieldOnSubmit)
 88        {
 089            view.chatHudView.ResetInputField();
 090            view.chatHudView.FocusInputField();
 91        }
 92
 193        if (!isValidMessage)
 094            return;
 95
 96        // If Kernel allowed for private messages without the whisper param we could avoid this line
 197        message.body = $"/w {message.recipient} {message.body}";
 98
 199        WebInterface.SendChatMessage(message);
 1100    }
 101
 102    public void SetVisibility(bool visible)
 103    {
 22104        if (view.gameObject.activeSelf == visible)
 4105            return;
 106
 18107        view.gameObject.SetActive(visible);
 108
 18109        if (visible)
 110        {
 111            // The messages from 'conversationUserId' are marked as read once the private chat is opened
 8112            MarkUserChatMessagesAsRead(conversationUserId);
 8113            view.chatHudView.scrollRect.verticalNormalizedPosition = 0;
 8114            conversationSnapshot?.AddListener(view.ConfigureAvatarSnapshot);
 115
 8116            AudioScriptableObjects.dialogOpen.Play(true);
 8117        }
 118        else
 119        {
 10120            conversationSnapshot?.RemoveListener(view.ConfigureAvatarSnapshot);
 10121            AudioScriptableObjects.dialogClose.Play(true);
 122        }
 10123    }
 124
 125    public void Dispose()
 126    {
 8127        conversationSnapshot?.RemoveListener(view.ConfigureAvatarSnapshot);
 128
 8129        view.chatHudView.inputField.onSelect.RemoveListener(ChatHUDViewInputField_OnSelect);
 130
 8131        view.OnPressBack -= View_OnPressBack;
 132
 8133        if (chatController != null)
 7134            chatController.OnAddMessage -= OnAddMessage;
 135
 8136        if (view != null)
 8137            UnityEngine.Object.Destroy(view.gameObject);
 8138    }
 139
 140    void OnAddMessage(ChatMessage message)
 141    {
 8142        if (!IsMessageFomCurrentConversation(message))
 1143            return;
 144
 7145        view.chatHudView.controller.AddChatMessage(ChatHUDController.ChatMessageToChatEntry(message));
 146
 7147        if (view.userId == conversationUserId)
 148        {
 149            // The messages from 'conversationUserId' are marked as read if his private chat window is currently open
 7150            MarkUserChatMessagesAsRead(conversationUserId, (long) message.timestamp);
 151        }
 7152    }
 153
 12154    bool IsMessageFomCurrentConversation(ChatMessage message) { return message.messageType == ChatMessage.Type.PRIVATE &
 155
 156    public void ForceFocus()
 157    {
 1158        SetVisibility(true);
 1159        view.chatHudView.FocusInputField();
 1160    }
 161
 162    private void MarkUserChatMessagesAsRead(string userId, long? timestamp = null)
 163    {
 15164        long timeMark = System.DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
 15165        if (timestamp != null && timestamp.Value > timeMark)
 0166            timeMark = timestamp.Value;
 167
 15168        CommonScriptableObjects.lastReadChatMessages.Remove(userId);
 15169        CommonScriptableObjects.lastReadChatMessages.Add(userId, timeMark);
 15170        SaveLatestReadChatMessagesStatus();
 15171    }
 172
 173    private void SaveLatestReadChatMessagesStatus()
 174    {
 15175        List<KeyValuePair<string, long>> lastReadChatMessagesList = new List<KeyValuePair<string, long>>();
 15176        using (var iterator = CommonScriptableObjects.lastReadChatMessages.GetEnumerator())
 177        {
 46178            while (iterator.MoveNext())
 179            {
 31180                lastReadChatMessagesList.Add(new KeyValuePair<string, long>(iterator.Current.Key, iterator.Current.Value
 181            }
 15182        }
 183
 15184        PlayerPrefsUtils.SetString(PLAYER_PREFS_LAST_READ_CHAT_MESSAGES, JsonConvert.SerializeObject(lastReadChatMessage
 15185        PlayerPrefsUtils.Save();
 15186    }
 187
 188    private void LoadLatestReadChatMessagesStatus()
 189    {
 8190        CommonScriptableObjects.lastReadChatMessages.Clear();
 191
 8192        List<KeyValuePair<string, long>> lastReadChatMessagesList = JsonConvert.DeserializeObject<List<KeyValuePair<stri
 8193        if (lastReadChatMessagesList != null)
 194        {
 40195            foreach (var item in lastReadChatMessagesList)
 196            {
 13197                CommonScriptableObjects.lastReadChatMessages.Add(item.Key, item.Value);
 198            }
 199        }
 8200    }
 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
 0205        MarkUserChatMessagesAsRead(conversationUserId);
 0206    }
 207}