< Summary

Class:ExploreEventsHelpers
Assembly:ExploreV2
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/ExploreV2/Scripts/Common/ExploreEventsHelpers.cs
Covered lines:67
Uncovered lines:9
Coverable lines:76
Total lines:211
Line coverage:88.1% (67 of 76)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ConfigureEventCardModal(...)0%220100%
ConfigureEventCardsPool(...)0%220100%
InstantiateAndConfigureEventCards(...)0%220100%
ConfigureEventCard(...)0%990100%
CreateEventCardModelFromAPIEvent(...)0%110100%
FormatEventDate(...)0%110100%
FormatEventStartDate(...)0%64050%
FormatEventStartDateFromTo(...)0%110100%
FormatEventOrganized(...)0%110100%
FormatEventPlace(...)0%220100%
JumpInToEvent(...)0%3.143075%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/ExploreV2/Scripts/Common/ExploreEventsHelpers.cs

#LineLine coverage
 1using DCL;
 2using DCL.Interface;
 3using System;
 4using System.Collections.Generic;
 5using System.Globalization;
 6using UnityEngine;
 7
 8/// <summary>
 9/// Helpers related to the events management in ExploreV2.
 10/// </summary>
 11public static class ExploreEventsHelpers
 12{
 13    internal const string EVENT_CARD_MODAL_ID = "EventCard_Modal";
 14    internal const string LIVE_TAG_TEXT = "LIVE";
 15    internal const string EVENT_DETAIL_URL = "https://events.decentraland.org/event/?id={0}";
 16
 17    /// <summary>
 18    /// Instantiates (if does not already exists) a event card modal from the given prefab.
 19    /// </summary>
 20    /// <param name="eventCardModalPrefab">Prefab to instantiate.</param>
 21    /// <returns>An instance of a event card modal.</returns>
 22    public static EventCardComponentView ConfigureEventCardModal(EventCardComponentView eventCardModalPrefab)
 23    {
 3624        EventCardComponentView eventModal = null;
 25
 3626        GameObject existingModal = GameObject.Find(EVENT_CARD_MODAL_ID);
 3627        if (existingModal != null)
 2428            eventModal = existingModal.GetComponent<EventCardComponentView>();
 29        else
 30        {
 1231            eventModal = GameObject.Instantiate(eventCardModalPrefab);
 1232            eventModal.name = EVENT_CARD_MODAL_ID;
 33        }
 34
 3635        eventModal.Hide(true);
 36
 3637        return eventModal;
 38    }
 39
 40    /// <summary>
 41    /// Creates and configures a pool for event cards.
 42    /// </summary>
 43    /// <param name="pool">Pool to configure.</param>
 44    /// <param name="poolName">Name of the pool.</param>
 45    /// <param name="eventCardPrefab">Event card prefab to use by the pool.</param>
 46    /// <param name="maxPrewarmCount">Max number of pre-created cards.</param>
 47    public static void ConfigureEventCardsPool(out Pool pool, string poolName, EventCardComponentView eventCardPrefab, i
 48    {
 11549        pool = PoolManager.i.GetPool(poolName);
 11550        if (pool == null)
 51        {
 652            pool = PoolManager.i.AddPool(
 53                poolName,
 54                GameObject.Instantiate(eventCardPrefab).gameObject,
 55                maxPrewarmCount: maxPrewarmCount,
 56                isPersistent: true);
 57        }
 11558    }
 59
 60    /// <summary>
 61    /// Instantiates and configures a given list of events.
 62    /// </summary>
 63    /// <param name="events">List of events data.</param>
 64    /// <param name="pool">Pool to use.</param>
 65    /// <param name="OnEventInfoClicked">Action to inform when the Info button has been clicked.</param>
 66    /// <param name="OnEventJumpInClicked">Action to inform when the JumpIn button has been clicked.</param>
 67    /// <param name="OnEventSubscribeEventClicked">Action to inform when the Subscribe button has been clicked.</param>
 68    /// <param name="OnEventUnsubscribeEventClicked">Action to inform when the Unsubscribe button has been clicked.</par
 69    /// <returns>A list of instances of events.</returns>
 70    public static List<BaseComponentView> InstantiateAndConfigureEventCards(
 71        List<EventCardComponentModel> events,
 72        Pool pool,
 73        Action<EventCardComponentModel> OnEventInfoClicked,
 74        Action<EventFromAPIModel> OnEventJumpInClicked,
 75        Action<string> OnEventSubscribeEventClicked,
 76        Action<string> OnEventUnsubscribeEventClicked)
 77    {
 778        List<BaseComponentView> instantiatedPlaces = new List<BaseComponentView>();
 79
 4280        foreach (EventCardComponentModel eventInfo in events)
 81        {
 1482            EventCardComponentView eventGO = pool.Get().gameObject.GetComponent<EventCardComponentView>();
 1483            ConfigureEventCard(eventGO, eventInfo, OnEventInfoClicked, OnEventJumpInClicked, OnEventSubscribeEventClicke
 1484            instantiatedPlaces.Add(eventGO);
 85        }
 86
 787        return instantiatedPlaces;
 88    }
 89
 90    /// <summary>
 91    /// Configure a event card with the given model.
 92    /// </summary>
 93    /// <param name="eventCard">Event card to configure.</param>
 94    /// <param name="eventInfo">Model to apply.</param>
 95    /// <param name="OnEventInfoClicked">Action to inform when the Info button has been clicked.</param>
 96    /// <param name="OnEventJumpInClicked">Action to inform when the JumpIn button has been clicked.</param>
 97    /// <param name="OnEventSubscribeEventClicked">Action to inform when the Subscribe button has been clicked.</param>
 98    /// <param name="OnEventUnsubscribeEventClicked">Action to inform when the Unsubscribe button has been clicked.</par
 99    public static void ConfigureEventCard(
 100        EventCardComponentView eventCard,
 101        EventCardComponentModel eventInfo,
 102        Action<EventCardComponentModel> OnEventInfoClicked,
 103        Action<EventFromAPIModel> OnEventJumpInClicked,
 104        Action<string> OnEventSubscribeEventClicked,
 105        Action<string> OnEventUnsubscribeEventClicked)
 106    {
 17107        eventCard.Configure(eventInfo);
 17108        eventCard.onInfoClick?.RemoveAllListeners();
 17109        eventCard.onInfoClick?.AddListener(() => OnEventInfoClicked?.Invoke(eventInfo));
 17110        eventCard.onJumpInClick?.RemoveAllListeners();
 17111        eventCard.onJumpInClick?.AddListener(() => OnEventJumpInClicked?.Invoke(eventInfo.eventFromAPIInfo));
 17112        eventCard.onSubscribeClick?.RemoveAllListeners();
 17113        eventCard.onSubscribeClick?.AddListener(() => OnEventSubscribeEventClicked?.Invoke(eventInfo.eventId));
 17114        eventCard.onUnsubscribeClick?.RemoveAllListeners();
 17115        eventCard.onUnsubscribeClick?.AddListener(() => OnEventUnsubscribeEventClicked?.Invoke(eventInfo.eventId));
 17116    }
 117
 118    /// <summary>
 119    /// Returs a event card model from the given API data.
 120    /// </summary>
 121    /// <param name="eventFromAPI">Data received from the API.</param>
 122    /// <returns>An event card model.</returns>
 123    public static EventCardComponentModel CreateEventCardModelFromAPIEvent(EventFromAPIModel eventFromAPI)
 124    {
 9125        EventCardComponentModel eventCardModel = new EventCardComponentModel();
 9126        eventCardModel.eventId = eventFromAPI.id;
 9127        eventCardModel.eventPictureUri = eventFromAPI.image;
 9128        eventCardModel.isLive = eventFromAPI.live;
 9129        eventCardModel.liveTagText = LIVE_TAG_TEXT;
 9130        eventCardModel.eventDateText = FormatEventDate(eventFromAPI);
 9131        eventCardModel.eventName = eventFromAPI.name;
 9132        eventCardModel.eventDescription = eventFromAPI.description;
 9133        eventCardModel.eventStartedIn = FormatEventStartDate(eventFromAPI);
 9134        eventCardModel.eventStartsInFromTo = FormatEventStartDateFromTo(eventFromAPI);
 9135        eventCardModel.eventOrganizer = FormatEventOrganized(eventFromAPI);
 9136        eventCardModel.eventPlace = FormatEventPlace(eventFromAPI);
 9137        eventCardModel.subscribedUsers = eventFromAPI.total_attendees;
 9138        eventCardModel.isSubscribed = false;
 9139        eventCardModel.coords = new Vector2Int(eventFromAPI.coordinates[0], eventFromAPI.coordinates[1]);
 9140        eventCardModel.eventFromAPIInfo = eventFromAPI;
 141
 142        // Card events
 9143        return eventCardModel;
 144    }
 145
 146    internal static string FormatEventDate(EventFromAPIModel eventFromAPI)
 147    {
 10148        DateTime eventDateTime = Convert.ToDateTime(eventFromAPI.next_start_at).ToUniversalTime();
 10149        return eventDateTime.ToString("MMMM d", new CultureInfo("en-US"));
 150    }
 151
 152    internal static string FormatEventStartDate(EventFromAPIModel eventFromAPI)
 153    {
 10154        DateTime eventDateTime = Convert.ToDateTime(eventFromAPI.next_start_at).ToUniversalTime();
 155        string formattedDate;
 10156        if (eventFromAPI.live)
 157        {
 10158            int daysAgo = (int)Math.Ceiling((DateTime.Now - eventDateTime).TotalDays);
 10159            int hoursAgo = (int)Math.Ceiling((DateTime.Now - eventDateTime).TotalHours);
 160
 10161            if (daysAgo > 0)
 10162                formattedDate = $"{daysAgo} days ago";
 163            else
 0164                formattedDate = $"{hoursAgo} hr ago";
 0165        }
 166        else
 167        {
 0168            int daysToStart = (int)Math.Ceiling((eventDateTime - DateTime.Now).TotalDays);
 0169            int hoursToStart = (int)Math.Ceiling((eventDateTime - DateTime.Now).TotalHours);
 170
 0171            if (daysToStart > 0)
 0172                formattedDate = $"in {daysToStart} days";
 173            else
 0174                formattedDate = $"in {hoursToStart} hours";
 175        }
 176
 10177        return formattedDate;
 178    }
 179
 180    internal static string FormatEventStartDateFromTo(EventFromAPIModel eventFromAPI)
 181    {
 10182        CultureInfo cultureInfo = new CultureInfo("en-US");
 10183        DateTime eventStartDateTime = Convert.ToDateTime(eventFromAPI.next_start_at).ToUniversalTime();
 10184        DateTime eventEndDateTime = Convert.ToDateTime(eventFromAPI.finish_at).ToUniversalTime();
 10185        string formattedDate = $"From {eventStartDateTime.ToString("dddd", cultureInfo)}, {eventStartDateTime.Day} {even
 186                               $" to {eventEndDateTime.ToString("dddd", cultureInfo)}, {eventEndDateTime.Day} {eventEndD
 187
 10188        return formattedDate;
 189    }
 190
 10191    internal static string FormatEventOrganized(EventFromAPIModel eventFromAPI) { return $"Public, Organized by {eventFr
 192
 10193    internal static string FormatEventPlace(EventFromAPIModel eventFromAPI) { return string.IsNullOrEmpty(eventFromAPI.s
 194
 195    /// <summary>
 196    /// Makes a jump in to the event defined by the given place data from API.
 197    /// </summary>
 198    /// <param name="eventFromAPI">Event data from API.</param>
 199    public static void JumpInToEvent(EventFromAPIModel eventFromAPI)
 200    {
 2201        Vector2Int coords = new Vector2Int(eventFromAPI.coordinates[0], eventFromAPI.coordinates[1]);
 2202        string[] realmFromAPI = string.IsNullOrEmpty(eventFromAPI.realm) ? new string[] { "", "" } : eventFromAPI.realm.
 2203        string serverName = realmFromAPI[0];
 2204        string layerName = realmFromAPI[1];
 205
 2206        if (string.IsNullOrEmpty(serverName))
 2207            WebInterface.GoTo(coords.x, coords.y);
 208        else
 0209            WebInterface.JumpIn(coords.x, coords.y, serverName, layerName);
 0210    }
 211}