| | 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 SectionSceneContributorsSettingsController : SectionBase, ISelectSceneListener, ISectionUpdateSceneCo |
| | 10 | | { |
| | 11 | | public const string VIEW_PREFAB_PATH = "BuilderProjectsPanelMenuSections/SectionSceneContributorsSettingsView"; |
| | 12 | |
|
| | 13 | | public event Action<string, SceneContributorsUpdatePayload> OnRequestUpdateSceneContributors; |
| | 14 | |
|
| | 15 | | private readonly SectionSceneContributorsSettingsView view; |
| | 16 | | private readonly FriendsSearchPromptController friendsSearchPromptController; |
| 4 | 17 | | private readonly UserProfileFetcher profileFetcher = new UserProfileFetcher(); |
| 4 | 18 | | private readonly SceneContributorsUpdatePayload contributorsUpdatePayload = new SceneContributorsUpdatePayload() |
| | 19 | |
|
| 4 | 20 | | private List<string> contributorsList = new List<string>(); |
| | 21 | | private string sceneId; |
| | 22 | |
|
| 0 | 23 | | public SectionSceneContributorsSettingsController() : this( |
| | 24 | | Object.Instantiate(Resources.Load<SectionSceneContributorsSettingsView>(VIEW_PREFAB_PATH)), |
| | 25 | | FriendsController.i |
| 0 | 26 | | ) { } |
| | 27 | |
|
| 4 | 28 | | public SectionSceneContributorsSettingsController(SectionSceneContributorsSettingsView view, IFriendsController |
| | 29 | | { |
| 4 | 30 | | this.view = view; |
| 4 | 31 | | friendsSearchPromptController = new FriendsSearchPromptController(view.GetSearchPromptView(), friendsControl |
| | 32 | |
|
| 4 | 33 | | view.OnSearchUserButtonPressed += () => friendsSearchPromptController.Show(); |
| 4 | 34 | | friendsSearchPromptController.OnAddUser += OnAddUserPressed; |
| 4 | 35 | | friendsSearchPromptController.OnRemoveUser += OnRemoveUserPressed; |
| 4 | 36 | | } |
| | 37 | |
|
| | 38 | | public override void Dispose() |
| | 39 | | { |
| 4 | 40 | | view.Dispose(); |
| 4 | 41 | | profileFetcher.Dispose(); |
| 4 | 42 | | friendsSearchPromptController.Dispose(); |
| 4 | 43 | | } |
| | 44 | |
|
| 0 | 45 | | public override void SetViewContainer(Transform viewContainer) { view.SetParent(viewContainer); } |
| | 46 | |
|
| 0 | 47 | | protected override void OnShow() { view.SetActive(true); } |
| | 48 | |
|
| 0 | 49 | | protected override void OnHide() { view.SetActive(false); } |
| | 50 | |
|
| | 51 | | void ISelectSceneListener.OnSelectScene(ISceneCardView sceneCardView) |
| | 52 | | { |
| 0 | 53 | | sceneId = sceneCardView.SceneData.id; |
| 0 | 54 | | UpdateContributors(sceneCardView.SceneData.contributors); |
| 0 | 55 | | } |
| | 56 | |
|
| | 57 | | internal void UpdateContributors(string[] contributors) |
| | 58 | | { |
| 3 | 59 | | if (contributors == null || contributors.Length == 0) |
| | 60 | | { |
| 0 | 61 | | if (contributorsList.Count > 0) |
| 0 | 62 | | contributorsList.Clear(); |
| | 63 | |
|
| 0 | 64 | | view.SetEmptyList(true); |
| 0 | 65 | | view.SetContributorsCount(0); |
| 0 | 66 | | return; |
| | 67 | | } |
| | 68 | |
|
| 3 | 69 | | var newContributors = new List<string>(contributors); |
| 18 | 70 | | for (int i = 0; i < newContributors.Count; i++) |
| | 71 | | { |
| 6 | 72 | | AddContributor(newContributors[i]); |
| 6 | 73 | | contributorsList.Remove(newContributors[i]); |
| | 74 | | } |
| | 75 | |
|
| 8 | 76 | | for (int i = 0; i < contributorsList.Count; i++) |
| | 77 | | { |
| 1 | 78 | | view.RemoveUser(contributorsList[i]); |
| | 79 | | } |
| | 80 | |
|
| 3 | 81 | | contributorsList = newContributors; |
| | 82 | |
|
| 3 | 83 | | friendsSearchPromptController.SetUsersInRolList(contributorsList); |
| 3 | 84 | | view.SetEmptyList(false); |
| 3 | 85 | | view.SetContributorsCount(contributorsList.Count); |
| 3 | 86 | | } |
| | 87 | |
|
| | 88 | | void AddContributor(string userId) |
| | 89 | | { |
| 6 | 90 | | if (string.IsNullOrEmpty(userId)) |
| 0 | 91 | | return; |
| | 92 | |
|
| 6 | 93 | | var userView = view.AddUser(userId); |
| 6 | 94 | | profileFetcher.FetchProfile(userId) |
| 0 | 95 | | .Then(userProfile => userView.SetUserProfile(userProfile)); |
| | 96 | |
|
| 6 | 97 | | userView.OnAddPressed -= OnAddUserPressed; |
| 6 | 98 | | userView.OnRemovePressed -= OnRemoveUserPressed; |
| 6 | 99 | | userView.OnAddPressed += OnAddUserPressed; |
| 6 | 100 | | userView.OnRemovePressed += OnRemoveUserPressed; |
| 6 | 101 | | } |
| | 102 | |
|
| | 103 | | void OnAddUserPressed(string userId) |
| | 104 | | { |
| 0 | 105 | | if (contributorsList.Contains(userId)) |
| 0 | 106 | | return; |
| | 107 | |
|
| 0 | 108 | | contributorsList.Add(userId); |
| 0 | 109 | | AddContributor(userId); |
| 0 | 110 | | friendsSearchPromptController.SetUsersInRolList(contributorsList); |
| 0 | 111 | | view.SetEmptyList(false); |
| 0 | 112 | | view.SetContributorsCount(contributorsList.Count); |
| 0 | 113 | | contributorsUpdatePayload.contributors = contributorsList.ToArray(); |
| 0 | 114 | | OnRequestUpdateSceneContributors?.Invoke(sceneId, contributorsUpdatePayload); |
| 0 | 115 | | } |
| | 116 | |
|
| | 117 | | void OnRemoveUserPressed(string userId) |
| | 118 | | { |
| 1 | 119 | | if (!contributorsList.Remove(userId)) |
| 0 | 120 | | return; |
| | 121 | |
|
| 1 | 122 | | view.RemoveUser(userId); |
| 1 | 123 | | friendsSearchPromptController.SetUsersInRolList(contributorsList); |
| 1 | 124 | | view.SetEmptyList(contributorsList.Count == 0); |
| 1 | 125 | | view.SetContributorsCount(contributorsList.Count); |
| 1 | 126 | | contributorsUpdatePayload.contributors = contributorsList.ToArray(); |
| 1 | 127 | | OnRequestUpdateSceneContributors?.Invoke(sceneId, contributorsUpdatePayload); |
| 1 | 128 | | } |
| | 129 | | } |
| | 130 | | } |