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