< Summary

Class:EventsSubSectionComponentController
Assembly:ExploreV2
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/ExploreV2/Scripts/Sections/PlacesAndEventsSection/SubSections/EventsSubSection/EventsSubSectionMenu/EventsSubSectionComponentController.cs
Covered lines:69
Uncovered lines:16
Coverable lines:85
Total lines:216
Line coverage:81.1% (69 of 85)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
EventsSubSectionComponentController(...)0%110100%
Dispose()0%110100%
FirstLoading()0%2100%
RequestAllEvents()0%220100%
RequestAllFromAPI()0%220100%
OnRequestedEventsUpdated(...)0%110100%
FilterFeaturedEvents()0%330100%
FilterTrendingEvents()0%220100%
FilterUpcomingEvents()0%110100%
FilterGoingEvents()0%220100%
ShowMoreUpcomingEvents()0%3.013088.89%
ShowEventDetailedInfo(...)0%110100%
OnJumpInToEvent(...)0%220100%
SubscribeToEvent(...)0%2100%
UnsubscribeToEvent(...)0%2100%
OnChannelToJoinChanged(...)0%12300%
JumpInToEvent(...)0%3.143075%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/ExploreV2/Scripts/Sections/PlacesAndEventsSection/SubSections/EventsSubSection/EventsSubSectionMenu/EventsSubSectionComponentController.cs

#LineLine coverage
 1using DCL;
 2using DCL.Interface;
 3using ExploreV2Analytics;
 4using System;
 5using System.Collections.Generic;
 6using System.Linq;
 7using UnityEngine;
 8using Environment = DCL.Environment;
 9
 10public class EventsSubSectionComponentController : IEventsSubSectionComponentController, IPlacesAndEventsAPIRequester
 11{
 12    public event Action OnCloseExploreV2;
 13
 14    private const int DEFAULT_NUMBER_OF_FEATURED_EVENTS = 3;
 15    internal const int INITIAL_NUMBER_OF_UPCOMING_ROWS = 1;
 16    private const int SHOW_MORE_UPCOMING_ROWS_INCREMENT = 2;
 17    private const string EVENT_DETAIL_URL = "https://events.decentraland.org/event/?id={0}";
 18
 19    internal readonly IEventsSubSectionComponentView view;
 20    internal readonly IEventsAPIController eventsAPIApiController;
 21    private readonly DataStore dataStore;
 22    private readonly IExploreV2Analytics exploreV2Analytics;
 23
 24    internal readonly PlaceAndEventsCardsReloader cardsReloader;
 25
 2126    internal List<EventFromAPIModel> eventsFromAPI = new ();
 27    internal int availableUISlots;
 28
 2129    public EventsSubSectionComponentController(IEventsSubSectionComponentView view, IEventsAPIController eventsAPI, IExp
 30    {
 2131        cardsReloader = new PlaceAndEventsCardsReloader(view, this, dataStore.exploreV2);
 32
 2133        this.view = view;
 34
 2135        this.view.OnReady += FirstLoading;
 36
 2137        this.view.OnInfoClicked += ShowEventDetailedInfo;
 2138        this.view.OnJumpInClicked += OnJumpInToEvent;
 39
 2140        this.view.OnSubscribeEventClicked += SubscribeToEvent;
 2141        this.view.OnUnsubscribeEventClicked += UnsubscribeToEvent;
 42
 2143        this.view.OnShowMoreUpcomingEventsClicked += ShowMoreUpcomingEvents;
 44
 2145        this.dataStore = dataStore;
 2146        this.dataStore.channels.currentJoinChannelModal.OnChange += OnChannelToJoinChanged;
 47
 2148        eventsAPIApiController = eventsAPI;
 49
 2150        this.exploreV2Analytics = exploreV2Analytics;
 51
 2152        view.ConfigurePools();
 2153    }
 54
 55    public void Dispose()
 56    {
 2157        view.OnReady -= FirstLoading;
 58
 2159        view.OnInfoClicked -= ShowEventDetailedInfo;
 2160        view.OnJumpInClicked -= OnJumpInToEvent;
 2161        view.OnSubscribeEventClicked -= SubscribeToEvent;
 2162        view.OnUnsubscribeEventClicked -= UnsubscribeToEvent;
 2163        view.OnShowMoreUpcomingEventsClicked -= ShowMoreUpcomingEvents;
 2164        view.OnEventsSubSectionEnable -= RequestAllEvents;
 65
 2166        dataStore.channels.currentJoinChannelModal.OnChange -= OnChannelToJoinChanged;
 67
 2168        cardsReloader.Dispose();
 2169    }
 70
 71    private void FirstLoading()
 72    {
 073        view.OnEventsSubSectionEnable += RequestAllEvents;
 074        cardsReloader.Initialize();
 075    }
 76
 77    internal void RequestAllEvents()
 78    {
 279        if (cardsReloader.CanReload())
 80        {
 281            availableUISlots = view.CurrentTilesPerRow * INITIAL_NUMBER_OF_UPCOMING_ROWS;
 282            view.SetShowMoreButtonActive(false);
 83
 284            cardsReloader.RequestAll();
 85        }
 286    }
 87
 88    public void RequestAllFromAPI()
 89    {
 390        eventsAPIApiController.GetAllEvents(
 91            OnSuccess: OnRequestedEventsUpdated,
 092            OnFail: error => { Debug.LogError($"Error receiving events from the API: {error}"); });
 393    }
 94
 95    internal void OnRequestedEventsUpdated(List<EventFromAPIModel> eventList)
 96    {
 197        eventsFromAPI = eventList;
 98
 199        view.SetFeaturedEvents(PlacesAndEventsCardsFactory.CreateEventsCards(FilterFeaturedEvents()));
 1100        view.SetTrendingEvents(PlacesAndEventsCardsFactory.CreateEventsCards(FilterTrendingEvents()));
 1101        view.SetUpcomingEvents(PlacesAndEventsCardsFactory.CreateEventsCards(FilterUpcomingEvents()));
 1102        view.SetGoingEvents(PlacesAndEventsCardsFactory.CreateEventsCards(FilterGoingEvents()));
 103
 1104        view.SetShowMoreUpcomingEventsButtonActive(availableUISlots < eventsFromAPI.Count);
 1105    }
 106
 107    internal List<EventFromAPIModel> FilterFeaturedEvents()
 108    {
 6109        List<EventFromAPIModel> eventsFiltered = eventsFromAPI.Where(e => e.highlighted).ToList();
 110
 2111        if (eventsFiltered.Count == 0)
 2112            eventsFiltered = eventsFromAPI.Take(DEFAULT_NUMBER_OF_FEATURED_EVENTS).ToList();
 113
 2114        return eventsFiltered;
 115    }
 6116    internal List<EventFromAPIModel> FilterTrendingEvents() => eventsFromAPI.Where(e => e.trending).ToList();
 2117    internal List<EventFromAPIModel> FilterUpcomingEvents() => eventsFromAPI.Take(availableUISlots).ToList();
 6118    internal List<EventFromAPIModel> FilterGoingEvents() => eventsFromAPI.Where(e => e.attending).ToList();
 119
 120    public void ShowMoreUpcomingEvents()
 121    {
 2122        int numberOfExtraItemsToAdd = ((int)Mathf.Ceil((float)availableUISlots / view.currentUpcomingEventsPerRow) * vie
 2123        int numberOfItemsToAdd = (view.currentUpcomingEventsPerRow * SHOW_MORE_UPCOMING_ROWS_INCREMENT) + numberOfExtraI
 124
 2125        List<EventFromAPIModel> eventsFiltered = availableUISlots + numberOfItemsToAdd <= eventsFromAPI.Count
 126            ? eventsFromAPI.GetRange(availableUISlots, numberOfItemsToAdd)
 127            : eventsFromAPI.GetRange(availableUISlots, eventsFromAPI.Count - availableUISlots);
 128
 2129        view.AddUpcomingEvents(PlacesAndEventsCardsFactory.CreateEventsCards(eventsFiltered));
 130
 2131        availableUISlots += numberOfItemsToAdd;
 2132        if (availableUISlots > eventsFromAPI.Count)
 0133            availableUISlots = eventsFromAPI.Count;
 134
 2135        view.SetShowMoreUpcomingEventsButtonActive(availableUISlots < eventsFromAPI.Count);
 2136    }
 137
 138    internal void ShowEventDetailedInfo(EventCardComponentModel eventModel)
 139    {
 1140        view.ShowEventModal(eventModel);
 1141        exploreV2Analytics.SendClickOnEventInfo(eventModel.eventId, eventModel.eventName);
 1142    }
 143
 144    internal void OnJumpInToEvent(EventFromAPIModel eventFromAPI)
 145    {
 1146        JumpInToEvent(eventFromAPI);
 1147        view.HideEventModal();
 148
 1149        OnCloseExploreV2?.Invoke();
 1150        exploreV2Analytics.SendEventTeleport(eventFromAPI.id, eventFromAPI.name, new Vector2Int(eventFromAPI.coordinates
 1151    }
 152
 153    public static void SubscribeToEvent(string eventId)
 154    {
 155        // TODO (Santi): Remove when the RegisterAttendEvent POST is available.
 0156        WebInterface.OpenURL(string.Format(EVENT_DETAIL_URL, eventId));
 157
 158        // TODO (Santi): Waiting for the new version of the Events API where we will be able to send a signed POST to re
 159        //eventsAPIApiController.RegisterAttendEvent(
 160        //    eventId,
 161        //    true,
 162        //    () =>
 163        //    {
 164        //        // ...
 165        //    },
 166        //    (error) =>
 167        //    {
 168        //        Debug.LogError($"Error posting 'attend' message to the API: {error}");
 169        //    });
 0170    }
 171
 172    public static void UnsubscribeToEvent(string eventId)
 173    {
 174        // TODO (Santi): Remove when the RegisterAttendEvent POST is available.
 0175        WebInterface.OpenURL(string.Format(EVENT_DETAIL_URL, eventId));
 176
 177        // TODO (Santi): Waiting for the new version of the Events API where we will be able to send a signed POST to un
 178        //eventsAPIApiController.RegisterAttendEvent(
 179        //    eventId,
 180        //    false,
 181        //    () =>
 182        //    {
 183        //        // ...
 184        //    },
 185        //    (error) =>
 186        //    {
 187        //        Debug.LogError($"Error posting 'attend' message to the API: {error}");
 188        //    });
 0189    }
 190
 191    private void OnChannelToJoinChanged(string currentChannelId, string previousChannelId)
 192    {
 0193        if (!string.IsNullOrEmpty(currentChannelId))
 0194            return;
 195
 0196        view.HideEventModal();
 0197        OnCloseExploreV2?.Invoke();
 0198    }
 199
 200    /// <summary>
 201    /// Makes a jump in to the event defined by the given place data from API.
 202    /// </summary>
 203    /// <param name="eventFromAPI">Event data from API.</param>
 204    public static void JumpInToEvent(EventFromAPIModel eventFromAPI)
 205    {
 2206        Vector2Int coords = new Vector2Int(eventFromAPI.coordinates[0], eventFromAPI.coordinates[1]);
 2207        string[] realmFromAPI = string.IsNullOrEmpty(eventFromAPI.realm) ? new[] { "", "" } : eventFromAPI.realm.Split('
 2208        string serverName = realmFromAPI[0];
 2209        string layerName = realmFromAPI[1];
 210
 2211        if (string.IsNullOrEmpty(serverName))
 2212            Environment.i.world.teleportController.Teleport(coords.x, coords.y);
 213        else
 0214            Environment.i.world.teleportController.JumpIn(coords.x, coords.y, serverName, layerName);
 0215    }
 216}