| | 1 | | using DCL; |
| | 2 | | using System; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using UnityEngine; |
| | 5 | | using static HotScenesController; |
| | 6 | | using Object = UnityEngine.Object; |
| | 7 | |
|
| | 8 | | public static class PlacesAndEventsCardsFactory |
| | 9 | | { |
| | 10 | | internal const string EVENT_CARD_MODAL_ID = "EventCard_Modal"; |
| | 11 | | internal const string PLACE_CARD_MODAL_ID = "PlaceCard_Modal"; |
| | 12 | |
|
| | 13 | | /// <summary> |
| | 14 | | /// Creates and configures a pool for cards. |
| | 15 | | /// </summary> |
| | 16 | | /// <param name="pool">Pool to configure.</param> |
| | 17 | | /// <param name="poolName">Name of the pool.</param> |
| | 18 | | /// <param name="cardPrefab">Card prefab to use by the pool.</param> |
| | 19 | | /// <param name="maxPrewarmCount">Max number of pre-created cards.</param> |
| | 20 | | public static Pool GetCardsPoolLazy(string poolName, BaseComponentView cardPrefab, int maxPrewarmCount) |
| | 21 | | { |
| 171 | 22 | | Pool pool = PoolManager.i.GetPool(poolName); |
| | 23 | |
|
| 171 | 24 | | if (pool != null) |
| 162 | 25 | | return pool; |
| | 26 | |
|
| 9 | 27 | | pool = PoolManager.i.AddPool(poolName, Object.Instantiate(cardPrefab).gameObject, maxPrewarmCount: maxPrewarmCou |
| 9 | 28 | | pool.ForcePrewarm(); |
| | 29 | |
|
| 9 | 30 | | return pool; |
| | 31 | | } |
| | 32 | |
|
| | 33 | | public static EventCardComponentView CreateConfiguredEventCard(Pool pool, EventCardComponentModel eventInfo, Action< |
| 24 | 34 | | EventsCardsConfigurator.Configure(pool.Get<EventCardComponentView>(), eventInfo, OnEventInfoClicked, OnEventJump |
| | 35 | |
|
| | 36 | | public static PlaceCardComponentView CreateConfiguredPlaceCard(Pool pool, PlaceCardComponentModel placeInfo, Action< |
| 38 | 37 | | PlacesCardsConfigurator.Configure(pool.Get<PlaceCardComponentView>(), placeInfo, OnPlaceInfoClicked, OnPlaceJump |
| | 38 | |
|
| | 39 | | /// <summary> |
| | 40 | | /// Instantiates (if does not already exists) a place card modal from the given prefab. |
| | 41 | | /// </summary> |
| | 42 | | /// <param name="placeCardModalPrefab">Prefab to instantiate.</param> |
| | 43 | | /// <returns>An instance of a place card modal.</returns> |
| | 44 | | public static PlaceCardComponentView GetPlaceCardTemplateHiddenLazy(PlaceCardComponentView placeCardModalPrefab) => |
| 138 | 45 | | GetCardTemplateHiddenLazy(placeCardModalPrefab, PLACE_CARD_MODAL_ID); |
| | 46 | |
|
| | 47 | | /// <summary> |
| | 48 | | /// Instantiates (if does not already exists) a event card modal from the given prefab. |
| | 49 | | /// </summary> |
| | 50 | | /// <param name="eventCardModalPrefab">Prefab to instantiate.</param> |
| | 51 | | /// <returns>An instance of a event card modal.</returns> |
| | 52 | | public static EventCardComponentView GetEventCardTemplateHiddenLazy(EventCardComponentView eventCardModalPrefab) => |
| 109 | 53 | | GetCardTemplateHiddenLazy(eventCardModalPrefab, EVENT_CARD_MODAL_ID); |
| | 54 | |
|
| | 55 | | private static TCardView GetCardTemplateHiddenLazy<TCardView>(TCardView modalPrefab, string cardModalId) where TCard |
| | 56 | | { |
| | 57 | | TCardView modal; |
| | 58 | |
|
| 247 | 59 | | GameObject existingModal = GameObject.Find(cardModalId); |
| | 60 | |
|
| 247 | 61 | | if (existingModal != null) |
| 205 | 62 | | modal = existingModal.GetComponent<TCardView>(); |
| | 63 | | else |
| | 64 | | { |
| 42 | 65 | | modal = Object.Instantiate(modalPrefab); |
| 42 | 66 | | modal.name = cardModalId; |
| | 67 | | } |
| | 68 | |
|
| 247 | 69 | | modal.Hide(true); |
| | 70 | |
|
| 247 | 71 | | return modal; |
| | 72 | | } |
| | 73 | |
|
| | 74 | | /// <summary> |
| | 75 | | /// Returns a place card model from the given API data. |
| | 76 | | /// </summary> |
| | 77 | | /// <param name="filteredPlaces">Data received from the API.</param> |
| | 78 | | /// <returns>A place card model.</returns> |
| | 79 | | public static List<PlaceCardComponentModel> CreatePlacesCards(List<HotSceneInfo> filteredPlaces) => |
| 7 | 80 | | CreateModelsListFromAPI<PlaceCardComponentModel, HotSceneInfo>(filteredPlaces, PlacesCardsConfigurator.Configure |
| | 81 | |
|
| | 82 | | /// <summary> |
| | 83 | | /// Returns a event card model from the given API data. |
| | 84 | | /// </summary> |
| | 85 | | /// <param name="filteredEvents">Data received from the API.</param> |
| | 86 | | /// <returns>An event card model.</returns> |
| | 87 | | public static List<EventCardComponentModel> CreateEventsCards(List<EventFromAPIModel> filteredEvents) => |
| 14 | 88 | | CreateModelsListFromAPI<EventCardComponentModel, EventFromAPIModel>(filteredEvents, EventsCardsConfigurator.Conf |
| | 89 | |
|
| | 90 | | private static List<TModel> CreateModelsListFromAPI<TModel, TInfo>(List<TInfo> filteredAPIModels, Func<TModel, TInfo |
| | 91 | | where TModel: BaseComponentModel, new() |
| | 92 | | { |
| 21 | 93 | | List<TModel> loadedCards = new List<TModel>(); |
| | 94 | |
|
| 74 | 95 | | foreach (TInfo filteredCardInfo in filteredAPIModels) |
| 16 | 96 | | loadedCards.Add(configureModelByApiData(new TModel(), filteredCardInfo)); |
| | 97 | |
|
| 21 | 98 | | return loadedCards; |
| | 99 | | } |
| | 100 | | } |