| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using DCL.Social.Friends; |
| | 4 | | using SocialFeaturesAnalytics; |
| | 5 | | using System; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using System.Threading; |
| | 8 | | using System.Threading.Tasks; |
| | 9 | | using TMPro; |
| | 10 | | using UIComponents.Scripts.Components; |
| | 11 | | using UnityEngine; |
| | 12 | | using UnityEngine.UI; |
| | 13 | |
|
| | 14 | | namespace DCL.Social.Passports |
| | 15 | | { |
| | 16 | | public class PassportPlayerInfoComponentView : BaseComponentView<PlayerPassportModel>, IPassportPlayerInfoComponentV |
| | 17 | | { |
| | 18 | | private const int COPY_TOAST_VISIBLE_TIME = 3000; |
| | 19 | |
|
| | 20 | | [SerializeField] private TextMeshProUGUI name; |
| | 21 | | [SerializeField] private TextMeshProUGUI address; |
| | 22 | | [SerializeField] private TextMeshProUGUI nameInOptionsPanel; |
| | 23 | | [SerializeField] private Button walletCopyButton; |
| | 24 | | [SerializeField] private Button usernameCopyButton; |
| | 25 | | [SerializeField] private RectTransform usernameRect; |
| | 26 | | [SerializeField] private TextMeshProUGUI wallet; |
| | 27 | | [SerializeField] private ButtonComponentView optionsButton; |
| | 28 | | [SerializeField] private ButtonComponentView addFriendButton; |
| | 29 | | [SerializeField] private ButtonComponentView alreadyFriendsButton; |
| | 30 | | [SerializeField] private ButtonComponentView cancelFriendRequestButton; |
| | 31 | | [SerializeField] private ButtonComponentView acceptFriendButton; |
| | 32 | | [SerializeField] private ButtonComponentView blockedFriendButton; |
| | 33 | | [SerializeField] private GameObject friendshipContainer; |
| | 34 | | [SerializeField] private Button whisperButton; |
| | 35 | | [SerializeField] private TooltipComponentView whisperNonFriendsPopup; |
| | 36 | | [SerializeField] private GameObject onlineStatus; |
| | 37 | | [SerializeField] private GameObject offlineStatus; |
| | 38 | | [SerializeField] private GameObject normalUserPanel; |
| | 39 | | [SerializeField] private GameObject friendsFlowContainer; |
| | 40 | | [SerializeField] private GameObject blockedLabel; |
| | 41 | | [SerializeField] private GameObject optionsContainer; |
| | 42 | | [SerializeField] private UserContextMenu userContextMenu; |
| | 43 | |
|
| | 44 | | [SerializeField] private GameObject alreadyFriendsVariation; |
| | 45 | | [SerializeField] private GameObject unfriendVariation; |
| | 46 | |
|
| | 47 | | [SerializeField] private JumpInButton jumpInButton; |
| | 48 | | [SerializeField] private ShowHideAnimator copyAddressToast; |
| | 49 | | [SerializeField] private ShowHideAnimator copyUsernameToast; |
| | 50 | | [SerializeField] private GameObject actionsContainer; |
| | 51 | |
|
| | 52 | | public event Action OnAddFriend; |
| | 53 | | public event Action OnRemoveFriend; |
| | 54 | | public event Action OnCancelFriendRequest; |
| | 55 | | public event Action OnAcceptFriendRequest; |
| | 56 | | public event Action OnBlockUser; |
| | 57 | | public event Action OnUnblockUser; |
| | 58 | | public event Action OnReportUser; |
| | 59 | | public event Action<string> OnWhisperUser; |
| | 60 | | public event Action OnJumpInUser; |
| | 61 | | public event Action<string> OnWalletCopy; |
| | 62 | | public event Action<string> OnUsernameCopy; |
| | 63 | |
|
| | 64 | | private string fullWalletAddress; |
| | 65 | | private bool areFriends; |
| | 66 | | private bool isBlocked = false; |
| | 67 | | private Dictionary<FriendshipStatus, GameObject> friendStatusButtonsMapping; |
| | 68 | | private CancellationTokenSource cts; |
| | 69 | |
|
| | 70 | | public void Start() |
| | 71 | | { |
| 0 | 72 | | walletCopyButton.onClick.AddListener(CopyWalletToClipboard); |
| 0 | 73 | | usernameCopyButton.onClick.AddListener(CopyUsernameToClipboard); |
| 0 | 74 | | addFriendButton.onClick.AddListener(() => OnAddFriend?.Invoke()); |
| 0 | 75 | | alreadyFriendsButton.onClick.AddListener(() => OnRemoveFriend?.Invoke()); |
| 0 | 76 | | cancelFriendRequestButton.onClick.AddListener(() => OnCancelFriendRequest?.Invoke()); |
| 0 | 77 | | acceptFriendButton.onClick.AddListener(() => OnAcceptFriendRequest?.Invoke()); |
| 0 | 78 | | userContextMenu.OnBlock += OnBlock; |
| 0 | 79 | | blockedFriendButton.onClick.AddListener(() => OnUnblockUser?.Invoke()); |
| 0 | 80 | | userContextMenu.OnReport += OnReport; |
| 0 | 81 | | whisperButton.onClick.AddListener(WhisperActionFlow); |
| 0 | 82 | | optionsButton.onClick.AddListener(OpenOptions); |
| 0 | 83 | | jumpInButton.OnClick += () => OnJumpInUser?.Invoke(); |
| 0 | 84 | | alreadyFriendsButton.onFocused += RemoveFriendsFocused; |
| | 85 | |
|
| 0 | 86 | | friendStatusButtonsMapping = new Dictionary<FriendshipStatus, GameObject>() |
| | 87 | | { |
| | 88 | | { FriendshipStatus.NOT_FRIEND, addFriendButton.gameObject }, |
| | 89 | | { FriendshipStatus.FRIEND, alreadyFriendsButton.gameObject }, |
| | 90 | | { FriendshipStatus.REQUESTED_FROM, acceptFriendButton.gameObject }, |
| | 91 | | { FriendshipStatus.REQUESTED_TO, cancelFriendRequestButton.gameObject } |
| | 92 | | }; |
| 0 | 93 | | } |
| | 94 | |
|
| | 95 | | private void OnReport(string userId) |
| | 96 | | { |
| 0 | 97 | | OnReportUser?.Invoke(); |
| 0 | 98 | | } |
| | 99 | |
|
| | 100 | | private void OnBlock(string userId, bool isBlocked) |
| | 101 | | { |
| 0 | 102 | | model.isBlocked = isBlocked; |
| 0 | 103 | | RefreshControl(); |
| 0 | 104 | | OnBlockUser?.Invoke(); |
| 0 | 105 | | } |
| | 106 | |
|
| | 107 | | public override void RefreshControl() |
| | 108 | | { |
| 0 | 109 | | if (model == null) |
| 0 | 110 | | return; |
| | 111 | |
|
| 0 | 112 | | SetGuestUser(model.isGuest); |
| 0 | 113 | | SetName(model.name); |
| 0 | 114 | | userContextMenu.Hide(); |
| 0 | 115 | | SetWallet(model.userId); |
| 0 | 116 | | SetPresence(model.presenceStatus); |
| 0 | 117 | | SetIsBlocked(model.isBlocked); |
| 0 | 118 | | SetHasBlockedOwnUser(model.hasBlocked); |
| 0 | 119 | | SetFriendStatus(model.friendshipStatus); |
| 0 | 120 | | SetFriendshipVisibility(model.isFriendshipVisible); |
| 0 | 121 | | } |
| | 122 | |
|
| | 123 | | public override void Dispose() |
| | 124 | | { |
| 0 | 125 | | base.Dispose(); |
| | 126 | |
|
| 0 | 127 | | walletCopyButton.onClick.RemoveAllListeners(); |
| 0 | 128 | | addFriendButton.onClick.RemoveAllListeners(); |
| | 129 | |
|
| 0 | 130 | | ResetCancellationToken(); |
| 0 | 131 | | } |
| | 132 | |
|
| | 133 | | public void ResetCopyToast() |
| | 134 | | { |
| 0 | 135 | | copyAddressToast.Hide(true); |
| 0 | 136 | | copyUsernameToast.Hide(true); |
| 0 | 137 | | ResetCancellationToken(); |
| 0 | 138 | | } |
| | 139 | |
|
| | 140 | | public void InitializeJumpInButton(IFriendsController friendsController, string userId, ISocialAnalytics socialA |
| | 141 | | { |
| 0 | 142 | | if (friendsController.IsFriend(userId)) |
| | 143 | | { |
| 0 | 144 | | jumpInButton.gameObject.SetActive(true); |
| 0 | 145 | | jumpInButton.Initialize(friendsController, userId, socialAnalytics); |
| | 146 | | } |
| 0 | 147 | | else { jumpInButton.gameObject.SetActive(false); } |
| 0 | 148 | | } |
| | 149 | |
|
| | 150 | | public void SetFriendStatus(FriendshipStatus friendStatus) |
| | 151 | | { |
| 0 | 152 | | areFriends = friendStatus == FriendshipStatus.FRIEND; |
| | 153 | |
|
| 0 | 154 | | if (isBlocked) |
| 0 | 155 | | return; |
| | 156 | |
|
| 0 | 157 | | DisableAllFriendFlowButtons(); |
| 0 | 158 | | friendStatusButtonsMapping[friendStatus].SetActive(true); |
| 0 | 159 | | whisperNonFriendsPopup.Hide(true); |
| 0 | 160 | | } |
| | 161 | |
|
| | 162 | | private void SetFriendshipVisibility(bool visible) => |
| 0 | 163 | | friendshipContainer.SetActive(visible); |
| | 164 | |
|
| | 165 | | private void RemoveFriendsFocused(bool isFocused) |
| | 166 | | { |
| 0 | 167 | | alreadyFriendsVariation.SetActive(!isFocused); |
| 0 | 168 | | unfriendVariation.SetActive(isFocused); |
| 0 | 169 | | } |
| | 170 | |
|
| | 171 | | private void SetName(string name) |
| | 172 | | { |
| 0 | 173 | | string[] splitName = name.Split('#'); |
| 0 | 174 | | this.name.SetText(splitName[0]); |
| 0 | 175 | | address.SetText($"{(splitName.Length == 2 ? "#" + splitName[1] : "")}"); |
| | 176 | |
|
| | 177 | | //We are forced to use this due to the UI not being correctly responsive with the placing of the copy icon |
| | 178 | | //without the force rebuild it's not setting the elements as dirty and not replacing them correctly |
| 0 | 179 | | Utils.ForceRebuildLayoutImmediate(usernameRect); |
| 0 | 180 | | nameInOptionsPanel.text = name; |
| 0 | 181 | | } |
| | 182 | |
|
| | 183 | | private void SetWallet(string wallet) |
| | 184 | | { |
| 0 | 185 | | if (string.IsNullOrEmpty(wallet)) |
| 0 | 186 | | return; |
| | 187 | |
|
| 0 | 188 | | fullWalletAddress = wallet; |
| 0 | 189 | | this.wallet.text = $"{wallet.Substring(0, 5)}...{wallet.Substring(wallet.Length - 5)}"; |
| 0 | 190 | | } |
| | 191 | |
|
| | 192 | | public void SetIsBlocked(bool isBlocked) |
| | 193 | | { |
| 0 | 194 | | this.isBlocked = isBlocked; |
| 0 | 195 | | DisableAllFriendFlowButtons(); |
| | 196 | |
|
| 0 | 197 | | blockedFriendButton.gameObject.SetActive(isBlocked); |
| 0 | 198 | | optionsContainer.SetActive(!isBlocked); |
| 0 | 199 | | blockedLabel.SetActive(isBlocked); |
| | 200 | |
|
| 0 | 201 | | if (!isBlocked) { SetFriendStatus(model.friendshipStatus); } |
| 0 | 202 | | } |
| | 203 | |
|
| | 204 | | public void SetActionsActive(bool isActive) => |
| 0 | 205 | | actionsContainer.SetActive(isActive); |
| | 206 | |
|
| | 207 | | private void SetPresence(PresenceStatus status) |
| | 208 | | { |
| 0 | 209 | | if (status == PresenceStatus.ONLINE) |
| | 210 | | { |
| 0 | 211 | | onlineStatus.SetActive(true); |
| 0 | 212 | | offlineStatus.SetActive(false); |
| | 213 | | } |
| | 214 | | else |
| | 215 | | { |
| 0 | 216 | | onlineStatus.SetActive(false); |
| 0 | 217 | | offlineStatus.SetActive(true); |
| | 218 | | } |
| 0 | 219 | | } |
| | 220 | |
|
| | 221 | | private void SetGuestUser(bool isGuest) |
| | 222 | | { |
| 0 | 223 | | normalUserPanel.SetActive(!isGuest); |
| 0 | 224 | | } |
| | 225 | |
|
| | 226 | | private void SetHasBlockedOwnUser(bool hasBlocked) |
| | 227 | | { |
| 0 | 228 | | friendsFlowContainer.SetActive(!hasBlocked); |
| 0 | 229 | | } |
| | 230 | |
|
| | 231 | | private void DisableAllFriendFlowButtons() |
| | 232 | | { |
| 0 | 233 | | alreadyFriendsButton.gameObject.SetActive(false); |
| 0 | 234 | | addFriendButton.gameObject.SetActive(false); |
| 0 | 235 | | cancelFriendRequestButton.gameObject.SetActive(false); |
| 0 | 236 | | acceptFriendButton.gameObject.SetActive(false); |
| 0 | 237 | | blockedFriendButton.gameObject.SetActive(false); |
| 0 | 238 | | } |
| | 239 | |
|
| | 240 | | private void CopyWalletToClipboard() |
| | 241 | | { |
| 0 | 242 | | if (fullWalletAddress == null) |
| 0 | 243 | | return; |
| | 244 | |
|
| 0 | 245 | | OnWalletCopy?.Invoke(fullWalletAddress); |
| 0 | 246 | | ResetCopyToast(); |
| 0 | 247 | | cts = new CancellationTokenSource(); |
| 0 | 248 | | ShowCopyToast(copyAddressToast, cts.Token).Forget(); |
| 0 | 249 | | } |
| | 250 | |
|
| | 251 | | private void CopyUsernameToClipboard() |
| | 252 | | { |
| 0 | 253 | | if (string.IsNullOrEmpty(model.name)) |
| 0 | 254 | | return; |
| | 255 | |
|
| 0 | 256 | | OnUsernameCopy?.Invoke(model.name); |
| 0 | 257 | | ResetCopyToast(); |
| 0 | 258 | | cts = new CancellationTokenSource(); |
| 0 | 259 | | ShowCopyToast(copyUsernameToast, cts.Token).Forget(); |
| 0 | 260 | | } |
| | 261 | |
|
| | 262 | | private async UniTaskVoid ShowCopyToast(ShowHideAnimator toast, CancellationToken ct) |
| | 263 | | { |
| 0 | 264 | | if (!toast.gameObject.activeSelf) { toast.gameObject.SetActive(true); } |
| | 265 | |
|
| 0 | 266 | | toast.Show(); |
| 0 | 267 | | await Task.Delay(COPY_TOAST_VISIBLE_TIME, ct); |
| 0 | 268 | | toast.Hide(); |
| 0 | 269 | | } |
| | 270 | |
|
| | 271 | | private void ResetCancellationToken() |
| | 272 | | { |
| 0 | 273 | | cts?.Cancel(); |
| 0 | 274 | | cts?.Dispose(); |
| 0 | 275 | | cts = null; |
| 0 | 276 | | } |
| | 277 | |
|
| | 278 | | private void WhisperActionFlow() |
| | 279 | | { |
| 0 | 280 | | if (areFriends) { OnWhisperUser?.Invoke(model.userId); } |
| | 281 | | else |
| | 282 | | { |
| 0 | 283 | | if (areFriends) { whisperNonFriendsPopup.Hide(); } |
| 0 | 284 | | else { whisperNonFriendsPopup.Show(); } |
| | 285 | | } |
| 0 | 286 | | } |
| | 287 | |
|
| | 288 | | private void OpenOptions() |
| | 289 | | { |
| 0 | 290 | | userContextMenu.Show(model.userId); |
| 0 | 291 | | } |
| | 292 | | } |
| | 293 | | } |