< Summary

Class:PrivateChatWindowHUDController
Assembly:PrivateChatWindowHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/PrivateChatWindow/PrivateChatWindowHUDController.cs
Covered lines:87
Uncovered lines:6
Coverable lines:93
Total lines:196
Line coverage:93.5% (87 of 93)
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%44093.33%
SendChatMessage(...)0%9.367063.64%
SetVisibility(...)0%330100%
Dispose()0%330100%
OnAddMessage(...)0%330100%
IsMessageFomCurrentConversation(...)0%330100%
ForceFocus()0%110100%
MarkUserChatMessagesAsRead(...)0%3.033085.71%
SaveLatestReadChatMessagesStatus()0%220100%
LoadLatestReadChatMessagesStatus()0%330100%
ChatHUDViewInputField_OnSelect(...)0%110100%

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.Helpers;
 6using UnityEngine;
 7
 8public class PrivateChatWindowHUDController : IHUD
 9{
 10    internal const string PLAYER_PREFS_LAST_READ_CHAT_MESSAGES = "LastReadChatMessages";
 11
 12    public PrivateChatWindowHUDView view;
 813    public bool resetInputFieldOnSubmit = true;
 14
 15    ChatHUDController chatHudController;
 16    IChatController chatController;
 817    public string conversationUserId { get; private set; } = string.Empty;
 818    public string conversationUserName { get; private set; } = string.Empty;
 19
 20    public event System.Action OnPressBack;
 21
 22    public void Initialize(IChatController chatController)
 23    {
 824        view = PrivateChatWindowHUDView.Create(this);
 825        view.OnPressBack -= View_OnPressBack;
 826        view.OnPressBack += View_OnPressBack;
 27
 828        view.chatHudView.inputField.onSelect.RemoveListener(ChatHUDViewInputField_OnSelect);
 829        view.chatHudView.inputField.onSelect.AddListener(ChatHUDViewInputField_OnSelect);
 30
 831        chatHudController = new ChatHUDController();
 832        chatHudController.Initialize(view.chatHudView);
 833        LoadLatestReadChatMessagesStatus();
 34
 835        view.OnSendMessage += SendChatMessage;
 36
 837        this.chatController = chatController;
 38
 839        if (chatController != null)
 40        {
 841            chatController.OnAddMessage -= OnAddMessage;
 842            chatController.OnAddMessage += OnAddMessage;
 43        }
 44
 845        SetVisibility(false);
 846    }
 47
 248    void View_OnPressBack() { OnPressBack?.Invoke(); }
 49
 50    public void Configure(string newConversationUserId)
 51    {
 752        if (string.IsNullOrEmpty(newConversationUserId) || newConversationUserId == conversationUserId)
 053            return;
 54
 755        UserProfile newConversationUserProfile = UserProfileController.userProfilesCatalog.Get(newConversationUserId);
 56
 757        conversationUserId = newConversationUserId;
 758        conversationUserName = newConversationUserProfile.userName;
 59
 760        view.ConfigureTitle(conversationUserName);
 761        view.ConfigureProfilePicture(newConversationUserProfile.faceSnapshot);
 762        view.ConfigureUserId(newConversationUserProfile.userId);
 63
 764        view.chatHudView.CleanAllEntries();
 65
 1166        var messageEntries = chatController.GetEntries().Where((x) => IsMessageFomCurrentConversation(x)).ToList();
 2267        foreach (var v in messageEntries)
 68        {
 469            OnAddMessage(v);
 70        }
 771    }
 72
 73    public void SendChatMessage(ChatMessage message)
 74    {
 175        if (string.IsNullOrEmpty(conversationUserName))
 076            return;
 77
 178        bool isValidMessage = !string.IsNullOrEmpty(message.body) && !string.IsNullOrWhiteSpace(message.body) && !string
 79
 180        if (!isValidMessage || resetInputFieldOnSubmit)
 81        {
 082            view.chatHudView.ResetInputField();
 083            view.chatHudView.FocusInputField();
 84        }
 85
 186        if (!isValidMessage)
 087            return;
 88
 89        // If Kernel allowed for private messages without the whisper param we could avoid this line
 190        message.body = $"/w {message.recipient} {message.body}";
 91
 192        WebInterface.SendChatMessage(message);
 193    }
 94
 95    public void SetVisibility(bool visible)
 96    {
 2297        if (view.gameObject.activeSelf == visible)
 498            return;
 99
 18100        view.gameObject.SetActive(visible);
 101
 18102        if (visible)
 103        {
 104            // The messages from 'conversationUserId' are marked as read once the private chat is opened
 8105            MarkUserChatMessagesAsRead(conversationUserId);
 8106            view.chatHudView.scrollRect.verticalNormalizedPosition = 0;
 107
 8108            AudioScriptableObjects.dialogOpen.Play(true);
 8109        }
 110        else
 111        {
 10112            AudioScriptableObjects.dialogClose.Play(true);
 113        }
 10114    }
 115
 116    public void Dispose()
 117    {
 8118        view.chatHudView.inputField.onSelect.RemoveListener(ChatHUDViewInputField_OnSelect);
 119
 8120        view.OnPressBack -= View_OnPressBack;
 121
 8122        if (chatController != null)
 8123            chatController.OnAddMessage -= OnAddMessage;
 124
 8125        if (view != null)
 8126            UnityEngine.Object.Destroy(view.gameObject);
 8127    }
 128
 129    void OnAddMessage(ChatMessage message)
 130    {
 8131        if (!IsMessageFomCurrentConversation(message))
 1132            return;
 133
 7134        view.chatHudView.controller.AddChatMessage(ChatHUDController.ChatMessageToChatEntry(message));
 135
 7136        if (view.userId == conversationUserId)
 137        {
 138            // The messages from 'conversationUserId' are marked as read if his private chat window is currently open
 7139            MarkUserChatMessagesAsRead(conversationUserId, (long) message.timestamp);
 140        }
 7141    }
 142
 12143    bool IsMessageFomCurrentConversation(ChatMessage message) { return message.messageType == ChatMessage.Type.PRIVATE &
 144
 145    public void ForceFocus()
 146    {
 1147        SetVisibility(true);
 1148        view.chatHudView.FocusInputField();
 1149    }
 150
 151    private void MarkUserChatMessagesAsRead(string userId, long? timestamp = null)
 152    {
 16153        long timeMark = System.DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
 16154        if (timestamp != null && timestamp.Value > timeMark)
 0155            timeMark = timestamp.Value;
 156
 16157        CommonScriptableObjects.lastReadChatMessages.Remove(userId);
 16158        CommonScriptableObjects.lastReadChatMessages.Add(userId, timeMark);
 16159        SaveLatestReadChatMessagesStatus();
 16160    }
 161
 162    private void SaveLatestReadChatMessagesStatus()
 163    {
 16164        List<KeyValuePair<string, long>> lastReadChatMessagesList = new List<KeyValuePair<string, long>>();
 16165        using (var iterator = CommonScriptableObjects.lastReadChatMessages.GetEnumerator())
 166        {
 50167            while (iterator.MoveNext())
 168            {
 34169                lastReadChatMessagesList.Add(new KeyValuePair<string, long>(iterator.Current.Key, iterator.Current.Value
 170            }
 16171        }
 172
 16173        PlayerPrefsUtils.SetString(PLAYER_PREFS_LAST_READ_CHAT_MESSAGES, JsonConvert.SerializeObject(lastReadChatMessage
 16174        PlayerPrefsUtils.Save();
 16175    }
 176
 177    private void LoadLatestReadChatMessagesStatus()
 178    {
 8179        CommonScriptableObjects.lastReadChatMessages.Clear();
 180
 8181        List<KeyValuePair<string, long>> lastReadChatMessagesList = JsonConvert.DeserializeObject<List<KeyValuePair<stri
 8182        if (lastReadChatMessagesList != null)
 183        {
 40184            foreach (var item in lastReadChatMessagesList)
 185            {
 13186                CommonScriptableObjects.lastReadChatMessages.Add(item.Key, item.Value);
 187            }
 188        }
 8189    }
 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
 1194        MarkUserChatMessagesAsRead(conversationUserId);
 1195    }
 196}