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