< Summary

Class:HighlightsSubSectionComponentController
Assembly:ExploreV2
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/ExploreV2/Scripts/Sections/PlacesAndEventsSection/SubSections/HighlightsSubSection/HighlightsSubSectionMenu/HighlightsSubSectionComponentController.cs
Covered lines:106
Uncovered lines:26
Coverable lines:132
Total lines:331
Line coverage:80.3% (106 of 132)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
HighlightsSubSectionComponentController(...)0%110100%
FirstLoading()0%110100%
OnExploreV2Open(...)0%6200%
RequestAllPlacesAndEvents()0%4.054085.71%
IsInShowAnimationTransitonChanged(...)0%110100%
RequestAllPlacesAndEventsFromAPI()0%110100%
OnRequestedPlacesAndEventsUpdated()0%110100%
LoadTrendingPlacesAndEvents()0%4.094082.35%
LoadFeaturedPlaces()0%5.335076.47%
LoadLiveEvents()0%330100%
Dispose()0%110100%
ShowPlaceDetailedInfo(...)0%110100%
JumpInToPlace(...)0%220100%
View_OnFriendHandlerAdded(...)0%2100%
ShowEventDetailedInfo(...)0%110100%
JumpInToEvent(...)0%220100%
SubscribeToEvent(...)0%2100%
UnsubscribeToEvent(...)0%2100%
GoToEventsSubSection()0%220100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/ExploreV2/Scripts/Sections/PlacesAndEventsSection/SubSections/HighlightsSubSection/HighlightsSubSectionMenu/HighlightsSubSectionComponentController.cs

#LineLine coverage
 1using DCL;
 2using DCL.Interface;
 3using ExploreV2Analytics;
 4using System;
 5using System.Collections.Generic;
 6using System.Linq;
 7using UnityEngine;
 8using static HotScenesController;
 9
 10public interface IHighlightsSubSectionComponentController : IDisposable
 11{
 12    /// <summary>
 13    /// It will be triggered when the sub-section want to request to close the ExploreV2 main menu.
 14    /// </summary>
 15    event Action OnCloseExploreV2;
 16
 17    /// <summary>
 18    /// It will be triggered when the sub-section want to request to go to the Events sub-section.
 19    /// </summary>
 20    event Action OnGoToEventsSubSection;
 21
 22    /// <summary>
 23    /// Request all places and events from the API.
 24    /// </summary>
 25    void RequestAllPlacesAndEvents();
 26
 27    /// <summary>
 28    /// Load the trending places and events with the last requested ones.
 29    /// </summary>
 30    void LoadTrendingPlacesAndEvents();
 31
 32    /// <summary>
 33    /// Load the featured places with the last requested ones.
 34    /// </summary>
 35    void LoadFeaturedPlaces();
 36
 37    /// <summary>
 38    /// Load the live events with the last requested ones.
 39    /// </summary>
 40    void LoadLiveEvents();
 41}
 42
 43public class HighlightsSubSectionComponentController : IHighlightsSubSectionComponentController
 44{
 45    public event Action OnCloseExploreV2;
 46    public event Action OnGoToEventsSubSection;
 47    internal event Action OnPlacesAndEventsFromAPIUpdated;
 48
 49    internal const int DEFAULT_NUMBER_OF_TRENDING_PLACES = 10;
 50    internal const int DEFAULT_NUMBER_OF_FEATURED_PLACES = 9;
 51    internal const int DEFAULT_NUMBER_OF_LIVE_EVENTS = 3;
 52    internal const string EVENT_DETAIL_URL = "https://events.decentraland.org/event/?id={0}";
 53
 54    internal IHighlightsSubSectionComponentView view;
 55    internal IPlacesAPIController placesAPIApiController;
 56    internal IEventsAPIController eventsAPIApiController;
 57    internal FriendTrackerController friendsTrackerController;
 2158    internal List<HotSceneInfo> placesFromAPI = new List<HotSceneInfo>();
 2159    internal List<EventFromAPIModel> eventsFromAPI = new List<EventFromAPIModel>();
 60    internal bool reloadHighlights = false;
 61    internal IExploreV2Analytics exploreV2Analytics;
 62    internal float lastTimeAPIChecked = 0;
 63
 2164    public HighlightsSubSectionComponentController(
 65        IHighlightsSubSectionComponentView view,
 66        IPlacesAPIController placesAPI,
 67        IEventsAPIController eventsAPI,
 68        IFriendsController friendsController,
 69        IExploreV2Analytics exploreV2Analytics)
 70    {
 2171        this.view = view;
 2172        this.view.OnReady += FirstLoading;
 2173        this.view.OnPlaceInfoClicked += ShowPlaceDetailedInfo;
 2174        this.view.OnEventInfoClicked += ShowEventDetailedInfo;
 2175        this.view.OnPlaceJumpInClicked += JumpInToPlace;
 2176        this.view.OnEventJumpInClicked += JumpInToEvent;
 2177        this.view.OnEventSubscribeEventClicked += SubscribeToEvent;
 2178        this.view.OnEventUnsubscribeEventClicked += UnsubscribeToEvent;
 2179        this.view.OnFriendHandlerAdded += View_OnFriendHandlerAdded;
 2180        this.view.OnViewAllEventsClicked += GoToEventsSubSection;
 81
 2182        placesAPIApiController = placesAPI;
 2183        eventsAPIApiController = eventsAPI;
 2184        OnPlacesAndEventsFromAPIUpdated += OnRequestedPlacesAndEventsUpdated;
 85
 2186        friendsTrackerController = new FriendTrackerController(friendsController, view.currentFriendColors);
 87
 2188        this.exploreV2Analytics = exploreV2Analytics;
 89
 2190        view.ConfigurePools();
 2191    }
 92
 93    internal void FirstLoading()
 94    {
 195        reloadHighlights = true;
 196        lastTimeAPIChecked = Time.realtimeSinceStartup - PlacesAndEventsSectionComponentController.MIN_TIME_TO_CHECK_API
 197        RequestAllPlacesAndEvents();
 98
 199        view.OnHighlightsSubSectionEnable += RequestAllPlacesAndEvents;
 1100        DataStore.i.exploreV2.isOpen.OnChange += OnExploreV2Open;
 1101    }
 102
 103    internal void OnExploreV2Open(bool current, bool previous)
 104    {
 0105        if (current)
 0106            return;
 107
 0108        reloadHighlights = true;
 0109    }
 110
 111    public void RequestAllPlacesAndEvents()
 112    {
 2113        if (!reloadHighlights)
 0114            return;
 115
 2116        view.RestartScrollViewPosition();
 117
 2118        if (Time.realtimeSinceStartup < lastTimeAPIChecked + PlacesAndEventsSectionComponentController.MIN_TIME_TO_CHECK
 0119            return;
 120
 2121        view.SetTrendingPlacesAndEventsAsLoading(true);
 2122        view.SetFeaturedPlacesAsLoading(true);
 2123        view.SetLiveAsLoading(true);
 124
 2125        reloadHighlights = false;
 2126        lastTimeAPIChecked = Time.realtimeSinceStartup;
 127
 2128        if (!DataStore.i.exploreV2.isInShowAnimationTransiton.Get())
 1129            RequestAllPlacesAndEventsFromAPI();
 130        else
 1131            DataStore.i.exploreV2.isInShowAnimationTransiton.OnChange += IsInShowAnimationTransitonChanged;
 1132    }
 133
 134    internal void IsInShowAnimationTransitonChanged(bool current, bool previous)
 135    {
 1136        DataStore.i.exploreV2.isInShowAnimationTransiton.OnChange -= IsInShowAnimationTransitonChanged;
 1137        RequestAllPlacesAndEventsFromAPI();
 1138    }
 139
 140    internal void RequestAllPlacesAndEventsFromAPI()
 141    {
 3142        placesAPIApiController.GetAllPlaces(
 143            (placeList) =>
 144            {
 0145                placesFromAPI = placeList;
 0146                eventsAPIApiController.GetAllEvents(
 147                    (eventList) =>
 148                    {
 0149                        eventsFromAPI = eventList;
 0150                        OnPlacesAndEventsFromAPIUpdated?.Invoke();
 0151                    },
 152                    (error) =>
 153                    {
 0154                        OnPlacesAndEventsFromAPIUpdated?.Invoke();
 0155                        Debug.LogError($"Error receiving events from the API: {error}");
 0156                    });
 0157            });
 3158    }
 159
 160    internal void OnRequestedPlacesAndEventsUpdated()
 161    {
 1162        friendsTrackerController.RemoveAllHandlers();
 163
 1164        LoadTrendingPlacesAndEvents();
 1165        LoadFeaturedPlaces();
 1166        LoadLiveEvents();
 1167    }
 168
 169    public void LoadTrendingPlacesAndEvents()
 170    {
 171        // Places
 2172        List<PlaceCardComponentModel> places = new List<PlaceCardComponentModel>();
 2173        List<HotSceneInfo> placesFiltered = placesFromAPI
 174                                            .Take(DEFAULT_NUMBER_OF_TRENDING_PLACES)
 175                                            .ToList();
 176
 12177        foreach (HotSceneInfo receivedPlace in placesFiltered)
 178        {
 4179            PlaceCardComponentModel placeCardModel = ExplorePlacesUtils.CreatePlaceCardModelFromAPIPlace(receivedPlace);
 4180            places.Add(placeCardModel);
 181        }
 182
 183        // Events
 2184        List<EventCardComponentModel> events = new List<EventCardComponentModel>();
 2185        List<EventFromAPIModel> eventsFiltered = eventsFromAPI
 4186            .Where(e => e.highlighted)
 187            .Take(placesFiltered.Count)
 188            .ToList();
 189
 4190        foreach (EventFromAPIModel receivedEvent in eventsFiltered)
 191        {
 0192            EventCardComponentModel eventCardModel = ExploreEventsUtils.CreateEventCardModelFromAPIEvent(receivedEvent);
 0193            events.Add(eventCardModel);
 194        }
 195
 2196        view.SetTrendingPlacesAndEventsAsLoading(false);
 2197        view.SetTrendingPlacesAndEvents(places, events);
 2198    }
 199
 200    public void LoadFeaturedPlaces()
 201    {
 2202        List<PlaceCardComponentModel> places = new List<PlaceCardComponentModel>();
 203        List<HotSceneInfo> placesFiltered;
 2204        if (placesFromAPI.Count >= DEFAULT_NUMBER_OF_TRENDING_PLACES)
 205        {
 0206            int numberOfPlaces = placesFromAPI.Count >= (DEFAULT_NUMBER_OF_TRENDING_PLACES + DEFAULT_NUMBER_OF_FEATURED_
 207                ? DEFAULT_NUMBER_OF_FEATURED_PLACES
 208                : placesFromAPI.Count - DEFAULT_NUMBER_OF_TRENDING_PLACES;
 209
 0210            placesFiltered = placesFromAPI
 211                             .GetRange(DEFAULT_NUMBER_OF_TRENDING_PLACES, numberOfPlaces)
 212                             .ToList();
 0213        }
 2214        else if (placesFromAPI.Count > 0)
 215        {
 2216            placesFiltered = placesFromAPI.Take(DEFAULT_NUMBER_OF_FEATURED_PLACES).ToList();
 2217        }
 218        else
 219        {
 0220            placesFiltered = new List<HotSceneInfo>();
 221        }
 222
 12223        foreach (HotSceneInfo receivedPlace in placesFiltered)
 224        {
 4225            PlaceCardComponentModel placeCardModel = ExplorePlacesUtils.CreatePlaceCardModelFromAPIPlace(receivedPlace);
 4226            places.Add(placeCardModel);
 227        }
 228
 2229        view.SetFeaturedPlaces(places);
 2230        view.SetFeaturedPlacesAsLoading(false);
 2231    }
 232
 233    public void LoadLiveEvents()
 234    {
 2235        List<EventCardComponentModel> events = new List<EventCardComponentModel>();
 6236        List<EventFromAPIModel> eventsFiltered = eventsFromAPI.Where(x => x.live)
 237                                                              .Take(DEFAULT_NUMBER_OF_LIVE_EVENTS)
 238                                                              .ToList();
 12239        foreach (EventFromAPIModel receivedEvent in eventsFiltered)
 240        {
 4241            EventCardComponentModel eventCardModel = ExploreEventsUtils.CreateEventCardModelFromAPIEvent(receivedEvent);
 4242            events.Add(eventCardModel);
 243        }
 244
 2245        view.SetLiveEvents(events);
 2246        view.SetLiveAsLoading(false);
 2247    }
 248
 249    public void Dispose()
 250    {
 21251        view.OnReady -= FirstLoading;
 21252        view.OnPlaceInfoClicked -= ShowPlaceDetailedInfo;
 21253        view.OnEventInfoClicked -= ShowEventDetailedInfo;
 21254        view.OnPlaceJumpInClicked -= JumpInToPlace;
 21255        view.OnEventJumpInClicked -= JumpInToEvent;
 21256        view.OnEventSubscribeEventClicked -= SubscribeToEvent;
 21257        view.OnEventUnsubscribeEventClicked -= UnsubscribeToEvent;
 21258        view.OnFriendHandlerAdded -= View_OnFriendHandlerAdded;
 21259        view.OnViewAllEventsClicked -= GoToEventsSubSection;
 21260    }
 261
 262    internal void ShowPlaceDetailedInfo(PlaceCardComponentModel placeModel)
 263    {
 1264        view.ShowPlaceModal(placeModel);
 1265        exploreV2Analytics.SendClickOnPlaceInfo(placeModel.hotSceneInfo.id, placeModel.placeName);
 1266    }
 267
 268    internal void JumpInToPlace(HotSceneInfo placeFromAPI)
 269    {
 1270        ExplorePlacesUtils.JumpInToPlace(placeFromAPI);
 1271        view.HidePlaceModal();
 1272        OnCloseExploreV2?.Invoke();
 1273        exploreV2Analytics.SendPlaceTeleport(placeFromAPI.id, placeFromAPI.name, placeFromAPI.baseCoords);
 1274    }
 275
 0276    internal void View_OnFriendHandlerAdded(FriendsHandler friendsHandler) { friendsTrackerController.AddHandler(friends
 277
 278    internal void ShowEventDetailedInfo(EventCardComponentModel eventModel)
 279    {
 1280        view.ShowEventModal(eventModel);
 1281        exploreV2Analytics.SendClickOnEventInfo(eventModel.eventId, eventModel.eventName);
 1282    }
 283
 284    internal void JumpInToEvent(EventFromAPIModel eventFromAPI)
 285    {
 1286        ExploreEventsUtils.JumpInToEvent(eventFromAPI);
 1287        view.HideEventModal();
 1288        OnCloseExploreV2?.Invoke();
 1289        exploreV2Analytics.SendEventTeleport(eventFromAPI.id, eventFromAPI.name, new Vector2Int(eventFromAPI.coordinates
 1290    }
 291
 292    internal void SubscribeToEvent(string eventId)
 293    {
 294        // TODO (Santi): Remove when the RegisterAttendEvent POST is available.
 0295        WebInterface.OpenURL(string.Format(EVENT_DETAIL_URL, eventId));
 296
 297        // TODO (Santi): Waiting for the new version of the Events API where we will be able to send a signed POST to re
 298        //eventsAPIApiController.RegisterAttendEvent(
 299        //    eventId,
 300        //    true,
 301        //    () =>
 302        //    {
 303        //        // ...
 304        //    },
 305        //    (error) =>
 306        //    {
 307        //        Debug.LogError($"Error posting 'attend' message to the API: {error}");
 308        //    });
 0309    }
 310
 311    internal void UnsubscribeToEvent(string eventId)
 312    {
 313        // TODO (Santi): Remove when the RegisterAttendEvent POST is available.
 0314        WebInterface.OpenURL(string.Format(EVENT_DETAIL_URL, eventId));
 315
 316        // TODO (Santi): Waiting for the new version of the Events API where we will be able to send a signed POST to un
 317        //eventsAPIApiController.RegisterAttendEvent(
 318        //    eventId,
 319        //    false,
 320        //    () =>
 321        //    {
 322        //        // ...
 323        //    },
 324        //    (error) =>
 325        //    {
 326        //        Debug.LogError($"Error posting 'attend' message to the API: {error}");
 327        //    });
 0328    }
 329
 2330    internal void GoToEventsSubSection() { OnGoToEventsSubSection?.Invoke(); }
 331}