< 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:100
Uncovered lines:30
Coverable lines:130
Total lines:313
Line coverage:76.9% (100 of 130)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
EventsSubSectionComponentController(...)0%110100%
FirstLoading()0%110100%
OnExploreV2Open(...)0%6200%
RequestAllEvents()0%4.214076.47%
IsInShowAnimationTransitonChanged(...)0%2100%
RequestAllEventsFromAPI()0%220100%
OnRequestedEventsUpdated()0%110100%
LoadFeaturedEvents()0%440100%
LoadTrendingEvents()0%3.243070%
LoadUpcomingEvents()0%2.082072.73%
ShowMoreUpcomingEvents()0%4.344072.22%
LoadGoingEvents()0%3.243070%
Dispose()0%110100%
ShowEventDetailedInfo(...)0%110100%
JumpInToEvent(...)0%220100%
SubscribeToEvent(...)0%2100%
UnsubscribeToEvent(...)0%2100%

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;
 8
 9public interface IEventsSubSectionComponentController : IDisposable
 10{
 11    /// <summary>
 12    /// It will be triggered when the sub-section want to request to close the ExploreV2 main menu.
 13    /// </summary>
 14    event Action OnCloseExploreV2;
 15
 16    /// <summary>
 17    /// Request all events from the API.
 18    /// </summary>
 19    void RequestAllEvents();
 20
 21    /// <summary>
 22    /// Load the featured events with the last requested ones.
 23    /// </summary>
 24    void LoadFeaturedEvents();
 25
 26    /// <summary>
 27    /// Load the trending events with the last requested ones.
 28    /// </summary>
 29    void LoadTrendingEvents();
 30
 31    /// <summary>
 32    /// Load the upcoming events with the last requested ones.
 33    /// </summary>
 34    void LoadUpcomingEvents();
 35
 36    /// <summary>
 37    /// Increment the number of upcoming events loaded.
 38    /// </summary>
 39    void ShowMoreUpcomingEvents();
 40
 41    /// <summary>
 42    /// Load the going events with the last requested ones.
 43    /// </summary>
 44    void LoadGoingEvents();
 45}
 46
 47public class EventsSubSectionComponentController : IEventsSubSectionComponentController
 48{
 49    public event Action OnCloseExploreV2;
 50    internal event Action OnEventsFromAPIUpdated;
 51
 52    internal const int DEFAULT_NUMBER_OF_FEATURED_EVENTS = 3;
 53    internal const int INITIAL_NUMBER_OF_UPCOMING_ROWS = 1;
 54    internal const int SHOW_MORE_UPCOMING_ROWS_INCREMENT = 2;
 55    internal const string EVENT_DETAIL_URL = "https://events.decentraland.org/event/?id={0}";
 56    internal IEventsSubSectionComponentView view;
 57    internal IEventsAPIController eventsAPIApiController;
 2158    internal List<EventFromAPIModel> eventsFromAPI = new List<EventFromAPIModel>();
 59    internal int currentUpcomingEventsShowed = 0;
 60    internal bool reloadEvents = false;
 61    internal IExploreV2Analytics exploreV2Analytics;
 62    internal float lastTimeAPIChecked = 0;
 63
 2164    public EventsSubSectionComponentController(
 65        IEventsSubSectionComponentView view,
 66        IEventsAPIController eventsAPI,
 67        IExploreV2Analytics exploreV2Analytics)
 68    {
 2169        this.view = view;
 2170        this.view.OnReady += FirstLoading;
 2171        this.view.OnInfoClicked += ShowEventDetailedInfo;
 2172        this.view.OnJumpInClicked += JumpInToEvent;
 2173        this.view.OnSubscribeEventClicked += SubscribeToEvent;
 2174        this.view.OnUnsubscribeEventClicked += UnsubscribeToEvent;
 2175        this.view.OnShowMoreUpcomingEventsClicked += ShowMoreUpcomingEvents;
 76
 2177        eventsAPIApiController = eventsAPI;
 2178        OnEventsFromAPIUpdated += OnRequestedEventsUpdated;
 79
 2180        this.exploreV2Analytics = exploreV2Analytics;
 81
 2182        view.ConfigurePools();
 2183    }
 84
 85    internal void FirstLoading()
 86    {
 187        reloadEvents = true;
 188        lastTimeAPIChecked = Time.realtimeSinceStartup - PlacesAndEventsSectionComponentController.MIN_TIME_TO_CHECK_API
 189        RequestAllEvents();
 90
 191        view.OnEventsSubSectionEnable += RequestAllEvents;
 192        DataStore.i.exploreV2.isOpen.OnChange += OnExploreV2Open;
 193    }
 94
 95    internal void OnExploreV2Open(bool current, bool previous)
 96    {
 097        if (current)
 098            return;
 99
 0100        reloadEvents = true;
 0101    }
 102
 103    public void RequestAllEvents()
 104    {
 2105        if (!reloadEvents)
 0106            return;
 107
 2108        view.RestartScrollViewPosition();
 109
 2110        if (Time.realtimeSinceStartup < lastTimeAPIChecked + PlacesAndEventsSectionComponentController.MIN_TIME_TO_CHECK
 0111            return;
 112
 2113        currentUpcomingEventsShowed = view.currentUpcomingEventsPerRow * INITIAL_NUMBER_OF_UPCOMING_ROWS;
 2114        view.SetFeaturedEventsAsLoading(true);
 2115        view.SetTrendingEventsAsLoading(true);
 2116        view.SetUpcomingEventsAsLoading(true);
 2117        view.SetShowMoreUpcomingEventsButtonActive(false);
 2118        view.SetGoingEventsAsLoading(true);
 119
 2120        reloadEvents = false;
 2121        lastTimeAPIChecked = Time.realtimeSinceStartup;
 122
 2123        if (!DataStore.i.exploreV2.isInShowAnimationTransiton.Get())
 2124            RequestAllEventsFromAPI();
 125        else
 0126            DataStore.i.exploreV2.isInShowAnimationTransiton.OnChange += IsInShowAnimationTransitonChanged;
 0127    }
 128
 129    internal void IsInShowAnimationTransitonChanged(bool current, bool previous)
 130    {
 0131        DataStore.i.exploreV2.isInShowAnimationTransiton.OnChange -= IsInShowAnimationTransitonChanged;
 0132        RequestAllEventsFromAPI();
 0133    }
 134
 135    internal void RequestAllEventsFromAPI()
 136    {
 3137        eventsAPIApiController.GetAllEvents(
 138            (eventList) =>
 139            {
 0140                eventsFromAPI = eventList;
 0141                OnEventsFromAPIUpdated?.Invoke();
 0142            },
 143            (error) =>
 144            {
 0145                Debug.LogError($"Error receiving events from the API: {error}");
 0146            });
 3147    }
 148
 149    internal void OnRequestedEventsUpdated()
 150    {
 1151        LoadFeaturedEvents();
 1152        LoadTrendingEvents();
 1153        LoadUpcomingEvents();
 1154        LoadGoingEvents();
 1155    }
 156
 157    public void LoadFeaturedEvents()
 158    {
 2159        List<EventCardComponentModel> featuredEvents = new List<EventCardComponentModel>();
 6160        List<EventFromAPIModel> eventsFiltered = eventsFromAPI.Where(e => e.highlighted).ToList();
 161
 2162        if (eventsFiltered.Count == 0)
 2163            eventsFiltered = eventsFromAPI.Take(DEFAULT_NUMBER_OF_FEATURED_EVENTS).ToList();
 164
 12165        foreach (EventFromAPIModel receivedEvent in eventsFiltered)
 166        {
 4167            EventCardComponentModel eventCardModel = ExploreEventsUtils.CreateEventCardModelFromAPIEvent(receivedEvent);
 4168            featuredEvents.Add(eventCardModel);
 169        }
 170
 2171        view.SetFeaturedEvents(featuredEvents);
 2172        view.SetFeaturedEventsAsLoading(false);
 2173        view.SetFeaturedEventsActive(featuredEvents.Count > 0);
 2174    }
 175
 176    public void LoadTrendingEvents()
 177    {
 2178        List<EventCardComponentModel> trendingEvents = new List<EventCardComponentModel>();
 6179        List<EventFromAPIModel> eventsFiltered = eventsFromAPI.Where(e => e.trending).ToList();
 180
 4181        foreach (EventFromAPIModel receivedEvent in eventsFiltered)
 182        {
 0183            EventCardComponentModel eventCardModel = ExploreEventsUtils.CreateEventCardModelFromAPIEvent(receivedEvent);
 0184            trendingEvents.Add(eventCardModel);
 185        }
 186
 2187        view.SetTrendingEvents(trendingEvents);
 2188        view.SetTrendingEventsAsLoading(false);
 2189    }
 190
 191    public void LoadUpcomingEvents()
 192    {
 2193        List<EventCardComponentModel> upcomingEvents = new List<EventCardComponentModel>();
 2194        List<EventFromAPIModel> eventsFiltered = eventsFromAPI.Take(currentUpcomingEventsShowed).ToList();
 195
 4196        foreach (EventFromAPIModel receivedEvent in eventsFiltered)
 197        {
 0198            EventCardComponentModel eventCardModel = ExploreEventsUtils.CreateEventCardModelFromAPIEvent(receivedEvent);
 0199            upcomingEvents.Add(eventCardModel);
 200        }
 201
 2202        view.SetUpcomingEvents(upcomingEvents);
 2203        view.SetShowMoreUpcomingEventsButtonActive(currentUpcomingEventsShowed < eventsFromAPI.Count);
 2204        view.SetUpcomingEventsAsLoading(false);
 2205    }
 206
 207    public void ShowMoreUpcomingEvents()
 208    {
 2209        List<EventCardComponentModel> upcomingEvents = new List<EventCardComponentModel>();
 2210        List<EventFromAPIModel> eventsFiltered = new List<EventFromAPIModel>();
 2211        int numberOfExtraItemsToAdd = ((int)Mathf.Ceil((float)currentUpcomingEventsShowed / view.currentUpcomingEventsPe
 2212        int numberOfItemsToAdd = view.currentUpcomingEventsPerRow * SHOW_MORE_UPCOMING_ROWS_INCREMENT + numberOfExtraIte
 213
 2214        if (currentUpcomingEventsShowed + numberOfItemsToAdd <= eventsFromAPI.Count)
 2215            eventsFiltered = eventsFromAPI.GetRange(currentUpcomingEventsShowed, numberOfItemsToAdd);
 216        else
 0217            eventsFiltered = eventsFromAPI.GetRange(currentUpcomingEventsShowed, eventsFromAPI.Count - currentUpcomingEv
 218
 4219        foreach (EventFromAPIModel receivedEvent in eventsFiltered)
 220        {
 0221            EventCardComponentModel placeCardModel = ExploreEventsUtils.CreateEventCardModelFromAPIEvent(receivedEvent);
 0222            upcomingEvents.Add(placeCardModel);
 223        }
 224
 2225        view.AddUpcomingEvents(upcomingEvents);
 226
 2227        currentUpcomingEventsShowed += numberOfItemsToAdd;
 2228        if (currentUpcomingEventsShowed > eventsFromAPI.Count)
 0229            currentUpcomingEventsShowed = eventsFromAPI.Count;
 230
 2231        view.SetShowMoreUpcomingEventsButtonActive(currentUpcomingEventsShowed < eventsFromAPI.Count);
 2232    }
 233
 234    public void LoadGoingEvents()
 235    {
 2236        List<EventCardComponentModel> goingEvents = new List<EventCardComponentModel>();
 6237        List<EventFromAPIModel> eventsFiltered = eventsFromAPI.Where(e => e.attending).ToList();
 238
 4239        foreach (EventFromAPIModel receivedEvent in eventsFiltered)
 240        {
 0241            EventCardComponentModel eventCardModel = ExploreEventsUtils.CreateEventCardModelFromAPIEvent(receivedEvent);
 0242            goingEvents.Add(eventCardModel);
 243        }
 244
 2245        view.SetGoingEvents(goingEvents);
 2246        view.SetGoingEventsAsLoading(false);
 2247    }
 248
 249    public void Dispose()
 250    {
 21251        view.OnReady -= FirstLoading;
 21252        view.OnInfoClicked -= ShowEventDetailedInfo;
 21253        view.OnJumpInClicked -= JumpInToEvent;
 21254        view.OnSubscribeEventClicked -= SubscribeToEvent;
 21255        view.OnUnsubscribeEventClicked -= UnsubscribeToEvent;
 21256        view.OnShowMoreUpcomingEventsClicked -= ShowMoreUpcomingEvents;
 21257        view.OnEventsSubSectionEnable -= RequestAllEvents;
 21258        OnEventsFromAPIUpdated -= OnRequestedEventsUpdated;
 21259        DataStore.i.exploreV2.isOpen.OnChange -= OnExploreV2Open;
 21260    }
 261
 262    internal void ShowEventDetailedInfo(EventCardComponentModel eventModel)
 263    {
 1264        view.ShowEventModal(eventModel);
 1265        exploreV2Analytics.SendClickOnEventInfo(eventModel.eventId, eventModel.eventName);
 1266    }
 267
 268    internal void JumpInToEvent(EventFromAPIModel eventFromAPI)
 269    {
 1270        ExploreEventsUtils.JumpInToEvent(eventFromAPI);
 1271        view.HideEventModal();
 1272        OnCloseExploreV2?.Invoke();
 1273        exploreV2Analytics.SendEventTeleport(eventFromAPI.id, eventFromAPI.name, new Vector2Int(eventFromAPI.coordinates
 1274    }
 275
 276    internal void SubscribeToEvent(string eventId)
 277    {
 278        // TODO (Santi): Remove when the RegisterAttendEvent POST is available.
 0279        WebInterface.OpenURL(string.Format(EVENT_DETAIL_URL, eventId));
 280
 281        // TODO (Santi): Waiting for the new version of the Events API where we will be able to send a signed POST to re
 282        //eventsAPIApiController.RegisterAttendEvent(
 283        //    eventId,
 284        //    true,
 285        //    () =>
 286        //    {
 287        //        // ...
 288        //    },
 289        //    (error) =>
 290        //    {
 291        //        Debug.LogError($"Error posting 'attend' message to the API: {error}");
 292        //    });
 0293    }
 294
 295    internal void UnsubscribeToEvent(string eventId)
 296    {
 297        // TODO (Santi): Remove when the RegisterAttendEvent POST is available.
 0298        WebInterface.OpenURL(string.Format(EVENT_DETAIL_URL, eventId));
 299
 300        // TODO (Santi): Waiting for the new version of the Events API where we will be able to send a signed POST to un
 301        //eventsAPIApiController.RegisterAttendEvent(
 302        //    eventId,
 303        //    false,
 304        //    () =>
 305        //    {
 306        //        // ...
 307        //    },
 308        //    (error) =>
 309        //    {
 310        //        Debug.LogError($"Error posting 'attend' message to the API: {error}");
 311        //    });
 0312    }
 313}