< Summary

Class:PlacesAndEventsCardsFactory
Assembly:ExploreV2
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/ExploreV2/Scripts/Sections/PlacesAndEventsSection/PlacesAndEventsCardsFactory.cs
Covered lines:46
Uncovered lines:15
Coverable lines:61
Total lines:355
Line coverage:75.4% (46 of 61)
Covered branches:0
Total branches:0
Covered methods:13
Total methods:15
Method coverage:86.6% (13 of 15)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
GetCardsPoolLazy(...)0%220100%
CreateConfiguredEventCard(...)0%110100%
CreateConfiguredPlaceCard(...)0%110100%
GetPlaceCardTemplateHiddenLazy(...)0%110100%
GetWorldCardTemplateHiddenLazy(...)0%110100%
GetEventCardTemplateHiddenLazy(...)0%110100%
GetCardTemplateHiddenLazy[TCardView](...)0%220100%
ConvertPlaceResponseToModel(...)0%7.187084.62%
ConvertWorldsResponseToModel(...)0%440100%
ConvertPlaceResponseToModel(...)0%12300%
ConvertWorldsResponseToModel(...)0%3.333066.67%
ConvertPlaceResponseToModel(...)0%3.333066.67%
ConvertCategoriesResponseToToggleModel(...)0%12300%
CreateEventsCards(...)0%110100%
CreateModelsListFromAPI[TModel, TInfo](...)0%220100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/ExploreV2/Scripts/Sections/PlacesAndEventsSection/PlacesAndEventsCardsFactory.cs

#LineLine coverage
 1using DCL;
 2using DCL.Controllers;
 3using DCL.Helpers;
 4using DCLServices.WorldsAPIService;
 5using System;
 6using System.Collections.Generic;
 7using UnityEngine;
 8using static MainScripts.DCL.Controllers.HotScenes.IHotScenesController;
 9using Object = UnityEngine.Object;
 10
 11public 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    {
 9828        Pool pool = PoolManager.i.GetPool(poolName);
 29
 9830        if (pool != null)
 9331            return pool;
 32
 533        pool = PoolManager.i.AddPool(poolName, Object.Instantiate(cardPrefab).gameObject, maxPrewarmCount: maxPrewarmCou
 534        pool.ForcePrewarm();
 35
 536        return pool;
 37    }
 38
 39    public static EventCardComponentView CreateConfiguredEventCard(Pool pool, EventCardComponentModel eventInfo, Action<
 640        EventsCardsConfigurator.Configure(pool.Get<EventCardComponentView>(), eventInfo, OnEventInfoClicked, OnEventJump
 41
 42    public static PlaceCardComponentView CreateConfiguredPlaceCard(Pool pool, PlaceCardComponentModel placeInfo, Action<
 4843        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) =>
 13851        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) =>
 13259        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) =>
 9367        GetCardTemplateHiddenLazy(eventCardModalPrefab, EVENT_CARD_MODAL_ID);
 68
 69    private static TCardView GetCardTemplateHiddenLazy<TCardView>(TCardView modalPrefab, string cardModalId) where TCard
 70    {
 71        TCardView modal;
 72
 36373        GameObject existingModal = GameObject.Find(cardModalId);
 74
 36375        if (existingModal != null)
 32476            modal = existingModal.GetComponent<TCardView>();
 77        else
 78        {
 3979            modal = Object.Instantiate(modalPrefab);
 3980            modal.name = cardModalId;
 81        }
 82
 36383        modal.Hide(true);
 84
 36385        return modal;
 86    }
 87
 88    public static List<PlaceCardComponentModel> ConvertPlaceResponseToModel(IEnumerable<PlaceInfo> placeInfo, int amount
 89    {
 390        List<PlaceCardComponentModel> modelsList = new List<PlaceCardComponentModel>();
 391        int count = 0;
 792        foreach (var place in placeInfo)
 93        {
 194            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                                {
 0115                                    "A" or "M" => SceneContentCategory.ADULT,
 0116                                    "R" => SceneContentCategory.RESTRICTED,
 1117                                    _ => SceneContentCategory.TEEN,
 118                                },
 119                    categories = place.categories,
 120                });
 1121            count++;
 1122            if(count >= amountToTake)
 1123                break;
 124        }
 125
 3126        return modelsList;
 127    }
 128
 129    public static List<PlaceCardComponentModel> ConvertWorldsResponseToModel(IEnumerable<WorldsResponse.WorldInfo> world
 130    {
 4131        List<PlaceCardComponentModel> modelsList = new List<PlaceCardComponentModel>();
 4132        int count = 0;
 9133        foreach (var world in worldInfos)
 134        {
 1135            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                });
 1177            count++;
 1178            if(count >= amountToTake)
 1179                break;
 180        }
 181
 4182        return modelsList;
 183    }
 184
 185    public static List<PlaceCardComponentModel> ConvertPlaceResponseToModel(
 186        IList<PlaceInfo> placeInfo,
 187        Predicate<(int index, PlaceInfo place)> filter)
 188    {
 0189        List<PlaceCardComponentModel> modelsList = new List<PlaceCardComponentModel>();
 190
 0191        for (var index = 0; index < placeInfo.Count; index++)
 192        {
 0193            PlaceInfo place = placeInfo[index];
 0194            if(!filter((index, place)))
 195                continue;
 0196            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
 0219        return modelsList;
 220    }
 221
 222    public static List<PlaceCardComponentModel> ConvertWorldsResponseToModel(IEnumerable<WorldsResponse.WorldInfo> world
 223    {
 2224        List<PlaceCardComponentModel> modelsList = new List<PlaceCardComponentModel>();
 4225        foreach (var world in worldInfo)
 226        {
 0227            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
 2270        return modelsList;
 271    }
 272
 273    public static List<PlaceCardComponentModel> ConvertPlaceResponseToModel(IEnumerable<PlaceInfo> placeInfo)
 274    {
 2275        List<PlaceCardComponentModel> modelsList = new List<PlaceCardComponentModel>();
 4276        foreach (var place in placeInfo)
 277        {
 0278            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
 2301        return modelsList;
 302    }
 303
 304    public static List<ToggleComponentModel> ConvertCategoriesResponseToToggleModel(List<CategoryFromAPIModel> categorie
 305    {
 0306        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
 0318        foreach (var category in categories)
 319        {
 0320            if (!category.active)
 321                continue;
 322
 0323            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
 0334        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) =>
 7343        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    {
 7348        List<TModel> loadedCards = new List<TModel>();
 349
 26350        foreach (TInfo filteredCardInfo in filteredAPIModels)
 6351            loadedCards.Add(configureModelByApiData(new TModel(), filteredCardInfo));
 352
 7353        return loadedCards;
 354    }
 355}