| | 1 | | using System.Collections.Generic; |
| | 2 | | using System.Linq; |
| | 3 | | using DCL.Interface; |
| | 4 | | using DCL.Social.Friends; |
| | 5 | | using SocialFeaturesAnalytics; |
| | 6 | | using TMPro; |
| | 7 | | using UnityEngine; |
| | 8 | | using UnityEngine.EventSystems; |
| | 9 | | using UnityEngine.UI; |
| | 10 | |
|
| | 11 | | /// <summary> |
| | 12 | | /// Contextual menu with different options about an user. |
| | 13 | | /// </summary> |
| | 14 | | [RequireComponent(typeof(RectTransform))] |
| | 15 | | public class UserContextMenu : MonoBehaviour |
| | 16 | | { |
| | 17 | | internal const string CURRENT_PLAYER_ID = "CurrentPlayerInfoCardId"; |
| | 18 | |
|
| | 19 | | const string BLOCK_BTN_BLOCK_TEXT = "Block"; |
| | 20 | | const string BLOCK_BTN_UNBLOCK_TEXT = "Unblock"; |
| | 21 | | const string DELETE_MSG_PATTERN = "Are you sure you want to delete {0} as a friend?"; |
| | 22 | |
|
| | 23 | | [System.Flags] |
| | 24 | | public enum MenuConfigFlags |
| | 25 | | { |
| | 26 | | Name = 1, |
| | 27 | | Friendship = 2, |
| | 28 | | Message = 4, |
| | 29 | | Passport = 8, |
| | 30 | | Block = 16, |
| | 31 | | Report = 32 |
| | 32 | | } |
| | 33 | |
|
| | 34 | | const MenuConfigFlags headerFlags = MenuConfigFlags.Name | MenuConfigFlags.Friendship; |
| | 35 | | const MenuConfigFlags usesFriendsApiFlags = MenuConfigFlags.Friendship | MenuConfigFlags.Message; |
| | 36 | |
|
| | 37 | | [Header("Optional: Set Confirmation Dialog")] |
| | 38 | | [SerializeField] internal UserContextConfirmationDialog confirmationDialog; |
| | 39 | |
|
| | 40 | | [Header("Enable Actions")] |
| 228 | 41 | | [SerializeField] internal MenuConfigFlags menuConfigFlags = MenuConfigFlags.Passport | MenuConfigFlags.Block | MenuC |
| | 42 | |
|
| | 43 | | [Header("Containers")] |
| | 44 | | [SerializeField] internal GameObject headerContainer; |
| | 45 | | [SerializeField] internal GameObject bodyContainer; |
| | 46 | | [SerializeField] internal GameObject friendshipContainer; |
| | 47 | | [SerializeField] internal GameObject friendAddContainer; |
| | 48 | | [SerializeField] internal GameObject friendRemoveContainer; |
| | 49 | | [SerializeField] internal GameObject friendRequestedContainer; |
| | 50 | |
|
| | 51 | | [Header("Texts")] |
| | 52 | | [SerializeField] internal TextMeshProUGUI userName; |
| | 53 | | [SerializeField] internal TextMeshProUGUI blockText; |
| | 54 | |
|
| | 55 | | [Header("Buttons")] |
| | 56 | | [SerializeField] internal Button passportButton; |
| | 57 | | [SerializeField] internal Button blockButton; |
| | 58 | | [SerializeField] internal Button reportButton; |
| | 59 | | [SerializeField] internal Button addFriendButton; |
| | 60 | | [SerializeField] internal Button cancelFriendButton; |
| | 61 | | [SerializeField] internal Button deleteFriendButton; |
| | 62 | | [SerializeField] internal Button messageButton; |
| | 63 | |
|
| | 64 | | public static event System.Action<string> OnOpenPrivateChatRequest; |
| | 65 | |
|
| 23 | 66 | | public bool isVisible => gameObject.activeSelf; |
| 0 | 67 | | public string UserId => userId; |
| | 68 | |
|
| | 69 | | public event System.Action OnShowMenu; |
| | 70 | | public event System.Action<string> OnPassport; |
| | 71 | | public event System.Action<string> OnReport; |
| | 72 | | public event System.Action<string, bool> OnBlock; |
| | 73 | | public event System.Action<string> OnUnfriend; |
| | 74 | | public event System.Action<string> OnAddFriend; |
| | 75 | | public event System.Action<string> OnCancelFriend; |
| | 76 | | public event System.Action<string> OnMessage; |
| | 77 | | public event System.Action OnHide; |
| | 78 | |
|
| | 79 | | private static StringVariable currentPlayerId = null; |
| | 80 | | private RectTransform rectTransform; |
| | 81 | | private string userId; |
| | 82 | | private bool isBlocked; |
| | 83 | | private MenuConfigFlags currentConfigFlags; |
| | 84 | | private IConfirmationDialog currentConfirmationDialog; |
| | 85 | | internal ISocialAnalytics socialAnalytics; |
| | 86 | |
|
| | 87 | | /// <summary> |
| | 88 | | /// Show context menu |
| | 89 | | /// </summary> |
| | 90 | | /// <param name="userId"> user id</param> |
| 10 | 91 | | public void Show(string userId) { Show(userId, menuConfigFlags); } |
| | 92 | |
|
| | 93 | | /// <summary> |
| | 94 | | /// Show context menu |
| | 95 | | /// </summary> |
| | 96 | | /// <param name="userId"> user id</param> |
| | 97 | | /// <param name="configFlags">set buttons to enable in menu</param> |
| | 98 | | public void Show(string userId, MenuConfigFlags configFlags) |
| | 99 | | { |
| 7 | 100 | | this.userId = userId; |
| 7 | 101 | | ProcessActiveElements(configFlags); |
| 7 | 102 | | Setup(userId, configFlags); |
| 7 | 103 | | if (currentConfirmationDialog == null && confirmationDialog != null) |
| | 104 | | { |
| 0 | 105 | | SetConfirmationDialog(confirmationDialog); |
| | 106 | | } |
| 7 | 107 | | gameObject.SetActive(true); |
| 7 | 108 | | OnShowMenu?.Invoke(); |
| 1 | 109 | | } |
| | 110 | |
|
| | 111 | | /// <summary> |
| | 112 | | /// Set confirmation popup to reference use |
| | 113 | | /// </summary> |
| | 114 | | /// <param name="confirmationPopup">confirmation popup reference</param> |
| 0 | 115 | | public void SetConfirmationDialog(IConfirmationDialog confirmationPopup) { this.currentConfirmationDialog = confirma |
| | 116 | |
|
| | 117 | | /// <summary> |
| | 118 | | /// Hides the context menu. |
| | 119 | | /// </summary> |
| | 120 | | public void Hide() |
| | 121 | | { |
| 20 | 122 | | gameObject.SetActive(false); |
| 20 | 123 | | OnHide?.Invoke(); |
| 0 | 124 | | } |
| | 125 | |
|
| | 126 | | /// <summary> |
| | 127 | | /// Shows/Hides the friendship container |
| | 128 | | /// </summary> |
| 2 | 129 | | public void SetFriendshipContentActive(bool isActive) => friendshipContainer.SetActive(isActive); |
| | 130 | |
|
| | 131 | | private void Awake() |
| | 132 | | { |
| 14 | 133 | | if (!currentPlayerId) |
| | 134 | | { |
| 1 | 135 | | currentPlayerId = Resources.Load<StringVariable>(CURRENT_PLAYER_ID); |
| | 136 | | } |
| | 137 | |
|
| 14 | 138 | | rectTransform = GetComponent<RectTransform>(); |
| | 139 | |
|
| 14 | 140 | | passportButton.onClick.AddListener(OnPassportButtonPressed); |
| 14 | 141 | | blockButton.onClick.AddListener(OnBlockUserButtonPressed); |
| 14 | 142 | | reportButton.onClick.AddListener(OnReportUserButtonPressed); |
| 14 | 143 | | deleteFriendButton.onClick.AddListener(OnDeleteUserButtonPressed); |
| 14 | 144 | | addFriendButton.onClick.AddListener(OnAddFriendButtonPressed); |
| 14 | 145 | | cancelFriendButton.onClick.AddListener(OnCancelFriendRequestButtonPressed); |
| 14 | 146 | | messageButton.onClick.AddListener(OnMessageButtonPressed); |
| 14 | 147 | | } |
| | 148 | |
|
| 120 | 149 | | private void Update() { HideIfClickedOutside(); } |
| | 150 | |
|
| | 151 | | private void OnDisable() |
| | 152 | | { |
| 14 | 153 | | FriendsController.i.OnUpdateFriendship -= OnFriendActionUpdate; |
| 14 | 154 | | } |
| | 155 | |
|
| 0 | 156 | | public void ClickReportButton() => reportButton.onClick.Invoke(); |
| | 157 | |
|
| | 158 | | private void OnPassportButtonPressed() |
| | 159 | | { |
| 1 | 160 | | OnPassport?.Invoke(userId); |
| 1 | 161 | | currentPlayerId.Set(userId); |
| 1 | 162 | | Hide(); |
| | 163 | |
|
| 1 | 164 | | AudioScriptableObjects.dialogOpen.Play(true); |
| 1 | 165 | | } |
| | 166 | |
|
| | 167 | | private void OnReportUserButtonPressed() |
| | 168 | | { |
| 1 | 169 | | OnReport?.Invoke(userId); |
| 1 | 170 | | WebInterface.SendReportPlayer(userId, UserProfileController.userProfilesCatalog.Get(userId)?.userName); |
| 1 | 171 | | GetSocialAnalytics().SendPlayerReport(PlayerReportIssueType.None, 0, PlayerActionSource.ProfileContextMenu); |
| 1 | 172 | | Hide(); |
| 1 | 173 | | } |
| | 174 | |
|
| | 175 | | private void OnDeleteUserButtonPressed() |
| | 176 | | { |
| 0 | 177 | | OnUnfriend?.Invoke(userId); |
| | 178 | |
|
| 0 | 179 | | if (currentConfirmationDialog != null) |
| | 180 | | { |
| 0 | 181 | | currentConfirmationDialog.SetText(string.Format(DELETE_MSG_PATTERN, UserProfileController.userProfilesCatalo |
| 0 | 182 | | currentConfirmationDialog.Show(() => |
| | 183 | | { |
| 0 | 184 | | UnfriendUser(); |
| 0 | 185 | | }); |
| | 186 | | } |
| | 187 | | else |
| | 188 | | { |
| 0 | 189 | | UnfriendUser(); |
| | 190 | | } |
| | 191 | |
|
| 0 | 192 | | GetSocialAnalytics().SendFriendDeleted(UserProfile.GetOwnUserProfile().userId, userId, PlayerActionSource.Profil |
| 0 | 193 | | Hide(); |
| 0 | 194 | | } |
| | 195 | |
|
| | 196 | | private void UnfriendUser() |
| | 197 | | { |
| 0 | 198 | | FriendsController.i.RemoveFriend(userId); |
| 0 | 199 | | } |
| | 200 | |
|
| | 201 | | private void OnAddFriendButtonPressed() |
| | 202 | | { |
| 0 | 203 | | OnAddFriend?.Invoke(userId); |
| | 204 | |
|
| | 205 | | // NOTE: if we don't add this, the friend request has strange behaviors |
| 0 | 206 | | UserProfileController.i.AddUserProfileToCatalog(new UserProfileModel() |
| | 207 | | { |
| | 208 | | userId = userId, |
| | 209 | | name = UserProfileController.userProfilesCatalog.Get(userId)?.userName |
| | 210 | | }); |
| | 211 | |
|
| 0 | 212 | | FriendsController.i.RequestFriendship(userId); |
| | 213 | |
|
| 0 | 214 | | GetSocialAnalytics().SendFriendRequestSent(UserProfile.GetOwnUserProfile().userId, userId, 0, PlayerActionSource |
| 0 | 215 | | } |
| | 216 | |
|
| | 217 | | private void OnCancelFriendRequestButtonPressed() |
| | 218 | | { |
| 0 | 219 | | OnCancelFriend?.Invoke(userId); |
| | 220 | |
|
| 0 | 221 | | FriendsController.i.CancelRequest(userId); |
| | 222 | |
|
| 0 | 223 | | GetSocialAnalytics().SendFriendRequestCancelled(UserProfile.GetOwnUserProfile().userId, userId, PlayerActionSour |
| 0 | 224 | | } |
| | 225 | |
|
| | 226 | | private void OnMessageButtonPressed() |
| | 227 | | { |
| 0 | 228 | | OnMessage?.Invoke(userId); |
| 0 | 229 | | OnOpenPrivateChatRequest?.Invoke(userId); |
| 0 | 230 | | Hide(); |
| 0 | 231 | | } |
| | 232 | |
|
| | 233 | | private void OnBlockUserButtonPressed() |
| | 234 | | { |
| 1 | 235 | | bool blockUser = !isBlocked; |
| 1 | 236 | | OnBlock?.Invoke(userId, blockUser); |
| 1 | 237 | | if (blockUser) |
| | 238 | | { |
| 1 | 239 | | WebInterface.SendBlockPlayer(userId); |
| 1 | 240 | | GetSocialAnalytics().SendPlayerBlocked(FriendsController.i.IsFriend(userId), PlayerActionSource.ProfileConte |
| | 241 | | } |
| | 242 | | else |
| | 243 | | { |
| 0 | 244 | | WebInterface.SendUnblockPlayer(userId); |
| 0 | 245 | | GetSocialAnalytics().SendPlayerUnblocked(FriendsController.i.IsFriend(userId), PlayerActionSource.ProfileCon |
| | 246 | | } |
| 1 | 247 | | Hide(); |
| 1 | 248 | | } |
| | 249 | |
|
| 10 | 250 | | private void UpdateBlockButton() { blockText.text = isBlocked ? BLOCK_BTN_UNBLOCK_TEXT : BLOCK_BTN_BLOCK_TEXT; } |
| | 251 | |
|
| | 252 | | private void HideIfClickedOutside() |
| | 253 | | { |
| 120 | 254 | | if (!Input.GetMouseButtonDown(0)) return; |
| 0 | 255 | | var pointerEventData = new PointerEventData(EventSystem.current) |
| | 256 | | { |
| | 257 | | position = Input.mousePosition |
| | 258 | | }; |
| 0 | 259 | | var raycastResults = new List<RaycastResult>(); |
| 0 | 260 | | EventSystem.current.RaycastAll(pointerEventData, raycastResults); |
| | 261 | |
|
| 0 | 262 | | if (raycastResults.All(result => result.gameObject != gameObject)) |
| 0 | 263 | | Hide(); |
| 0 | 264 | | } |
| | 265 | |
|
| | 266 | | private void ProcessActiveElements(MenuConfigFlags flags) |
| | 267 | | { |
| 31 | 268 | | headerContainer.SetActive((flags & headerFlags) != 0); |
| 31 | 269 | | userName.gameObject.SetActive((flags & MenuConfigFlags.Name) != 0); |
| 31 | 270 | | friendshipContainer.SetActive((flags & MenuConfigFlags.Friendship) != 0); |
| 31 | 271 | | deleteFriendButton.gameObject.SetActive((flags & MenuConfigFlags.Friendship) != 0); |
| 31 | 272 | | passportButton.gameObject.SetActive((flags & MenuConfigFlags.Passport) != 0); |
| 31 | 273 | | blockButton.gameObject.SetActive((flags & MenuConfigFlags.Block) != 0); |
| 31 | 274 | | reportButton.gameObject.SetActive((flags & MenuConfigFlags.Report) != 0); |
| 31 | 275 | | messageButton.gameObject.SetActive((flags & MenuConfigFlags.Message) != 0); |
| 31 | 276 | | } |
| | 277 | |
|
| | 278 | | private void Setup(string userId, MenuConfigFlags configFlags) |
| | 279 | | { |
| 7 | 280 | | this.userId = userId; |
| | 281 | |
|
| 7 | 282 | | UserProfile profile = UserProfileController.userProfilesCatalog.Get(userId); |
| 7 | 283 | | bool userHasWallet = profile?.hasConnectedWeb3 ?? false; |
| | 284 | |
|
| 7 | 285 | | if (!userHasWallet || !UserProfile.GetOwnUserProfile().hasConnectedWeb3) |
| | 286 | | { |
| 1 | 287 | | configFlags &= ~usesFriendsApiFlags; |
| | 288 | | } |
| | 289 | |
|
| 7 | 290 | | currentConfigFlags = configFlags; |
| 7 | 291 | | ProcessActiveElements(configFlags); |
| | 292 | |
|
| 7 | 293 | | if ((configFlags & MenuConfigFlags.Block) != 0) |
| | 294 | | { |
| 5 | 295 | | isBlocked = UserProfile.GetOwnUserProfile().blocked.Contains(userId); |
| 5 | 296 | | UpdateBlockButton(); |
| | 297 | | } |
| 7 | 298 | | if ((configFlags & MenuConfigFlags.Name) != 0) |
| | 299 | | { |
| 5 | 300 | | string name = profile?.userName; |
| 5 | 301 | | userName.text = name; |
| | 302 | | } |
| 7 | 303 | | if ((configFlags & usesFriendsApiFlags) != 0) |
| | 304 | | { |
| 6 | 305 | | if (FriendsController.i.friends.TryGetValue(userId, out UserStatus status)) |
| | 306 | | { |
| 0 | 307 | | SetupFriendship(status.friendshipStatus); |
| | 308 | | } |
| | 309 | | else |
| | 310 | | { |
| 6 | 311 | | SetupFriendship(FriendshipStatus.NOT_FRIEND); |
| | 312 | | } |
| 6 | 313 | | FriendsController.i.OnUpdateFriendship -= OnFriendActionUpdate; |
| 6 | 314 | | FriendsController.i.OnUpdateFriendship += OnFriendActionUpdate; |
| | 315 | | } |
| 7 | 316 | | } |
| | 317 | |
|
| | 318 | | private void SetupFriendship(FriendshipStatus friendshipStatus) |
| | 319 | | { |
| 12 | 320 | | bool friendshipEnabled = (currentConfigFlags & MenuConfigFlags.Friendship) != 0; |
| 12 | 321 | | bool messageEnabled = (currentConfigFlags & MenuConfigFlags.Message) != 0; |
| | 322 | |
|
| 12 | 323 | | if (friendshipStatus == FriendshipStatus.FRIEND) |
| | 324 | | { |
| 2 | 325 | | if (friendshipEnabled) |
| | 326 | | { |
| 1 | 327 | | friendAddContainer.SetActive(false); |
| 1 | 328 | | friendRemoveContainer.SetActive(true); |
| 1 | 329 | | friendRequestedContainer.SetActive(false); |
| 1 | 330 | | deleteFriendButton.gameObject.SetActive(true); |
| | 331 | | } |
| 2 | 332 | | if (messageEnabled) |
| | 333 | | { |
| 1 | 334 | | messageButton.gameObject.SetActive(true); |
| | 335 | | } |
| | 336 | | } |
| 10 | 337 | | else if (friendshipStatus == FriendshipStatus.REQUESTED_TO) |
| | 338 | | { |
| 2 | 339 | | if (friendshipEnabled) |
| | 340 | | { |
| 1 | 341 | | friendAddContainer.SetActive(false); |
| 1 | 342 | | friendRemoveContainer.SetActive(false); |
| 1 | 343 | | friendRequestedContainer.SetActive(true); |
| 1 | 344 | | deleteFriendButton.gameObject.SetActive(false); |
| | 345 | | } |
| 2 | 346 | | if (messageEnabled) |
| | 347 | | { |
| 1 | 348 | | messageButton.gameObject.SetActive(false); |
| | 349 | | } |
| | 350 | | } |
| 8 | 351 | | else if (friendshipStatus == FriendshipStatus.NOT_FRIEND) |
| | 352 | | { |
| 8 | 353 | | if (friendshipEnabled) |
| | 354 | | { |
| 6 | 355 | | friendAddContainer.SetActive(true); |
| 6 | 356 | | friendRemoveContainer.SetActive(false); |
| 6 | 357 | | friendRequestedContainer.SetActive(false); |
| 6 | 358 | | deleteFriendButton.gameObject.SetActive(false); |
| | 359 | | } |
| 8 | 360 | | if (messageEnabled) |
| | 361 | | { |
| 6 | 362 | | messageButton.gameObject.SetActive(false); |
| | 363 | | } |
| | 364 | | } |
| 0 | 365 | | else if (friendshipStatus == FriendshipStatus.REQUESTED_FROM) |
| | 366 | | { |
| 0 | 367 | | if (friendshipEnabled) |
| | 368 | | { |
| 0 | 369 | | friendAddContainer.SetActive(true); |
| 0 | 370 | | friendRemoveContainer.SetActive(false); |
| 0 | 371 | | friendRequestedContainer.SetActive(false); |
| 0 | 372 | | deleteFriendButton.gameObject.SetActive(false); |
| | 373 | | } |
| 0 | 374 | | if (messageEnabled) |
| | 375 | | { |
| 0 | 376 | | messageButton.gameObject.SetActive(false); |
| | 377 | | } |
| | 378 | | } |
| 4 | 379 | | } |
| | 380 | |
|
| | 381 | | private void OnFriendActionUpdate(string userId, FriendshipAction action) |
| | 382 | | { |
| 6 | 383 | | if (this.userId != userId) |
| | 384 | | { |
| 0 | 385 | | return; |
| | 386 | | } |
| | 387 | |
|
| 6 | 388 | | if (action == FriendshipAction.APPROVED) |
| | 389 | | { |
| 2 | 390 | | SetupFriendship(FriendshipStatus.FRIEND); |
| | 391 | | } |
| 4 | 392 | | else if (action == FriendshipAction.REQUESTED_TO) |
| | 393 | | { |
| 2 | 394 | | SetupFriendship(FriendshipStatus.REQUESTED_TO); |
| | 395 | | } |
| 2 | 396 | | else if (action == FriendshipAction.DELETED || action == FriendshipAction.CANCELLED || action == FriendshipActio |
| | 397 | | { |
| 2 | 398 | | SetupFriendship(FriendshipStatus.NOT_FRIEND); |
| | 399 | | } |
| 2 | 400 | | } |
| | 401 | |
|
| | 402 | | private ISocialAnalytics GetSocialAnalytics() |
| | 403 | | { |
| 2 | 404 | | if (socialAnalytics == null) |
| | 405 | | { |
| 0 | 406 | | socialAnalytics = new SocialAnalytics( |
| | 407 | | DCL.Environment.i.platform.serviceProviders.analytics, |
| | 408 | | new UserProfileWebInterfaceBridge()); |
| | 409 | | } |
| | 410 | |
|
| 2 | 411 | | return socialAnalytics; |
| | 412 | | } |
| | 413 | |
|
| | 414 | | #if UNITY_EDITOR |
| | 415 | | //This is just to process buttons and container visibility on editor |
| | 416 | | private void OnValidate() |
| | 417 | | { |
| 17 | 418 | | if (headerContainer == null) |
| 0 | 419 | | return; |
| 17 | 420 | | ProcessActiveElements(menuConfigFlags); |
| 17 | 421 | | } |
| | 422 | | #endif |
| | 423 | | } |