| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Browser; |
| | 3 | | using DCL.Tasks; |
| | 4 | | using DCLServices.MapRendererV2.ConsumerUtils; |
| | 5 | | using DCLServices.PlacesAPIService; |
| | 6 | | using ExploreV2Analytics; |
| | 7 | | using MainScripts.DCL.Controllers.HotScenes; |
| | 8 | | using System; |
| | 9 | | using System.Collections.Generic; |
| | 10 | | using System.Linq; |
| | 11 | | using System.Threading; |
| | 12 | | using UnityEngine; |
| | 13 | |
|
| | 14 | | namespace DCL |
| | 15 | | { |
| | 16 | | public class NavmapToastViewController : IDisposable, INavmapToastViewController |
| | 17 | | { |
| | 18 | | private const string COPY_LINK = "https://play.decentraland.org/?position={0},{1}"; |
| | 19 | |
|
| | 20 | | private readonly MinimapMetadata minimapMetadata; |
| | 21 | | private readonly NavmapToastView view; |
| | 22 | | private readonly IPlaceCardComponentView placeCardModal; |
| | 23 | | private readonly MapRenderImage mapRenderImage; |
| | 24 | | private readonly IExploreV2Analytics exploreV2Analytics; |
| | 25 | | private readonly IBrowserBridge browserBridge; |
| | 26 | | private readonly float sqrDistanceToCloseView; |
| | 27 | |
|
| | 28 | | private Vector2 lastClickPosition; |
| | 29 | | private Vector2Int currentParcel; |
| | 30 | | private readonly IPlacesAPIService placesAPIService; |
| | 31 | | private readonly IPlacesAnalytics placesAnalytics; |
| | 32 | |
|
| 1 | 33 | | private readonly CancellationTokenSource disposingCts = new (); |
| | 34 | | private CancellationTokenSource retrievingFavoritesCts; |
| | 35 | | private bool showUntilClick; |
| | 36 | |
|
| | 37 | | private string placeId; |
| | 38 | | private string placeName; |
| | 39 | | private IReadOnlyList<string> allPointOfInterest; |
| | 40 | |
|
| 1 | 41 | | public NavmapToastViewController( |
| | 42 | | MinimapMetadata minimapMetadata, |
| | 43 | | NavmapToastView view, |
| | 44 | | MapRenderImage mapRenderImage, |
| | 45 | | IPlacesAPIService placesAPIService, |
| | 46 | | IPlacesAnalytics placesAnalytics, |
| | 47 | | IPlaceCardComponentView placeCardModal, |
| | 48 | | IExploreV2Analytics exploreV2Analytics, |
| | 49 | | IBrowserBridge browserBridge) |
| | 50 | | { |
| 1 | 51 | | this.placesAPIService = placesAPIService; |
| 1 | 52 | | this.placesAnalytics = placesAnalytics; |
| 1 | 53 | | this.minimapMetadata = minimapMetadata; |
| 1 | 54 | | this.view = view; |
| 1 | 55 | | this.mapRenderImage = mapRenderImage; |
| 1 | 56 | | this.placeCardModal = placeCardModal; |
| 1 | 57 | | this.exploreV2Analytics = exploreV2Analytics; |
| 1 | 58 | | this.browserBridge = browserBridge; |
| 1 | 59 | | this.sqrDistanceToCloseView = view.distanceToCloseView * view.distanceToCloseView; |
| | 60 | |
|
| 1 | 61 | | this.view.OnFavoriteToggleClicked += OnFavoriteToggleClicked; |
| 1 | 62 | | this.view.OnGoto += JumpIn; |
| 1 | 63 | | this.view.OnInfoClick += EnablePlaceCardModal; |
| 1 | 64 | | this.view.OnVoteChanged += ChangeVote; |
| 1 | 65 | | this.view.OnPressedLinkCopy += CopyLink; |
| 1 | 66 | | this.view.OnPressedTwitterButton += OpenTwitter; |
| | 67 | |
|
| 1 | 68 | | this.placeCardModal.OnFavoriteChanged += OnFavoriteToggleClicked; |
| 1 | 69 | | this.placeCardModal.OnVoteChanged += ChangeVote; |
| 1 | 70 | | this.placeCardModal.onJumpInClick.RemoveAllListeners(); |
| 1 | 71 | | this.placeCardModal.onJumpInClick.AddListener(() => JumpIn(currentParcel.x, currentParcel.y)); |
| 1 | 72 | | this.placeCardModal.OnPressedLinkCopy += CopyLink; |
| 1 | 73 | | this.placeCardModal.OnPressedTwitterButton += OpenTwitter; |
| 1 | 74 | | RequestAllPOIs().Forget(); |
| 1 | 75 | | } |
| | 76 | |
|
| | 77 | | private void OpenTwitter(Vector2Int coordinates, string sceneName) |
| | 78 | | { |
| 0 | 79 | | var description = $"Check out {sceneName}, a cool place I found in Decentraland!".Replace(" ", "%20"); |
| 0 | 80 | | var twitterUrl = $"https://twitter.com/intent/tweet?text={description}&hashtags=DCLPlace&url={string.Format( |
| | 81 | |
|
| 0 | 82 | | browserBridge.OpenUrl(twitterUrl); |
| 0 | 83 | | } |
| | 84 | |
|
| | 85 | | private void CopyLink(Vector2Int coordinates) |
| | 86 | | { |
| 0 | 87 | | Environment.i.platform.clipboard.WriteText(string.Format(COPY_LINK, coordinates.x, coordinates.y)); |
| 0 | 88 | | } |
| | 89 | |
|
| | 90 | | private async UniTaskVoid RequestAllPOIs() |
| | 91 | | { |
| 3 | 92 | | allPointOfInterest = await placesAPIService.GetPointsOfInterestCoords(CancellationToken.None); |
| 1 | 93 | | } |
| | 94 | |
|
| | 95 | | private void EnablePlaceCardModal() => |
| 0 | 96 | | placeCardModal.SetActive(true); |
| | 97 | |
|
| | 98 | | private void JumpIn(int x, int y) |
| | 99 | | { |
| 0 | 100 | | DataStore.i.HUDs.navmapVisible.Set(false); |
| 0 | 101 | | Environment.i.world.teleportController.Teleport(x, y); |
| 0 | 102 | | exploreV2Analytics.SendPlaceTeleport(placeId, placeName, currentParcel, ActionSource.FromNavmap); |
| 0 | 103 | | } |
| | 104 | |
|
| | 105 | | public void Activate() |
| | 106 | | { |
| 0 | 107 | | Unsubscribe(); |
| | 108 | |
|
| 0 | 109 | | mapRenderImage.ParcelClicked += ShowPlaceToast; |
| 0 | 110 | | mapRenderImage.Hovered += OnHovered; |
| 0 | 111 | | mapRenderImage.DragStarted += OnDragStarted; |
| 0 | 112 | | minimapMetadata.OnSceneInfoUpdated += OnMapMetadataInfoUpdated; |
| 0 | 113 | | } |
| | 114 | |
|
| | 115 | | public void Deactivate() |
| | 116 | | { |
| 0 | 117 | | view.Close(); |
| 0 | 118 | | Unsubscribe(); |
| 0 | 119 | | } |
| | 120 | |
|
| | 121 | | private void Unsubscribe() |
| | 122 | | { |
| 1 | 123 | | mapRenderImage.ParcelClicked -= ShowPlaceToast; |
| 1 | 124 | | mapRenderImage.Hovered -= OnHovered; |
| 1 | 125 | | mapRenderImage.DragStarted -= OnDragStarted; |
| 1 | 126 | | minimapMetadata.OnSceneInfoUpdated -= OnMapMetadataInfoUpdated; |
| 1 | 127 | | } |
| | 128 | |
|
| | 129 | | public void CloseCurrentToast() => |
| 0 | 130 | | view.Close(); |
| | 131 | |
|
| | 132 | | private void OnHovered(Vector2 localPosition) |
| | 133 | | { |
| 0 | 134 | | if (!view.gameObject.activeSelf || showUntilClick) |
| 0 | 135 | | return; |
| | 136 | |
|
| 0 | 137 | | if (Vector2.SqrMagnitude(localPosition - lastClickPosition) >= sqrDistanceToCloseView) |
| 0 | 138 | | view.Close(); |
| 0 | 139 | | } |
| | 140 | |
|
| | 141 | | private void OnDragStarted() |
| | 142 | | { |
| 0 | 143 | | if (!view.gameObject.activeSelf) |
| 0 | 144 | | return; |
| | 145 | |
|
| 0 | 146 | | showUntilClick = false; |
| 0 | 147 | | view.Close(); |
| 0 | 148 | | } |
| | 149 | |
|
| | 150 | | public void ShowPlaceToast(MapRenderImage.ParcelClickData parcelClickData, bool showUntilClick) |
| | 151 | | { |
| 0 | 152 | | ShowPlaceToast(parcelClickData); |
| 0 | 153 | | this.showUntilClick = showUntilClick; |
| 0 | 154 | | } |
| | 155 | |
|
| | 156 | | private void ShowPlaceToast(MapRenderImage.ParcelClickData parcelClickData) |
| | 157 | | { |
| 0 | 158 | | showUntilClick = false; |
| | 159 | |
|
| 0 | 160 | | lastClickPosition = parcelClickData.WorldPosition; |
| 0 | 161 | | currentParcel = parcelClickData.Parcel; |
| | 162 | |
|
| | 163 | | // transform coordinates from rect coordinates to parent of view coordinates |
| 0 | 164 | | view.Open(currentParcel, lastClickPosition); |
| 0 | 165 | | RetrieveFavoriteState(); |
| 0 | 166 | | } |
| | 167 | |
|
| | 168 | | private void OnMapMetadataInfoUpdated(MinimapMetadata.MinimapSceneInfo sceneInfo) |
| | 169 | | { |
| 0 | 170 | | if (!view.gameObject.activeInHierarchy) |
| 0 | 171 | | return; |
| | 172 | |
|
| 0 | 173 | | var updatedCurrentLocationInfo = false; |
| 0 | 174 | | foreach (Vector2Int parcel in sceneInfo.parcels) |
| | 175 | | { |
| 0 | 176 | | if (parcel == currentParcel) |
| | 177 | | { |
| 0 | 178 | | updatedCurrentLocationInfo = true; |
| 0 | 179 | | break; |
| | 180 | | } |
| | 181 | | } |
| | 182 | |
|
| 0 | 183 | | if (updatedCurrentLocationInfo) |
| 0 | 184 | | view.Populate(currentParcel, lastClickPosition, sceneInfo); |
| 0 | 185 | | } |
| | 186 | |
|
| | 187 | | private void RetrieveFavoriteState() |
| | 188 | | { |
| 0 | 189 | | retrievingFavoritesCts?.SafeCancelAndDispose(); |
| 0 | 190 | | retrievingFavoritesCts = CancellationTokenSource.CreateLinkedTokenSource(disposingCts.Token); |
| | 191 | |
|
| 0 | 192 | | RetrieveAdditionalData(retrievingFavoritesCts.Token).Forget(); |
| 0 | 193 | | } |
| | 194 | |
|
| | 195 | | private async UniTaskVoid RetrieveAdditionalData(CancellationToken ct) |
| | 196 | | { |
| | 197 | | try |
| | 198 | | { |
| 0 | 199 | | view.SetFavoriteLoading(true); |
| 0 | 200 | | var place = await placesAPIService.GetPlace(currentParcel, ct); |
| 0 | 201 | | placeId = place.id; |
| 0 | 202 | | placeName = place.title; |
| 0 | 203 | | view.SetPlaceId(place.id); |
| 0 | 204 | | view.SetVoteButtons(place.user_like, place.user_dislike); |
| 0 | 205 | | view.SetIsAPlace(true); |
| 0 | 206 | | bool isFavorite = await placesAPIService.IsFavoritePlace(place, ct); |
| 0 | 207 | | view.SetFavoriteLoading(false); |
| 0 | 208 | | view.SetCurrentFavoriteStatus(place.id, isFavorite); |
| 0 | 209 | | view.SetPlayerCount(place.user_count); |
| 0 | 210 | | view.SetUserRating(place.like_rate_as_float); |
| 0 | 211 | | view.SetUserVisits(place.user_visits); |
| 0 | 212 | | view.RebuildLayouts(); |
| 0 | 213 | | SetPlaceCardModalData(place); |
| 0 | 214 | | } |
| 0 | 215 | | catch (NotAPlaceException) |
| | 216 | | { |
| 0 | 217 | | view.SetIsAPlace(false); |
| 0 | 218 | | } |
| 0 | 219 | | catch (OperationCanceledException) |
| | 220 | | { |
| 0 | 221 | | view.SetFavoriteLoading(true); |
| 0 | 222 | | } |
| 0 | 223 | | } |
| | 224 | |
|
| | 225 | | private void SetPlaceCardModalData(IHotScenesController.PlaceInfo place) |
| | 226 | | { |
| 0 | 227 | | placeCardModal.SetPlacePicture(place.image); |
| 0 | 228 | | placeCardModal.SetCoords(currentParcel); |
| 0 | 229 | | placeCardModal.SetDeployedAt(place.deployed_at); |
| 0 | 230 | | placeCardModal.SetPlaceAuthor(place.contact_name); |
| 0 | 231 | | placeCardModal.SetPlaceName(place.title); |
| 0 | 232 | | placeCardModal.SetPlaceDescription(place.description); |
| 0 | 233 | | placeCardModal.SetUserVisits(place.user_visits); |
| 0 | 234 | | placeCardModal.SetNumberOfUsers(place.user_count); |
| 0 | 235 | | placeCardModal.SetUserRating(place.like_rate_as_float); |
| 0 | 236 | | placeCardModal.SetNumberOfFavorites(place.favorites); |
| 0 | 237 | | placeCardModal.SetTotalVotes(place.likes + place.dislikes); |
| 0 | 238 | | placeCardModal.SetFavoriteButton(place.user_favorite, place.id); |
| 0 | 239 | | placeCardModal.SetVoteButtons(place.user_like, place.user_dislike); |
| 0 | 240 | | placeCardModal.SetIsPOI(allPointOfInterest.Contains(place.base_position)); |
| 0 | 241 | | } |
| | 242 | |
|
| | 243 | | private void OnFavoriteToggleClicked(string uuid, bool isFavorite) |
| | 244 | | { |
| 0 | 245 | | if(isFavorite) |
| 0 | 246 | | placesAnalytics.AddFavorite(uuid, IPlacesAnalytics.ActionSource.FromNavmap); |
| | 247 | | else |
| 0 | 248 | | placesAnalytics.RemoveFavorite(uuid, IPlacesAnalytics.ActionSource.FromNavmap); |
| | 249 | |
|
| 0 | 250 | | placesAPIService.SetPlaceFavorite(uuid, isFavorite, default).Forget(); |
| 0 | 251 | | } |
| | 252 | |
|
| | 253 | | private void ChangeVote(string placeId, bool? isUpvote) |
| | 254 | | { |
| 0 | 255 | | if (isUpvote != null) |
| | 256 | | { |
| 0 | 257 | | if (isUpvote.Value) |
| 0 | 258 | | placesAnalytics.Like(placeId, IPlacesAnalytics.ActionSource.FromNavmap); |
| | 259 | | else |
| 0 | 260 | | placesAnalytics.Dislike(placeId, IPlacesAnalytics.ActionSource.FromNavmap); |
| | 261 | | } |
| | 262 | | else |
| 0 | 263 | | placesAnalytics.RemoveVote(placeId, IPlacesAnalytics.ActionSource.FromNavmap); |
| | 264 | |
|
| 0 | 265 | | placesAPIService.SetPlaceVote(isUpvote, placeId, default).Forget(); |
| 0 | 266 | | } |
| | 267 | |
|
| | 268 | | public void Dispose() |
| | 269 | | { |
| 1 | 270 | | disposingCts?.SafeCancelAndDispose(); |
| 1 | 271 | | Unsubscribe(); |
| 1 | 272 | | this.view.OnFavoriteToggleClicked -= OnFavoriteToggleClicked; |
| 1 | 273 | | this.view.OnGoto -= JumpIn; |
| 1 | 274 | | this.view.OnInfoClick -= EnablePlaceCardModal; |
| 1 | 275 | | this.view.OnVoteChanged -= ChangeVote; |
| 1 | 276 | | this.placeCardModal.OnFavoriteChanged -= OnFavoriteToggleClicked; |
| 1 | 277 | | this.placeCardModal.OnVoteChanged -= ChangeVote; |
| 1 | 278 | | this.placeCardModal.OnPressedLinkCopy -= CopyLink; |
| 1 | 279 | | this.placeCardModal.OnPressedTwitterButton -= OpenTwitter; |
| 1 | 280 | | this.placeCardModal.onJumpInClick.RemoveAllListeners(); |
| 1 | 281 | | } |
| | 282 | | } |
| | 283 | | } |