< Summary

Class:DCL.Backpack.OutfitsController
Assembly:BackpackEditorHUDV2
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/BackpackEditorHUDV2/Outfits/OutfitsController.cs
Covered lines:16
Uncovered lines:48
Coverable lines:64
Total lines:154
Line coverage:25% (16 of 64)
Covered branches:0
Total branches:0
Covered methods:3
Total methods:11
Method coverage:27.2% (3 of 11)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
OutfitsController(...)0%110100%
SaveOutfitLocally(...)0%2100%
ShowGuestModal()0%2100%
DiscardOutfit(...)0%2100%
OutfitEquip(...)0%6200%
ChangedVisibility(...)0%2.152066.67%
RequestOwnedOutfits()0%2100%
RequestOwnedOutfitsAsync()0%42600%
GenerateAvatarModel(...)0%2100%
UpdateAvatarPreview(...)0%110100%
Dispose()0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/BackpackEditorHUDV2/Outfits/OutfitsController.cs

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using DCL.Interface;
 3using DCL.Tasks;
 4using System;
 5using System.Collections.Generic;
 6using System.Linq;
 7using System.Threading;
 8
 9namespace DCL.Backpack
 10{
 11    public class OutfitsController : IDisposable
 12    {
 13        private readonly LambdaOutfitsService lambdaOutfitsService;
 14        private readonly IUserProfileBridge userProfileBridge;
 15        private readonly IOutfitsSectionComponentView view;
 16        private readonly IBackpackAnalyticsService backpackAnalyticsService;
 17        private readonly DataStore dataStore;
 18        public event Action<OutfitItem> OnOutfitEquipped;
 19
 20        private CancellationTokenSource cts;
 21        private CancellationTokenSource ctsShowOutfit;
 22        private OutfitItem[] localOutfits;
 23        private bool shouldDeploy;
 24        private AvatarModel currentAvatarModel;
 25
 2526        public OutfitsController(
 27            IOutfitsSectionComponentView view,
 28            LambdaOutfitsService lambdaOutfitsService,
 29            IUserProfileBridge userProfileBridge,
 30            DataStore dataStore,
 31            IBackpackAnalyticsService backpackAnalyticsService)
 32        {
 2533            this.view = view;
 2534            this.lambdaOutfitsService = lambdaOutfitsService;
 2535            this.userProfileBridge = userProfileBridge;
 2536            this.backpackAnalyticsService = backpackAnalyticsService;
 2537            this.dataStore = dataStore;
 38
 2539            view.OnOutfitEquipped += OutfitEquip;
 2540            view.OnOutfitDiscarded += DiscardOutfit;
 2541            view.OnTrySaveAsGuest += ShowGuestModal;
 2542            view.OnOutfitLocalSave += SaveOutfitLocally;
 43
 2544            localOutfits = new OutfitItem[]
 45            {
 46                new () { slot = 0 },
 47                new () { slot = 1 },
 48                new () { slot = 2 },
 49                new () { slot = 3 },
 50                new () { slot = 4 }
 51            };
 52
 2553            dataStore.HUDs.avatarEditorVisible.OnChange += ChangedVisibility;
 2554        }
 55
 56        private void SaveOutfitLocally(int outfitIndex)
 57        {
 058            ctsShowOutfit.SafeCancelAndDispose();
 059            ctsShowOutfit = new CancellationTokenSource();
 060            var outfitItem = new OutfitItem()
 61            {
 62                outfit = new OutfitItem.Outfit()
 63                {
 64                    bodyShape = currentAvatarModel.bodyShape,
 65                    eyes = new OutfitItem.eyes() { color = currentAvatarModel.eyeColor },
 66                    hair = new OutfitItem.hair() { color = currentAvatarModel.hairColor },
 67                    skin = new OutfitItem.skin() { color = currentAvatarModel.skinColor },
 68                    wearables = new List<string>(currentAvatarModel.wearables).ToArray(),
 69                    forceRender = new List<string>(currentAvatarModel.forceRender).ToArray()
 70                },
 71                slot = outfitIndex
 72            };
 73
 074            localOutfits[outfitIndex] = outfitItem;
 075            view.ShowOutfit(outfitItem, GenerateAvatarModel(outfitItem), ctsShowOutfit.Token).Forget();
 076            shouldDeploy = true;
 77
 078            backpackAnalyticsService.SendOutfitSave(outfitIndex);
 079        }
 80
 81        private void ShowGuestModal() =>
 082            dataStore.HUDs.connectWalletModalVisible.Set(true);
 83
 84        private void DiscardOutfit(int outfitIndex)
 85        {
 086            backpackAnalyticsService.SendOutfitDelete(outfitIndex);
 087            localOutfits[outfitIndex] = new () { slot = outfitIndex };
 088            shouldDeploy = true;
 089        }
 90
 91        private void OutfitEquip(OutfitItem outfit)
 92        {
 093            backpackAnalyticsService.SendOutfitEquipped(outfit.slot);
 094            OnOutfitEquipped?.Invoke(outfit);
 095        }
 96
 97        private void ChangedVisibility(bool current, bool previous)
 98        {
 17399            if (shouldDeploy)
 0100                WebInterface.SaveUserOutfits(localOutfits);
 173101        }
 102
 103        public void RequestOwnedOutfits()
 104        {
 0105            cts.SafeCancelAndDispose();
 0106            cts = new CancellationTokenSource();
 0107            RequestOwnedOutfitsAsync(cts.Token).Forget();
 0108        }
 109
 110        private async UniTask RequestOwnedOutfitsAsync(CancellationToken ct)
 111        {
 0112            view.SetIsGuest(userProfileBridge.GetOwn().isGuest);
 0113            (IReadOnlyList<OutfitItem> outfits, int totalAmount) requestOwnedOutfits = await lambdaOutfitsService.Reques
 0114            OutfitItem[] outfitItems = requestOwnedOutfits.outfits.ToArray();
 0115            AudioScriptableObjects.listItemAppear.ResetPitch();
 0116            view.SetSlotsAsLoading(outfitItems);
 117
 0118            foreach (OutfitItem outfitItem in outfitItems)
 119            {
 0120                localOutfits[outfitItem.slot] = outfitItem;
 0121                await view.ShowOutfit(outfitItem, GenerateAvatarModel(outfitItem), ct);
 122            }
 0123        }
 124
 125        private AvatarModel GenerateAvatarModel(OutfitItem outfitItem)
 126        {
 0127            AvatarModel avatarModel = new AvatarModel();
 0128            avatarModel.CopyFrom(currentAvatarModel);
 0129            avatarModel.bodyShape = outfitItem.outfit.bodyShape;
 0130            avatarModel.wearables = new List<string>(outfitItem.outfit.wearables.ToList());
 0131            avatarModel.eyeColor = outfitItem.outfit.eyes.color;
 0132            avatarModel.hairColor = outfitItem.outfit.hair.color;
 0133            avatarModel.skinColor = outfitItem.outfit.skin.color;
 0134            avatarModel.forceRender = new HashSet<string>(outfitItem.outfit.forceRender);
 0135            return avatarModel;
 136        }
 137
 138        public void UpdateAvatarPreview(AvatarModel newAvatarModel) =>
 26139            currentAvatarModel = newAvatarModel;
 140
 141        public void Dispose()
 142        {
 0143            cts.SafeCancelAndDispose();
 0144            cts = null;
 0145            ctsShowOutfit.SafeCancelAndDispose();
 0146            ctsShowOutfit = null;
 0147            view.OnOutfitEquipped -= OutfitEquip;
 0148            view.OnOutfitDiscarded -= DiscardOutfit;
 0149            view.OnTrySaveAsGuest -= ShowGuestModal;
 150
 0151            dataStore.HUDs.avatarEditorVisible.OnChange -= ChangedVisibility;
 0152        }
 153    }
 154}