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