| | 1 | | using DCL; |
| | 2 | | using System; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using TMPro; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.UI; |
| | 7 | |
|
| | 8 | | public interface IPlacesSubSectionComponentView |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// It will be triggered when all the UI components have been fully initialized. |
| | 12 | | /// </summary> |
| | 13 | | event Action OnReady; |
| | 14 | |
|
| | 15 | | /// <summary> |
| | 16 | | /// It will be triggered when the info button is clicked. |
| | 17 | | /// </summary> |
| | 18 | | event Action<PlaceCardComponentModel> OnInfoClicked; |
| | 19 | |
|
| | 20 | | /// <summary> |
| | 21 | | /// It will be triggered when the JumpIn button is clicked. |
| | 22 | | /// </summary> |
| | 23 | | event Action<HotScenesController.HotSceneInfo> OnJumpInClicked; |
| | 24 | |
|
| | 25 | | /// <summary> |
| | 26 | | /// It will be triggered when a new friend handler is added by a place card. |
| | 27 | | /// </summary> |
| | 28 | | event Action<FriendsHandler> OnFriendHandlerAdded; |
| | 29 | |
|
| | 30 | | /// <summary> |
| | 31 | | /// It will be triggered each time the view is enabled. |
| | 32 | | /// </summary> |
| | 33 | | event Action OnPlacesSubSectionEnable; |
| | 34 | |
|
| | 35 | | /// <summary> |
| | 36 | | /// It will be triggered when the "Show More" button is clicked. |
| | 37 | | /// </summary> |
| | 38 | | event Action OnShowMorePlacesClicked; |
| | 39 | |
|
| | 40 | | /// <summary> |
| | 41 | | /// Colors used for the background of the friends heads. |
| | 42 | | /// </summary> |
| | 43 | | Color[] currentFriendColors { get; } |
| | 44 | |
|
| | 45 | | /// <summary> |
| | 46 | | /// Number of places per row that fit with the current places grid configuration. |
| | 47 | | /// </summary> |
| | 48 | | int currentPlacesPerRow { get; } |
| | 49 | |
|
| | 50 | | /// <summary> |
| | 51 | | /// Set the places component with a list of places. |
| | 52 | | /// </summary> |
| | 53 | | /// <param name="places">List of places (model) to be loaded.</param> |
| | 54 | | void SetPlaces(List<PlaceCardComponentModel> places); |
| | 55 | |
|
| | 56 | | /// <summary> |
| | 57 | | /// Add a list of places in the places component. |
| | 58 | | /// </summary> |
| | 59 | | /// <param name="places">List of places (model) to be added.</param> |
| | 60 | | void AddPlaces(List<PlaceCardComponentModel> places); |
| | 61 | |
|
| | 62 | | /// <summary> |
| | 63 | | /// Set the places component in loading mode. |
| | 64 | | /// </summary> |
| | 65 | | /// <param name="isVisible">True for activating the loading mode.</param> |
| | 66 | | void SetPlacesAsLoading(bool isVisible); |
| | 67 | |
|
| | 68 | | /// <summary> |
| | 69 | | /// Activates/Deactivates the "Show More" button. |
| | 70 | | /// </summary> |
| | 71 | | /// <param name="isActive">True for activating it.</param> |
| | 72 | | void SetShowMorePlacesButtonActive(bool isActive); |
| | 73 | |
|
| | 74 | | /// <summary> |
| | 75 | | /// Shows the Place Card modal with the provided information. |
| | 76 | | /// </summary> |
| | 77 | | /// <param name="placeInfo">Place (model) to be loaded in the card.</param> |
| | 78 | | void ShowPlaceModal(PlaceCardComponentModel placeInfo); |
| | 79 | |
|
| | 80 | | /// <summary> |
| | 81 | | /// Hides the Place Card modal. |
| | 82 | | /// </summary> |
| | 83 | | void HidePlaceModal(); |
| | 84 | |
|
| | 85 | | /// <summary> |
| | 86 | | /// Set the current scroll view position to 1. |
| | 87 | | /// </summary> |
| | 88 | | void RestartScrollViewPosition(); |
| | 89 | |
|
| | 90 | | /// <summary> |
| | 91 | | /// Configure the needed pools for the places instantiation. |
| | 92 | | /// </summary> |
| | 93 | | void ConfigurePools(); |
| | 94 | | } |
| | 95 | |
|
| | 96 | | public class PlacesSubSectionComponentView : BaseComponentView, IPlacesSubSectionComponentView |
| | 97 | | { |
| | 98 | | internal const string PLACE_CARDS_POOL_NAME = "Places_PlaceCardsPool"; |
| | 99 | | internal const int PLACE_CARDS_POOL_PREWARM = 20; |
| | 100 | |
|
| | 101 | | [Header("Assets References")] |
| | 102 | | [SerializeField] internal PlaceCardComponentView placeCardPrefab; |
| | 103 | | [SerializeField] internal PlaceCardComponentView placeCardModalPrefab; |
| | 104 | |
|
| | 105 | | [Header("Prefab References")] |
| | 106 | | [SerializeField] internal ScrollRect scrollView; |
| | 107 | | [SerializeField] internal GridContainerComponentView places; |
| | 108 | | [SerializeField] internal GameObject placesLoading; |
| | 109 | | [SerializeField] internal TMP_Text placesNoDataText; |
| | 110 | | [SerializeField] internal Color[] friendColors = null; |
| | 111 | | [SerializeField] internal GameObject showMorePlacesButtonContainer; |
| | 112 | | [SerializeField] internal ButtonComponentView showMorePlacesButton; |
| | 113 | |
|
| | 114 | | public event Action OnReady; |
| | 115 | | public event Action<PlaceCardComponentModel> OnInfoClicked; |
| | 116 | | public event Action<HotScenesController.HotSceneInfo> OnJumpInClicked; |
| | 117 | | public event Action<FriendsHandler> OnFriendHandlerAdded; |
| | 118 | | public event Action OnPlacesSubSectionEnable; |
| | 119 | | public event Action OnShowMorePlacesClicked; |
| | 120 | |
|
| | 121 | | internal PlaceCardComponentView placeModal; |
| | 122 | | internal Pool placeCardsPool; |
| | 123 | |
|
| 0 | 124 | | public Color[] currentFriendColors => friendColors; |
| | 125 | |
|
| 0 | 126 | | public int currentPlacesPerRow => places.currentItemsPerRow; |
| | 127 | |
|
| 13 | 128 | | public override void OnEnable() { OnPlacesSubSectionEnable?.Invoke(); } |
| | 129 | |
|
| | 130 | | public void ConfigurePools() |
| | 131 | | { |
| 11 | 132 | | ExplorePlacesUtils.ConfigurePlaceCardsPool(out placeCardsPool, PLACE_CARDS_POOL_NAME, placeCardPrefab, PLACE_CAR |
| 11 | 133 | | } |
| | 134 | |
|
| | 135 | | public override void Start() |
| | 136 | | { |
| 11 | 137 | | placeModal = ExplorePlacesUtils.ConfigurePlaceCardModal(placeCardModalPrefab); |
| | 138 | |
|
| 11 | 139 | | places.RemoveItems(); |
| | 140 | |
|
| 11 | 141 | | showMorePlacesButton.onClick.RemoveAllListeners(); |
| 11 | 142 | | showMorePlacesButton.onClick.AddListener(() => OnShowMorePlacesClicked?.Invoke()); |
| | 143 | |
|
| 11 | 144 | | OnReady?.Invoke(); |
| 0 | 145 | | } |
| | 146 | |
|
| 0 | 147 | | public override void RefreshControl() { places.RefreshControl(); } |
| | 148 | |
|
| | 149 | | public override void Dispose() |
| | 150 | | { |
| 47 | 151 | | base.Dispose(); |
| | 152 | |
|
| 47 | 153 | | showMorePlacesButton.onClick.RemoveAllListeners(); |
| | 154 | |
|
| 47 | 155 | | places.Dispose(); |
| | 156 | |
|
| 47 | 157 | | if (placeModal != null) |
| | 158 | | { |
| 15 | 159 | | placeModal.Dispose(); |
| 15 | 160 | | Destroy(placeModal.gameObject); |
| | 161 | | } |
| 47 | 162 | | } |
| | 163 | |
|
| | 164 | | public void SetPlaces(List<PlaceCardComponentModel> places) |
| | 165 | | { |
| 1 | 166 | | this.places.ExtractItems(); |
| 1 | 167 | | placeCardsPool.ReleaseAll(); |
| | 168 | |
|
| 1 | 169 | | List<BaseComponentView> placeComponentsToAdd = ExplorePlacesUtils.InstantiateAndConfigurePlaceCards( |
| | 170 | | places, |
| | 171 | | placeCardsPool, |
| | 172 | | OnFriendHandlerAdded, |
| | 173 | | OnInfoClicked, |
| | 174 | | OnJumpInClicked); |
| | 175 | |
|
| 1 | 176 | | this.places.SetItems(placeComponentsToAdd); |
| 1 | 177 | | placesNoDataText.gameObject.SetActive(places.Count == 0); |
| 1 | 178 | | } |
| | 179 | |
|
| | 180 | | public void AddPlaces(List<PlaceCardComponentModel> places) |
| | 181 | | { |
| 1 | 182 | | List<BaseComponentView> placeComponentsToAdd = ExplorePlacesUtils.InstantiateAndConfigurePlaceCards( |
| | 183 | | places, |
| | 184 | | placeCardsPool, |
| | 185 | | OnFriendHandlerAdded, |
| | 186 | | OnInfoClicked, |
| | 187 | | OnJumpInClicked); |
| | 188 | |
|
| 6 | 189 | | foreach (var place in placeComponentsToAdd) |
| | 190 | | { |
| 2 | 191 | | this.places.AddItem(place); |
| | 192 | | } |
| 1 | 193 | | } |
| | 194 | |
|
| | 195 | | public void SetPlacesAsLoading(bool isVisible) |
| | 196 | | { |
| 2 | 197 | | places.gameObject.SetActive(!isVisible); |
| 2 | 198 | | placesLoading.SetActive(isVisible); |
| | 199 | |
|
| 2 | 200 | | if (isVisible) |
| 1 | 201 | | placesNoDataText.gameObject.SetActive(false); |
| 2 | 202 | | } |
| | 203 | |
|
| 4 | 204 | | public void SetShowMorePlacesButtonActive(bool isActive) { showMorePlacesButtonContainer.gameObject.SetActive(isActi |
| | 205 | |
|
| | 206 | | public void ShowPlaceModal(PlaceCardComponentModel placeInfo) |
| | 207 | | { |
| 1 | 208 | | placeModal.Show(); |
| 1 | 209 | | ExplorePlacesUtils.ConfigurePlaceCard(placeModal, placeInfo, OnInfoClicked, OnJumpInClicked); |
| 1 | 210 | | } |
| | 211 | |
|
| 2 | 212 | | public void HidePlaceModal() { placeModal.Hide(); } |
| | 213 | |
|
| 0 | 214 | | public void RestartScrollViewPosition() { scrollView.verticalNormalizedPosition = 1; } |
| | 215 | | } |