< Summary

Class:FriendsTabView
Assembly:FriendsHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/FriendsHUD/Scripts/Tabs/FriendsTabView.cs
Covered lines:60
Uncovered lines:24
Coverable lines:84
Total lines:185
Line coverage:71.4% (60 of 84)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
FriendsTabView()0%110100%
Initialize(...)0%220100%
OnDestroy()0%220100%
CreateEntry(...)0%2.022083.33%
RemoveEntry(...)0%2.012085.71%
UpdateEntry(...)0%4.114081.25%
OnPressDeleteButton(...)0%3.033085.71%
ChatController_OnAddMessage(...)0%56700%
CreateOrUpdateEntryDeferred(...)0%2.062075%
UpdateLayout()0%5.015092.31%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/FriendsHUD/Scripts/Tabs/FriendsTabView.cs

#LineLine coverage
 1using System.Collections.Generic;
 2using System.Linq;
 3using DCL.Interface;
 4
 5public class FriendsTabView : FriendsTabViewBase
 6{
 7    private const int CREATION_AMOUNT_PER_FRAME = 5;
 8
 449    public EntryList onlineFriendsList = new EntryList();
 4410    public EntryList offlineFriendsList = new EntryList();
 11    public event System.Action<FriendEntry> OnWhisper;
 12    public event System.Action<string> OnDeleteConfirmation;
 13
 14    private string lastProcessedFriend;
 4415    internal readonly Dictionary<string, FriendEntryBase.Model> creationQueue = new Dictionary<string, FriendEntryBase.M
 16
 17    public override void Initialize(FriendsHUDView owner, int preinstantiatedEntries)
 18    {
 2219        base.Initialize(owner, preinstantiatedEntries);
 20
 2221        onlineFriendsList.toggleTextPrefix = "ONLINE";
 2222        offlineFriendsList.toggleTextPrefix = "OFFLINE";
 23
 2224        if (ChatController.i != null)
 25        {
 2226            ChatController.i.OnAddMessage -= ChatController_OnAddMessage;
 2227            ChatController.i.OnAddMessage += ChatController_OnAddMessage;
 28        }
 2229    }
 30
 31    public override void OnDestroy()
 32    {
 2233        base.OnDestroy();
 34
 2235        if (ChatController.i != null)
 2236            ChatController.i.OnAddMessage -= ChatController_OnAddMessage;
 2237    }
 38
 39    protected override bool CreateEntry(string userId)
 40    {
 1941        if (!base.CreateEntry(userId))
 042            return false;
 43
 1944        var entry = GetEntry(userId) as FriendEntry;
 45
 2046        entry.OnWhisperClick += (x) => OnWhisper?.Invoke(x);
 1947        entry.OnJumpInClick += (x) => this.owner.OnCloseButtonPressed();
 48
 1949        return true;
 50    }
 51
 52    public override bool RemoveEntry(string userId)
 53    {
 454        if (!base.RemoveEntry(userId))
 055            return false;
 56
 457        offlineFriendsList.Remove(userId);
 458        onlineFriendsList.Remove(userId);
 459        offlineFriendsList.RemoveLastTimestamp(userId);
 460        onlineFriendsList.RemoveLastTimestamp(userId);
 461        return true;
 62    }
 63
 64    public override bool UpdateEntry(string userId, FriendEntryBase.Model model)
 65    {
 1966        if (!base.UpdateEntry(userId, model))
 67        {
 68            //Replace the queued model for creation for the updated one.
 069            if (creationQueue.ContainsKey(userId))
 070                creationQueue[userId] = model;
 071            return false;
 72        }
 73
 1974        var entry = entries[userId];
 75
 1976        if (model.status == PresenceStatus.ONLINE)
 77        {
 578            offlineFriendsList.Remove(userId);
 579            onlineFriendsList.Add(userId, entry);
 80
 581            var removedTimestamp = offlineFriendsList.RemoveLastTimestamp(userId);
 582            onlineFriendsList.AddOrUpdateLastTimestamp(removedTimestamp);
 583        }
 84        else
 85        {
 1486            onlineFriendsList.Remove(userId);
 1487            offlineFriendsList.Add(userId, entry);
 88
 1489            var removedTimestamp = onlineFriendsList.RemoveLastTimestamp(userId);
 1490            offlineFriendsList.AddOrUpdateLastTimestamp(removedTimestamp);
 91        }
 92
 1993        return true;
 94    }
 95
 96    protected override void OnPressDeleteButton(string userId)
 97    {
 198        if (string.IsNullOrEmpty(userId))
 099            return;
 100
 1101        FriendEntryBase friendEntryToDelete = GetEntry(userId);
 1102        if (friendEntryToDelete != null)
 103        {
 1104            confirmationDialog.SetText($"Are you sure you want to delete {friendEntryToDelete.model.userName} as a frien
 1105            confirmationDialog.Show(() =>
 106            {
 1107                RemoveEntry(userId);
 1108                OnDeleteConfirmation?.Invoke(userId);
 1109            });
 110        }
 1111    }
 112
 113    private void ChatController_OnAddMessage(ChatMessage message)
 114    {
 0115        if (message.messageType != ChatMessage.Type.PRIVATE)
 0116            return;
 117
 0118        FriendEntryBase friend = GetEntry(message.sender != UserProfile.GetOwnUserProfile().userId
 119            ? message.sender
 120            : message.recipient);
 121
 0122        if (friend == null)
 0123            return;
 124
 0125        bool reorderFriendEntries = false;
 126
 0127        if (friend.userId != lastProcessedFriend)
 128        {
 0129            lastProcessedFriend = friend.userId;
 0130            reorderFriendEntries = true;
 131        }
 132
 0133        LastFriendTimestampModel timestampToUpdate = new LastFriendTimestampModel
 134        {
 135            userId = friend.userId,
 136            lastMessageTimestamp = message.timestamp
 137        };
 138
 139        // Each time a private message is received (or sent by the player), we sort the online and offline lists by time
 0140        if (friend.model.status == PresenceStatus.ONLINE)
 141        {
 0142            onlineFriendsList.AddOrUpdateLastTimestamp(timestampToUpdate, reorderFriendEntries);
 0143        }
 144        else
 145        {
 0146            offlineFriendsList.AddOrUpdateLastTimestamp(timestampToUpdate, reorderFriendEntries);
 147        }
 148
 0149        lastProcessedFriend = friend.userId;
 0150    }
 151
 152    public void CreateOrUpdateEntryDeferred(string userId, FriendEntryBase.Model model)
 153    {
 20154        if (creationQueue.ContainsKey(userId))
 0155            creationQueue[userId] = model;
 156        else
 20157            creationQueue.Add(userId, model);
 20158    }
 159
 160    /// <summary>
 161    /// To avoid having hiccups when a player with dozens of friends load into the game
 162    /// we deferred the entries instantiation to multiple frames
 163    /// </summary>
 164    protected override void UpdateLayout()
 165    {
 65166        if (creationQueue.Count == 0)
 50167            return;
 168
 68169        for (int i = 0; i < CREATION_AMOUNT_PER_FRAME && creationQueue.Count != 0; i++)
 170        {
 19171            var pair = creationQueue.FirstOrDefault();
 19172            creationQueue.Remove(pair.Key);
 19173            CreateEntry(pair.Key);
 19174            UpdateEntry(pair.Key, pair.Value);
 175        }
 176
 177        //If we have creations to process we avoid reconstructing the layout until we are done
 15178        if (creationQueue.Count != 0)
 179        {
 0180            layoutIsDirty = false;
 181        }
 182
 15183        base.UpdateLayout();
 15184    }
 185}