| | 1 | | using DCL; |
| | 2 | | using DCL.Controllers; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using DCLServices.WorldsAPIService; |
| | 5 | | using System; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using UnityEngine; |
| | 8 | | using static MainScripts.DCL.Controllers.HotScenes.IHotScenesController; |
| | 9 | | using Object = UnityEngine.Object; |
| | 10 | |
|
| | 11 | | public static class PlacesAndEventsCardsFactory |
| | 12 | | { |
| | 13 | | internal const string EVENT_CARD_MODAL_ID = "EventCard_Modal"; |
| | 14 | | internal const string PLACE_CARD_MODAL_ID = "PlaceCard_Modal"; |
| | 15 | | internal const string WORLD_CARD_MODAL_ID = "WorldCard_Modal"; |
| | 16 | | internal const string ALL_ID = "all"; |
| | 17 | | internal const string ALL_TEXT = "All"; |
| | 18 | |
|
| | 19 | | /// <summary> |
| | 20 | | /// Creates and configures a pool for cards. |
| | 21 | | /// </summary> |
| | 22 | | /// <param name="pool">Pool to configure.</param> |
| | 23 | | /// <param name="poolName">Name of the pool.</param> |
| | 24 | | /// <param name="cardPrefab">Card prefab to use by the pool.</param> |
| | 25 | | /// <param name="maxPrewarmCount">Max number of pre-created cards.</param> |
| | 26 | | public static Pool GetCardsPoolLazy(string poolName, MonoBehaviour cardPrefab, int maxPrewarmCount) |
| | 27 | | { |
| 98 | 28 | | Pool pool = PoolManager.i.GetPool(poolName); |
| | 29 | |
|
| 98 | 30 | | if (pool != null) |
| 93 | 31 | | return pool; |
| | 32 | |
|
| 5 | 33 | | pool = PoolManager.i.AddPool(poolName, Object.Instantiate(cardPrefab).gameObject, maxPrewarmCount: maxPrewarmCou |
| 5 | 34 | | pool.ForcePrewarm(); |
| | 35 | |
|
| 5 | 36 | | return pool; |
| | 37 | | } |
| | 38 | |
|
| | 39 | | public static EventCardComponentView CreateConfiguredEventCard(Pool pool, EventCardComponentModel eventInfo, Action< |
| 6 | 40 | | EventsCardsConfigurator.Configure(pool.Get<EventCardComponentView>(), eventInfo, OnEventInfoClicked, OnEventJump |
| | 41 | |
|
| | 42 | | public static PlaceCardComponentView CreateConfiguredPlaceCard(Pool pool, PlaceCardComponentModel placeInfo, Action< |
| 48 | 43 | | PlacesCardsConfigurator.Configure(pool.Get<PlaceCardComponentView>(), placeInfo, OnPlaceInfoClicked, OnPlaceJump |
| | 44 | |
|
| | 45 | | /// <summary> |
| | 46 | | /// Instantiates (if does not already exists) a place card modal from the given prefab. |
| | 47 | | /// </summary> |
| | 48 | | /// <param name="placeCardModalPrefab">Prefab to instantiate.</param> |
| | 49 | | /// <returns>An instance of a place card modal.</returns> |
| | 50 | | public static PlaceCardComponentView GetPlaceCardTemplateHiddenLazy(PlaceCardComponentView placeCardModalPrefab) => |
| 138 | 51 | | GetCardTemplateHiddenLazy(placeCardModalPrefab, PLACE_CARD_MODAL_ID); |
| | 52 | |
|
| | 53 | | /// <summary> |
| | 54 | | /// Instantiates (if does not already exists) a world card modal from the given prefab. |
| | 55 | | /// </summary> |
| | 56 | | /// <param name="worldCardModalPrefab">Prefab to instantiate.</param> |
| | 57 | | /// <returns>An instance of a world card modal.</returns> |
| | 58 | | public static PlaceCardComponentView GetWorldCardTemplateHiddenLazy(PlaceCardComponentView worldCardModalPrefab) => |
| 132 | 59 | | GetCardTemplateHiddenLazy(worldCardModalPrefab, WORLD_CARD_MODAL_ID); |
| | 60 | |
|
| | 61 | | /// <summary> |
| | 62 | | /// Instantiates (if does not already exists) a event card modal from the given prefab. |
| | 63 | | /// </summary> |
| | 64 | | /// <param name="eventCardModalPrefab">Prefab to instantiate.</param> |
| | 65 | | /// <returns>An instance of a event card modal.</returns> |
| | 66 | | public static EventCardComponentView GetEventCardTemplateHiddenLazy(EventCardComponentView eventCardModalPrefab) => |
| 93 | 67 | | GetCardTemplateHiddenLazy(eventCardModalPrefab, EVENT_CARD_MODAL_ID); |
| | 68 | |
|
| | 69 | | private static TCardView GetCardTemplateHiddenLazy<TCardView>(TCardView modalPrefab, string cardModalId) where TCard |
| | 70 | | { |
| | 71 | | TCardView modal; |
| | 72 | |
|
| 363 | 73 | | GameObject existingModal = GameObject.Find(cardModalId); |
| | 74 | |
|
| 363 | 75 | | if (existingModal != null) |
| 324 | 76 | | modal = existingModal.GetComponent<TCardView>(); |
| | 77 | | else |
| | 78 | | { |
| 39 | 79 | | modal = Object.Instantiate(modalPrefab); |
| 39 | 80 | | modal.name = cardModalId; |
| | 81 | | } |
| | 82 | |
|
| 363 | 83 | | modal.Hide(true); |
| | 84 | |
|
| 363 | 85 | | return modal; |
| | 86 | | } |
| | 87 | |
|
| | 88 | | public static List<PlaceCardComponentModel> ConvertPlaceResponseToModel(IEnumerable<PlaceInfo> placeInfo, int amount |
| | 89 | | { |
| 3 | 90 | | List<PlaceCardComponentModel> modelsList = new List<PlaceCardComponentModel>(); |
| 3 | 91 | | int count = 0; |
| 7 | 92 | | foreach (var place in placeInfo) |
| | 93 | | { |
| 1 | 94 | | modelsList.Add( |
| | 95 | | new PlaceCardComponentModel() |
| | 96 | | { |
| | 97 | | placePictureUri = place.image, |
| | 98 | | placeName = place.title, |
| | 99 | | placeDescription = place.description, |
| | 100 | | placeAuthor = place.contact_name, |
| | 101 | | numberOfUsers = place.user_count, |
| | 102 | | coords = Utils.ConvertStringToVector(place.base_position), |
| | 103 | | parcels = place.Positions, |
| | 104 | | isFavorite = place.user_favorite, |
| | 105 | | userVisits = place.user_visits, |
| | 106 | | userRating = place.like_rate_as_float, |
| | 107 | | placeInfo = place, |
| | 108 | | isUpvote = place.user_like, |
| | 109 | | isDownvote = place.user_dislike, |
| | 110 | | totalVotes = place.likes + place.dislikes, |
| | 111 | | numberOfFavorites = place.favorites, |
| | 112 | | deployedAt = place.deployed_at, |
| | 113 | | ageRating = place.content_rating switch |
| | 114 | | { |
| 0 | 115 | | "A" or "M" => SceneContentCategory.ADULT, |
| 0 | 116 | | "R" => SceneContentCategory.RESTRICTED, |
| 1 | 117 | | _ => SceneContentCategory.TEEN, |
| | 118 | | }, |
| | 119 | | categories = place.categories, |
| | 120 | | }); |
| 1 | 121 | | count++; |
| 1 | 122 | | if(count >= amountToTake) |
| 1 | 123 | | break; |
| | 124 | | } |
| | 125 | |
|
| 3 | 126 | | return modelsList; |
| | 127 | | } |
| | 128 | |
|
| | 129 | | public static List<PlaceCardComponentModel> ConvertWorldsResponseToModel(IEnumerable<WorldsResponse.WorldInfo> world |
| | 130 | | { |
| 4 | 131 | | List<PlaceCardComponentModel> modelsList = new List<PlaceCardComponentModel>(); |
| 4 | 132 | | int count = 0; |
| 9 | 133 | | foreach (var world in worldInfos) |
| | 134 | | { |
| 1 | 135 | | modelsList.Add( |
| | 136 | | new PlaceCardComponentModel() |
| | 137 | | { |
| | 138 | | placePictureUri = world.image, |
| | 139 | | placeName = world.title, |
| | 140 | | placeDescription = world.description, |
| | 141 | | placeAuthor = world.contact_name, |
| | 142 | | numberOfUsers = world.user_count, |
| | 143 | | coords = Utils.ConvertStringToVector(world.base_position), |
| | 144 | | parcels = world.Positions, |
| | 145 | | isFavorite = world.user_favorite, |
| | 146 | | userVisits = world.user_visits, |
| | 147 | | userRating = world.like_rate_as_float, |
| | 148 | | placeInfo = new PlaceInfo() |
| | 149 | | { |
| | 150 | | base_position = world.base_position, |
| | 151 | | categories = new []{""}, |
| | 152 | | contact_name = world.contact_name, |
| | 153 | | description = world.description, |
| | 154 | | world_name = world.world_name, |
| | 155 | | id = world.id, |
| | 156 | | image = world.image, |
| | 157 | | likes = world.likes, |
| | 158 | | dislikes = world.dislikes, |
| | 159 | | title = world.title, |
| | 160 | | user_count = world.user_count, |
| | 161 | | user_favorite = world.user_favorite, |
| | 162 | | user_like = world.user_like, |
| | 163 | | user_dislike = world.user_dislike, |
| | 164 | | user_visits = world.user_visits, |
| | 165 | | favorites = world.favorites, |
| | 166 | | deployed_at = world.deployed_at, |
| | 167 | | Positions = world.Positions, |
| | 168 | |
|
| | 169 | | }, |
| | 170 | | isUpvote = world.user_like, |
| | 171 | | isDownvote = world.user_dislike, |
| | 172 | | totalVotes = world.likes + world.dislikes, |
| | 173 | | numberOfFavorites = world.favorites, |
| | 174 | | deployedAt = world.deployed_at, |
| | 175 | | categories = world.categories, |
| | 176 | | }); |
| 1 | 177 | | count++; |
| 1 | 178 | | if(count >= amountToTake) |
| 1 | 179 | | break; |
| | 180 | | } |
| | 181 | |
|
| 4 | 182 | | return modelsList; |
| | 183 | | } |
| | 184 | |
|
| | 185 | | public static List<PlaceCardComponentModel> ConvertPlaceResponseToModel( |
| | 186 | | IList<PlaceInfo> placeInfo, |
| | 187 | | Predicate<(int index, PlaceInfo place)> filter) |
| | 188 | | { |
| 0 | 189 | | List<PlaceCardComponentModel> modelsList = new List<PlaceCardComponentModel>(); |
| | 190 | |
|
| 0 | 191 | | for (var index = 0; index < placeInfo.Count; index++) |
| | 192 | | { |
| 0 | 193 | | PlaceInfo place = placeInfo[index]; |
| 0 | 194 | | if(!filter((index, place))) |
| | 195 | | continue; |
| 0 | 196 | | modelsList.Add( |
| | 197 | | new PlaceCardComponentModel() |
| | 198 | | { |
| | 199 | | placePictureUri = place.image, |
| | 200 | | placeName = place.title, |
| | 201 | | placeDescription = place.description, |
| | 202 | | placeAuthor = place.contact_name, |
| | 203 | | numberOfUsers = place.user_count, |
| | 204 | | coords = Utils.ConvertStringToVector(place.base_position), |
| | 205 | | parcels = place.Positions, |
| | 206 | | isFavorite = place.user_favorite, |
| | 207 | | userVisits = place.user_visits, |
| | 208 | | userRating = place.like_rate_as_float, |
| | 209 | | placeInfo = place, |
| | 210 | | isUpvote = place.user_like, |
| | 211 | | isDownvote = place.user_dislike, |
| | 212 | | totalVotes = place.likes + place.dislikes, |
| | 213 | | numberOfFavorites = place.favorites, |
| | 214 | | deployedAt = place.deployed_at, |
| | 215 | | categories = place.categories, |
| | 216 | | }); |
| | 217 | | } |
| | 218 | |
|
| 0 | 219 | | return modelsList; |
| | 220 | | } |
| | 221 | |
|
| | 222 | | public static List<PlaceCardComponentModel> ConvertWorldsResponseToModel(IEnumerable<WorldsResponse.WorldInfo> world |
| | 223 | | { |
| 2 | 224 | | List<PlaceCardComponentModel> modelsList = new List<PlaceCardComponentModel>(); |
| 4 | 225 | | foreach (var world in worldInfo) |
| | 226 | | { |
| 0 | 227 | | modelsList.Add( |
| | 228 | | new PlaceCardComponentModel() |
| | 229 | | { |
| | 230 | | placePictureUri = world.image, |
| | 231 | | placeName = world.title, |
| | 232 | | placeDescription = world.description, |
| | 233 | | placeAuthor = world.contact_name, |
| | 234 | | numberOfUsers = world.user_count, |
| | 235 | | coords = Utils.ConvertStringToVector(world.base_position), |
| | 236 | | parcels = world.Positions, |
| | 237 | | isFavorite = world.user_favorite, |
| | 238 | | userVisits = world.user_visits, |
| | 239 | | userRating = world.like_rate_as_float, |
| | 240 | | placeInfo = new PlaceInfo() |
| | 241 | | { |
| | 242 | | base_position = world.base_position, |
| | 243 | | categories = new []{""}, |
| | 244 | | contact_name = world.contact_name, |
| | 245 | | world_name = world.world_name, |
| | 246 | | description = world.description, |
| | 247 | | id = world.id, |
| | 248 | | image = world.image, |
| | 249 | | likes = world.likes, |
| | 250 | | dislikes = world.dislikes, |
| | 251 | | title = world.title, |
| | 252 | | user_count = world.user_count, |
| | 253 | | user_favorite = world.user_favorite, |
| | 254 | | user_like = world.user_like, |
| | 255 | | user_dislike = world.user_dislike, |
| | 256 | | user_visits = world.user_visits, |
| | 257 | | favorites = world.favorites, |
| | 258 | | deployed_at = world.deployed_at, |
| | 259 | | Positions = world.Positions, |
| | 260 | | }, |
| | 261 | | isUpvote = world.user_like, |
| | 262 | | isDownvote = world.user_dislike, |
| | 263 | | totalVotes = world.likes + world.dislikes, |
| | 264 | | numberOfFavorites = world.favorites, |
| | 265 | | deployedAt = world.deployed_at, |
| | 266 | | categories = world.categories, |
| | 267 | | }); |
| | 268 | | } |
| | 269 | |
|
| 2 | 270 | | return modelsList; |
| | 271 | | } |
| | 272 | |
|
| | 273 | | public static List<PlaceCardComponentModel> ConvertPlaceResponseToModel(IEnumerable<PlaceInfo> placeInfo) |
| | 274 | | { |
| 2 | 275 | | List<PlaceCardComponentModel> modelsList = new List<PlaceCardComponentModel>(); |
| 4 | 276 | | foreach (var place in placeInfo) |
| | 277 | | { |
| 0 | 278 | | modelsList.Add( |
| | 279 | | new PlaceCardComponentModel() |
| | 280 | | { |
| | 281 | | placePictureUri = place.image, |
| | 282 | | placeName = place.title, |
| | 283 | | placeDescription = place.description, |
| | 284 | | placeAuthor = place.contact_name, |
| | 285 | | numberOfUsers = place.user_count, |
| | 286 | | coords = Utils.ConvertStringToVector(place.base_position), |
| | 287 | | parcels = place.Positions, |
| | 288 | | isFavorite = place.user_favorite, |
| | 289 | | userVisits = place.user_visits, |
| | 290 | | userRating = place.like_rate_as_float, |
| | 291 | | placeInfo = place, |
| | 292 | | isUpvote = place.user_like, |
| | 293 | | isDownvote = place.user_dislike, |
| | 294 | | totalVotes = place.likes + place.dislikes, |
| | 295 | | numberOfFavorites = place.favorites, |
| | 296 | | deployedAt = place.deployed_at, |
| | 297 | | categories = place.categories, |
| | 298 | | }); |
| | 299 | | } |
| | 300 | |
|
| 2 | 301 | | return modelsList; |
| | 302 | | } |
| | 303 | |
|
| | 304 | | public static List<ToggleComponentModel> ConvertCategoriesResponseToToggleModel(List<CategoryFromAPIModel> categorie |
| | 305 | | { |
| 0 | 306 | | List<ToggleComponentModel> modelsList = new List<ToggleComponentModel> |
| | 307 | | { |
| | 308 | | new() |
| | 309 | | { |
| | 310 | | id = ALL_ID, |
| | 311 | | text = ALL_TEXT, |
| | 312 | | isOn = true, |
| | 313 | | isTextActive = true, |
| | 314 | | changeTextColorOnSelect = true, |
| | 315 | | }, |
| | 316 | | }; |
| | 317 | |
|
| 0 | 318 | | foreach (var category in categories) |
| | 319 | | { |
| 0 | 320 | | if (!category.active) |
| | 321 | | continue; |
| | 322 | |
|
| 0 | 323 | | modelsList.Add( |
| | 324 | | new ToggleComponentModel |
| | 325 | | { |
| | 326 | | id = category.name, |
| | 327 | | text = category.i18n.en, |
| | 328 | | isOn = false, |
| | 329 | | isTextActive = true, |
| | 330 | | changeTextColorOnSelect = true, |
| | 331 | | }); |
| | 332 | | } |
| | 333 | |
|
| 0 | 334 | | return modelsList; |
| | 335 | | } |
| | 336 | |
|
| | 337 | | /// <summary> |
| | 338 | | /// Returns a event card model from the given API data. |
| | 339 | | /// </summary> |
| | 340 | | /// <param name="filteredEvents">Data received from the API.</param> |
| | 341 | | /// <returns>An event card model.</returns> |
| | 342 | | public static List<EventCardComponentModel> CreateEventsCards(List<EventFromAPIModel> filteredEvents) => |
| 7 | 343 | | CreateModelsListFromAPI<EventCardComponentModel, EventFromAPIModel>(filteredEvents, EventsCardsConfigurator.Conf |
| | 344 | |
|
| | 345 | | private static List<TModel> CreateModelsListFromAPI<TModel, TInfo>(List<TInfo> filteredAPIModels, Func<TModel, TInfo |
| | 346 | | where TModel: BaseComponentModel, new() |
| | 347 | | { |
| 7 | 348 | | List<TModel> loadedCards = new List<TModel>(); |
| | 349 | |
|
| 26 | 350 | | foreach (TInfo filteredCardInfo in filteredAPIModels) |
| 6 | 351 | | loadedCards.Add(configureModelByApiData(new TModel(), filteredCardInfo)); |
| | 352 | |
|
| 7 | 353 | | return loadedCards; |
| | 354 | | } |
| | 355 | | } |