| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using DCL.Social.Friends; |
| | 5 | | using DCL.Tasks; |
| | 6 | | using DCLServices.PlacesAPIService; |
| | 7 | | using ExploreV2Analytics; |
| | 8 | | using System; |
| | 9 | | using System.Collections.Generic; |
| | 10 | | using System.Linq; |
| | 11 | | using UnityEngine; |
| | 12 | | using System.Threading; |
| | 13 | | using Environment = DCL.Environment; |
| | 14 | | using static MainScripts.DCL.Controllers.HotScenes.IHotScenesController; |
| | 15 | |
|
| | 16 | | public class PlacesSubSectionComponentController : IPlacesSubSectionComponentController, IPlacesAndEventsAPIRequester |
| | 17 | | { |
| | 18 | | public event Action OnCloseExploreV2; |
| | 19 | |
|
| | 20 | | internal const int INITIAL_NUMBER_OF_ROWS = 4; |
| | 21 | | private const int PAGE_SIZE = 12; |
| | 22 | | private const string MOST_ACTIVE_FILTER_ID = "most_active"; |
| | 23 | |
|
| | 24 | | internal readonly IPlacesSubSectionComponentView view; |
| | 25 | | internal readonly IPlacesAPIService placesAPIService; |
| | 26 | | internal readonly FriendTrackerController friendsTrackerController; |
| | 27 | | private readonly IExploreV2Analytics exploreV2Analytics; |
| | 28 | | private readonly IPlacesAnalytics placesAnalytics; |
| | 29 | | private readonly DataStore dataStore; |
| | 30 | | private readonly IUserProfileBridge userProfileBridge; |
| | 31 | | private IReadOnlyList<string> allPointOfInterest; |
| | 32 | |
|
| | 33 | | internal readonly PlaceAndEventsCardsReloader cardsReloader; |
| | 34 | |
|
| 17 | 35 | | internal readonly List<PlaceInfo> placesFromAPI = new (); |
| | 36 | | internal int availableUISlots; |
| | 37 | | private CancellationTokenSource getCategoriesCts; |
| 17 | 38 | | private CancellationTokenSource getPlacesCts = new (); |
| 17 | 39 | | private CancellationTokenSource showMoreCts = new (); |
| 17 | 40 | | private CancellationTokenSource disposeCts = new (); |
| | 41 | |
|
| 17 | 42 | | public PlacesSubSectionComponentController( |
| | 43 | | IPlacesSubSectionComponentView view, |
| | 44 | | IPlacesAPIService placesAPI, |
| | 45 | | IFriendsController friendsController, |
| | 46 | | IExploreV2Analytics exploreV2Analytics, |
| | 47 | | IPlacesAnalytics placesAnalytics, |
| | 48 | | DataStore dataStore, |
| | 49 | | IUserProfileBridge userProfileBridge) |
| | 50 | | { |
| 17 | 51 | | cardsReloader = new PlaceAndEventsCardsReloader(view, this, dataStore.exploreV2); |
| | 52 | |
|
| 17 | 53 | | this.view = view; |
| | 54 | |
|
| 17 | 55 | | this.view.OnReady += FirstLoading; |
| | 56 | |
|
| 17 | 57 | | this.view.OnInfoClicked += ShowPlaceDetailedInfo; |
| 17 | 58 | | this.view.OnJumpInClicked += OnJumpInToPlace; |
| 17 | 59 | | this.view.OnFavoriteClicked += View_OnFavoritesClicked; |
| 17 | 60 | | this.view.OnVoteChanged += View_OnVoteChanged; |
| 17 | 61 | | this.view.OnShowMorePlacesClicked += ShowMorePlaces; |
| | 62 | |
|
| 17 | 63 | | this.view.OnFriendHandlerAdded += View_OnFriendHandlerAdded; |
| | 64 | |
|
| 17 | 65 | | this.dataStore = dataStore; |
| 17 | 66 | | this.dataStore.channels.currentJoinChannelModal.OnChange += OnChannelToJoinChanged; |
| | 67 | |
|
| 17 | 68 | | placesAPIService = placesAPI; |
| | 69 | |
|
| 17 | 70 | | friendsTrackerController = new FriendTrackerController(friendsController, view.currentFriendColors); |
| | 71 | |
|
| 17 | 72 | | this.exploreV2Analytics = exploreV2Analytics; |
| 17 | 73 | | this.placesAnalytics = placesAnalytics; |
| | 74 | |
|
| 17 | 75 | | this.userProfileBridge = userProfileBridge; |
| | 76 | |
|
| 17 | 77 | | view.ConfigurePools(); |
| 17 | 78 | | } |
| | 79 | |
|
| | 80 | | public void Dispose() |
| | 81 | | { |
| 17 | 82 | | disposeCts?.SafeCancelAndDispose(); |
| 17 | 83 | | showMoreCts?.SafeCancelAndDispose(); |
| 17 | 84 | | getCategoriesCts?.SafeCancelAndDispose(); |
| 17 | 85 | | getPlacesCts?.SafeCancelAndDispose(); |
| | 86 | |
|
| 17 | 87 | | view.OnReady -= FirstLoading; |
| 17 | 88 | | view.OnInfoClicked -= ShowPlaceDetailedInfo; |
| 17 | 89 | | view.OnJumpInClicked -= OnJumpInToPlace; |
| 17 | 90 | | view.OnFavoriteClicked -= View_OnFavoritesClicked; |
| 17 | 91 | | this.view.OnVoteChanged -= View_OnVoteChanged; |
| 17 | 92 | | view.OnPlacesSubSectionEnable -= OpenTab; |
| 17 | 93 | | view.OnFilterChanged -= ApplyFilters; |
| 17 | 94 | | view.OnSortingChanged -= ApplySorting; |
| 17 | 95 | | view.OnFriendHandlerAdded -= View_OnFriendHandlerAdded; |
| 17 | 96 | | view.OnShowMorePlacesClicked -= ShowMorePlaces; |
| | 97 | |
|
| 17 | 98 | | dataStore.channels.currentJoinChannelModal.OnChange -= OnChannelToJoinChanged; |
| | 99 | |
|
| 17 | 100 | | cardsReloader.Dispose(); |
| 17 | 101 | | } |
| | 102 | |
|
| | 103 | | private void View_OnVoteChanged(string placeId, bool? isUpvote) |
| | 104 | | { |
| 0 | 105 | | if (userProfileBridge.GetOwn().isGuest) |
| 0 | 106 | | dataStore.HUDs.connectWalletModalVisible.Set(true); |
| | 107 | | else |
| | 108 | | { |
| 0 | 109 | | if (isUpvote != null) |
| | 110 | | { |
| 0 | 111 | | if (isUpvote.Value) |
| 0 | 112 | | placesAnalytics.Like(placeId, IPlacesAnalytics.ActionSource.FromExplore); |
| | 113 | | else |
| 0 | 114 | | placesAnalytics.Dislike(placeId, IPlacesAnalytics.ActionSource.FromExplore); |
| | 115 | | } |
| | 116 | | else |
| 0 | 117 | | placesAnalytics.RemoveVote(placeId, IPlacesAnalytics.ActionSource.FromExplore); |
| | 118 | |
|
| 0 | 119 | | placesAPIService.SetPlaceVote(isUpvote, placeId, disposeCts.Token); |
| | 120 | | } |
| 0 | 121 | | } |
| | 122 | |
|
| | 123 | | private void View_OnFavoritesClicked(string placeUUID, bool isFavorite) |
| | 124 | | { |
| 0 | 125 | | if (userProfileBridge.GetOwn().isGuest) |
| 0 | 126 | | dataStore.HUDs.connectWalletModalVisible.Set(true); |
| | 127 | | else |
| | 128 | | { |
| 0 | 129 | | if (isFavorite) |
| 0 | 130 | | placesAnalytics.AddFavorite(placeUUID, IPlacesAnalytics.ActionSource.FromExplore); |
| | 131 | | else |
| 0 | 132 | | placesAnalytics.RemoveFavorite(placeUUID, IPlacesAnalytics.ActionSource.FromExplore); |
| | 133 | |
|
| 0 | 134 | | placesAPIService.SetPlaceFavorite(placeUUID, isFavorite, disposeCts.Token); |
| | 135 | | } |
| 0 | 136 | | } |
| | 137 | |
|
| | 138 | | private void FirstLoading() |
| | 139 | | { |
| 0 | 140 | | view.OnPlacesSubSectionEnable += OpenTab; |
| 0 | 141 | | view.OnFilterChanged += ApplyFilters; |
| 0 | 142 | | view.OnSortingChanged += ApplySorting; |
| 0 | 143 | | cardsReloader.Initialize(); |
| | 144 | |
|
| 0 | 145 | | getCategoriesCts = getCategoriesCts.SafeRestart(); |
| 0 | 146 | | RequestPlaceCategoriesAsync(getCategoriesCts.Token).Forget(); |
| 0 | 147 | | } |
| | 148 | |
|
| | 149 | | private void OpenTab() |
| | 150 | | { |
| 0 | 151 | | exploreV2Analytics.SendPlacesTabOpen(); |
| 0 | 152 | | RequestAllPlaces(); |
| 0 | 153 | | } |
| | 154 | |
|
| | 155 | | private void ApplyFilters() |
| | 156 | | { |
| 0 | 157 | | if (!string.IsNullOrEmpty(view.filter)) |
| 0 | 158 | | placesAnalytics.Filter(view.filter.Replace("categories=", "")); |
| | 159 | |
|
| 0 | 160 | | RequestAllPlaces(); |
| 0 | 161 | | } |
| | 162 | |
|
| | 163 | | private void ApplySorting() |
| | 164 | | { |
| 0 | 165 | | placesAnalytics.Sort(view.sort == MOST_ACTIVE_FILTER_ID ? IPlacesAnalytics.SortingType.MostActive : IPlacesAnaly |
| 0 | 166 | | RequestAllPlaces(); |
| 0 | 167 | | } |
| | 168 | |
|
| | 169 | | internal void RequestAllPlaces() |
| | 170 | | { |
| 2 | 171 | | if (cardsReloader.CanReload()) |
| | 172 | | { |
| 2 | 173 | | availableUISlots = view.CurrentTilesPerRow * INITIAL_NUMBER_OF_ROWS; |
| 2 | 174 | | view.SetShowMoreButtonActive(false); |
| | 175 | |
|
| 2 | 176 | | cardsReloader.RequestAll(); |
| | 177 | | } |
| 2 | 178 | | } |
| | 179 | |
|
| | 180 | | public void RequestAllFromAPI() |
| | 181 | | { |
| 2 | 182 | | getPlacesCts?.SafeCancelAndDispose(); |
| 2 | 183 | | getPlacesCts = CancellationTokenSource.CreateLinkedTokenSource(disposeCts.Token); |
| | 184 | |
|
| 2 | 185 | | RequestAllFromAPIAsync(getPlacesCts.Token).Forget(); |
| 2 | 186 | | } |
| | 187 | |
|
| | 188 | | private async UniTaskVoid RequestPlaceCategoriesAsync(CancellationToken ct) |
| | 189 | | { |
| | 190 | | try |
| | 191 | | { |
| 0 | 192 | | var allCategories = await placesAPIService.GetPlaceCategories(ct); |
| 0 | 193 | | view.SetPlaceCategories(allCategories.Select(x => (x.name, x.i18n.en)).ToList()); |
| 0 | 194 | | } |
| 0 | 195 | | catch (OperationCanceledException) { } |
| 0 | 196 | | } |
| | 197 | |
|
| | 198 | | private async UniTaskVoid RequestAllFromAPIAsync(CancellationToken ct) |
| | 199 | | { |
| | 200 | | try |
| | 201 | | { |
| 2 | 202 | | allPointOfInterest = await placesAPIService.GetPointsOfInterestCoords(ct); |
| | 203 | |
|
| 2 | 204 | | if (allPointOfInterest != null) |
| 0 | 205 | | view.SetPOICoords(allPointOfInterest.ToList()); |
| | 206 | |
|
| 2 | 207 | | (IReadOnlyList<PlaceInfo> places, int total) firstPage = await placesAPIService.GetMostActivePlaces(0, PAGE_ |
| 2 | 208 | | friendsTrackerController.RemoveAllHandlers(); |
| 2 | 209 | | placesFromAPI.Clear(); |
| 2 | 210 | | placesFromAPI.AddRange(firstPage.places); |
| 2 | 211 | | if (firstPage.total > PAGE_SIZE) |
| | 212 | | { |
| 0 | 213 | | (IReadOnlyList<PlaceInfo> places, int total) secondPage = await placesAPIService.GetMostActivePlaces(1, |
| 0 | 214 | | placesFromAPI.AddRange(secondPage.places); |
| | 215 | | } |
| | 216 | |
|
| 2 | 217 | | view.SetPlaces(PlacesAndEventsCardsFactory.ConvertPlaceResponseToModel(placesFromAPI, availableUISlots)); |
| 2 | 218 | | view.SetShowMorePlacesButtonActive(placesFromAPI.Count < firstPage.total); |
| 2 | 219 | | view.SetResultCounter(firstPage.total); |
| 2 | 220 | | } |
| 0 | 221 | | catch (OperationCanceledException) { } |
| 2 | 222 | | } |
| | 223 | |
|
| | 224 | | internal void ShowMorePlaces() |
| | 225 | | { |
| 2 | 226 | | showMoreCts?.SafeCancelAndDispose(); |
| 2 | 227 | | showMoreCts = CancellationTokenSource.CreateLinkedTokenSource(disposeCts.Token); |
| 2 | 228 | | ShowMorePlacesAsync(showMoreCts.Token).Forget(); |
| 2 | 229 | | } |
| | 230 | |
|
| | 231 | | private async UniTask ShowMorePlacesAsync(CancellationToken ct) |
| | 232 | | { |
| 2 | 233 | | (IReadOnlyList<PlaceInfo> places, int total) = await placesAPIService.GetMostActivePlaces((placesFromAPI.Count/P |
| | 234 | |
|
| 2 | 235 | | placesFromAPI.AddRange(places); |
| 2 | 236 | | view.AddPlaces(PlacesAndEventsCardsFactory.ConvertPlaceResponseToModel(places)); |
| 2 | 237 | | view.SetShowMorePlacesButtonActive(placesFromAPI.Count < total); |
| 2 | 238 | | } |
| | 239 | |
|
| | 240 | | internal void ShowPlaceDetailedInfo(PlaceCardComponentModel placeModel) |
| | 241 | | { |
| 1 | 242 | | view.ShowPlaceModal(placeModel); |
| 1 | 243 | | exploreV2Analytics.SendClickOnPlaceInfo(placeModel.placeInfo.id, placeModel.placeName); |
| | 244 | |
|
| 1 | 245 | | dataStore.exploreV2.currentVisibleModal.Set(ExploreV2CurrentModal.Places); |
| 1 | 246 | | } |
| | 247 | |
|
| | 248 | | internal void OnJumpInToPlace(PlaceInfo placeFromAPI) |
| | 249 | | { |
| 1 | 250 | | JumpInToPlace(placeFromAPI); |
| 1 | 251 | | view.HidePlaceModal(); |
| | 252 | |
|
| 1 | 253 | | dataStore.exploreV2.currentVisibleModal.Set(ExploreV2CurrentModal.None); |
| 1 | 254 | | OnCloseExploreV2?.Invoke(); |
| 1 | 255 | | exploreV2Analytics.SendPlaceTeleport(placeFromAPI.id, placeFromAPI.title, Utils.ConvertStringToVector(placeFromA |
| 1 | 256 | | } |
| | 257 | |
|
| | 258 | | private void View_OnFriendHandlerAdded(IFriendTrackerHandler friendsHandler) => |
| 0 | 259 | | friendsTrackerController.AddHandler(friendsHandler); |
| | 260 | |
|
| | 261 | | private void OnChannelToJoinChanged(string currentChannelId, string previousChannelId) |
| | 262 | | { |
| 0 | 263 | | if (!string.IsNullOrEmpty(currentChannelId)) |
| 0 | 264 | | return; |
| | 265 | |
|
| 0 | 266 | | view.HidePlaceModal(); |
| 0 | 267 | | dataStore.exploreV2.currentVisibleModal.Set(ExploreV2CurrentModal.None); |
| 0 | 268 | | OnCloseExploreV2?.Invoke(); |
| 0 | 269 | | } |
| | 270 | |
|
| | 271 | | /// <summary> |
| | 272 | | /// Makes a jump in to the place defined by the given place data from API. |
| | 273 | | /// </summary> |
| | 274 | | /// <param name="placeFromAPI">Place data from API.</param> |
| | 275 | | public static void JumpInToPlace(PlaceInfo placeFromAPI) |
| | 276 | | { |
| 2 | 277 | | PlaceInfo.Realm realm = new PlaceInfo.Realm() { layer = null, serverName = null }; |
| 5 | 278 | | placeFromAPI.realms_detail = placeFromAPI.realms_detail.OrderByDescending(x => x.usersCount).ToArray(); |
| | 279 | |
|
| 8 | 280 | | for (int i = 0; i < placeFromAPI.realms_detail.Length; i++) |
| | 281 | | { |
| 3 | 282 | | bool isArchipelagoRealm = string.IsNullOrEmpty(placeFromAPI.realms_detail[i].layer); |
| | 283 | |
|
| 3 | 284 | | if (isArchipelagoRealm || placeFromAPI.realms_detail[i].usersCount < placeFromAPI.realms_detail[i].maxUsers) |
| | 285 | | { |
| 1 | 286 | | realm = placeFromAPI.realms_detail[i]; |
| 1 | 287 | | break; |
| | 288 | | } |
| | 289 | | } |
| | 290 | |
|
| 2 | 291 | | Vector2Int position = Utils.ConvertStringToVector(placeFromAPI.base_position); |
| | 292 | |
|
| 2 | 293 | | if (string.IsNullOrEmpty(realm.serverName)) |
| 1 | 294 | | Environment.i.world.teleportController.Teleport(position.x, position.y); |
| | 295 | | else |
| 1 | 296 | | Environment.i.world.teleportController.JumpIn(position.x, position.y, realm.serverName, realm.layer); |
| 1 | 297 | | } |
| | 298 | | } |