| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using UnityEngine; |
| | 5 | | using Object = UnityEngine.Object; |
| | 6 | |
|
| | 7 | | namespace 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; |
| 8 | 20 | | private readonly UserProfileFetcher profileFetcher = new UserProfileFetcher(); |
| | 21 | |
|
| 8 | 22 | | internal readonly SceneAdminsUpdatePayload adminsUpdatePayload = new SceneAdminsUpdatePayload(); |
| 8 | 23 | | internal readonly SceneBannedUsersUpdatePayload bannedUsersUpdatePayload = new SceneBannedUsersUpdatePayload(); |
| | 24 | |
|
| 8 | 25 | | private List<string> admins = new List<string>(); |
| 8 | 26 | | private List<string> bannedUsers = new List<string>(); |
| | 27 | | private string sceneId; |
| | 28 | |
|
| 0 | 29 | | public SectionSceneAdminsSettingsController() : this( |
| | 30 | | Object.Instantiate(Resources.Load<SectionSceneAdminsSettingsView>(VIEW_PREFAB_PATH)), |
| | 31 | | FriendsController.i |
| 0 | 32 | | ) { } |
| | 33 | |
|
| 8 | 34 | | public SectionSceneAdminsSettingsController(SectionSceneAdminsSettingsView view, IFriendsController friendsContr |
| | 35 | | { |
| 8 | 36 | | this.view = view; |
| 8 | 37 | | friendsSearchPromptController = new FriendsSearchPromptController(view.GetAdminsSearchPromptView(), friendsC |
| 8 | 38 | | usersSearchPromptController = new UsersSearchPromptController(view.GetBlockedSearchPromptView()); |
| | 39 | |
|
| 8 | 40 | | view.OnSearchFriendButtonPressed += () => friendsSearchPromptController.Show(); |
| 8 | 41 | | view.OnSearchUserButtonPressed += () => usersSearchPromptController.Show(); |
| | 42 | |
|
| 8 | 43 | | friendsSearchPromptController.OnAddUser += OnAddAdminPressed; |
| 8 | 44 | | friendsSearchPromptController.OnRemoveUser += OnRemoveAdminPressed; |
| 8 | 45 | | usersSearchPromptController.OnAddUser += OnAddBannedUserPressed; |
| 8 | 46 | | usersSearchPromptController.OnRemoveUser += OnRemoveBannedUserPressed; |
| 8 | 47 | | } |
| | 48 | |
|
| | 49 | | public override void Dispose() |
| | 50 | | { |
| 8 | 51 | | view.Dispose(); |
| 8 | 52 | | profileFetcher.Dispose(); |
| 8 | 53 | | friendsSearchPromptController.Dispose(); |
| 8 | 54 | | usersSearchPromptController.Dispose(); |
| 8 | 55 | | } |
| | 56 | |
|
| 0 | 57 | | public override void SetViewContainer(Transform viewContainer) { view.SetParent(viewContainer); } |
| | 58 | |
|
| 0 | 59 | | protected override void OnShow() { view.SetActive(true); } |
| | 60 | |
|
| 0 | 61 | | protected override void OnHide() { view.SetActive(false); } |
| | 62 | |
|
| | 63 | | void ISelectSceneListener.OnSelectScene(ISceneCardView sceneCardView) |
| | 64 | | { |
| 0 | 65 | | sceneId = sceneCardView.SceneData.id; |
| 0 | 66 | | SetAdmins(sceneCardView.SceneData.admins); |
| 0 | 67 | | SetBannedUsers(sceneCardView.SceneData.bannedUsers); |
| 0 | 68 | | } |
| | 69 | |
|
| | 70 | | internal void SetAdmins(string[] usersId) |
| | 71 | | { |
| 3 | 72 | | if (usersId == null || usersId.Length == 0) |
| | 73 | | { |
| 0 | 74 | | if (admins.Count > 0) |
| 0 | 75 | | admins.Clear(); |
| | 76 | |
|
| 0 | 77 | | view.SetAdminsEmptyList(true); |
| 0 | 78 | | view.SetAdminsCount(0); |
| 0 | 79 | | return; |
| | 80 | | } |
| | 81 | |
|
| 3 | 82 | | var newAdmins = new List<string>(usersId); |
| 18 | 83 | | for (int i = 0; i < newAdmins.Count; i++) |
| | 84 | | { |
| 6 | 85 | | AddAdmin(newAdmins[i]); |
| 6 | 86 | | admins.Remove(newAdmins[i]); |
| | 87 | | } |
| | 88 | |
|
| 8 | 89 | | for (int i = 0; i < admins.Count; i++) |
| | 90 | | { |
| 1 | 91 | | view.RemoveAdmin(admins[i]); |
| | 92 | | } |
| | 93 | |
|
| 3 | 94 | | admins = newAdmins; |
| | 95 | |
|
| 3 | 96 | | friendsSearchPromptController.SetUsersInRolList(admins); |
| 3 | 97 | | view.SetAdminsEmptyList(false); |
| 3 | 98 | | view.SetAdminsCount(admins.Count); |
| 3 | 99 | | } |
| | 100 | |
|
| | 101 | | internal void SetBannedUsers(string[] usersId) |
| | 102 | | { |
| 3 | 103 | | if (usersId == null || usersId.Length == 0) |
| | 104 | | { |
| 0 | 105 | | if (bannedUsers.Count > 0) |
| 0 | 106 | | bannedUsers.Clear(); |
| | 107 | |
|
| 0 | 108 | | view.SetBannedUsersEmptyList(true); |
| 0 | 109 | | view.SetBannedUsersCount(0); |
| 0 | 110 | | return; |
| | 111 | | } |
| | 112 | |
|
| 3 | 113 | | var newBlocked = new List<string>(usersId); |
| 22 | 114 | | for (int i = 0; i < newBlocked.Count; i++) |
| | 115 | | { |
| 8 | 116 | | AddBannedUser(newBlocked[i]); |
| 8 | 117 | | bannedUsers.Remove(newBlocked[i]); |
| | 118 | | } |
| | 119 | |
|
| 6 | 120 | | for (int i = 0; i < bannedUsers.Count; i++) |
| | 121 | | { |
| 0 | 122 | | view.RemoveBannedUser(bannedUsers[i]); |
| | 123 | | } |
| | 124 | |
|
| 3 | 125 | | bannedUsers = newBlocked; |
| | 126 | |
|
| 3 | 127 | | usersSearchPromptController.SetUsersInRolList(bannedUsers); |
| 3 | 128 | | view.SetBannedUsersEmptyList(false); |
| 3 | 129 | | view.SetBannedUsersCount(bannedUsers.Count); |
| 3 | 130 | | } |
| | 131 | |
|
| | 132 | | internal void AddAdmin(string userId) |
| | 133 | | { |
| 7 | 134 | | if (string.IsNullOrEmpty(userId)) |
| 0 | 135 | | return; |
| | 136 | |
|
| 7 | 137 | | var userView = view.AddAdmin(userId); |
| 7 | 138 | | profileFetcher.FetchProfile(userId) |
| 0 | 139 | | .Then(userProfile => userView.SetUserProfile(userProfile)); |
| | 140 | |
|
| 7 | 141 | | userView.OnAddPressed -= OnAddAdminPressed; |
| 7 | 142 | | userView.OnRemovePressed -= OnRemoveAdminPressed; |
| 7 | 143 | | userView.OnAddPressed += OnAddAdminPressed; |
| 7 | 144 | | userView.OnRemovePressed += OnRemoveAdminPressed; |
| 7 | 145 | | } |
| | 146 | |
|
| | 147 | | internal void AddBannedUser(string userId) |
| | 148 | | { |
| 9 | 149 | | if (string.IsNullOrEmpty(userId)) |
| 0 | 150 | | return; |
| | 151 | |
|
| 9 | 152 | | var userView = view.AddBannedUser(userId); |
| 9 | 153 | | profileFetcher.FetchProfile(userId) |
| 0 | 154 | | .Then(userProfile => userView.SetUserProfile(userProfile)); |
| | 155 | |
|
| 9 | 156 | | userView.OnAddPressed -= OnAddBannedUserPressed; |
| 9 | 157 | | userView.OnRemovePressed -= OnRemoveBannedUserPressed; |
| 9 | 158 | | userView.OnAddPressed += OnAddBannedUserPressed; |
| 9 | 159 | | userView.OnRemovePressed += OnRemoveBannedUserPressed; |
| 9 | 160 | | } |
| | 161 | |
|
| | 162 | | internal void OnAddAdminPressed(string userId) |
| | 163 | | { |
| 1 | 164 | | if (admins.Contains(userId)) |
| 0 | 165 | | return; |
| | 166 | |
|
| 1 | 167 | | admins.Add(userId); |
| 1 | 168 | | AddAdmin(userId); |
| 1 | 169 | | friendsSearchPromptController.SetUsersInRolList(admins); |
| 1 | 170 | | view.SetAdminsEmptyList(false); |
| 1 | 171 | | view.SetAdminsCount(admins.Count); |
| 1 | 172 | | adminsUpdatePayload.admins = admins.ToArray(); |
| 1 | 173 | | OnRequestUpdateSceneAdmins?.Invoke(sceneId, adminsUpdatePayload); |
| 0 | 174 | | } |
| | 175 | |
|
| | 176 | | void OnRemoveAdminPressed(string userId) |
| | 177 | | { |
| 1 | 178 | | if (!admins.Remove(userId)) |
| 0 | 179 | | return; |
| | 180 | |
|
| 1 | 181 | | view.RemoveAdmin(userId); |
| 1 | 182 | | friendsSearchPromptController.SetUsersInRolList(admins); |
| 1 | 183 | | view.SetAdminsEmptyList(admins.Count == 0); |
| 1 | 184 | | view.SetAdminsCount(admins.Count); |
| 1 | 185 | | adminsUpdatePayload.admins = admins.ToArray(); |
| 1 | 186 | | OnRequestUpdateSceneAdmins?.Invoke(sceneId, adminsUpdatePayload); |
| 1 | 187 | | } |
| | 188 | |
|
| | 189 | | internal void OnAddBannedUserPressed(string userId) |
| | 190 | | { |
| 1 | 191 | | if (bannedUsers.Contains(userId)) |
| 0 | 192 | | return; |
| | 193 | |
|
| 1 | 194 | | bannedUsers.Add(userId); |
| 1 | 195 | | AddBannedUser(userId); |
| 1 | 196 | | usersSearchPromptController.SetUsersInRolList(bannedUsers); |
| 1 | 197 | | view.SetBannedUsersEmptyList(false); |
| 1 | 198 | | view.SetBannedUsersCount(bannedUsers.Count); |
| 1 | 199 | | bannedUsersUpdatePayload.bannedUsers = bannedUsers.ToArray(); |
| 1 | 200 | | OnRequestUpdateSceneBannedUsers?.Invoke(sceneId, bannedUsersUpdatePayload); |
| 0 | 201 | | } |
| | 202 | |
|
| | 203 | | void OnRemoveBannedUserPressed(string userId) |
| | 204 | | { |
| 1 | 205 | | if (!bannedUsers.Remove(userId)) |
| 0 | 206 | | return; |
| | 207 | |
|
| 1 | 208 | | view.RemoveBannedUser(userId); |
| 1 | 209 | | usersSearchPromptController.SetUsersInRolList(bannedUsers); |
| 1 | 210 | | view.SetBannedUsersEmptyList(bannedUsers.Count == 0); |
| 1 | 211 | | view.SetBannedUsersCount(bannedUsers.Count); |
| 1 | 212 | | bannedUsersUpdatePayload.bannedUsers = bannedUsers.ToArray(); |
| 1 | 213 | | OnRequestUpdateSceneBannedUsers?.Invoke(sceneId, bannedUsersUpdatePayload); |
| 1 | 214 | | } |
| | 215 | | } |
| | 216 | | } |