< 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:110
Uncovered lines:33
Coverable lines:143
Total lines:349
Line coverage:76.9% (110 of 143)
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.114081.25%
LoadFeaturedPlaces()0%5.395075%
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%
OnChannelToJoinChanged(...)0%12300%

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
 48    internal const int DEFAULT_NUMBER_OF_TRENDING_PLACES = 10;
 49    internal const int DEFAULT_NUMBER_OF_FEATURED_PLACES = 9;
 50    internal const int DEFAULT_NUMBER_OF_LIVE_EVENTS = 3;
 51    internal const string EVENT_DETAIL_URL = "https://events.decentraland.org/event/?id={0}";
 52
 53    internal IHighlightsSubSectionComponentView view;
 54    internal IPlacesAPIController placesAPIApiController;
 55    internal IEventsAPIController eventsAPIApiController;
 56    internal FriendTrackerController friendsTrackerController;
 2157    internal List<HotSceneInfo> placesFromAPI = new List<HotSceneInfo>();
 2158    internal List<EventFromAPIModel> eventsFromAPI = new List<EventFromAPIModel>();
 59    internal bool reloadHighlights = false;
 60    internal IExploreV2Analytics exploreV2Analytics;
 61    internal float lastTimeAPIChecked = 0;
 62    private DataStore dataStore;
 63
 2164    public HighlightsSubSectionComponentController(
 65        IHighlightsSubSectionComponentView view,
 66        IPlacesAPIController placesAPI,
 67        IEventsAPIController eventsAPI,
 68        IFriendsController friendsController,
 69        IExploreV2Analytics exploreV2Analytics,
 70        DataStore dataStore)
 71    {
 2172        this.view = view;
 2173        this.view.OnReady += FirstLoading;
 2174        this.view.OnPlaceInfoClicked += ShowPlaceDetailedInfo;
 2175        this.view.OnEventInfoClicked += ShowEventDetailedInfo;
 2176        this.view.OnPlaceJumpInClicked += JumpInToPlace;
 2177        this.view.OnEventJumpInClicked += JumpInToEvent;
 2178        this.view.OnEventSubscribeEventClicked += SubscribeToEvent;
 2179        this.view.OnEventUnsubscribeEventClicked += UnsubscribeToEvent;
 2180        this.view.OnFriendHandlerAdded += View_OnFriendHandlerAdded;
 2181        this.view.OnViewAllEventsClicked += GoToEventsSubSection;
 82
 2183        this.dataStore = dataStore;
 2184        this.dataStore.channels.currentJoinChannelModal.OnChange += OnChannelToJoinChanged;
 85
 2186        placesAPIApiController = placesAPI;
 2187        eventsAPIApiController = eventsAPI;
 88
 2189        friendsTrackerController = new FriendTrackerController(friendsController, view.currentFriendColors);
 90
 2191        this.exploreV2Analytics = exploreV2Analytics;
 92
 2193        view.ConfigurePools();
 2194    }
 95
 96    internal void FirstLoading()
 97    {
 198        reloadHighlights = true;
 199        lastTimeAPIChecked = Time.realtimeSinceStartup - PlacesAndEventsSectionComponentController.MIN_TIME_TO_CHECK_API
 1100        RequestAllPlacesAndEvents();
 101
 1102        view.OnHighlightsSubSectionEnable += RequestAllPlacesAndEvents;
 1103        dataStore.exploreV2.isOpen.OnChange += OnExploreV2Open;
 1104    }
 105
 106    internal void OnExploreV2Open(bool current, bool previous)
 107    {
 0108        if (current)
 0109            return;
 110
 0111        reloadHighlights = true;
 0112    }
 113
 114    public void RequestAllPlacesAndEvents()
 115    {
 2116        if (!reloadHighlights)
 0117            return;
 118
 2119        view.RestartScrollViewPosition();
 120
 2121        if (Time.realtimeSinceStartup < lastTimeAPIChecked + PlacesAndEventsSectionComponentController.MIN_TIME_TO_CHECK
 0122            return;
 123
 2124        view.SetTrendingPlacesAndEventsAsLoading(true);
 2125        view.SetFeaturedPlacesAsLoading(true);
 2126        view.SetLiveAsLoading(true);
 127
 2128        reloadHighlights = false;
 2129        lastTimeAPIChecked = Time.realtimeSinceStartup;
 130
 2131        if (!dataStore.exploreV2.isInShowAnimationTransiton.Get())
 1132            RequestAllPlacesAndEventsFromAPI();
 133        else
 1134            dataStore.exploreV2.isInShowAnimationTransiton.OnChange += IsInShowAnimationTransitonChanged;
 1135    }
 136
 137    internal void IsInShowAnimationTransitonChanged(bool current, bool previous)
 138    {
 1139        dataStore.exploreV2.isInShowAnimationTransiton.OnChange -= IsInShowAnimationTransitonChanged;
 1140        RequestAllPlacesAndEventsFromAPI();
 1141    }
 142
 143    internal void RequestAllPlacesAndEventsFromAPI()
 144    {
 3145        placesAPIApiController.GetAllPlaces(
 146            (placeList) =>
 147            {
 0148                placesFromAPI = placeList;
 0149                eventsAPIApiController.GetAllEvents(
 150                    (eventList) =>
 151                    {
 0152                        eventsFromAPI = eventList;
 0153                        OnRequestedPlacesAndEventsUpdated();
 0154                    },
 155                    (error) =>
 156                    {
 0157                        OnRequestedPlacesAndEventsUpdated();
 0158                        Debug.LogError($"Error receiving events from the API: {error}");
 0159                    });
 0160            });
 3161    }
 162
 163    internal void OnRequestedPlacesAndEventsUpdated()
 164    {
 1165        friendsTrackerController.RemoveAllHandlers();
 166
 1167        LoadTrendingPlacesAndEvents();
 1168        LoadFeaturedPlaces();
 1169        LoadLiveEvents();
 1170    }
 171
 172    public void LoadTrendingPlacesAndEvents()
 173    {
 174        // Places
 2175        List<PlaceCardComponentModel> places = new List<PlaceCardComponentModel>();
 2176        List<HotSceneInfo> placesFiltered = placesFromAPI
 177                                            .Take(DEFAULT_NUMBER_OF_TRENDING_PLACES)
 178                                            .ToList();
 179
 12180        foreach (HotSceneInfo receivedPlace in placesFiltered)
 181        {
 4182            PlaceCardComponentModel placeCardModel = ExplorePlacesUtils.CreatePlaceCardModelFromAPIPlace(receivedPlace);
 4183            places.Add(placeCardModel);
 184        }
 185
 186        // Events
 2187        List<EventCardComponentModel> events = new List<EventCardComponentModel>();
 2188        List<EventFromAPIModel> eventsFiltered = eventsFromAPI
 4189            .Where(e => e.highlighted)
 190            .Take(placesFiltered.Count)
 191            .ToList();
 192
 4193        foreach (EventFromAPIModel receivedEvent in eventsFiltered)
 194        {
 0195            EventCardComponentModel eventCardModel = ExploreEventsUtils.CreateEventCardModelFromAPIEvent(receivedEvent);
 0196            events.Add(eventCardModel);
 197        }
 198
 2199        view.SetTrendingPlacesAndEvents(places, events);
 2200    }
 201
 202    public void LoadFeaturedPlaces()
 203    {
 2204        List<PlaceCardComponentModel> places = new List<PlaceCardComponentModel>();
 205        List<HotSceneInfo> placesFiltered;
 2206        if (placesFromAPI.Count >= DEFAULT_NUMBER_OF_TRENDING_PLACES)
 207        {
 0208            int numberOfPlaces = placesFromAPI.Count >= (DEFAULT_NUMBER_OF_TRENDING_PLACES + DEFAULT_NUMBER_OF_FEATURED_
 209                ? DEFAULT_NUMBER_OF_FEATURED_PLACES
 210                : placesFromAPI.Count - DEFAULT_NUMBER_OF_TRENDING_PLACES;
 211
 0212            placesFiltered = placesFromAPI
 213                             .GetRange(DEFAULT_NUMBER_OF_TRENDING_PLACES, numberOfPlaces)
 214                             .ToList();
 0215        }
 2216        else if (placesFromAPI.Count > 0)
 217        {
 2218            placesFiltered = placesFromAPI.Take(DEFAULT_NUMBER_OF_FEATURED_PLACES).ToList();
 2219        }
 220        else
 221        {
 0222            placesFiltered = new List<HotSceneInfo>();
 223        }
 224
 12225        foreach (HotSceneInfo receivedPlace in placesFiltered)
 226        {
 4227            PlaceCardComponentModel placeCardModel = ExplorePlacesUtils.CreatePlaceCardModelFromAPIPlace(receivedPlace);
 4228            places.Add(placeCardModel);
 229        }
 230
 2231        view.SetFeaturedPlaces(places);
 2232    }
 233
 234    public void LoadLiveEvents()
 235    {
 2236        List<EventCardComponentModel> events = new List<EventCardComponentModel>();
 6237        List<EventFromAPIModel> eventsFiltered = eventsFromAPI.Where(x => x.live)
 238                                                              .Take(DEFAULT_NUMBER_OF_LIVE_EVENTS)
 239                                                              .ToList();
 12240        foreach (EventFromAPIModel receivedEvent in eventsFiltered)
 241        {
 4242            EventCardComponentModel eventCardModel = ExploreEventsUtils.CreateEventCardModelFromAPIEvent(receivedEvent);
 4243            events.Add(eventCardModel);
 244        }
 245
 2246        view.SetLiveEvents(events);
 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;
 260
 21261        dataStore.exploreV2.isOpen.OnChange -= OnExploreV2Open;
 21262        dataStore.channels.currentJoinChannelModal.OnChange -= OnChannelToJoinChanged;
 21263    }
 264
 265    internal void ShowPlaceDetailedInfo(PlaceCardComponentModel placeModel)
 266    {
 1267        view.ShowPlaceModal(placeModel);
 1268        exploreV2Analytics.SendClickOnPlaceInfo(placeModel.hotSceneInfo.id, placeModel.placeName);
 1269        dataStore.exploreV2.currentVisibleModal.Set(ExploreV2CurrentModal.Places);
 1270    }
 271
 272    internal void JumpInToPlace(HotSceneInfo placeFromAPI)
 273    {
 1274        ExplorePlacesUtils.JumpInToPlace(placeFromAPI);
 1275        view.HidePlaceModal();
 1276        dataStore.exploreV2.currentVisibleModal.Set(ExploreV2CurrentModal.None);
 1277        OnCloseExploreV2?.Invoke();
 1278        exploreV2Analytics.SendPlaceTeleport(placeFromAPI.id, placeFromAPI.name, placeFromAPI.baseCoords);
 1279    }
 280
 0281    internal void View_OnFriendHandlerAdded(FriendsHandler friendsHandler) { friendsTrackerController.AddHandler(friends
 282
 283    internal void ShowEventDetailedInfo(EventCardComponentModel eventModel)
 284    {
 1285        view.ShowEventModal(eventModel);
 1286        exploreV2Analytics.SendClickOnEventInfo(eventModel.eventId, eventModel.eventName);
 1287        dataStore.exploreV2.currentVisibleModal.Set(ExploreV2CurrentModal.Events);
 1288    }
 289
 290    internal void JumpInToEvent(EventFromAPIModel eventFromAPI)
 291    {
 1292        ExploreEventsUtils.JumpInToEvent(eventFromAPI);
 1293        view.HideEventModal();
 1294        dataStore.exploreV2.currentVisibleModal.Set(ExploreV2CurrentModal.None);
 1295        OnCloseExploreV2?.Invoke();
 1296        exploreV2Analytics.SendEventTeleport(eventFromAPI.id, eventFromAPI.name, new Vector2Int(eventFromAPI.coordinates
 1297    }
 298
 299    internal void SubscribeToEvent(string eventId)
 300    {
 301        // TODO (Santi): Remove when the RegisterAttendEvent POST is available.
 0302        WebInterface.OpenURL(string.Format(EVENT_DETAIL_URL, eventId));
 303
 304        // TODO (Santi): Waiting for the new version of the Events API where we will be able to send a signed POST to re
 305        //eventsAPIApiController.RegisterAttendEvent(
 306        //    eventId,
 307        //    true,
 308        //    () =>
 309        //    {
 310        //        // ...
 311        //    },
 312        //    (error) =>
 313        //    {
 314        //        Debug.LogError($"Error posting 'attend' message to the API: {error}");
 315        //    });
 0316    }
 317
 318    internal void UnsubscribeToEvent(string eventId)
 319    {
 320        // TODO (Santi): Remove when the RegisterAttendEvent POST is available.
 0321        WebInterface.OpenURL(string.Format(EVENT_DETAIL_URL, eventId));
 322
 323        // TODO (Santi): Waiting for the new version of the Events API where we will be able to send a signed POST to un
 324        //eventsAPIApiController.RegisterAttendEvent(
 325        //    eventId,
 326        //    false,
 327        //    () =>
 328        //    {
 329        //        // ...
 330        //    },
 331        //    (error) =>
 332        //    {
 333        //        Debug.LogError($"Error posting 'attend' message to the API: {error}");
 334        //    });
 0335    }
 336
 2337    internal void GoToEventsSubSection() { OnGoToEventsSubSection?.Invoke(); }
 338
 339    private void OnChannelToJoinChanged(string currentChannelId, string previousChannelId)
 340    {
 0341        if (!string.IsNullOrEmpty(currentChannelId))
 0342            return;
 343
 0344        view.HidePlaceModal();
 0345        view.HideEventModal();
 0346        dataStore.exploreV2.currentVisibleModal.Set(ExploreV2CurrentModal.None);
 0347        OnCloseExploreV2?.Invoke();
 0348    }
 349}