< Summary

Class:EventsCardsConfigurator
Assembly:ExploreV2
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/ExploreV2/Scripts/Sections/PlacesAndEventsSection/EventsCardsConfigurator.cs
Covered lines:63
Uncovered lines:23
Coverable lines:86
Total lines:195
Line coverage:73.2% (63 of 86)
Covered branches:0
Total branches:0
Covered methods:9
Total methods:9
Method coverage:100% (9 of 9)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Configure(...)0%11110100%
ConfigureFromAPIData(...)0%440100%
GetNumberOfUsersInCoords(...)0%20.287035.29%
GetNumberOfUsersInWorld(...)0%8.125050%
FormatEventDate(...)0%110100%
FormatEventStartDate(...)0%5.574053.85%
FormatEventStartDateFromTo(...)0%10.279075%
FormatEventOrganized(...)0%110100%
FormatEventPlace(...)0%220100%

File(s)

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

#LineLine coverage
 1using DCL;
 2using MainScripts.DCL.Controllers.HotScenes;
 3using System;
 4using System.Globalization;
 5using UnityEngine;
 6using Environment = DCL.Environment;
 7
 8/// <summary>
 9/// Utils related to the events management in ExploreV2.
 10/// </summary>
 11public static class EventsCardsConfigurator
 12{
 13    internal const string LIVE_TAG_TEXT = "LIVE";
 14    private static Service<IHotScenesFetcher> hotScenesFetcher;
 15
 16    /// <summary>
 17    /// Configure a event card with the given model.
 18    /// </summary>
 19    /// <param name="eventCard">Event card to configure.</param>
 20    /// <param name="eventInfo">Model to apply.</param>
 21    /// <param name="OnEventInfoClicked">Action to inform when the Info button has been clicked.</param>
 22    /// <param name="OnEventJumpInClicked">Action to inform when the JumpIn button has been clicked.</param>
 23    /// <param name="OnEventSubscribeEventClicked">Action to inform when the Subscribe button has been clicked.</param>
 24    /// <param name="OnEventUnsubscribeEventClicked">Action to inform when the Unsubscribe button has been clicked.</par
 25    public static EventCardComponentView Configure(EventCardComponentView eventCard, EventCardComponentModel eventInfo, 
 26        Action<string, bool> OnEventUnsubscribeEventClicked)
 27    {
 828        eventCard.Configure(eventInfo);
 29
 830        eventCard.onInfoClick?.RemoveAllListeners();
 831        eventCard.onInfoClick?.AddListener(() => OnEventInfoClicked?.Invoke(eventInfo));
 832        eventCard.onBackgroundClick?.RemoveAllListeners();
 833        eventCard.onBackgroundClick?.AddListener(() => OnEventInfoClicked?.Invoke(eventInfo));
 834        eventCard.onJumpInClick?.RemoveAllListeners();
 835        eventCard.onJumpInClick?.AddListener(() => OnEventJumpInClicked?.Invoke(eventInfo.eventFromAPIInfo));
 836        eventCard.onSecondaryJumpInClick?.RemoveAllListeners();
 837        eventCard.onSecondaryJumpInClick?.AddListener(() => OnEventJumpInClicked?.Invoke(eventInfo.eventFromAPIInfo));
 838        eventCard.onSubscribeClick?.AddListener(() => OnEventSubscribeEventClicked?.Invoke(eventInfo.eventId, !string.Is
 839        eventCard.onUnsubscribeClick?.AddListener(() => OnEventUnsubscribeEventClicked?.Invoke(eventInfo.eventId, !strin
 40
 841        return eventCard;
 42    }
 43
 44    public static EventCardComponentModel ConfigureFromAPIData(EventCardComponentModel cardModel, EventFromAPIModel even
 45    {
 846        cardModel.eventId = eventFromAPI.id;
 847        cardModel.eventPictureUri = eventFromAPI.image;
 848        cardModel.isLive = eventFromAPI.live;
 849        cardModel.liveTagText = LIVE_TAG_TEXT;
 850        cardModel.eventDateText = FormatEventDate(eventFromAPI);
 851        cardModel.eventName = eventFromAPI.name;
 852        cardModel.eventDescription = eventFromAPI.description;
 853        cardModel.eventStartedIn = FormatEventStartDate(eventFromAPI);
 854        cardModel.eventStartsInFromTo = FormatEventStartDateFromTo(eventFromAPI);
 855        cardModel.eventOrganizer = FormatEventOrganized(eventFromAPI);
 856        cardModel.eventPlace = FormatEventPlace(eventFromAPI);
 857        cardModel.subscribedUsers = eventFromAPI.total_attendees;
 858        cardModel.isSubscribed = false;
 859        cardModel.coords = new Vector2Int(eventFromAPI.coordinates[0], eventFromAPI.coordinates[1]);
 860        cardModel.eventFromAPIInfo = eventFromAPI;
 861        cardModel.numberOfUsers = eventFromAPI.world ? GetNumberOfUsersInWorld(eventFromAPI.server) : GetNumberOfUsersIn
 862        cardModel.worldAddress = eventFromAPI.world ? eventFromAPI.server : null;
 63
 864        return cardModel;
 65    }
 66
 67    private static int GetNumberOfUsersInCoords(Vector2Int coords)
 68    {
 769        var numberOfUsers = 0;
 70
 771        if (hotScenesFetcher.Ref.ScenesInfo == null)
 072            return numberOfUsers;
 73
 774        var sceneFound = false;
 1475        foreach (var hotSceneInfo in hotScenesFetcher.Ref.ScenesInfo.Value)
 76        {
 077            foreach (Vector2Int hotSceneParcel in hotSceneInfo.parcels)
 78            {
 079                if (hotSceneParcel != coords)
 80                    continue;
 81
 082                numberOfUsers = hotSceneInfo.usersTotalCount;
 083                sceneFound = true;
 084                break;
 85            }
 86
 087            if (sceneFound)
 088                break;
 89        }
 90
 791        return numberOfUsers;
 92    }
 93
 94    private static int GetNumberOfUsersInWorld(string worldName)
 95    {
 196        var numberOfUsers = 0;
 97
 198        if (hotScenesFetcher.Ref.WorldsInfo == null)
 099            return numberOfUsers;
 100
 2101        foreach (var worldInfo in hotScenesFetcher.Ref.WorldsInfo.Value)
 102        {
 0103            if (worldInfo.worldName == worldName)
 104            {
 0105                numberOfUsers = worldInfo.users;
 0106                break;
 107            }
 108        }
 109
 1110        return numberOfUsers;
 111    }
 112
 113    internal static string FormatEventDate(EventFromAPIModel eventFromAPI)
 114    {
 10115        DateTime eventDateTime = Convert.ToDateTime(eventFromAPI.next_start_at).ToUniversalTime();
 10116        return eventDateTime.ToString("MMMM d", new CultureInfo("en-US"));
 117    }
 118
 119    internal static string FormatEventStartDate(EventFromAPIModel eventFromAPI)
 120    {
 10121        DateTime eventDateTime = Convert.ToDateTime(eventFromAPI.next_start_at).ToUniversalTime();
 122        string formattedDate;
 123
 10124        if (eventFromAPI.live)
 125        {
 10126            int daysAgo = (int)Math.Ceiling((DateTime.Now - eventDateTime).TotalDays);
 10127            int hoursAgo = (int)Math.Ceiling((DateTime.Now - eventDateTime).TotalHours);
 128
 10129            if (daysAgo > 0)
 10130                formattedDate = $"{daysAgo} days ago";
 131            else
 0132                formattedDate = $"{hoursAgo} hr ago";
 133        }
 134        else
 135        {
 0136            int daysToStart = (int)Math.Ceiling((eventDateTime - DateTime.Now).TotalDays);
 0137            int hoursToStart = (int)Math.Ceiling((eventDateTime - DateTime.Now).TotalHours);
 138
 0139            if (daysToStart > 0)
 0140                formattedDate = $"in {daysToStart} days";
 141            else
 0142                formattedDate = $"in {hoursToStart} hours";
 143        }
 144
 10145        return formattedDate;
 146    }
 147
 148    internal static string FormatEventStartDateFromTo(EventFromAPIModel eventFromAPI)
 149    {
 10150        CultureInfo cultureInfo = new CultureInfo("en-US");
 10151        string formattedDate = string.Empty;
 152
 10153        DateTime startTimeDT = Convert.ToDateTime(eventFromAPI.start_at).ToLocalTime();
 10154        var startTime12Hour = startTimeDT.ToString("hh:mmtt", CultureInfo.InvariantCulture);
 10155        startTime12Hour = startTime12Hour.Replace("AM", "am").Replace("PM", "pm");
 156
 10157        DateTime endTimeDT = Convert.ToDateTime(eventFromAPI.finish_at).ToLocalTime();
 10158        var endTime12Hour = endTimeDT.ToString("hh:mmtt", CultureInfo.InvariantCulture);
 10159        endTime12Hour = endTime12Hour.Replace("AM", "am").Replace("PM", "pm");
 160
 10161        TimeSpan startUtcOffset = TimeZoneInfo.Local.GetUtcOffset(startTimeDT);
 162
 40163        for (var i = 0; i < eventFromAPI.recurrent_dates.Length; i++)
 164        {
 10165            DateTime recurrentDateDT = Convert.ToDateTime(eventFromAPI.recurrent_dates[i]).ToLocalTime();
 166
 10167            if (recurrentDateDT < DateTime.Today)
 168                continue;
 169
 0170            var formattedRecurrentDate = $"{recurrentDateDT.ToString("dddd", cultureInfo)}, {recurrentDateDT.ToString("M
 0171            if (i == eventFromAPI.recurrent_dates.Length - 1 && endTimeDT.DayOfYear > recurrentDateDT.DayOfYear)
 172            {
 0173                var formattedEndDate = $"{endTimeDT.ToString("dddd", cultureInfo)}, {endTimeDT.ToString("MMM", cultureIn
 174
 0175                formattedDate = string.Concat(
 176                    formattedDate,
 177                    $"{formattedRecurrentDate} from {startTime12Hour} to {formattedEndDate} at {endTime12Hour} (UTC{(sta
 178            }
 179            else
 180            {
 0181                formattedDate = string.Concat(
 182                    formattedDate,
 183                    $"{formattedRecurrentDate} from {startTime12Hour} to {endTime12Hour} (UTC{(startUtcOffset >= TimeSpa
 184            }
 185        }
 186
 10187        return formattedDate;
 188    }
 189
 190    internal static string FormatEventOrganized(EventFromAPIModel eventFromAPI) =>
 10191        $"Public, Organized by {eventFromAPI.user_name}";
 192
 193    internal static string FormatEventPlace(EventFromAPIModel eventFromAPI) =>
 10194        string.IsNullOrEmpty(eventFromAPI.scene_name) ? "Decentraland" : eventFromAPI.scene_name;
 195}