| | 1 | | using DCL.Interface; |
| | 2 | | using System.Linq; |
| | 3 | | using TMPro; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | /// <summary> |
| | 7 | | /// Shows the number of unread messages from a friend. |
| | 8 | | /// </summary> |
| | 9 | | public class UnreadNotificationBadge : MonoBehaviour |
| | 10 | | { |
| | 11 | | public TextMeshProUGUI notificationText; |
| | 12 | | public GameObject notificationContainer; |
| 54 | 13 | | public int maxNumberToShow = 9; |
| | 14 | |
|
| | 15 | | private IChatController currentChatController; |
| | 16 | | private string currentUserId; |
| | 17 | | private long currentTimestampReading; |
| | 18 | | private int currentUnreadMessagesValue; |
| | 19 | |
|
| | 20 | | public int currentUnreadMessages |
| | 21 | | { |
| 0 | 22 | | get => currentUnreadMessagesValue; |
| | 23 | | set |
| | 24 | | { |
| 21 | 25 | | currentUnreadMessagesValue = value; |
| 21 | 26 | | RefreshNotificationBadge(); |
| 21 | 27 | | } |
| | 28 | | } |
| | 29 | |
|
| | 30 | | /// <summary> |
| | 31 | | /// Prepares the notification badge for listening to a specific user |
| | 32 | | /// </summary> |
| | 33 | | /// <param name="chatController">Chat Controlled to be listened</param> |
| | 34 | | /// <param name="userId">User ID to listen to</param> |
| | 35 | | public void Initialize(IChatController chatController, string userId) |
| | 36 | | { |
| 22 | 37 | | if (chatController == null) |
| 16 | 38 | | return; |
| | 39 | |
|
| 6 | 40 | | currentChatController = chatController; |
| 6 | 41 | | currentUserId = userId; |
| | 42 | |
|
| 6 | 43 | | CommonScriptableObjects.lastReadChatMessages.TryGetValue(currentUserId, out currentTimestampReading); |
| 6 | 44 | | UpdateUnreadMessages(); |
| | 45 | |
|
| 6 | 46 | | currentChatController.OnAddMessage -= ChatController_OnAddMessage; |
| 6 | 47 | | currentChatController.OnAddMessage += ChatController_OnAddMessage; |
| | 48 | |
|
| 6 | 49 | | CommonScriptableObjects.lastReadChatMessages.OnAdded -= LastReadChatMessages_OnAdded; |
| 6 | 50 | | CommonScriptableObjects.lastReadChatMessages.OnAdded += LastReadChatMessages_OnAdded; |
| 6 | 51 | | } |
| | 52 | |
|
| | 53 | | private void OnDestroy() |
| | 54 | | { |
| 51 | 55 | | if (currentChatController == null) |
| 45 | 56 | | return; |
| | 57 | |
|
| 6 | 58 | | currentChatController.OnAddMessage -= ChatController_OnAddMessage; |
| 6 | 59 | | CommonScriptableObjects.lastReadChatMessages.OnAdded -= LastReadChatMessages_OnAdded; |
| 6 | 60 | | } |
| | 61 | |
|
| | 62 | | private void ChatController_OnAddMessage(ChatMessage newMessage) |
| | 63 | | { |
| 15 | 64 | | if (newMessage.messageType == ChatMessage.Type.PRIVATE && |
| | 65 | | newMessage.sender == currentUserId) |
| | 66 | | { |
| | 67 | | // A new message from [userId] is received |
| 13 | 68 | | UpdateUnreadMessages(); |
| | 69 | | } |
| 15 | 70 | | } |
| | 71 | |
|
| | 72 | | private void LastReadChatMessages_OnAdded(string addedKey, long addedValue) |
| | 73 | | { |
| 3 | 74 | | if (addedKey == currentUserId) |
| | 75 | | { |
| | 76 | | // The player reads the latest messages of [userId] |
| 1 | 77 | | currentTimestampReading = addedValue; |
| 1 | 78 | | currentUnreadMessages = 0; |
| 1 | 79 | | UpdateUnreadMessages(); |
| | 80 | | } |
| 3 | 81 | | } |
| | 82 | |
|
| | 83 | | private void UpdateUnreadMessages() |
| | 84 | | { |
| 20 | 85 | | currentUnreadMessages = currentChatController.GetEntries() |
| | 86 | | .Count( |
| 59 | 87 | | msg => msg.messageType == ChatMessage.Type.PRIVATE && |
| | 88 | | msg.sender == currentUserId && |
| | 89 | | msg.timestamp > (ulong)currentTimestampReading); |
| 20 | 90 | | } |
| | 91 | |
|
| | 92 | | private void RefreshNotificationBadge() |
| | 93 | | { |
| 21 | 94 | | if (currentUnreadMessagesValue > 0) |
| | 95 | | { |
| 13 | 96 | | notificationContainer.SetActive(true); |
| 13 | 97 | | notificationText.text = currentUnreadMessagesValue <= maxNumberToShow ? currentUnreadMessagesValue.ToString( |
| 13 | 98 | | } |
| | 99 | | else |
| | 100 | | { |
| 8 | 101 | | notificationContainer.SetActive(false); |
| | 102 | | } |
| 8 | 103 | | } |
| | 104 | | } |