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