< Summary

Class:PrivateChatWindowHUDController
Assembly:PrivateChatWindowHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/PrivateChatWindow/PrivateChatWindowHUDController.cs
Covered lines:92
Uncovered lines:7
Coverable lines:99
Total lines:206
Line coverage:92.9% (92 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%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    private ILazyTextureObserver conversationSnapshot;
 21
 22    public event System.Action OnPressBack;
 23
 24    public void Initialize(IChatController chatController)
 25    {
 826        view = PrivateChatWindowHUDView.Create(this);
 827        view.OnPressBack -= View_OnPressBack;
 828        view.OnPressBack += View_OnPressBack;
 29
 830        view.chatHudView.inputField.onSelect.RemoveListener(ChatHUDViewInputField_OnSelect);
 831        view.chatHudView.inputField.onSelect.AddListener(ChatHUDViewInputField_OnSelect);
 32
 833        chatHudController = new ChatHUDController();
 834        chatHudController.Initialize(view.chatHudView);
 835        LoadLatestReadChatMessagesStatus();
 36
 837        view.OnSendMessage += SendChatMessage;
 38
 839        this.chatController = chatController;
 40
 841        if (chatController != null)
 42        {
 843            chatController.OnAddMessage -= OnAddMessage;
 844            chatController.OnAddMessage += OnAddMessage;
 45        }
 46
 847        SetVisibility(false);
 848    }
 49
 250    void View_OnPressBack() { OnPressBack?.Invoke(); }
 51
 52    public void Configure(string newConversationUserId)
 53    {
 754        if (string.IsNullOrEmpty(newConversationUserId) || newConversationUserId == conversationUserId)
 055            return;
 56
 757        UserProfile newConversationUserProfile = UserProfileController.userProfilesCatalog.Get(newConversationUserId);
 58
 759        if ( conversationSnapshot != null )
 060            conversationSnapshot.RemoveListener(view.ConfigureAvatarSnapshot);
 61
 762        conversationUserId = newConversationUserId;
 763        conversationUserName = newConversationUserProfile.userName;
 764        conversationSnapshot = newConversationUserProfile.snapshotObserver;
 65
 766        view.ConfigureTitle(conversationUserName);
 767        view.ConfigureUserId(newConversationUserProfile.userId);
 768        conversationSnapshot.AddListener(view.ConfigureAvatarSnapshot);
 69
 770        view.chatHudView.CleanAllEntries();
 71
 1172        var messageEntries = chatController.GetEntries().Where((x) => IsMessageFomCurrentConversation(x)).ToList();
 2273        foreach (var v in messageEntries)
 74        {
 475            OnAddMessage(v);
 76        }
 777    }
 78
 79    public void SendChatMessage(ChatMessage message)
 80    {
 181        if (string.IsNullOrEmpty(conversationUserName))
 082            return;
 83
 184        bool isValidMessage = !string.IsNullOrEmpty(message.body) && !string.IsNullOrWhiteSpace(message.body) && !string
 85
 186        if (!isValidMessage || resetInputFieldOnSubmit)
 87        {
 088            view.chatHudView.ResetInputField();
 089            view.chatHudView.FocusInputField();
 90        }
 91
 192        if (!isValidMessage)
 093            return;
 94
 95        // If Kernel allowed for private messages without the whisper param we could avoid this line
 196        message.body = $"/w {message.recipient} {message.body}";
 97
 198        WebInterface.SendChatMessage(message);
 199    }
 100
 101    public void SetVisibility(bool visible)
 102    {
 22103        if (view.gameObject.activeSelf == visible)
 4104            return;
 105
 18106        view.gameObject.SetActive(visible);
 107
 18108        if (visible)
 109        {
 110            // The messages from 'conversationUserId' are marked as read once the private chat is opened
 8111            MarkUserChatMessagesAsRead(conversationUserId);
 8112            view.chatHudView.scrollRect.verticalNormalizedPosition = 0;
 8113            conversationSnapshot?.AddListener(view.ConfigureAvatarSnapshot);
 114
 8115            AudioScriptableObjects.dialogOpen.Play(true);
 8116        }
 117        else
 118        {
 10119            conversationSnapshot?.RemoveListener(view.ConfigureAvatarSnapshot);
 10120            AudioScriptableObjects.dialogClose.Play(true);
 121        }
 10122    }
 123
 124    public void Dispose()
 125    {
 8126        conversationSnapshot?.RemoveListener(view.ConfigureAvatarSnapshot);
 127
 8128        view.chatHudView.inputField.onSelect.RemoveListener(ChatHUDViewInputField_OnSelect);
 129
 8130        view.OnPressBack -= View_OnPressBack;
 131
 8132        if (chatController != null)
 8133            chatController.OnAddMessage -= OnAddMessage;
 134
 8135        if (view != null)
 8136            UnityEngine.Object.Destroy(view.gameObject);
 8137    }
 138
 139    void OnAddMessage(ChatMessage message)
 140    {
 8141        if (!IsMessageFomCurrentConversation(message))
 1142            return;
 143
 7144        view.chatHudView.controller.AddChatMessage(ChatHUDController.ChatMessageToChatEntry(message));
 145
 7146        if (view.userId == conversationUserId)
 147        {
 148            // The messages from 'conversationUserId' are marked as read if his private chat window is currently open
 7149            MarkUserChatMessagesAsRead(conversationUserId, (long) message.timestamp);
 150        }
 7151    }
 152
 12153    bool IsMessageFomCurrentConversation(ChatMessage message) { return message.messageType == ChatMessage.Type.PRIVATE &
 154
 155    public void ForceFocus()
 156    {
 1157        SetVisibility(true);
 1158        view.chatHudView.FocusInputField();
 1159    }
 160
 161    private void MarkUserChatMessagesAsRead(string userId, long? timestamp = null)
 162    {
 16163        long timeMark = System.DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
 16164        if (timestamp != null && timestamp.Value > timeMark)
 0165            timeMark = timestamp.Value;
 166
 16167        CommonScriptableObjects.lastReadChatMessages.Remove(userId);
 16168        CommonScriptableObjects.lastReadChatMessages.Add(userId, timeMark);
 16169        SaveLatestReadChatMessagesStatus();
 16170    }
 171
 172    private void SaveLatestReadChatMessagesStatus()
 173    {
 16174        List<KeyValuePair<string, long>> lastReadChatMessagesList = new List<KeyValuePair<string, long>>();
 16175        using (var iterator = CommonScriptableObjects.lastReadChatMessages.GetEnumerator())
 176        {
 50177            while (iterator.MoveNext())
 178            {
 34179                lastReadChatMessagesList.Add(new KeyValuePair<string, long>(iterator.Current.Key, iterator.Current.Value
 180            }
 16181        }
 182
 16183        PlayerPrefsUtils.SetString(PLAYER_PREFS_LAST_READ_CHAT_MESSAGES, JsonConvert.SerializeObject(lastReadChatMessage
 16184        PlayerPrefsUtils.Save();
 16185    }
 186
 187    private void LoadLatestReadChatMessagesStatus()
 188    {
 8189        CommonScriptableObjects.lastReadChatMessages.Clear();
 190
 8191        List<KeyValuePair<string, long>> lastReadChatMessagesList = JsonConvert.DeserializeObject<List<KeyValuePair<stri
 8192        if (lastReadChatMessagesList != null)
 193        {
 40194            foreach (var item in lastReadChatMessagesList)
 195            {
 13196                CommonScriptableObjects.lastReadChatMessages.Add(item.Key, item.Value);
 197            }
 198        }
 8199    }
 200
 201    private void ChatHUDViewInputField_OnSelect(string message)
 202    {
 203        // The messages from 'conversationUserId' are marked as read if the player clicks on the input field of the priv
 1204        MarkUserChatMessagesAsRead(conversationUserId);
 1205    }
 206}