< Summary

Class:DCL.Builder.SectionSceneAdminsSettingsController
Assembly:BuilderProjectsPanel
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/HUD/ProjectsPanelHUD/Scripts/SectionController/MenuSections/SectionSceneAdminsSettingsController.cs
Covered lines:92
Uncovered lines:30
Coverable lines:122
Total lines:216
Line coverage:75.4% (92 of 122)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
SectionSceneAdminsSettingsController(...)0%110100%
SectionSceneAdminsSettingsController()0%2100%
Dispose()0%110100%
SetViewContainer(...)0%2100%
OnShow()0%2100%
OnHide()0%2100%
OnSelectScene(...)0%2100%
SetAdmins(...)0%6.496076.19%
SetBannedUsers(...)0%7.336066.67%
AddAdmin(...)0%2.012088.89%
AddBannedUser(...)0%2.012088.89%
OnAddAdminPressed(...)0%3.073080%
OnRemoveAdminPressed(...)0%3.013088.89%
OnAddBannedUserPressed(...)0%3.073080%
OnRemoveBannedUserPressed(...)0%3.013088.89%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/HUD/ProjectsPanelHUD/Scripts/SectionController/MenuSections/SectionSceneAdminsSettingsController.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using UnityEngine;
 5using Object = UnityEngine.Object;
 6
 7namespace DCL.Builder
 8{
 9    internal class SectionSceneAdminsSettingsController : SectionBase, ISelectSceneListener,
 10                                                          ISectionUpdateSceneAdminsRequester, ISectionUpdateSceneBannedU
 11    {
 12        public const string VIEW_PREFAB_PATH = "BuilderProjectsPanelMenuSections/SectionSceneAdminsSettingsView";
 13
 14        public event Action<string, SceneAdminsUpdatePayload> OnRequestUpdateSceneAdmins;
 15        public event Action<string, SceneBannedUsersUpdatePayload> OnRequestUpdateSceneBannedUsers;
 16
 17        private readonly SectionSceneAdminsSettingsView view;
 18        private readonly FriendsSearchPromptController friendsSearchPromptController;
 19        private readonly UsersSearchPromptController usersSearchPromptController;
 820        private readonly UserProfileFetcher profileFetcher = new UserProfileFetcher();
 21
 822        internal readonly SceneAdminsUpdatePayload adminsUpdatePayload = new SceneAdminsUpdatePayload();
 823        internal readonly SceneBannedUsersUpdatePayload bannedUsersUpdatePayload = new SceneBannedUsersUpdatePayload();
 24
 825        private List<string> admins = new List<string>();
 826        private List<string> bannedUsers = new List<string>();
 27        private string sceneId;
 28
 029        public SectionSceneAdminsSettingsController() : this(
 30            Object.Instantiate(Resources.Load<SectionSceneAdminsSettingsView>(VIEW_PREFAB_PATH)),
 31            FriendsController.i
 032        ) { }
 33
 834        public SectionSceneAdminsSettingsController(SectionSceneAdminsSettingsView view, IFriendsController friendsContr
 35        {
 836            this.view = view;
 837            friendsSearchPromptController = new FriendsSearchPromptController(view.GetAdminsSearchPromptView(), friendsC
 838            usersSearchPromptController = new UsersSearchPromptController(view.GetBlockedSearchPromptView());
 39
 840            view.OnSearchFriendButtonPressed += () => friendsSearchPromptController.Show();
 841            view.OnSearchUserButtonPressed += () => usersSearchPromptController.Show();
 42
 843            friendsSearchPromptController.OnAddUser += OnAddAdminPressed;
 844            friendsSearchPromptController.OnRemoveUser += OnRemoveAdminPressed;
 845            usersSearchPromptController.OnAddUser += OnAddBannedUserPressed;
 846            usersSearchPromptController.OnRemoveUser += OnRemoveBannedUserPressed;
 847        }
 48
 49        public override void Dispose()
 50        {
 851            view.Dispose();
 852            profileFetcher.Dispose();
 853            friendsSearchPromptController.Dispose();
 854            usersSearchPromptController.Dispose();
 855        }
 56
 057        public override void SetViewContainer(Transform viewContainer) { view.SetParent(viewContainer); }
 58
 059        protected override void OnShow() { view.SetActive(true); }
 60
 061        protected override void OnHide() { view.SetActive(false); }
 62
 63        void ISelectSceneListener.OnSelectScene(ISceneCardView sceneCardView)
 64        {
 065            sceneId = sceneCardView.SceneData.id;
 066            SetAdmins(sceneCardView.SceneData.admins);
 067            SetBannedUsers(sceneCardView.SceneData.bannedUsers);
 068        }
 69
 70        internal void SetAdmins(string[] usersId)
 71        {
 372            if (usersId == null || usersId.Length == 0)
 73            {
 074                if (admins.Count > 0)
 075                    admins.Clear();
 76
 077                view.SetAdminsEmptyList(true);
 078                view.SetAdminsCount(0);
 079                return;
 80            }
 81
 382            var newAdmins = new List<string>(usersId);
 1883            for (int i = 0; i < newAdmins.Count; i++)
 84            {
 685                AddAdmin(newAdmins[i]);
 686                admins.Remove(newAdmins[i]);
 87            }
 88
 889            for (int i = 0; i < admins.Count; i++)
 90            {
 191                view.RemoveAdmin(admins[i]);
 92            }
 93
 394            admins = newAdmins;
 95
 396            friendsSearchPromptController.SetUsersInRolList(admins);
 397            view.SetAdminsEmptyList(false);
 398            view.SetAdminsCount(admins.Count);
 399        }
 100
 101        internal void SetBannedUsers(string[] usersId)
 102        {
 3103            if (usersId == null || usersId.Length == 0)
 104            {
 0105                if (bannedUsers.Count > 0)
 0106                    bannedUsers.Clear();
 107
 0108                view.SetBannedUsersEmptyList(true);
 0109                view.SetBannedUsersCount(0);
 0110                return;
 111            }
 112
 3113            var newBlocked = new List<string>(usersId);
 22114            for (int i = 0; i < newBlocked.Count; i++)
 115            {
 8116                AddBannedUser(newBlocked[i]);
 8117                bannedUsers.Remove(newBlocked[i]);
 118            }
 119
 6120            for (int i = 0; i < bannedUsers.Count; i++)
 121            {
 0122                view.RemoveBannedUser(bannedUsers[i]);
 123            }
 124
 3125            bannedUsers = newBlocked;
 126
 3127            usersSearchPromptController.SetUsersInRolList(bannedUsers);
 3128            view.SetBannedUsersEmptyList(false);
 3129            view.SetBannedUsersCount(bannedUsers.Count);
 3130        }
 131
 132        internal void AddAdmin(string userId)
 133        {
 7134            if (string.IsNullOrEmpty(userId))
 0135                return;
 136
 7137            var userView = view.AddAdmin(userId);
 7138            profileFetcher.FetchProfile(userId)
 0139                          .Then(userProfile => userView.SetUserProfile(userProfile));
 140
 7141            userView.OnAddPressed -= OnAddAdminPressed;
 7142            userView.OnRemovePressed -= OnRemoveAdminPressed;
 7143            userView.OnAddPressed += OnAddAdminPressed;
 7144            userView.OnRemovePressed += OnRemoveAdminPressed;
 7145        }
 146
 147        internal void AddBannedUser(string userId)
 148        {
 9149            if (string.IsNullOrEmpty(userId))
 0150                return;
 151
 9152            var userView = view.AddBannedUser(userId);
 9153            profileFetcher.FetchProfile(userId)
 0154                          .Then(userProfile => userView.SetUserProfile(userProfile));
 155
 9156            userView.OnAddPressed -= OnAddBannedUserPressed;
 9157            userView.OnRemovePressed -= OnRemoveBannedUserPressed;
 9158            userView.OnAddPressed += OnAddBannedUserPressed;
 9159            userView.OnRemovePressed += OnRemoveBannedUserPressed;
 9160        }
 161
 162        internal void OnAddAdminPressed(string userId)
 163        {
 1164            if (admins.Contains(userId))
 0165                return;
 166
 1167            admins.Add(userId);
 1168            AddAdmin(userId);
 1169            friendsSearchPromptController.SetUsersInRolList(admins);
 1170            view.SetAdminsEmptyList(false);
 1171            view.SetAdminsCount(admins.Count);
 1172            adminsUpdatePayload.admins = admins.ToArray();
 1173            OnRequestUpdateSceneAdmins?.Invoke(sceneId, adminsUpdatePayload);
 0174        }
 175
 176        void OnRemoveAdminPressed(string userId)
 177        {
 1178            if (!admins.Remove(userId))
 0179                return;
 180
 1181            view.RemoveAdmin(userId);
 1182            friendsSearchPromptController.SetUsersInRolList(admins);
 1183            view.SetAdminsEmptyList(admins.Count == 0);
 1184            view.SetAdminsCount(admins.Count);
 1185            adminsUpdatePayload.admins = admins.ToArray();
 1186            OnRequestUpdateSceneAdmins?.Invoke(sceneId, adminsUpdatePayload);
 1187        }
 188
 189        internal void OnAddBannedUserPressed(string userId)
 190        {
 1191            if (bannedUsers.Contains(userId))
 0192                return;
 193
 1194            bannedUsers.Add(userId);
 1195            AddBannedUser(userId);
 1196            usersSearchPromptController.SetUsersInRolList(bannedUsers);
 1197            view.SetBannedUsersEmptyList(false);
 1198            view.SetBannedUsersCount(bannedUsers.Count);
 1199            bannedUsersUpdatePayload.bannedUsers = bannedUsers.ToArray();
 1200            OnRequestUpdateSceneBannedUsers?.Invoke(sceneId, bannedUsersUpdatePayload);
 0201        }
 202
 203        void OnRemoveBannedUserPressed(string userId)
 204        {
 1205            if (!bannedUsers.Remove(userId))
 0206                return;
 207
 1208            view.RemoveBannedUser(userId);
 1209            usersSearchPromptController.SetUsersInRolList(bannedUsers);
 1210            view.SetBannedUsersEmptyList(bannedUsers.Count == 0);
 1211            view.SetBannedUsersCount(bannedUsers.Count);
 1212            bannedUsersUpdatePayload.bannedUsers = bannedUsers.ToArray();
 1213            OnRequestUpdateSceneBannedUsers?.Invoke(sceneId, bannedUsersUpdatePayload);
 1214        }
 215    }
 216}