| | 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 | |
|
| 1 | 24 | | public override SearchBarConfig searchBarConfig { get; protected set; } = new SearchBarConfig() |
| | 25 | | { |
| | 26 | | showFilterContributor = false, |
| | 27 | | showFilterOperator = false, |
| | 28 | | showFilterOwner = false, |
| | 29 | | showResultLabel = true |
| | 30 | | }; |
| | 31 | |
|
| 0 | 32 | | public override ISectionSearchHandler searchHandler => sceneSearchHandler; |
| | 33 | |
|
| | 34 | | private readonly SectionProjectView view; |
| | 35 | |
|
| 1 | 36 | | private readonly ISectionSearchHandler sceneSearchHandler = new SectionSearchHandler(); |
| 1 | 37 | | internal Dictionary<string, IProjectCardView> projectsViews = new Dictionary<string, IProjectCardView>(); |
| | 38 | |
|
| 1 | 39 | | public SectionProjectController() : this( |
| | 40 | | Object.Instantiate(Resources.Load<SectionProjectView>(VIEW_PREFAB_PATH)) |
| 1 | 41 | | ) { } |
| | 42 | |
|
| 1 | 43 | | public SectionProjectController(SectionProjectView view) |
| | 44 | | { |
| 1 | 45 | | this.view = view; |
| | 46 | |
|
| 1 | 47 | | view.OnScrollRectValueChanged += OnRequestContextMenuHide; |
| 1 | 48 | | view.OnCreateProjectRequest += CreateProjectRequest; |
| 1 | 49 | | sceneSearchHandler.OnResult += OnSearchResult; |
| 1 | 50 | | } |
| | 51 | |
|
| 0 | 52 | | public override void SetViewContainer(Transform viewContainer) { view.SetParent(viewContainer); } |
| | 53 | |
|
| | 54 | | public override void Dispose() |
| | 55 | | { |
| 1 | 56 | | view.OnScrollRectValueChanged -= OnRequestContextMenuHide; |
| 1 | 57 | | view.OnCreateProjectRequest -= CreateProjectRequest; |
| 1 | 58 | | sceneSearchHandler.OnResult -= OnSearchResult; |
| 1 | 59 | | view.Dispose(); |
| 1 | 60 | | } |
| | 61 | |
|
| | 62 | | protected override void OnShow() |
| | 63 | | { |
| 0 | 64 | | view.SetActive(true); |
| 0 | 65 | | if (projectsViews.Count == 0) |
| | 66 | | { |
| 0 | 67 | | if (isLoading) |
| | 68 | | { |
| 0 | 69 | | view.SetLoading(); |
| 0 | 70 | | OnNotEmptyContent?.Invoke(); |
| 0 | 71 | | } |
| | 72 | | else |
| | 73 | | { |
| 0 | 74 | | view.SetEmpty(); |
| 0 | 75 | | OnEmptyContent?.Invoke(); |
| | 76 | | } |
| 0 | 77 | | } |
| | 78 | | else |
| | 79 | | { |
| 0 | 80 | | OnNotEmptyContent?.Invoke(); |
| | 81 | | } |
| 0 | 82 | | } |
| | 83 | |
|
| 0 | 84 | | protected override void OnHide() { view.SetActive(false); } |
| | 85 | |
|
| | 86 | | private void CreateProjectRequest() |
| | 87 | | { |
| 0 | 88 | | OnCreateProjectRequest?.Invoke(); |
| 0 | 89 | | } |
| | 90 | |
|
| | 91 | | void IProjectsListener.OnSetProjects(Dictionary<string, IProjectCardView> projectsViews) |
| | 92 | | { |
| 1 | 93 | | this.projectsViews = new Dictionary<string, IProjectCardView>(projectsViews); |
| 2 | 94 | | sceneSearchHandler.SetSearchableList(projectsViews.Values.Select(project => project.searchInfo).ToList()); |
| 1 | 95 | | } |
| | 96 | |
|
| | 97 | | void IProjectsListener.OnProjectAdded(IProjectCardView projectView) |
| | 98 | | { |
| 0 | 99 | | projectsViews.Add(projectView.projectData.id, projectView); |
| 0 | 100 | | sceneSearchHandler.AddItem(projectView.searchInfo); |
| 0 | 101 | | } |
| | 102 | |
|
| | 103 | | void IProjectsListener.OnProjectRemoved(IProjectCardView projectView) |
| | 104 | | { |
| 0 | 105 | | projectsViews.Remove(projectView.projectData.id); |
| 0 | 106 | | projectView.SetActive(false); |
| 0 | 107 | | } |
| | 108 | |
|
| | 109 | | protected override void OnFetchingStateChange(bool isLoading) |
| | 110 | | { |
| 0 | 111 | | base.OnFetchingStateChange(isLoading); |
| 0 | 112 | | if (isLoading) |
| | 113 | | { |
| 0 | 114 | | view.SetLoading(); |
| | 115 | | } |
| 0 | 116 | | } |
| | 117 | |
|
| | 118 | | internal void OnSearchResult(List<ISearchInfo> searchInfoScenes) |
| | 119 | | { |
| 2 | 120 | | if (projectsViews == null) |
| 0 | 121 | | return; |
| | 122 | |
|
| 2 | 123 | | using (var iterator = projectsViews.GetEnumerator()) |
| | 124 | | { |
| 4 | 125 | | while (iterator.MoveNext()) |
| | 126 | | { |
| 2 | 127 | | iterator.Current.Value.SetParent(view.contentContainer.transform); |
| 2 | 128 | | iterator.Current.Value.SetActive(false); |
| | 129 | | } |
| 2 | 130 | | } |
| | 131 | |
|
| 8 | 132 | | for (int i = 0; i < searchInfoScenes.Count; i++) |
| | 133 | | { |
| 2 | 134 | | if (!projectsViews.TryGetValue(searchInfoScenes[i].id, out IProjectCardView cardView)) |
| | 135 | | continue; |
| | 136 | |
|
| 1 | 137 | | cardView.SetActive(true); |
| 1 | 138 | | cardView.SetSiblingIndex(i); |
| | 139 | | } |
| 2 | 140 | | view.ResetScrollRect(); |
| | 141 | |
|
| 2 | 142 | | if (projectsViews.Count == 0) |
| | 143 | | { |
| 0 | 144 | | if (isLoading) |
| | 145 | | { |
| 0 | 146 | | view.SetLoading(); |
| 0 | 147 | | OnNotEmptyContent?.Invoke(); |
| 0 | 148 | | } |
| | 149 | | else |
| | 150 | | { |
| 0 | 151 | | view.SetEmpty(); |
| 0 | 152 | | OnEmptyContent?.Invoke(); |
| | 153 | | } |
| 0 | 154 | | } |
| 2 | 155 | | else if (searchInfoScenes.Count == 0) |
| | 156 | | { |
| 0 | 157 | | view.SetNoSearchResult(); |
| | 158 | |
|
| 0 | 159 | | OnNotEmptyContent?.Invoke(); |
| 0 | 160 | | } |
| | 161 | | else |
| | 162 | | { |
| 2 | 163 | | view.SetFilled(); |
| 2 | 164 | | OnNotEmptyContent?.Invoke(); |
| | 165 | | } |
| 0 | 166 | | } |
| | 167 | | } |
| | 168 | | } |