< Summary

Class:SectionSceneContributorsSettingsController
Assembly:BuilderProjectsPanel
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/BuilderProjectsPanel/Scripts/SectionController/MenuSections/SectionSceneContributorsSettingsController.cs
Covered lines:42
Uncovered lines:26
Coverable lines:68
Total lines:127
Line coverage:61.7% (42 of 68)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
SectionSceneContributorsSettingsController(...)0%110100%
SectionSceneContributorsSettingsController()0%2100%
Dispose()0%110100%
SetViewContainer(...)0%2100%
OnShow()0%2100%
OnHide()0%2100%
OnSelectScene(...)0%2100%
UpdateContributors(...)0%6.496076.19%
AddContributor(...)0%2.012088.89%
OnAddUserPressed(...)0%12300%
OnRemoveUserPressed(...)0%3.013088.89%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/BuilderProjectsPanel/Scripts/SectionController/MenuSections/SectionSceneContributorsSettingsController.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using UnityEngine;
 5using Object = UnityEngine.Object;
 6
 7internal class SectionSceneContributorsSettingsController : SectionBase, ISelectSceneListener, ISectionUpdateSceneContri
 8{
 9    public const string VIEW_PREFAB_PATH = "BuilderProjectsPanelMenuSections/SectionSceneContributorsSettingsView";
 10
 11    public event Action<string, SceneContributorsUpdatePayload> OnRequestUpdateSceneContributors;
 12
 13    private readonly SectionSceneContributorsSettingsView view;
 14    private readonly FriendsSearchPromptController friendsSearchPromptController;
 415    private readonly UserProfileFetcher profileFetcher = new UserProfileFetcher();
 416    private readonly SceneContributorsUpdatePayload contributorsUpdatePayload = new SceneContributorsUpdatePayload();
 17
 418    private List<string> contributorsList = new List<string>();
 19    private string sceneId;
 20
 021    public SectionSceneContributorsSettingsController() : this(
 22        Object.Instantiate(Resources.Load<SectionSceneContributorsSettingsView>(VIEW_PREFAB_PATH)),
 23        FriendsController.i
 024    ) { }
 25
 426    public SectionSceneContributorsSettingsController(SectionSceneContributorsSettingsView view, IFriendsController frie
 27    {
 428        this.view = view;
 429        friendsSearchPromptController = new FriendsSearchPromptController(view.GetSearchPromptView(), friendsController)
 30
 431        view.OnSearchUserButtonPressed += () => friendsSearchPromptController.Show();
 432        friendsSearchPromptController.OnAddUser += OnAddUserPressed;
 433        friendsSearchPromptController.OnRemoveUser += OnRemoveUserPressed;
 434    }
 35
 36    public override void Dispose()
 37    {
 438        view.Dispose();
 439        profileFetcher.Dispose();
 440        friendsSearchPromptController.Dispose();
 441    }
 42
 043    public override void SetViewContainer(Transform viewContainer) { view.SetParent(viewContainer); }
 44
 045    protected override void OnShow() { view.SetActive(true); }
 46
 047    protected override void OnHide() { view.SetActive(false); }
 48
 49    void ISelectSceneListener.OnSelectScene(ISceneCardView sceneCardView)
 50    {
 051        sceneId = sceneCardView.sceneData.id;
 052        UpdateContributors(sceneCardView.sceneData.contributors);
 053    }
 54
 55    internal void UpdateContributors(string[] contributors)
 56    {
 357        if (contributors == null || contributors.Length == 0)
 58        {
 059            if (contributorsList.Count > 0)
 060                contributorsList.Clear();
 61
 062            view.SetEmptyList(true);
 063            view.SetContributorsCount(0);
 064            return;
 65        }
 66
 367        var newContributors = new List<string>(contributors);
 1868        for (int i = 0; i < newContributors.Count; i++)
 69        {
 670            AddContributor(newContributors[i]);
 671            contributorsList.Remove(newContributors[i]);
 72        }
 73
 874        for (int i = 0; i < contributorsList.Count; i++)
 75        {
 176            view.RemoveUser(contributorsList[i]);
 77        }
 78
 379        contributorsList = newContributors;
 80
 381        friendsSearchPromptController.SetUsersInRolList(contributorsList);
 382        view.SetEmptyList(false);
 383        view.SetContributorsCount(contributorsList.Count);
 384    }
 85
 86    void AddContributor(string userId)
 87    {
 688        if (string.IsNullOrEmpty(userId))
 089            return;
 90
 691        var userView = view.AddUser(userId);
 692        profileFetcher.FetchProfile(userId)
 093                      .Then(userProfile => userView.SetUserProfile(userProfile));
 94
 695        userView.OnAddPressed -= OnAddUserPressed;
 696        userView.OnRemovePressed -= OnRemoveUserPressed;
 697        userView.OnAddPressed += OnAddUserPressed;
 698        userView.OnRemovePressed += OnRemoveUserPressed;
 699    }
 100
 101    void OnAddUserPressed(string userId)
 102    {
 0103        if (contributorsList.Contains(userId))
 0104            return;
 105
 0106        contributorsList.Add(userId);
 0107        AddContributor(userId);
 0108        friendsSearchPromptController.SetUsersInRolList(contributorsList);
 0109        view.SetEmptyList(false);
 0110        view.SetContributorsCount(contributorsList.Count);
 0111        contributorsUpdatePayload.contributors = contributorsList.ToArray();
 0112        OnRequestUpdateSceneContributors?.Invoke(sceneId, contributorsUpdatePayload);
 0113    }
 114
 115    void OnRemoveUserPressed(string userId)
 116    {
 1117        if (!contributorsList.Remove(userId))
 0118            return;
 119
 1120        view.RemoveUser(userId);
 1121        friendsSearchPromptController.SetUsersInRolList(contributorsList);
 1122        view.SetEmptyList(contributorsList.Count == 0);
 1123        view.SetContributorsCount(contributorsList.Count);
 1124        contributorsUpdatePayload.contributors = contributorsList.ToArray();
 1125        OnRequestUpdateSceneContributors?.Invoke(sceneId, contributorsUpdatePayload);
 1126    }
 127}