< 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:58
Coverable lines:58
Total lines:138
Line coverage:0% (0 of 58)
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%6200%
OnEquippedEmoteAddedOrRemoved(...)0%6200%
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 || !DataStore.i.emotes.newFlowEnabled.Get())
 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>>(PlayerPrefsUtils.GetString(PLAYER_PRE
 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        {
 094            if (!DataStore.i.emotes.newFlowEnabled.Get())
 095                SaveEquippedEmotesInLocalStorage();
 096        }
 97
 98        internal void OnEquippedEmoteAddedOrRemoved(EquippedEmoteData equippedEmote)
 99        {
 0100            if (!DataStore.i.emotes.newFlowEnabled.Get())
 0101                SaveEquippedEmotesInLocalStorage();
 0102        }
 103
 104        internal void SaveEquippedEmotesInLocalStorage()
 105        {
 0106            List<string> emotesIdsToStore = new List<string>();
 0107            foreach (EquippedEmoteData equippedEmoteData in emotesCustomizationDataStore.equippedEmotes.Get())
 108            {
 0109                emotesIdsToStore.Add(equippedEmoteData != null ? equippedEmoteData.id : null);
 110            }
 111
 112            // TODO: We should avoid static calls and create injectable interfaces
 0113            PlayerPrefsUtils.SetString(PLAYER_PREFS_EQUIPPED_EMOTES_KEY, JsonConvert.SerializeObject(emotesIdsToStore));
 0114            PlayerPrefsUtils.Save();
 0115        }
 116
 117        internal void SetEquippedEmotes(List<string> storedEquippedEmotes)
 118        {
 0119            List<EquippedEmoteData> storedEquippedEmotesData = new List<EquippedEmoteData>();
 0120            foreach (string emoteId in storedEquippedEmotes)
 121            {
 0122                storedEquippedEmotesData.Add(
 123                    string.IsNullOrEmpty(emoteId) ? null : new EquippedEmoteData { id = emoteId, cachedThumbnail = null 
 124            }
 0125            emotesCustomizationDataStore.equippedEmotes.Set(storedEquippedEmotesData);
 0126        }
 127
 128        public void Dispose()
 129        {
 0130            emotesCustomizationDataStore.equippedEmotes.OnSet -= OnEquippedEmotesSet;
 0131            emotesCustomizationDataStore.equippedEmotes.OnAdded -= OnEquippedEmoteAddedOrRemoved;
 0132            emotesCustomizationDataStore.equippedEmotes.OnRemoved -= OnEquippedEmoteAddedOrRemoved;
 0133            commonDataStore.isSignUpFlow.OnChange -= OnSignupFlowChanged;
 0134            if (ownUserProfile != null)
 0135                ownUserProfile.OnUpdate -= OnOwnUserProfileUpdated;
 0136        }
 137    }
 138}