< Summary

Class:SearchSubSectionComponentController
Assembly:ExploreV2
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/ExploreV2/Scripts/Sections/PlacesAndEventsSection/SubSections/SearchSubSection/SearchSubSectionComponentController.cs
Covered lines:88
Uncovered lines:66
Coverable lines:154
Total lines:311
Line coverage:57.1% (88 of 154)
Covered branches:0
Total branches:0
Covered methods:12
Total methods:21
Method coverage:57.1% (12 of 21)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
SearchSubSectionComponentController(...)0%220100%
ChangeVote(...)0%4.024088.89%
ChangePlaceFavorite(...)0%3.033085.71%
CloseSearchPanel()0%2100%
JumpInToEvent(...)0%220100%
JumpInToPlace(...)0%220100%
OpenEventDetailsModal(...)0%110100%
OpenPlaceDetailsModal(...)0%110100%
OpenWorldDetailsModal(...)0%2100%
Search(...)0%2100%
SubscribeToEvent(...)0%2.032080%
UnsubscribeToEvent(...)0%110100%
SearchAllEvents(...)0%110100%
SearchAllPlaces(...)0%2100%
SearchAllWorlds(...)0%2100%
SearchEvents()0%3.333066.67%
>c__DisplayClass30_0/<<SearchEvents()0%41.5122065.71%
SearchPlaces()0%42600%
SearchWorlds()0%30500%
JumpInToWorld(...)0%6200%
Dispose()0%6200%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/ExploreV2/Scripts/Sections/PlacesAndEventsSection/SubSections/SearchSubSection/SearchSubSectionComponentController.cs

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using DCL;
 3using DCL.Helpers;
 4using DCL.Tasks;
 5using DCLServices.PlacesAPIService;
 6using DCLServices.WorldsAPIService;
 7using System.Collections.Generic;
 8using ExploreV2Analytics;
 9using MainScripts.DCL.Controllers.HotScenes;
 10using System;
 11using System.Linq;
 12using System.Threading;
 13using UnityEngine;
 14using Environment = DCL.Environment;
 15
 16public class SearchSubSectionComponentController : ISearchSubSectionComponentController
 17{
 18    private readonly ISearchSubSectionComponentView view;
 19    private readonly SearchBarComponentView searchBarComponentView;
 20    private readonly IEventsAPIController eventsAPI;
 21    private readonly IPlacesAPIService placesAPIService;
 22    private readonly IWorldsAPIService worldsAPIService;
 23    private readonly IUserProfileBridge userProfileBridge;
 24    private readonly IExploreV2Analytics exploreV2Analytics;
 25    private readonly IPlacesAnalytics placesAnalytics;
 26    private readonly DataStore dataStore;
 27
 28    public event Action OnCloseExploreV2;
 29
 30    private CancellationTokenSource minimalSearchCts;
 31    private CancellationTokenSource fullSearchCts;
 32    private CancellationTokenSource getPlacesAssociatedToEventsCts;
 33
 1834    public SearchSubSectionComponentController(ISearchSubSectionComponentView view,
 35        SearchBarComponentView searchBarComponentView,
 36        IEventsAPIController eventsAPI,
 37        IPlacesAPIService placesAPIService,
 38        IWorldsAPIService worldsAPIService,
 39        IUserProfileBridge userProfileBridge,
 40        IExploreV2Analytics exploreV2Analytics,
 41        IPlacesAnalytics placesAnalytics,
 42        DataStore dataStore)
 43    {
 1844        this.view = view;
 1845        this.searchBarComponentView = searchBarComponentView;
 1846        this.eventsAPI = eventsAPI;
 1847        this.placesAPIService = placesAPIService;
 1848        this.worldsAPIService = worldsAPIService;
 1849        this.userProfileBridge = userProfileBridge;
 1850        this.exploreV2Analytics = exploreV2Analytics;
 1851        this.placesAnalytics = placesAnalytics;
 1852        this.dataStore = dataStore;
 53
 1854        view.OnRequestAllEvents += SearchAllEvents;
 1855        view.OnRequestAllPlaces += SearchAllPlaces;
 1856        view.OnRequestAllWorlds += SearchAllWorlds;
 1857        view.OnEventInfoClicked += OpenEventDetailsModal;
 1858        view.OnPlaceInfoClicked += OpenPlaceDetailsModal;
 1859        view.OnWorldInfoClicked += OpenWorldDetailsModal;
 1860        view.OnVoteChanged += ChangeVote;
 1861        view.OnSubscribeEventClicked += SubscribeToEvent;
 1862        view.OnUnsubscribeEventClicked += UnsubscribeToEvent;
 1863        view.OnEventJumpInClicked += JumpInToEvent;
 1864        view.OnPlaceJumpInClicked += JumpInToPlace;
 1865        view.OnWorldJumpInClicked += JumpInToWorld;
 1866        view.OnBackFromSearch += CloseSearchPanel;
 1867        view.OnPlaceFavoriteChanged += ChangePlaceFavorite;
 68
 1869        if(searchBarComponentView != null)
 1270            searchBarComponentView.OnSearchText += Search;
 1871    }
 72
 73    private void ChangeVote(string placeId, bool? isUpvote)
 74    {
 375        if (userProfileBridge.GetOwn().isGuest)
 076            dataStore.HUDs.connectWalletModalVisible.Set(true);
 77        else
 78        {
 379            if (isUpvote != null)
 80            {
 281                if (isUpvote.Value)
 182                    placesAnalytics.Like(placeId, IPlacesAnalytics.ActionSource.FromSearch);
 83                else
 184                    placesAnalytics.Dislike(placeId, IPlacesAnalytics.ActionSource.FromSearch);
 85            }
 86            else
 187                placesAnalytics.RemoveVote(placeId, IPlacesAnalytics.ActionSource.FromSearch);
 88
 389            placesAPIService.SetPlaceVote(isUpvote, placeId, default).Forget();
 90        }
 391    }
 92
 93    private void ChangePlaceFavorite(string placeId, bool isFavorite)
 94    {
 295        if (userProfileBridge.GetOwn().isGuest)
 096            dataStore.HUDs.connectWalletModalVisible.Set(true);
 97        else
 98        {
 299            if(isFavorite)
 1100                placesAnalytics.AddFavorite(placeId, IPlacesAnalytics.ActionSource.FromSearch);
 101            else
 1102                placesAnalytics.RemoveFavorite(placeId, IPlacesAnalytics.ActionSource.FromSearch);
 103
 2104            placesAPIService.SetPlaceFavorite(placeId, isFavorite, default).Forget();
 105        }
 2106    }
 107
 108    private void CloseSearchPanel()
 109    {
 0110        searchBarComponentView.SubmitSearch("");
 0111    }
 112
 113    private void JumpInToEvent(EventFromAPIModel eventFromAPI)
 114    {
 1115        OnCloseExploreV2?.Invoke();
 1116        EventsSubSectionComponentController.JumpInToEvent(eventFromAPI);
 1117        exploreV2Analytics.SendEventTeleport(eventFromAPI.id, eventFromAPI.name, eventFromAPI.world, new Vector2Int(even
 1118    }
 119
 120    private void JumpInToPlace(IHotScenesController.PlaceInfo place)
 121    {
 1122        OnCloseExploreV2?.Invoke();
 1123        PlacesSubSectionComponentController.JumpInToPlace(place);
 1124        exploreV2Analytics.SendPlaceTeleport(place.id, place.title, Utils.ConvertStringToVector(place.base_position), Ac
 1125    }
 126
 127    private void OpenEventDetailsModal(EventCardComponentModel eventModel, int index)
 128    {
 1129        view.ShowEventModal(eventModel);
 1130        exploreV2Analytics.SendClickOnEventInfo(eventModel.eventId, eventModel.eventName, !string.IsNullOrEmpty(eventMod
 1131    }
 132
 133    private void OpenPlaceDetailsModal(PlaceCardComponentModel placeModel, int index)
 134    {
 1135        view.ShowPlaceModal(placeModel);
 1136        exploreV2Analytics.SendClickOnPlaceInfo(placeModel.placeInfo.id, placeModel.placeName, index, ActionSource.FromS
 1137    }
 138
 139    private void OpenWorldDetailsModal(PlaceCardComponentModel placeModel, int index)
 140    {
 0141        view.ShowWorldModal(placeModel);
 0142        exploreV2Analytics.SendClickOnPlaceInfo(placeModel.placeInfo.id, placeModel.placeName, index, ActionSource.FromS
 0143    }
 144
 145    private void Search(string searchText)
 146    {
 0147        minimalSearchCts.SafeCancelAndDispose();
 0148        minimalSearchCts = new CancellationTokenSource();
 149
 0150        view.SetAllAsLoading();
 0151        view.SetHeaderEnabled(searchText);
 0152        SearchEvents(searchText, cancellationToken: minimalSearchCts.Token).Forget();
 0153        SearchPlaces(searchText, cancellationToken: minimalSearchCts.Token).Forget();
 0154        SearchWorlds(searchText, cancellationToken: minimalSearchCts.Token).Forget();
 0155    }
 156
 157    private void SubscribeToEvent(string eventId, bool isWorld)
 158    {
 1159        if (userProfileBridge.GetOwn().isGuest)
 0160            dataStore.HUDs.connectWalletModalVisible.Set(true);
 161        else
 162        {
 1163            exploreV2Analytics.SendParticipateEvent(eventId, isWorld, ActionSource.FromSearch);
 1164            eventsAPI.RegisterParticipation(eventId);
 165        }
 1166    }
 167
 168    private void UnsubscribeToEvent(string eventId, bool isWorld)
 169    {
 1170        eventsAPI.RemoveParticipation(eventId);
 1171        exploreV2Analytics.SendParticipateEvent(eventId, isWorld, ActionSource.FromSearch);
 1172    }
 173
 174    private void SearchAllEvents(int pageNumber)
 175    {
 1176        fullSearchCts.SafeCancelAndDispose();
 1177        fullSearchCts = new CancellationTokenSource();
 1178        SearchEvents(searchBarComponentView.Text, pageNumber, 18, fullSearchCts.Token, true).Forget();
 1179    }
 180
 181    private void SearchAllPlaces(int pageNumber)
 182    {
 0183        fullSearchCts.SafeCancelAndDispose();
 0184        fullSearchCts = new CancellationTokenSource();
 0185        SearchPlaces(searchBarComponentView.Text, pageNumber, 18, fullSearchCts.Token, true).Forget();
 0186    }
 187
 188    private void SearchAllWorlds(int pageNumber)
 189    {
 0190        fullSearchCts.SafeCancelAndDispose();
 0191        fullSearchCts = new CancellationTokenSource();
 0192        SearchWorlds(searchBarComponentView.Text, pageNumber, 18, fullSearchCts.Token, true).Forget();
 0193    }
 194
 195    private async UniTaskVoid SearchEvents(string searchText, int pageNumber = 0, int pageSize = 6, CancellationToken ca
 196    {
 1197        var results = await eventsAPI.SearchEvents(searchText, pageNumber,pageSize, cancellationToken);
 198
 1199        getPlacesAssociatedToEventsCts = getPlacesAssociatedToEventsCts.SafeRestart();
 1200        GetPlacesAssociatedToEventsAsync(results, getPlacesAssociatedToEventsCts.Token).Forget();
 201
 202        async UniTaskVoid GetPlacesAssociatedToEventsAsync((List<EventFromAPIModel> eventsFromAPI, int total) searchData
 203        {
 204            // Land's events
 3205            var landEventsFromAPI = searchData.eventsFromAPI.Where(e => !e.world).ToList();
 1206            var coordsList = landEventsFromAPI.Select(e => new Vector2Int(e.coordinates[0], e.coordinates[1]));
 1207            var places = await placesAPIService.GetPlacesByCoordsList(coordsList, ct);
 208
 6209            foreach (EventFromAPIModel landEventFromAPI in landEventsFromAPI)
 210            {
 2211                Vector2Int landEventCoords = new Vector2Int(landEventFromAPI.coordinates[0], landEventFromAPI.coordinate
 10212                foreach (IHotScenesController.PlaceInfo place in places)
 213                {
 4214                    if (!place.Positions.Contains(landEventCoords))
 215                        continue;
 216
 2217                    landEventFromAPI.scene_name = place.title;
 2218                    break;
 219                }
 220            }
 221
 222            // World's events
 3223            var worldEventsFromAPI = searchData.eventsFromAPI.Where(e => e.world).ToList();
 1224            var worldNamesList = worldEventsFromAPI.Select(e => e.server);
 1225            var worlds = await worldsAPIService.GetWorldsByNamesList(worldNamesList, ct);
 226
 2227            foreach (EventFromAPIModel worldEventFromAPI in worldEventsFromAPI)
 228            {
 0229                foreach (WorldsResponse.WorldInfo world in worlds)
 230                {
 0231                    if (world.world_name != worldEventFromAPI.server)
 232                        continue;
 233
 0234                    worldEventFromAPI.scene_name = world.title;
 0235                    break;
 236                }
 237            }
 238
 1239            List<EventCardComponentModel> searchedEvents = PlacesAndEventsCardsFactory.CreateEventsCards(searchData.even
 5240            exploreV2Analytics.SendSearchEvents(searchText, searchedEvents.Select(e=>e.coords).ToArray(), searchedEvents
 241
 1242            if (isFullSearch)
 1243                view.ShowAllEvents(searchedEvents, (pageNumber + 1) * pageSize < searchData.total);
 244            else
 0245                view.ShowEvents(searchedEvents, searchText);
 1246        }
 1247    }
 248
 249    private async UniTaskVoid SearchPlaces(string searchText, int pageNumber = 0, int pageSize = 6, CancellationToken ca
 250    {
 0251        var results = await placesAPIService.SearchPlaces(searchText, pageNumber, pageSize, cancellationToken);
 0252        List<PlaceCardComponentModel> places = PlacesAndEventsCardsFactory.ConvertPlaceResponseToModel(results.Item1);
 0253        exploreV2Analytics.SendSearchPlaces(searchText, places.Select(p=>p.coords).ToArray(), places.Select(p=>p.placeIn
 254
 0255        if (isFullSearch)
 256        {
 0257            view.ShowAllPlaces(places, (pageNumber + 1) * pageSize < results.total);
 258        }
 259        else
 260        {
 0261            view.ShowPlaces(places, searchText);
 262        }
 0263    }
 264
 265    private async UniTaskVoid SearchWorlds(string searchText, int pageNumber = 0, int pageSize = 6, CancellationToken ca
 266    {
 0267        var results = await worldsAPIService.SearchWorlds(searchText, pageNumber, pageSize, cancellationToken);
 0268        List<PlaceCardComponentModel> worlds = PlacesAndEventsCardsFactory.ConvertWorldsResponseToModel(results.Item1);
 0269        exploreV2Analytics.SendSearchWorlds(searchText, worlds.Select(p=>p.placeInfo.id).ToArray());
 270
 0271        if (isFullSearch)
 272        {
 0273            view.ShowAllWorlds(worlds, (pageNumber + 1) * pageSize < results.total);
 274        }
 275        else
 276        {
 0277            view.ShowWorlds(worlds, searchText);
 278        }
 0279    }
 280
 281    private void JumpInToWorld(IHotScenesController.PlaceInfo worldFromAPI)
 282    {
 0283        Environment.i.world.teleportController.JumpInWorld(worldFromAPI.world_name);
 0284        view.HideWorldModal();
 0285        dataStore.exploreV2.currentVisibleModal.Set(ExploreV2CurrentModal.None);
 0286        OnCloseExploreV2?.Invoke();
 0287        exploreV2Analytics.SendWorldTeleport(worldFromAPI.id, worldFromAPI.title);
 0288    }
 289
 290    public void Dispose()
 291    {
 0292        getPlacesAssociatedToEventsCts.SafeCancelAndDispose();
 293
 0294        view.OnRequestAllEvents -= SearchAllEvents;
 0295        view.OnRequestAllPlaces -= SearchAllPlaces;
 0296        view.OnRequestAllWorlds -= SearchAllWorlds;
 0297        view.OnEventInfoClicked -= OpenEventDetailsModal;
 0298        view.OnPlaceInfoClicked -= OpenPlaceDetailsModal;
 0299        view.OnEventInfoClicked -= OpenEventDetailsModal;
 0300        view.OnSubscribeEventClicked -= SubscribeToEvent;
 0301        view.OnUnsubscribeEventClicked -= UnsubscribeToEvent;
 0302        view.OnEventJumpInClicked -= JumpInToEvent;
 0303        view.OnPlaceJumpInClicked -= JumpInToPlace;
 0304        view.OnWorldJumpInClicked -= JumpInToWorld;
 0305        view.OnBackFromSearch -= CloseSearchPanel;
 0306        view.OnPlaceFavoriteChanged -= ChangePlaceFavorite;
 307
 0308        if(searchBarComponentView != null)
 0309            searchBarComponentView.OnSearchText -= Search;
 0310    }
 311}