< 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:95
Uncovered lines:35
Coverable lines:130
Total lines:320
Line coverage:73% (95 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.374071.43%
IsInShowAnimationTransitonChanged(...)0%2100%
RequestAllEventsFromAPI()0%220100%
OnRequestedEventsUpdated()0%110100%
LoadFeaturedEvents()0%440100%
LoadTrendingEvents()0%3.333066.67%
LoadUpcomingEvents()0%2.112070%
ShowMoreUpcomingEvents()0%4.344072.22%
LoadGoingEvents()0%3.333066.67%
Dispose()0%110100%
ShowEventDetailedInfo(...)0%110100%
JumpInToEvent(...)0%220100%
SubscribeToEvent(...)0%2100%
UnsubscribeToEvent(...)0%2100%
OnChannelToJoinChanged(...)0%12300%

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    private DataStore dataStore;
 64
 2165    public EventsSubSectionComponentController(
 66        IEventsSubSectionComponentView view,
 67        IEventsAPIController eventsAPI,
 68        IExploreV2Analytics exploreV2Analytics,
 69        DataStore dataStore)
 70    {
 2171        this.view = view;
 2172        this.view.OnReady += FirstLoading;
 2173        this.view.OnInfoClicked += ShowEventDetailedInfo;
 2174        this.view.OnJumpInClicked += JumpInToEvent;
 2175        this.view.OnSubscribeEventClicked += SubscribeToEvent;
 2176        this.view.OnUnsubscribeEventClicked += UnsubscribeToEvent;
 2177        this.view.OnShowMoreUpcomingEventsClicked += ShowMoreUpcomingEvents;
 2178        this.dataStore = dataStore;
 2179        this.dataStore.channels.currentJoinChannelModal.OnChange += OnChannelToJoinChanged;
 80
 2181        eventsAPIApiController = eventsAPI;
 2182        OnEventsFromAPIUpdated += OnRequestedEventsUpdated;
 83
 2184        this.exploreV2Analytics = exploreV2Analytics;
 85
 2186        view.ConfigurePools();
 2187    }
 88
 89    internal void FirstLoading()
 90    {
 191        reloadEvents = true;
 192        lastTimeAPIChecked = Time.realtimeSinceStartup - PlacesAndEventsSectionComponentController.MIN_TIME_TO_CHECK_API
 193        RequestAllEvents();
 94
 195        view.OnEventsSubSectionEnable += RequestAllEvents;
 196        dataStore.exploreV2.isOpen.OnChange += OnExploreV2Open;
 197    }
 98
 99    internal void OnExploreV2Open(bool current, bool previous)
 100    {
 0101        if (current)
 0102            return;
 103
 0104        reloadEvents = true;
 0105    }
 106
 107    public void RequestAllEvents()
 108    {
 2109        if (!reloadEvents)
 0110            return;
 111
 2112        view.RestartScrollViewPosition();
 113
 2114        if (Time.realtimeSinceStartup < lastTimeAPIChecked + PlacesAndEventsSectionComponentController.MIN_TIME_TO_CHECK
 0115            return;
 116
 2117        currentUpcomingEventsShowed = view.currentUpcomingEventsPerRow * INITIAL_NUMBER_OF_UPCOMING_ROWS;
 118
 2119        view.SetAllEventGroupsAsLoading();
 2120        view.SetShowMoreUpcomingEventsButtonActive(false);
 121
 2122        reloadEvents = false;
 2123        lastTimeAPIChecked = Time.realtimeSinceStartup;
 124
 2125        if (!dataStore.exploreV2.isInShowAnimationTransiton.Get())
 2126            RequestAllEventsFromAPI();
 127        else
 0128            dataStore.exploreV2.isInShowAnimationTransiton.OnChange += IsInShowAnimationTransitonChanged;
 0129    }
 130
 131    internal void IsInShowAnimationTransitonChanged(bool current, bool previous)
 132    {
 0133        dataStore.exploreV2.isInShowAnimationTransiton.OnChange -= IsInShowAnimationTransitonChanged;
 0134        RequestAllEventsFromAPI();
 0135    }
 136
 137    internal void RequestAllEventsFromAPI()
 138    {
 3139        eventsAPIApiController.GetAllEvents(
 140            (eventList) =>
 141            {
 0142                eventsFromAPI = eventList;
 0143                OnEventsFromAPIUpdated?.Invoke();
 0144            },
 145            (error) =>
 146            {
 0147                Debug.LogError($"Error receiving events from the API: {error}");
 0148            });
 3149    }
 150
 151    internal void OnRequestedEventsUpdated()
 152    {
 1153        LoadFeaturedEvents();
 1154        LoadTrendingEvents();
 1155        LoadUpcomingEvents();
 1156        LoadGoingEvents();
 1157    }
 158
 159    public void LoadFeaturedEvents()
 160    {
 2161        List<EventCardComponentModel> featuredEvents = new List<EventCardComponentModel>();
 6162        List<EventFromAPIModel> eventsFiltered = eventsFromAPI.Where(e => e.highlighted).ToList();
 163
 2164        if (eventsFiltered.Count == 0)
 2165            eventsFiltered = eventsFromAPI.Take(DEFAULT_NUMBER_OF_FEATURED_EVENTS).ToList();
 166
 12167        foreach (EventFromAPIModel receivedEvent in eventsFiltered)
 168        {
 4169            EventCardComponentModel eventCardModel = ExploreEventsUtils.CreateEventCardModelFromAPIEvent(receivedEvent);
 4170            featuredEvents.Add(eventCardModel);
 171        }
 172
 2173        view.SetFeaturedEvents(featuredEvents);
 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    }
 189
 190    public void LoadUpcomingEvents()
 191    {
 2192        List<EventCardComponentModel> upcomingEvents = new List<EventCardComponentModel>();
 2193        List<EventFromAPIModel> eventsFiltered = eventsFromAPI.Take(currentUpcomingEventsShowed).ToList();
 194
 4195        foreach (EventFromAPIModel receivedEvent in eventsFiltered)
 196        {
 0197            EventCardComponentModel eventCardModel = ExploreEventsUtils.CreateEventCardModelFromAPIEvent(receivedEvent);
 0198            upcomingEvents.Add(eventCardModel);
 199        }
 200
 2201        view.SetUpcomingEvents(upcomingEvents);
 2202        view.SetShowMoreUpcomingEventsButtonActive(currentUpcomingEventsShowed < eventsFromAPI.Count);
 2203    }
 204
 205    public void ShowMoreUpcomingEvents()
 206    {
 2207        List<EventCardComponentModel> upcomingEvents = new List<EventCardComponentModel>();
 2208        List<EventFromAPIModel> eventsFiltered = new List<EventFromAPIModel>();
 2209        int numberOfExtraItemsToAdd = ((int)Mathf.Ceil((float)currentUpcomingEventsShowed / view.currentUpcomingEventsPe
 2210        int numberOfItemsToAdd = view.currentUpcomingEventsPerRow * SHOW_MORE_UPCOMING_ROWS_INCREMENT + numberOfExtraIte
 211
 2212        if (currentUpcomingEventsShowed + numberOfItemsToAdd <= eventsFromAPI.Count)
 2213            eventsFiltered = eventsFromAPI.GetRange(currentUpcomingEventsShowed, numberOfItemsToAdd);
 214        else
 0215            eventsFiltered = eventsFromAPI.GetRange(currentUpcomingEventsShowed, eventsFromAPI.Count - currentUpcomingEv
 216
 4217        foreach (EventFromAPIModel receivedEvent in eventsFiltered)
 218        {
 0219            EventCardComponentModel placeCardModel = ExploreEventsUtils.CreateEventCardModelFromAPIEvent(receivedEvent);
 0220            upcomingEvents.Add(placeCardModel);
 221        }
 222
 2223        view.AddUpcomingEvents(upcomingEvents);
 224
 2225        currentUpcomingEventsShowed += numberOfItemsToAdd;
 2226        if (currentUpcomingEventsShowed > eventsFromAPI.Count)
 0227            currentUpcomingEventsShowed = eventsFromAPI.Count;
 228
 2229        view.SetShowMoreUpcomingEventsButtonActive(currentUpcomingEventsShowed < eventsFromAPI.Count);
 2230    }
 231
 232    public void LoadGoingEvents()
 233    {
 2234        List<EventCardComponentModel> goingEvents = new List<EventCardComponentModel>();
 6235        List<EventFromAPIModel> eventsFiltered = eventsFromAPI.Where(e => e.attending).ToList();
 236
 4237        foreach (EventFromAPIModel receivedEvent in eventsFiltered)
 238        {
 0239            EventCardComponentModel eventCardModel = ExploreEventsUtils.CreateEventCardModelFromAPIEvent(receivedEvent);
 0240            goingEvents.Add(eventCardModel);
 241        }
 242
 2243        view.SetGoingEvents(goingEvents);
 2244    }
 245
 246    public void Dispose()
 247    {
 21248        view.OnReady -= FirstLoading;
 21249        view.OnInfoClicked -= ShowEventDetailedInfo;
 21250        view.OnJumpInClicked -= JumpInToEvent;
 21251        view.OnSubscribeEventClicked -= SubscribeToEvent;
 21252        view.OnUnsubscribeEventClicked -= UnsubscribeToEvent;
 21253        view.OnShowMoreUpcomingEventsClicked -= ShowMoreUpcomingEvents;
 21254        view.OnEventsSubSectionEnable -= RequestAllEvents;
 21255        OnEventsFromAPIUpdated -= OnRequestedEventsUpdated;
 21256        dataStore.exploreV2.isOpen.OnChange -= OnExploreV2Open;
 21257        dataStore.channels.currentJoinChannelModal.OnChange -= OnChannelToJoinChanged;
 21258    }
 259
 260    internal void ShowEventDetailedInfo(EventCardComponentModel eventModel)
 261    {
 1262        view.ShowEventModal(eventModel);
 1263        exploreV2Analytics.SendClickOnEventInfo(eventModel.eventId, eventModel.eventName);
 1264    }
 265
 266    internal void JumpInToEvent(EventFromAPIModel eventFromAPI)
 267    {
 1268        ExploreEventsUtils.JumpInToEvent(eventFromAPI);
 1269        view.HideEventModal();
 1270        OnCloseExploreV2?.Invoke();
 1271        exploreV2Analytics.SendEventTeleport(eventFromAPI.id, eventFromAPI.name, new Vector2Int(eventFromAPI.coordinates
 1272    }
 273
 274    internal void SubscribeToEvent(string eventId)
 275    {
 276        // TODO (Santi): Remove when the RegisterAttendEvent POST is available.
 0277        WebInterface.OpenURL(string.Format(EVENT_DETAIL_URL, eventId));
 278
 279        // TODO (Santi): Waiting for the new version of the Events API where we will be able to send a signed POST to re
 280        //eventsAPIApiController.RegisterAttendEvent(
 281        //    eventId,
 282        //    true,
 283        //    () =>
 284        //    {
 285        //        // ...
 286        //    },
 287        //    (error) =>
 288        //    {
 289        //        Debug.LogError($"Error posting 'attend' message to the API: {error}");
 290        //    });
 0291    }
 292
 293    internal void UnsubscribeToEvent(string eventId)
 294    {
 295        // TODO (Santi): Remove when the RegisterAttendEvent POST is available.
 0296        WebInterface.OpenURL(string.Format(EVENT_DETAIL_URL, eventId));
 297
 298        // TODO (Santi): Waiting for the new version of the Events API where we will be able to send a signed POST to un
 299        //eventsAPIApiController.RegisterAttendEvent(
 300        //    eventId,
 301        //    false,
 302        //    () =>
 303        //    {
 304        //        // ...
 305        //    },
 306        //    (error) =>
 307        //    {
 308        //        Debug.LogError($"Error posting 'attend' message to the API: {error}");
 309        //    });
 0310    }
 311
 312    private void OnChannelToJoinChanged(string currentChannelId, string previousChannelId)
 313    {
 0314        if (!string.IsNullOrEmpty(currentChannelId))
 0315            return;
 316
 0317        view.HideEventModal();
 0318        OnCloseExploreV2?.Invoke();
 0319    }
 320}