| | 1 | | using System; |
| | 2 | | using TMPro; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.UI; |
| | 5 | |
|
| | 6 | | namespace DCL.Chat.HUD |
| | 7 | | { |
| | 8 | | public class PrivateChatEntry : BaseComponentView, IComponentModelConfig<PrivateChatEntryModel> |
| | 9 | | { |
| | 10 | | [SerializeField] internal Button openChatButton; |
| | 11 | | [SerializeField] internal PrivateChatEntryModel model; |
| | 12 | | [SerializeField] internal TMP_Text userNameLabel; |
| | 13 | | [SerializeField] internal TMP_Text lastMessageLabel; |
| | 14 | | [SerializeField] internal ImageComponentView picture; |
| | 15 | | [SerializeField] internal UnreadNotificationBadge unreadNotifications; |
| | 16 | | [SerializeField] internal Button optionsButton; |
| | 17 | | [SerializeField] internal GameObject blockedContainer; |
| | 18 | | [SerializeField] internal GameObject onlineStatusContainer; |
| | 19 | | [SerializeField] internal GameObject offlineStatusContainer; |
| | 20 | | [SerializeField] internal RectTransform userContextMenuPositionReference; |
| | 21 | |
|
| | 22 | | private UserContextMenu userContextMenu; |
| | 23 | | private IChatController chatController; |
| | 24 | |
|
| 0 | 25 | | public PrivateChatEntryModel Model => model; |
| | 26 | |
|
| | 27 | | public event Action<PrivateChatEntry> OnOpenChat; |
| | 28 | |
|
| | 29 | | public static PrivateChatEntry Create() |
| | 30 | | { |
| 7 | 31 | | return Instantiate(Resources.Load<PrivateChatEntry>("SocialBarV1/WhisperChannelElement")); |
| | 32 | | } |
| | 33 | |
|
| | 34 | | public override void Awake() |
| | 35 | | { |
| 50 | 36 | | base.Awake(); |
| 50 | 37 | | optionsButton.onClick.AddListener(() => |
| | 38 | | { |
| 0 | 39 | | userContextMenu.Show(model.userId); |
| 0 | 40 | | Dock(userContextMenu); |
| 0 | 41 | | }); |
| 52 | 42 | | openChatButton.onClick.AddListener(() => OnOpenChat?.Invoke(this)); |
| 50 | 43 | | onFocused += isFocused => |
| | 44 | | { |
| 93 | 45 | | if (optionsButton == null) return; |
| 93 | 46 | | var isContextualMenuOpenWithThisUser = userContextMenu != null |
| | 47 | | && userContextMenu.isVisible |
| | 48 | | && userContextMenu.UserId == model.userId; |
| 93 | 49 | | optionsButton.gameObject.SetActive(isFocused || isContextualMenuOpenWithThisUser); |
| 93 | 50 | | }; |
| 50 | 51 | | } |
| | 52 | |
|
| | 53 | | public void Initialize(IChatController chatController, |
| | 54 | | UserContextMenu userContextMenu) |
| | 55 | | { |
| 41 | 56 | | this.chatController = chatController; |
| 41 | 57 | | this.userContextMenu = userContextMenu; |
| 41 | 58 | | userContextMenu.OnHide -= HandleContextMenuHidden; |
| 41 | 59 | | userContextMenu.OnHide += HandleContextMenuHidden; |
| 41 | 60 | | userContextMenu.OnBlock -= HandleUserBlocked; |
| 41 | 61 | | userContextMenu.OnBlock += HandleUserBlocked; |
| 41 | 62 | | } |
| | 63 | |
|
| | 64 | | public void Configure(PrivateChatEntryModel newModel) |
| | 65 | | { |
| 48 | 66 | | model = newModel; |
| 48 | 67 | | RefreshControl(); |
| 48 | 68 | | } |
| | 69 | |
|
| | 70 | | public override void RefreshControl() |
| | 71 | | { |
| 48 | 72 | | userNameLabel.text = model.userName; |
| 48 | 73 | | lastMessageLabel.text = model.lastMessage; |
| 48 | 74 | | lastMessageLabel.gameObject.SetActive(!string.IsNullOrEmpty(model.lastMessage)); |
| 48 | 75 | | SetBlockStatus(model.isBlocked); |
| 48 | 76 | | SetPresence(model.isOnline); |
| 48 | 77 | | unreadNotifications.Initialize(chatController, model.userId); |
| | 78 | |
|
| 48 | 79 | | if (model.imageFetchingEnabled) |
| 1 | 80 | | EnableAvatarSnapshotFetching(); |
| | 81 | | else |
| 47 | 82 | | DisableAvatarSnapshotFetching(); |
| 47 | 83 | | } |
| | 84 | |
|
| | 85 | | private void HandleUserBlocked(string userId, bool blocked) |
| | 86 | | { |
| 0 | 87 | | if (userId != model.userId) return; |
| 0 | 88 | | SetBlockStatus(blocked); |
| 0 | 89 | | } |
| | 90 | |
|
| | 91 | | public void SetBlockStatus(bool isBlocked) |
| | 92 | | { |
| 48 | 93 | | model.isBlocked = isBlocked; |
| 48 | 94 | | blockedContainer.SetActive(isBlocked); |
| 48 | 95 | | } |
| | 96 | |
|
| | 97 | | private void SetPresence(bool isOnline) |
| | 98 | | { |
| 48 | 99 | | model.isOnline = isOnline; |
| 48 | 100 | | onlineStatusContainer.SetActive(isOnline && !model.isBlocked); |
| 48 | 101 | | offlineStatusContainer.SetActive(!isOnline && !model.isBlocked); |
| 48 | 102 | | } |
| | 103 | |
|
| | 104 | | private void Dock(UserContextMenu userContextMenu) |
| | 105 | | { |
| 0 | 106 | | var menuTransform = (RectTransform) userContextMenu.transform; |
| 0 | 107 | | menuTransform.pivot = userContextMenuPositionReference.pivot; |
| 0 | 108 | | menuTransform.position = userContextMenuPositionReference.position; |
| 0 | 109 | | } |
| | 110 | |
|
| | 111 | | public bool IsVisible(RectTransform container) |
| | 112 | | { |
| 19 | 113 | | if (!gameObject.activeSelf) return false; |
| 19 | 114 | | return ((RectTransform) transform).CountCornersVisibleFrom(container) > 0; |
| | 115 | | } |
| | 116 | |
|
| | 117 | | public void EnableAvatarSnapshotFetching() |
| | 118 | | { |
| 29 | 119 | | if (model.imageFetchingEnabled) return; |
| 27 | 120 | | picture.Configure(new ImageComponentModel {uri = model.pictureUrl}); |
| 27 | 121 | | model.imageFetchingEnabled = true; |
| 27 | 122 | | } |
| | 123 | |
|
| | 124 | | public void DisableAvatarSnapshotFetching() |
| | 125 | | { |
| 127 | 126 | | if (!model.imageFetchingEnabled) return; |
| 1 | 127 | | picture.SetImage((string) null); |
| 1 | 128 | | model.imageFetchingEnabled = false; |
| 1 | 129 | | } |
| | 130 | |
|
| | 131 | | private void HandleContextMenuHidden() |
| | 132 | | { |
| 0 | 133 | | optionsButton.gameObject.SetActive(false); |
| 0 | 134 | | } |
| | 135 | | } |
| | 136 | | } |