< Summary

Class:DCL.EquippedEmotes.EquippedEmotesInitializerPlugin
Assembly:EquippedEmotesPlugin
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/EquippedEmotes/EquippedEmotesPlugin.cs
Covered lines:0
Uncovered lines:54
Coverable lines:54
Total lines:136
Line coverage:0% (0 of 54)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
EquippedEmotesInitializerPlugin()0%2100%
OnSignupFlowChanged(...)0%6200%
OnOwnUserProfileUpdated(...)0%30500%
GetDefaultEmotes()0%2100%
LoadDefaultEquippedEmotes()0%2100%
LoadEquippedEmotesFromLocalStorage()0%6200%
OnEquippedEmotesSet(...)0%2100%
OnEquippedEmoteAddedOrRemoved(...)0%2100%
SaveEquippedEmotesInLocalStorage()0%30500%
SetEquippedEmotes(...)0%12300%
Dispose()0%6200%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/EquippedEmotes/EquippedEmotesPlugin.cs

#LineLine coverage
 1using DCL.Helpers;
 2using Newtonsoft.Json;
 3using System.Collections.Generic;
 4using System.Linq;
 5using UnityEngine;
 6
 7namespace DCL.EquippedEmotes
 8{
 9    /// <summary>
 10    /// Plugin feature that initialize the Equipped Emotes feature.
 11    /// </summary>
 12    public class EquippedEmotesInitializerPlugin : IPlugin
 13    {
 14        internal const string PLAYER_PREFS_EQUIPPED_EMOTES_KEY = "EquippedNFTEmotes";
 15
 016        internal DataStore_EmotesCustomization emotesCustomizationDataStore => DataStore.i.emotesCustomization;
 017        internal DataStore_Common commonDataStore => DataStore.i.common;
 018        internal DataStore_FeatureFlag featureFlagsDataStore => DataStore.i.featureFlags;
 19        internal UserProfile ownUserProfile;
 20
 021        public EquippedEmotesInitializerPlugin()
 22        {
 023            ownUserProfile = UserProfile.GetOwnUserProfile();
 024            ownUserProfile.OnUpdate += OnOwnUserProfileUpdated;
 025            LoadDefaultEquippedEmotes();
 26
 027            LoadEquippedEmotesFromLocalStorage();
 28
 029            emotesCustomizationDataStore.equippedEmotes.OnSet += OnEquippedEmotesSet;
 030            emotesCustomizationDataStore.equippedEmotes.OnAdded += OnEquippedEmoteAddedOrRemoved;
 031            emotesCustomizationDataStore.equippedEmotes.OnRemoved += OnEquippedEmoteAddedOrRemoved;
 032            commonDataStore.isSignUpFlow.OnChange += OnSignupFlowChanged;
 033        }
 34
 35        private void OnSignupFlowChanged(bool current, bool previous)
 36        {
 037            if (current)
 038                LoadDefaultEquippedEmotes();
 039        }
 40
 41        private void OnOwnUserProfileUpdated(UserProfile userProfile)
 42        {
 043            if (userProfile == null || userProfile.avatar == null || userProfile.avatar.emotes.Count == 0)
 044                return;
 45
 046            List<string> equippedEmotes = new List<string> (Enumerable.Repeat((string) null, 10));
 047            foreach (AvatarModel.AvatarEmoteEntry avatarEmoteEntry in userProfile.avatar.emotes)
 48            {
 049                equippedEmotes[avatarEmoteEntry.slot] = avatarEmoteEntry.urn;
 50            }
 051            SetEquippedEmotes(equippedEmotes);
 052        }
 53
 54        internal List<string> GetDefaultEmotes()
 55        {
 056            return new List<string>
 57            {
 58                "handsair",
 59                "wave",
 60                "fistpump",
 61                "dance",
 62                "raiseHand",
 63                "clap",
 64                "money",
 65                "kiss",
 66                "headexplode",
 67                "shrug"
 68            };
 69        }
 70
 071        internal void LoadDefaultEquippedEmotes() { SetEquippedEmotes(GetDefaultEmotes()); }
 72
 73        internal void LoadEquippedEmotesFromLocalStorage()
 74        {
 75            List<string> storedEquippedEmotes;
 76
 77            try
 78            {
 079                storedEquippedEmotes = JsonConvert.DeserializeObject<List<string>>(PlayerPrefsBridge.GetString(PLAYER_PR
 080            }
 081            catch
 82            {
 083                storedEquippedEmotes = null;
 084            }
 85
 086            if (storedEquippedEmotes == null)
 087                storedEquippedEmotes = GetDefaultEmotes();
 88
 089            SetEquippedEmotes(storedEquippedEmotes);
 090        }
 91
 92        internal void OnEquippedEmotesSet(IEnumerable<EquippedEmoteData> equippedEmotes)
 93        {
 94
 095        }
 96
 97        internal void OnEquippedEmoteAddedOrRemoved(EquippedEmoteData equippedEmote)
 98        {
 99
 0100        }
 101
 102        internal void SaveEquippedEmotesInLocalStorage()
 103        {
 0104            List<string> emotesIdsToStore = new List<string>();
 0105            foreach (EquippedEmoteData equippedEmoteData in emotesCustomizationDataStore.equippedEmotes.Get())
 106            {
 0107                emotesIdsToStore.Add(equippedEmoteData != null ? equippedEmoteData.id : null);
 108            }
 109
 110            // TODO: We should avoid static calls and create injectable interfaces
 0111            PlayerPrefsBridge.SetString(PLAYER_PREFS_EQUIPPED_EMOTES_KEY, JsonConvert.SerializeObject(emotesIdsToStore))
 0112            PlayerPrefsBridge.Save();
 0113        }
 114
 115        internal void SetEquippedEmotes(List<string> storedEquippedEmotes)
 116        {
 0117            List<EquippedEmoteData> storedEquippedEmotesData = new List<EquippedEmoteData>();
 0118            foreach (string emoteId in storedEquippedEmotes)
 119            {
 0120                storedEquippedEmotesData.Add(
 121                    string.IsNullOrEmpty(emoteId) ? null : new EquippedEmoteData { id = emoteId, cachedThumbnail = null 
 122            }
 0123            emotesCustomizationDataStore.equippedEmotes.Set(storedEquippedEmotesData);
 0124        }
 125
 126        public void Dispose()
 127        {
 0128            emotesCustomizationDataStore.equippedEmotes.OnSet -= OnEquippedEmotesSet;
 0129            emotesCustomizationDataStore.equippedEmotes.OnAdded -= OnEquippedEmoteAddedOrRemoved;
 0130            emotesCustomizationDataStore.equippedEmotes.OnRemoved -= OnEquippedEmoteAddedOrRemoved;
 0131            commonDataStore.isSignUpFlow.OnChange -= OnSignupFlowChanged;
 0132            if (ownUserProfile != null)
 0133                ownUserProfile.OnUpdate -= OnOwnUserProfileUpdated;
 0134        }
 135    }
 136}