| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using SocialBar.UserThumbnail; |
| | 4 | | using SocialFeaturesAnalytics; |
| | 5 | | using TMPro; |
| | 6 | | using UnityEngine; |
| | 7 | | using UnityEngine.EventSystems; |
| | 8 | | using UnityEngine.UI; |
| | 9 | |
|
| | 10 | | public class PrivateChatWindowComponentView : BaseComponentView, IPrivateChatComponentView, IPointerDownHandler |
| | 11 | | { |
| | 12 | | private const float REQUEST_MORE_ENTRIES_SCROLL_THRESHOLD = 0.995f; |
| | 13 | |
|
| | 14 | | [SerializeField] internal Button backButton; |
| | 15 | | [SerializeField] internal Button closeButton; |
| | 16 | | [SerializeField] internal UserThumbnailComponentView userThumbnail; |
| | 17 | | [SerializeField] internal TMP_Text userNameLabel; |
| | 18 | | [SerializeField] internal PrivateChatHUDView chatView; |
| | 19 | | [SerializeField] internal GameObject jumpInButtonContainer; |
| | 20 | | [SerializeField] internal JumpInButton jumpInButton; |
| | 21 | | [SerializeField] internal UserContextMenu userContextMenu; |
| | 22 | | [SerializeField] internal RectTransform userContextMenuReferencePoint; |
| | 23 | | [SerializeField] internal Button optionsButton; |
| | 24 | | [SerializeField] internal GameObject messagesLoading; |
| | 25 | | [SerializeField] internal ScrollRect scroll; |
| | 26 | | [SerializeField] internal GameObject oldMessagesLoadingContainer; |
| | 27 | | [SerializeField] private Model model; |
| | 28 | | [SerializeField] internal CanvasGroup[] previewCanvasGroup; |
| | 29 | | [SerializeField] private Vector2 previewModeSize; |
| | 30 | |
|
| | 31 | | private IFriendsController friendsController; |
| | 32 | | private ISocialAnalytics socialAnalytics; |
| | 33 | | private Coroutine alphaRoutine; |
| | 34 | | private Vector2 originalSize; |
| | 35 | | private bool isPreviewActivated; |
| | 36 | |
|
| | 37 | | public event Action OnPressBack; |
| | 38 | | public event Action OnMinimize; |
| | 39 | | public event Action OnClose; |
| | 40 | | public event Action<string> OnUnfriend |
| | 41 | | { |
| 1 | 42 | | add => userContextMenu.OnUnfriend += value; |
| 1 | 43 | | remove => userContextMenu.OnUnfriend -= value; |
| | 44 | | } |
| | 45 | |
|
| | 46 | | public event Action OnRequireMoreMessages; |
| | 47 | | public event Action<bool> OnFocused |
| | 48 | | { |
| 3 | 49 | | add => onFocused += value; |
| 1 | 50 | | remove => onFocused -= value; |
| | 51 | | } |
| | 52 | | public event Action OnClickOverWindow; |
| | 53 | |
|
| 1 | 54 | | public IChatHUDComponentView ChatHUD => chatView; |
| 2 | 55 | | public bool IsActive => gameObject.activeInHierarchy; |
| 0 | 56 | | public RectTransform Transform => (RectTransform) transform; |
| 0 | 57 | | public bool IsFocused => isFocused; |
| | 58 | |
|
| | 59 | | public static PrivateChatWindowComponentView Create() |
| | 60 | | { |
| 16 | 61 | | return Instantiate(Resources.Load<PrivateChatWindowComponentView>("SocialBarV1/PrivateChatHUD")); |
| | 62 | | } |
| | 63 | |
|
| | 64 | | public override void Awake() |
| | 65 | | { |
| 16 | 66 | | base.Awake(); |
| 16 | 67 | | originalSize = ((RectTransform) transform).sizeDelta; |
| 17 | 68 | | backButton.onClick.AddListener(() => OnPressBack?.Invoke()); |
| 17 | 69 | | closeButton.onClick.AddListener(() => OnClose?.Invoke()); |
| 16 | 70 | | optionsButton.onClick.AddListener(ShowOptions); |
| 16 | 71 | | userContextMenu.OnBlock += HandleBlockFromContextMenu; |
| 16 | 72 | | scroll.onValueChanged.AddListener((scrollPos) => |
| | 73 | | { |
| 2 | 74 | | if (isPreviewActivated) |
| 2 | 75 | | return; |
| | 76 | |
|
| 0 | 77 | | if (scrollPos.y > REQUEST_MORE_ENTRIES_SCROLL_THRESHOLD) |
| 0 | 78 | | OnRequireMoreMessages?.Invoke(); |
| 0 | 79 | | }); |
| 16 | 80 | | } |
| | 81 | |
|
| | 82 | | public void Initialize(IFriendsController friendsController, ISocialAnalytics socialAnalytics) |
| | 83 | | { |
| 1 | 84 | | this.friendsController = friendsController; |
| 1 | 85 | | this.socialAnalytics = socialAnalytics; |
| 1 | 86 | | } |
| | 87 | |
|
| | 88 | | public override void Dispose() |
| | 89 | | { |
| 32 | 90 | | if (!this) return; |
| 32 | 91 | | if (!gameObject) return; |
| | 92 | |
|
| 32 | 93 | | if (userContextMenu != null) |
| | 94 | | { |
| 32 | 95 | | userContextMenu.OnBlock -= HandleBlockFromContextMenu; |
| | 96 | | } |
| | 97 | |
|
| 32 | 98 | | base.Dispose(); |
| 32 | 99 | | } |
| | 100 | |
|
| | 101 | | public void ActivatePreview() |
| | 102 | | { |
| 4 | 103 | | if (!this) return; |
| 2 | 104 | | if (!gameObject) return; |
| | 105 | |
|
| 2 | 106 | | isPreviewActivated = true; |
| 2 | 107 | | SetLoadingMessagesActive(false); |
| 2 | 108 | | SetOldMessagesLoadingActive(false); |
| | 109 | |
|
| | 110 | | const float alphaTarget = 0f; |
| | 111 | |
|
| 2 | 112 | | if (!gameObject.activeInHierarchy) |
| | 113 | | { |
| 0 | 114 | | foreach (var group in previewCanvasGroup) |
| 0 | 115 | | group.alpha = alphaTarget; |
| | 116 | |
|
| 0 | 117 | | return; |
| | 118 | | } |
| | 119 | |
|
| 2 | 120 | | if (alphaRoutine != null) |
| 1 | 121 | | StopCoroutine(alphaRoutine); |
| | 122 | |
|
| 2 | 123 | | alphaRoutine = StartCoroutine(SetAlpha(alphaTarget, 0.5f)); |
| 2 | 124 | | ((RectTransform) transform).sizeDelta = previewModeSize; |
| 2 | 125 | | } |
| | 126 | |
|
| | 127 | | public void DeactivatePreview() |
| | 128 | | { |
| 2 | 129 | | isPreviewActivated = false; |
| | 130 | |
|
| | 131 | | const float alphaTarget = 1f; |
| | 132 | |
|
| 2 | 133 | | if (!gameObject.activeInHierarchy) |
| | 134 | | { |
| 0 | 135 | | foreach (var group in previewCanvasGroup) |
| 0 | 136 | | group.alpha = alphaTarget; |
| | 137 | |
|
| 0 | 138 | | return; |
| | 139 | | } |
| | 140 | |
|
| 2 | 141 | | if (alphaRoutine != null) |
| 1 | 142 | | StopCoroutine(alphaRoutine); |
| | 143 | |
|
| 2 | 144 | | alphaRoutine = StartCoroutine(SetAlpha(alphaTarget, 0.5f)); |
| 2 | 145 | | ((RectTransform) transform).sizeDelta = originalSize; |
| 2 | 146 | | } |
| | 147 | |
|
| | 148 | | public void SetLoadingMessagesActive(bool isActive) |
| | 149 | | { |
| 5 | 150 | | if (messagesLoading == null) |
| 0 | 151 | | return; |
| | 152 | |
|
| 5 | 153 | | messagesLoading.SetActive(isActive); |
| 5 | 154 | | } |
| | 155 | |
|
| | 156 | | public void SetOldMessagesLoadingActive(bool isActive) |
| | 157 | | { |
| 5 | 158 | | if (oldMessagesLoadingContainer == null) |
| 0 | 159 | | return; |
| | 160 | |
|
| 5 | 161 | | oldMessagesLoadingContainer.SetActive(isActive); |
| 5 | 162 | | oldMessagesLoadingContainer.transform.SetAsFirstSibling(); |
| 5 | 163 | | } |
| | 164 | |
|
| | 165 | | public override void RefreshControl() |
| | 166 | | { |
| 2 | 167 | | userThumbnail.Configure(new UserThumbnailComponentModel |
| | 168 | | { |
| | 169 | | faceUrl = model.faceSnapshotUrl, |
| | 170 | | isBlocked = model.isUserBlocked, |
| | 171 | | isOnline = model.isUserOnline |
| | 172 | | }); |
| 2 | 173 | | userNameLabel.SetText(model.userName); |
| 2 | 174 | | jumpInButtonContainer.SetActive(model.isUserOnline); |
| 2 | 175 | | } |
| | 176 | |
|
| | 177 | | public void Setup(UserProfile profile, bool isOnline, bool isBlocked) |
| | 178 | | { |
| 2 | 179 | | model = new Model |
| | 180 | | { |
| | 181 | | userId = profile.userId, |
| | 182 | | faceSnapshotUrl = profile.face256SnapshotURL, |
| | 183 | | userName = profile.userName, |
| | 184 | | isUserOnline = isOnline, |
| | 185 | | isUserBlocked = isBlocked |
| | 186 | | }; |
| 2 | 187 | | RefreshControl(); |
| | 188 | |
|
| 2 | 189 | | jumpInButton.Initialize(friendsController, profile.userId, socialAnalytics); |
| 2 | 190 | | } |
| | 191 | |
|
| 2 | 192 | | public void Show() => gameObject.SetActive(true); |
| | 193 | |
|
| 2 | 194 | | public void Hide() => gameObject.SetActive(false); |
| | 195 | |
|
| 1 | 196 | | public void OnPointerDown(PointerEventData eventData) => OnClickOverWindow?.Invoke(); |
| | 197 | |
|
| | 198 | | private void ShowOptions() |
| | 199 | | { |
| 0 | 200 | | var contextMenuTransform = (RectTransform) userContextMenu.transform; |
| 0 | 201 | | contextMenuTransform.pivot = userContextMenuReferencePoint.pivot; |
| 0 | 202 | | contextMenuTransform.position = userContextMenuReferencePoint.position; |
| 0 | 203 | | userContextMenu.Show(model.userId); |
| 0 | 204 | | } |
| | 205 | |
|
| | 206 | | private void HandleBlockFromContextMenu(string userId, bool isBlocked) |
| | 207 | | { |
| 0 | 208 | | if (userId != model.userId) return; |
| 0 | 209 | | model.isUserBlocked = isBlocked; |
| 0 | 210 | | RefreshControl(); |
| 0 | 211 | | } |
| | 212 | |
|
| | 213 | | private IEnumerator SetAlpha(float target, float duration) |
| | 214 | | { |
| 4 | 215 | | var t = 0f; |
| | 216 | |
|
| 226 | 217 | | while (t < duration) |
| | 218 | | { |
| 224 | 219 | | t += Time.deltaTime; |
| | 220 | |
|
| 3584 | 221 | | foreach (var group in previewCanvasGroup) |
| 1568 | 222 | | group.alpha = Mathf.Lerp(group.alpha, target, t / duration); |
| | 223 | |
|
| 224 | 224 | | yield return null; |
| | 225 | | } |
| | 226 | |
|
| 32 | 227 | | foreach (var group in previewCanvasGroup) |
| 14 | 228 | | group.alpha = target; |
| 2 | 229 | | } |
| | 230 | |
|
| | 231 | | [Serializable] |
| | 232 | | private struct Model |
| | 233 | | { |
| | 234 | | public string userId; |
| | 235 | | public string userName; |
| | 236 | | public string faceSnapshotUrl; |
| | 237 | | public bool isUserBlocked; |
| | 238 | | public bool isUserOnline; |
| | 239 | | } |
| | 240 | | } |