| | 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 | |
|
| | 91 | | public class PlacesSubSectionComponentView : BaseComponentView, IPlacesSubSectionComponentView |
| | 92 | | { |
| | 93 | | internal const string PLACE_CARDS_POOL_NAME = "PlaceCardsPool"; |
| | 94 | |
|
| | 95 | | [Header("Assets References")] |
| | 96 | | [SerializeField] internal PlaceCardComponentView placeCardPrefab; |
| | 97 | | [SerializeField] internal PlaceCardComponentView placeCardModalPrefab; |
| | 98 | |
|
| | 99 | | [Header("Prefab References")] |
| | 100 | | [SerializeField] internal ScrollRect scrollView; |
| | 101 | | [SerializeField] internal GridContainerComponentView places; |
| | 102 | | [SerializeField] internal GameObject placesLoading; |
| | 103 | | [SerializeField] internal TMP_Text placesNoDataText; |
| | 104 | | [SerializeField] internal Color[] friendColors = null; |
| | 105 | | [SerializeField] internal GameObject showMorePlacesButtonContainer; |
| | 106 | | [SerializeField] internal ButtonComponentView showMorePlacesButton; |
| | 107 | |
|
| | 108 | | public event Action OnReady; |
| | 109 | | public event Action<PlaceCardComponentModel> OnInfoClicked; |
| | 110 | | public event Action<HotScenesController.HotSceneInfo> OnJumpInClicked; |
| | 111 | | public event Action<FriendsHandler> OnFriendHandlerAdded; |
| | 112 | | public event Action OnPlacesSubSectionEnable; |
| | 113 | | public event Action OnShowMorePlacesClicked; |
| | 114 | |
|
| | 115 | | internal PlaceCardComponentView placeModal; |
| | 116 | | internal Pool placeCardsPool; |
| | 117 | |
|
| 0 | 118 | | public Color[] currentFriendColors => friendColors; |
| | 119 | |
|
| 0 | 120 | | public int currentPlacesPerRow => places.currentItemsPerRow; |
| | 121 | |
|
| 14 | 122 | | public override void OnEnable() { OnPlacesSubSectionEnable?.Invoke(); } |
| | 123 | |
|
| | 124 | | public override void Start() |
| | 125 | | { |
| 11 | 126 | | ConfigurePlaceCardModal(); |
| 11 | 127 | | ConfigurePlaceCardsPool(); |
| | 128 | |
|
| 11 | 129 | | places.RemoveItems(); |
| | 130 | |
|
| 11 | 131 | | showMorePlacesButton.onClick.RemoveAllListeners(); |
| 11 | 132 | | showMorePlacesButton.onClick.AddListener(() => OnShowMorePlacesClicked?.Invoke()); |
| | 133 | |
|
| 11 | 134 | | OnReady?.Invoke(); |
| 0 | 135 | | } |
| | 136 | |
|
| 0 | 137 | | public override void RefreshControl() { places.RefreshControl(); } |
| | 138 | |
|
| | 139 | | public override void Dispose() |
| | 140 | | { |
| 35 | 141 | | base.Dispose(); |
| | 142 | |
|
| 35 | 143 | | showMorePlacesButton.onClick.RemoveAllListeners(); |
| | 144 | |
|
| 35 | 145 | | places.Dispose(); |
| | 146 | |
|
| 35 | 147 | | if (placeModal != null) |
| | 148 | | { |
| 12 | 149 | | placeModal.Dispose(); |
| 12 | 150 | | Destroy(placeModal.gameObject); |
| | 151 | | } |
| 35 | 152 | | } |
| | 153 | |
|
| | 154 | | public void SetPlaces(List<PlaceCardComponentModel> places) |
| | 155 | | { |
| 1 | 156 | | this.places.ExtractItems(); |
| 1 | 157 | | placeCardsPool.ReleaseAll(); |
| 1 | 158 | | List<BaseComponentView> placeComponentsToAdd = InstantiateAndConfigurePlaceCards(places); |
| 1 | 159 | | this.places.SetItems(placeComponentsToAdd); |
| 1 | 160 | | placesNoDataText.gameObject.SetActive(places.Count == 0); |
| 1 | 161 | | } |
| | 162 | |
|
| | 163 | | public void AddPlaces(List<PlaceCardComponentModel> places) |
| | 164 | | { |
| 1 | 165 | | List<BaseComponentView> placeComponentsToAdd = InstantiateAndConfigurePlaceCards(places); |
| 6 | 166 | | foreach (var place in placeComponentsToAdd) |
| | 167 | | { |
| 2 | 168 | | this.places.AddItem(place); |
| | 169 | | } |
| 1 | 170 | | } |
| | 171 | |
|
| | 172 | | public void SetPlacesAsLoading(bool isVisible) |
| | 173 | | { |
| 2 | 174 | | places.gameObject.SetActive(!isVisible); |
| 2 | 175 | | placesLoading.SetActive(isVisible); |
| | 176 | |
|
| 2 | 177 | | if (isVisible) |
| 1 | 178 | | placesNoDataText.gameObject.SetActive(false); |
| 2 | 179 | | } |
| | 180 | |
|
| 4 | 181 | | public void SetShowMorePlacesButtonActive(bool isActive) { showMorePlacesButtonContainer.gameObject.SetActive(isActi |
| | 182 | |
|
| | 183 | | public void ShowPlaceModal(PlaceCardComponentModel placeInfo) |
| | 184 | | { |
| 1 | 185 | | placeModal.Show(); |
| 1 | 186 | | ConfigurePlaceCard(placeModal, placeInfo); |
| 1 | 187 | | } |
| | 188 | |
|
| 2 | 189 | | public void HidePlaceModal() { placeModal.Hide(); } |
| | 190 | |
|
| 0 | 191 | | public void RestartScrollViewPosition() { scrollView.verticalNormalizedPosition = 1; } |
| | 192 | |
|
| | 193 | | internal void ConfigurePlaceCardModal() |
| | 194 | | { |
| 12 | 195 | | placeModal = Instantiate(placeCardModalPrefab); |
| 12 | 196 | | placeModal.Hide(true); |
| 12 | 197 | | } |
| | 198 | |
|
| | 199 | | internal void ConfigurePlaceCardsPool() |
| | 200 | | { |
| 12 | 201 | | placeCardsPool = PoolManager.i.GetPool(PLACE_CARDS_POOL_NAME); |
| 12 | 202 | | if (placeCardsPool == null) |
| | 203 | | { |
| 1 | 204 | | placeCardsPool = PoolManager.i.AddPool( |
| | 205 | | PLACE_CARDS_POOL_NAME, |
| | 206 | | Instantiate(placeCardPrefab).gameObject, |
| | 207 | | maxPrewarmCount: 200, |
| | 208 | | isPersistent: true); |
| | 209 | | } |
| 12 | 210 | | } |
| | 211 | |
|
| | 212 | | internal List<BaseComponentView> InstantiateAndConfigurePlaceCards(List<PlaceCardComponentModel> places) |
| | 213 | | { |
| 2 | 214 | | List<BaseComponentView> instantiatedPlaces = new List<BaseComponentView>(); |
| | 215 | |
|
| 12 | 216 | | foreach (PlaceCardComponentModel placeInfo in places) |
| | 217 | | { |
| 4 | 218 | | PlaceCardComponentView placeGO = placeCardsPool.Get().gameObject.GetComponent<PlaceCardComponentView>(); |
| 4 | 219 | | ConfigurePlaceCard(placeGO, placeInfo); |
| 4 | 220 | | OnFriendHandlerAdded?.Invoke(placeGO.friendsHandler); |
| 4 | 221 | | instantiatedPlaces.Add(placeGO); |
| | 222 | | } |
| | 223 | |
|
| 2 | 224 | | return instantiatedPlaces; |
| | 225 | | } |
| | 226 | |
|
| | 227 | | internal void ConfigurePlaceCard(PlaceCardComponentView placeCard, PlaceCardComponentModel placeInfo) |
| | 228 | | { |
| 6 | 229 | | placeCard.Configure(placeInfo); |
| 6 | 230 | | placeCard.onInfoClick?.RemoveAllListeners(); |
| 6 | 231 | | placeCard.onInfoClick?.AddListener(() => OnInfoClicked?.Invoke(placeInfo)); |
| 6 | 232 | | placeCard.onJumpInClick?.RemoveAllListeners(); |
| 6 | 233 | | placeCard.onJumpInClick?.AddListener(() => OnJumpInClicked?.Invoke(placeInfo.hotSceneInfo)); |
| 6 | 234 | | } |
| | 235 | | } |