< 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:218
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 DCl.Social.Friends;
 5using DCL.Social.Friends;
 6using UnityEngine;
 7using Object = UnityEngine.Object;
 8
 9namespace DCL.Builder
 10{
 11    internal class SectionSceneAdminsSettingsController : SectionBase, ISelectSceneListener,
 12                                                          ISectionUpdateSceneAdminsRequester, ISectionUpdateSceneBannedU
 13    {
 14        public const string VIEW_PREFAB_PATH = "BuilderProjectsPanelMenuSections/SectionSceneAdminsSettingsView";
 15
 16        public event Action<string, SceneAdminsUpdatePayload> OnRequestUpdateSceneAdmins;
 17        public event Action<string, SceneBannedUsersUpdatePayload> OnRequestUpdateSceneBannedUsers;
 18
 19        private readonly SectionSceneAdminsSettingsView view;
 20        private readonly FriendsSearchPromptController friendsSearchPromptController;
 21        private readonly UsersSearchPromptController usersSearchPromptController;
 822        private readonly UserProfileFetcher profileFetcher = new UserProfileFetcher();
 23
 824        internal readonly SceneAdminsUpdatePayload adminsUpdatePayload = new SceneAdminsUpdatePayload();
 825        internal readonly SceneBannedUsersUpdatePayload bannedUsersUpdatePayload = new SceneBannedUsersUpdatePayload();
 26
 827        private List<string> admins = new List<string>();
 828        private List<string> bannedUsers = new List<string>();
 29        private string sceneId;
 30
 031        public SectionSceneAdminsSettingsController() : this(
 32            Object.Instantiate(Resources.Load<SectionSceneAdminsSettingsView>(VIEW_PREFAB_PATH)),
 33            FriendsController.i
 034        ) { }
 35
 836        public SectionSceneAdminsSettingsController(SectionSceneAdminsSettingsView view, IFriendsController friendsContr
 37        {
 838            this.view = view;
 839            friendsSearchPromptController = new FriendsSearchPromptController(view.GetAdminsSearchPromptView(), friendsC
 840            usersSearchPromptController = new UsersSearchPromptController(view.GetBlockedSearchPromptView());
 41
 842            view.OnSearchFriendButtonPressed += () => friendsSearchPromptController.Show();
 843            view.OnSearchUserButtonPressed += () => usersSearchPromptController.Show();
 44
 845            friendsSearchPromptController.OnAddUser += OnAddAdminPressed;
 846            friendsSearchPromptController.OnRemoveUser += OnRemoveAdminPressed;
 847            usersSearchPromptController.OnAddUser += OnAddBannedUserPressed;
 848            usersSearchPromptController.OnRemoveUser += OnRemoveBannedUserPressed;
 849        }
 50
 51        public override void Dispose()
 52        {
 853            view.Dispose();
 854            profileFetcher.Dispose();
 855            friendsSearchPromptController.Dispose();
 856            usersSearchPromptController.Dispose();
 857        }
 58
 059        public override void SetViewContainer(Transform viewContainer) { view.SetParent(viewContainer); }
 60
 061        protected override void OnShow() { view.SetActive(true); }
 62
 063        protected override void OnHide() { view.SetActive(false); }
 64
 65        void ISelectSceneListener.OnSelectScene(ISceneCardView sceneCardView)
 66        {
 067            sceneId = sceneCardView.SceneData.id;
 068            SetAdmins(sceneCardView.SceneData.admins);
 069            SetBannedUsers(sceneCardView.SceneData.bannedUsers);
 070        }
 71
 72        internal void SetAdmins(string[] usersId)
 73        {
 374            if (usersId == null || usersId.Length == 0)
 75            {
 076                if (admins.Count > 0)
 077                    admins.Clear();
 78
 079                view.SetAdminsEmptyList(true);
 080                view.SetAdminsCount(0);
 081                return;
 82            }
 83
 384            var newAdmins = new List<string>(usersId);
 1885            for (int i = 0; i < newAdmins.Count; i++)
 86            {
 687                AddAdmin(newAdmins[i]);
 688                admins.Remove(newAdmins[i]);
 89            }
 90
 891            for (int i = 0; i < admins.Count; i++)
 92            {
 193                view.RemoveAdmin(admins[i]);
 94            }
 95
 396            admins = newAdmins;
 97
 398            friendsSearchPromptController.SetUsersInRolList(admins);
 399            view.SetAdminsEmptyList(false);
 3100            view.SetAdminsCount(admins.Count);
 3101        }
 102
 103        internal void SetBannedUsers(string[] usersId)
 104        {
 3105            if (usersId == null || usersId.Length == 0)
 106            {
 0107                if (bannedUsers.Count > 0)
 0108                    bannedUsers.Clear();
 109
 0110                view.SetBannedUsersEmptyList(true);
 0111                view.SetBannedUsersCount(0);
 0112                return;
 113            }
 114
 3115            var newBlocked = new List<string>(usersId);
 22116            for (int i = 0; i < newBlocked.Count; i++)
 117            {
 8118                AddBannedUser(newBlocked[i]);
 8119                bannedUsers.Remove(newBlocked[i]);
 120            }
 121
 6122            for (int i = 0; i < bannedUsers.Count; i++)
 123            {
 0124                view.RemoveBannedUser(bannedUsers[i]);
 125            }
 126
 3127            bannedUsers = newBlocked;
 128
 3129            usersSearchPromptController.SetUsersInRolList(bannedUsers);
 3130            view.SetBannedUsersEmptyList(false);
 3131            view.SetBannedUsersCount(bannedUsers.Count);
 3132        }
 133
 134        internal void AddAdmin(string userId)
 135        {
 7136            if (string.IsNullOrEmpty(userId))
 0137                return;
 138
 7139            var userView = view.AddAdmin(userId);
 7140            profileFetcher.FetchProfile(userId)
 0141                          .Then(userProfile => userView.SetUserProfile(userProfile));
 142
 7143            userView.OnAddPressed -= OnAddAdminPressed;
 7144            userView.OnRemovePressed -= OnRemoveAdminPressed;
 7145            userView.OnAddPressed += OnAddAdminPressed;
 7146            userView.OnRemovePressed += OnRemoveAdminPressed;
 7147        }
 148
 149        internal void AddBannedUser(string userId)
 150        {
 9151            if (string.IsNullOrEmpty(userId))
 0152                return;
 153
 9154            var userView = view.AddBannedUser(userId);
 9155            profileFetcher.FetchProfile(userId)
 0156                          .Then(userProfile => userView.SetUserProfile(userProfile));
 157
 9158            userView.OnAddPressed -= OnAddBannedUserPressed;
 9159            userView.OnRemovePressed -= OnRemoveBannedUserPressed;
 9160            userView.OnAddPressed += OnAddBannedUserPressed;
 9161            userView.OnRemovePressed += OnRemoveBannedUserPressed;
 9162        }
 163
 164        internal void OnAddAdminPressed(string userId)
 165        {
 1166            if (admins.Contains(userId))
 0167                return;
 168
 1169            admins.Add(userId);
 1170            AddAdmin(userId);
 1171            friendsSearchPromptController.SetUsersInRolList(admins);
 1172            view.SetAdminsEmptyList(false);
 1173            view.SetAdminsCount(admins.Count);
 1174            adminsUpdatePayload.admins = admins.ToArray();
 1175            OnRequestUpdateSceneAdmins?.Invoke(sceneId, adminsUpdatePayload);
 0176        }
 177
 178        void OnRemoveAdminPressed(string userId)
 179        {
 1180            if (!admins.Remove(userId))
 0181                return;
 182
 1183            view.RemoveAdmin(userId);
 1184            friendsSearchPromptController.SetUsersInRolList(admins);
 1185            view.SetAdminsEmptyList(admins.Count == 0);
 1186            view.SetAdminsCount(admins.Count);
 1187            adminsUpdatePayload.admins = admins.ToArray();
 1188            OnRequestUpdateSceneAdmins?.Invoke(sceneId, adminsUpdatePayload);
 1189        }
 190
 191        internal void OnAddBannedUserPressed(string userId)
 192        {
 1193            if (bannedUsers.Contains(userId))
 0194                return;
 195
 1196            bannedUsers.Add(userId);
 1197            AddBannedUser(userId);
 1198            usersSearchPromptController.SetUsersInRolList(bannedUsers);
 1199            view.SetBannedUsersEmptyList(false);
 1200            view.SetBannedUsersCount(bannedUsers.Count);
 1201            bannedUsersUpdatePayload.bannedUsers = bannedUsers.ToArray();
 1202            OnRequestUpdateSceneBannedUsers?.Invoke(sceneId, bannedUsersUpdatePayload);
 0203        }
 204
 205        void OnRemoveBannedUserPressed(string userId)
 206        {
 1207            if (!bannedUsers.Remove(userId))
 0208                return;
 209
 1210            view.RemoveBannedUser(userId);
 1211            usersSearchPromptController.SetUsersInRolList(bannedUsers);
 1212            view.SetBannedUsersEmptyList(bannedUsers.Count == 0);
 1213            view.SetBannedUsersCount(bannedUsers.Count);
 1214            bannedUsersUpdatePayload.bannedUsers = bannedUsers.ToArray();
 1215            OnRequestUpdateSceneBannedUsers?.Invoke(sceneId, bannedUsersUpdatePayload);
 1216        }
 217    }
 218}