| | 1 | | using DCL; |
| | 2 | | using ExploreV2Analytics; |
| | 3 | | using System; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Linq; |
| | 6 | | using UnityEngine; |
| | 7 | | using static HotScenesController; |
| | 8 | |
|
| | 9 | | public interface IPlacesSubSectionComponentController : 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 places from the API. |
| | 18 | | /// </summary> |
| | 19 | | void RequestAllPlaces(); |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// Load the places with the last requested ones. |
| | 23 | | /// </summary> |
| | 24 | | void LoadPlaces(); |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// Increment the number of places loaded. |
| | 28 | | /// </summary> |
| | 29 | | void ShowMorePlaces(); |
| | 30 | | } |
| | 31 | |
|
| | 32 | | public class PlacesSubSectionComponentController : IPlacesSubSectionComponentController |
| | 33 | | { |
| | 34 | | public event Action OnCloseExploreV2; |
| | 35 | | internal event Action OnPlacesFromAPIUpdated; |
| | 36 | |
|
| | 37 | | internal const int INITIAL_NUMBER_OF_ROWS = 5; |
| | 38 | | internal const int SHOW_MORE_ROWS_INCREMENT = 3; |
| | 39 | | internal IPlacesSubSectionComponentView view; |
| | 40 | | internal IPlacesAPIController placesAPIApiController; |
| | 41 | | internal FriendTrackerController friendsTrackerController; |
| 18 | 42 | | internal List<HotSceneInfo> placesFromAPI = new List<HotSceneInfo>(); |
| | 43 | | internal int currentPlacesShowed = 0; |
| | 44 | | internal bool reloadPlaces = false; |
| | 45 | | internal IExploreV2Analytics exploreV2Analytics; |
| | 46 | | internal float lastTimeAPIChecked = 0; |
| | 47 | |
|
| 18 | 48 | | public PlacesSubSectionComponentController( |
| | 49 | | IPlacesSubSectionComponentView view, |
| | 50 | | IPlacesAPIController placesAPI, |
| | 51 | | IFriendsController friendsController, |
| | 52 | | IExploreV2Analytics exploreV2Analytics) |
| | 53 | | { |
| 18 | 54 | | this.view = view; |
| 18 | 55 | | this.view.OnReady += FirstLoading; |
| 18 | 56 | | this.view.OnInfoClicked += ShowPlaceDetailedInfo; |
| 18 | 57 | | this.view.OnJumpInClicked += JumpInToPlace; |
| 18 | 58 | | this.view.OnFriendHandlerAdded += View_OnFriendHandlerAdded; |
| 18 | 59 | | this.view.OnShowMorePlacesClicked += ShowMorePlaces; |
| | 60 | |
|
| 18 | 61 | | placesAPIApiController = placesAPI; |
| 18 | 62 | | OnPlacesFromAPIUpdated += OnRequestedPlacesUpdated; |
| | 63 | |
|
| 18 | 64 | | friendsTrackerController = new FriendTrackerController(friendsController, view.currentFriendColors); |
| | 65 | |
|
| 18 | 66 | | this.exploreV2Analytics = exploreV2Analytics; |
| | 67 | |
|
| 18 | 68 | | view.ConfigurePools(); |
| 18 | 69 | | } |
| | 70 | |
|
| | 71 | | internal void FirstLoading() |
| | 72 | | { |
| 1 | 73 | | reloadPlaces = true; |
| 1 | 74 | | lastTimeAPIChecked = Time.realtimeSinceStartup - PlacesAndEventsSectionComponentController.MIN_TIME_TO_CHECK_API |
| 1 | 75 | | RequestAllPlaces(); |
| | 76 | |
|
| 1 | 77 | | view.OnPlacesSubSectionEnable += RequestAllPlaces; |
| 1 | 78 | | DataStore.i.exploreV2.isOpen.OnChange += OnExploreV2Open; |
| 1 | 79 | | } |
| | 80 | |
|
| | 81 | | internal void OnExploreV2Open(bool current, bool previous) |
| | 82 | | { |
| 0 | 83 | | if (current) |
| 0 | 84 | | return; |
| | 85 | |
|
| 0 | 86 | | reloadPlaces = true; |
| 0 | 87 | | } |
| | 88 | |
|
| | 89 | | public void RequestAllPlaces() |
| | 90 | | { |
| 2 | 91 | | if (!reloadPlaces) |
| 0 | 92 | | return; |
| | 93 | |
|
| 2 | 94 | | view.RestartScrollViewPosition(); |
| | 95 | |
|
| 2 | 96 | | if (Time.realtimeSinceStartup < lastTimeAPIChecked + PlacesAndEventsSectionComponentController.MIN_TIME_TO_CHECK |
| 0 | 97 | | return; |
| | 98 | |
|
| 2 | 99 | | currentPlacesShowed = view.currentPlacesPerRow * INITIAL_NUMBER_OF_ROWS; |
| 2 | 100 | | view.SetPlacesAsLoading(true); |
| 2 | 101 | | view.SetShowMorePlacesButtonActive(false); |
| | 102 | |
|
| 2 | 103 | | reloadPlaces = false; |
| 2 | 104 | | lastTimeAPIChecked = Time.realtimeSinceStartup; |
| | 105 | |
|
| 2 | 106 | | if (!DataStore.i.exploreV2.isInShowAnimationTransiton.Get()) |
| 2 | 107 | | RequestAllPlacesFromAPI(); |
| | 108 | | else |
| 0 | 109 | | DataStore.i.exploreV2.isInShowAnimationTransiton.OnChange += IsInShowAnimationTransitonChanged; |
| 0 | 110 | | } |
| | 111 | |
|
| | 112 | | internal void IsInShowAnimationTransitonChanged(bool current, bool previous) |
| | 113 | | { |
| 0 | 114 | | DataStore.i.exploreV2.isInShowAnimationTransiton.OnChange -= IsInShowAnimationTransitonChanged; |
| 0 | 115 | | RequestAllPlacesFromAPI(); |
| 0 | 116 | | } |
| | 117 | |
|
| | 118 | | internal void RequestAllPlacesFromAPI() |
| | 119 | | { |
| 3 | 120 | | placesAPIApiController.GetAllPlaces( |
| | 121 | | (placeList) => |
| | 122 | | { |
| 0 | 123 | | placesFromAPI = placeList; |
| 0 | 124 | | OnPlacesFromAPIUpdated?.Invoke(); |
| 0 | 125 | | }); |
| 3 | 126 | | } |
| | 127 | |
|
| 2 | 128 | | internal void OnRequestedPlacesUpdated() { LoadPlaces(); } |
| | 129 | |
|
| | 130 | | public void LoadPlaces() |
| | 131 | | { |
| 2 | 132 | | friendsTrackerController.RemoveAllHandlers(); |
| | 133 | |
|
| 2 | 134 | | List<PlaceCardComponentModel> places = new List<PlaceCardComponentModel>(); |
| 2 | 135 | | List<HotSceneInfo> placesFiltered = placesFromAPI.Take(currentPlacesShowed).ToList(); |
| 4 | 136 | | foreach (HotSceneInfo receivedPlace in placesFiltered) |
| | 137 | | { |
| 0 | 138 | | PlaceCardComponentModel placeCardModel = ExplorePlacesUtils.CreatePlaceCardModelFromAPIPlace(receivedPlace); |
| 0 | 139 | | places.Add(placeCardModel); |
| | 140 | | } |
| | 141 | |
|
| 2 | 142 | | view.SetPlaces(places); |
| 2 | 143 | | view.SetShowMorePlacesButtonActive(currentPlacesShowed < placesFromAPI.Count); |
| 2 | 144 | | view.SetPlacesAsLoading(false); |
| 2 | 145 | | } |
| | 146 | |
|
| | 147 | | public void ShowMorePlaces() |
| | 148 | | { |
| 2 | 149 | | List<PlaceCardComponentModel> places = new List<PlaceCardComponentModel>(); |
| 2 | 150 | | List<HotSceneInfo> placesFiltered = new List<HotSceneInfo>(); |
| 2 | 151 | | int numberOfExtraItemsToAdd = ((int)Mathf.Ceil((float)currentPlacesShowed / view.currentPlacesPerRow) * view.cur |
| 2 | 152 | | int numberOfItemsToAdd = view.currentPlacesPerRow * SHOW_MORE_ROWS_INCREMENT + numberOfExtraItemsToAdd; |
| | 153 | |
|
| 2 | 154 | | if (currentPlacesShowed + numberOfItemsToAdd <= placesFromAPI.Count) |
| 2 | 155 | | placesFiltered = placesFromAPI.GetRange(currentPlacesShowed, numberOfItemsToAdd); |
| | 156 | | else |
| 0 | 157 | | placesFiltered = placesFromAPI.GetRange(currentPlacesShowed, placesFromAPI.Count - currentPlacesShowed); |
| | 158 | |
|
| 4 | 159 | | foreach (HotSceneInfo receivedPlace in placesFiltered) |
| | 160 | | { |
| 0 | 161 | | PlaceCardComponentModel placeCardModel = ExplorePlacesUtils.CreatePlaceCardModelFromAPIPlace(receivedPlace); |
| 0 | 162 | | places.Add(placeCardModel); |
| | 163 | | } |
| | 164 | |
|
| 2 | 165 | | view.AddPlaces(places); |
| | 166 | |
|
| 2 | 167 | | currentPlacesShowed += numberOfItemsToAdd; |
| 2 | 168 | | if (currentPlacesShowed > placesFromAPI.Count) |
| 0 | 169 | | currentPlacesShowed = placesFromAPI.Count; |
| | 170 | |
|
| 2 | 171 | | view.SetShowMorePlacesButtonActive(currentPlacesShowed < placesFromAPI.Count); |
| 2 | 172 | | } |
| | 173 | |
|
| | 174 | | public void Dispose() |
| | 175 | | { |
| 18 | 176 | | view.OnReady -= FirstLoading; |
| 18 | 177 | | view.OnInfoClicked -= ShowPlaceDetailedInfo; |
| 18 | 178 | | view.OnJumpInClicked -= JumpInToPlace; |
| 18 | 179 | | view.OnPlacesSubSectionEnable -= RequestAllPlaces; |
| 18 | 180 | | view.OnFriendHandlerAdded -= View_OnFriendHandlerAdded; |
| 18 | 181 | | view.OnShowMorePlacesClicked -= ShowMorePlaces; |
| 18 | 182 | | OnPlacesFromAPIUpdated -= OnRequestedPlacesUpdated; |
| 18 | 183 | | DataStore.i.exploreV2.isOpen.OnChange -= OnExploreV2Open; |
| 18 | 184 | | } |
| | 185 | |
|
| | 186 | | internal void ShowPlaceDetailedInfo(PlaceCardComponentModel placeModel) |
| | 187 | | { |
| 1 | 188 | | view.ShowPlaceModal(placeModel); |
| 1 | 189 | | exploreV2Analytics.SendClickOnPlaceInfo(placeModel.hotSceneInfo.id, placeModel.placeName); |
| 1 | 190 | | } |
| | 191 | |
|
| | 192 | | internal void JumpInToPlace(HotSceneInfo placeFromAPI) |
| | 193 | | { |
| 1 | 194 | | ExplorePlacesUtils.JumpInToPlace(placeFromAPI); |
| 1 | 195 | | view.HidePlaceModal(); |
| 1 | 196 | | OnCloseExploreV2?.Invoke(); |
| 1 | 197 | | exploreV2Analytics.SendPlaceTeleport(placeFromAPI.id, placeFromAPI.name, placeFromAPI.baseCoords); |
| 1 | 198 | | } |
| | 199 | |
|
| 0 | 200 | | internal void View_OnFriendHandlerAdded(FriendsHandler friendsHandler) { friendsTrackerController.AddHandler(friends |
| | 201 | | } |