| | 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 | | public interface ISectionProjectController |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// This event will be fired when the view request a new project creation |
| | 13 | | /// </summary> |
| | 14 | | event Action OnCreateProjectRequest; |
| | 15 | | } |
| | 16 | |
|
| | 17 | | internal class SectionProjectController : SectionBase, IProjectsListener, ISectionHideContextMenuRequester,ISectionP |
| | 18 | | { |
| | 19 | | public const string VIEW_PREFAB_PATH = "BuilderProjectsPanelMenuSections/SectionProjectView"; |
| | 20 | |
|
| | 21 | | public event Action OnRequestContextMenuHide; |
| | 22 | | public event Action OnCreateProjectRequest; |
| | 23 | |
|
| 0 | 24 | | public override ISectionSearchHandler searchHandler => sceneSearchHandler; |
| | 25 | |
|
| | 26 | | private readonly SectionProjectView view; |
| | 27 | |
|
| 1 | 28 | | private readonly ISectionSearchHandler sceneSearchHandler = new SectionSearchHandler(); |
| 1 | 29 | | internal Dictionary<string, IProjectCardView> projectsViews = new Dictionary<string, IProjectCardView>(); |
| | 30 | |
|
| 1 | 31 | | public SectionProjectController() : this( |
| | 32 | | Object.Instantiate(Resources.Load<SectionProjectView>(VIEW_PREFAB_PATH)) |
| 1 | 33 | | ) { } |
| | 34 | |
|
| 1 | 35 | | public SectionProjectController(SectionProjectView view) |
| | 36 | | { |
| 1 | 37 | | this.view = view; |
| | 38 | |
|
| 1 | 39 | | view.OnScrollRectValueChanged += OnRequestContextMenuHide; |
| 1 | 40 | | view.OnCreateProjectRequest += CreateProjectRequest; |
| 1 | 41 | | sceneSearchHandler.OnResult += OnSearchResult; |
| 1 | 42 | | } |
| | 43 | |
|
| 0 | 44 | | public override void SetViewContainer(Transform viewContainer) { view.SetParent(viewContainer); } |
| | 45 | |
|
| | 46 | | public override void Dispose() |
| | 47 | | { |
| 1 | 48 | | view.OnScrollRectValueChanged -= OnRequestContextMenuHide; |
| 1 | 49 | | view.OnCreateProjectRequest -= CreateProjectRequest; |
| 1 | 50 | | sceneSearchHandler.OnResult -= OnSearchResult; |
| 1 | 51 | | view.Dispose(); |
| 1 | 52 | | } |
| | 53 | |
|
| 0 | 54 | | protected override void OnShow() { view.SetActive(true); } |
| | 55 | |
|
| 0 | 56 | | protected override void OnHide() { view.SetActive(false); } |
| | 57 | |
|
| | 58 | | private void CreateProjectRequest() |
| | 59 | | { |
| 0 | 60 | | OnCreateProjectRequest?.Invoke(); |
| 0 | 61 | | } |
| | 62 | |
|
| | 63 | | void IProjectsListener.OnSetProjects(Dictionary<string, IProjectCardView> projectsViews) |
| | 64 | | { |
| 1 | 65 | | this.projectsViews = new Dictionary<string, IProjectCardView>(projectsViews); |
| 2 | 66 | | sceneSearchHandler.SetSearchableList(projectsViews.Values.Select(project => project.searchInfo).ToList()); |
| 1 | 67 | | } |
| | 68 | |
|
| | 69 | | void IProjectsListener.OnProjectAdded(IProjectCardView projectView) |
| | 70 | | { |
| 0 | 71 | | projectsViews.Add(projectView.projectData.id, projectView); |
| 0 | 72 | | sceneSearchHandler.AddItem(projectView.searchInfo); |
| 0 | 73 | | } |
| | 74 | |
|
| | 75 | | void IProjectsListener.OnProjectRemoved(IProjectCardView projectView) |
| | 76 | | { |
| 0 | 77 | | projectsViews.Remove(projectView.projectData.id); |
| 0 | 78 | | projectView.SetActive(false); |
| 0 | 79 | | } |
| | 80 | |
|
| | 81 | | internal void OnSearchResult(List<ISearchInfo> searchInfoScenes) |
| | 82 | | { |
| 2 | 83 | | if (projectsViews == null) |
| 0 | 84 | | return; |
| | 85 | |
|
| 2 | 86 | | using (var iterator = projectsViews.GetEnumerator()) |
| | 87 | | { |
| 4 | 88 | | while (iterator.MoveNext()) |
| | 89 | | { |
| 2 | 90 | | iterator.Current.Value.SetParent(view.contentContainer.transform); |
| 2 | 91 | | iterator.Current.Value.SetActive(false); |
| | 92 | | } |
| 2 | 93 | | } |
| | 94 | |
|
| 8 | 95 | | for (int i = 0; i < searchInfoScenes.Count; i++) |
| | 96 | | { |
| 2 | 97 | | if (!projectsViews.TryGetValue(searchInfoScenes[i].id, out IProjectCardView cardView)) |
| | 98 | | continue; |
| | 99 | |
|
| 1 | 100 | | cardView.SetActive(true); |
| 1 | 101 | | cardView.SetSiblingIndex(i); |
| | 102 | | } |
| 2 | 103 | | view.ResetScrollRect(); |
| | 104 | |
|
| 2 | 105 | | if (projectsViews.Count == 0) |
| | 106 | | { |
| 0 | 107 | | if (isLoading) |
| | 108 | | { |
| 0 | 109 | | view.SetLoading(); |
| 0 | 110 | | } |
| | 111 | | else |
| | 112 | | { |
| 0 | 113 | | view.SetEmpty(); |
| | 114 | | } |
| 0 | 115 | | } |
| 2 | 116 | | else if (searchInfoScenes.Count == 0) |
| | 117 | | { |
| 0 | 118 | | view.SetNoSearchResult(); |
| 0 | 119 | | } |
| | 120 | | else |
| | 121 | | { |
| 2 | 122 | | view.SetFilled(); |
| | 123 | | } |
| 2 | 124 | | } |
| | 125 | | } |
| | 126 | | } |