| | 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 SectionScenesController : SectionBase, ISceneListener, ISectionHideContextMenuRequester |
| | 10 | | { |
| | 11 | | public const string VIEW_PREFAB_PATH = "BuilderProjectsPanelMenuSections/SectionDeployedScenesView"; |
| | 12 | |
|
| | 13 | | public event Action OnRequestContextMenuHide; |
| | 14 | |
|
| 1 | 15 | | public override ISectionSearchHandler searchHandler => sceneSearchHandler; |
| | 16 | |
|
| | 17 | | private readonly SectionPlacesView view; |
| | 18 | |
|
| 4 | 19 | | private readonly ISectionSearchHandler sceneSearchHandler = new SectionSearchHandler(); |
| 4 | 20 | | private Dictionary<string, ISceneCardView> scenesViews = new Dictionary<string, ISceneCardView>(); |
| | 21 | |
|
| 2 | 22 | | public SectionScenesController() : this( |
| | 23 | | Object.Instantiate(Resources.Load<SectionPlacesView>(VIEW_PREFAB_PATH)) |
| 2 | 24 | | ) { } |
| | 25 | |
|
| 4 | 26 | | public SectionScenesController(SectionPlacesView view) |
| | 27 | | { |
| 4 | 28 | | this.view = view; |
| | 29 | |
|
| 4 | 30 | | view.OnScrollRectValueChanged += OnRequestContextMenuHide; |
| 4 | 31 | | sceneSearchHandler.OnResult += OnSearchResult; |
| 4 | 32 | | } |
| | 33 | |
|
| 0 | 34 | | public override void SetViewContainer(Transform viewContainer) { view.SetParent(viewContainer); } |
| | 35 | |
|
| | 36 | | public override void Dispose() |
| | 37 | | { |
| 2 | 38 | | view.OnScrollRectValueChanged -= OnRequestContextMenuHide; |
| 2 | 39 | | view.Dispose(); |
| 2 | 40 | | } |
| | 41 | |
|
| | 42 | | protected override void OnShow() |
| | 43 | | { |
| 0 | 44 | | view.SetActive(true); |
| 0 | 45 | | if (scenesViews != null && scenesViews.Count == 0) |
| | 46 | | { |
| 0 | 47 | | if (isLoading) |
| | 48 | | { |
| 0 | 49 | | view.SetLoading(); |
| 0 | 50 | | OnNotEmptyContent?.Invoke(); |
| 0 | 51 | | } |
| | 52 | | else |
| | 53 | | { |
| 0 | 54 | | view.SetEmpty(); |
| 0 | 55 | | OnEmptyContent?.Invoke(); |
| | 56 | | } |
| 0 | 57 | | } |
| | 58 | | else |
| | 59 | | { |
| 0 | 60 | | OnNotEmptyContent?.Invoke(); |
| | 61 | | } |
| 0 | 62 | | } |
| | 63 | |
|
| 0 | 64 | | protected override void OnHide() { view.SetActive(false); } |
| | 65 | |
|
| | 66 | | void ISceneListener.SetScenes(Dictionary<string, ISceneCardView> scenes) |
| | 67 | | { |
| 5 | 68 | | scenesViews = new Dictionary<string, ISceneCardView>(scenes); |
| 18 | 69 | | sceneSearchHandler.SetSearchableList(scenes.Values.Select(scene => scene.searchInfo).ToList()); |
| 5 | 70 | | } |
| | 71 | |
|
| | 72 | | void ISceneListener.SceneAdded(ISceneCardView scene) |
| | 73 | | { |
| 0 | 74 | | scenesViews.Add(scene.SceneData.id, scene); |
| 0 | 75 | | sceneSearchHandler.AddItem(scene.searchInfo); |
| 0 | 76 | | } |
| | 77 | |
|
| | 78 | | void ISceneListener.SceneRemoved(ISceneCardView scene) |
| | 79 | | { |
| 0 | 80 | | scenesViews.Remove(scene.SceneData.id); |
| 0 | 81 | | scene.SetActive(false); |
| 0 | 82 | | } |
| | 83 | |
|
| | 84 | | private void OnSearchResult(List<ISearchInfo> searchInfoScenes) |
| | 85 | | { |
| 6 | 86 | | if (scenesViews == null || searchInfoScenes == null) |
| 1 | 87 | | return; |
| | 88 | |
|
| 5 | 89 | | using (var iterator = scenesViews.GetEnumerator()) |
| | 90 | | { |
| 18 | 91 | | while (iterator.MoveNext()) |
| | 92 | | { |
| 13 | 93 | | iterator.Current.Value.SetParent(view.GetCardsContainer()); |
| 13 | 94 | | iterator.Current.Value.SetActive(false); |
| | 95 | | } |
| 5 | 96 | | } |
| | 97 | |
|
| 36 | 98 | | for (int i = 0; i < searchInfoScenes.Count; i++) |
| | 99 | | { |
| 13 | 100 | | if (!scenesViews.TryGetValue(searchInfoScenes[i].id, out ISceneCardView cardView)) |
| | 101 | | continue; |
| | 102 | |
|
| 10 | 103 | | cardView.SetActive(true); |
| 10 | 104 | | cardView.SetSiblingIndex(i); |
| | 105 | | } |
| | 106 | |
|
| 5 | 107 | | if (scenesViews.Count == 0) |
| | 108 | | { |
| 2 | 109 | | if (isLoading) |
| | 110 | | { |
| 1 | 111 | | view.SetLoading(); |
| 1 | 112 | | OnNotEmptyContent?.Invoke(); |
| 0 | 113 | | } |
| | 114 | | else |
| | 115 | | { |
| 1 | 116 | | view.SetEmpty(); |
| 1 | 117 | | OnEmptyContent?.Invoke(); |
| | 118 | | } |
| 0 | 119 | | } |
| 3 | 120 | | else if (searchInfoScenes.Count == 0) |
| | 121 | | { |
| 0 | 122 | | view.SetNoSearchResult(); |
| 0 | 123 | | OnNotEmptyContent?.Invoke(); |
| 0 | 124 | | } |
| | 125 | | else |
| | 126 | | { |
| 3 | 127 | | view.SetFilled(); |
| 3 | 128 | | OnNotEmptyContent?.Invoke(); |
| | 129 | | } |
| 0 | 130 | | } |
| | 131 | | } |
| | 132 | | } |