| | 1 | | using TMPro; |
| | 2 | | using UnityEngine; |
| | 3 | |
|
| | 4 | | /// <summary> |
| | 5 | | /// Shows the number of unread messages from a friend. |
| | 6 | | /// </summary> |
| | 7 | | public class UnreadNotificationBadge : MonoBehaviour |
| | 8 | | { |
| | 9 | | public TextMeshProUGUI notificationText; |
| | 10 | | public GameObject notificationContainer; |
| 95 | 11 | | public int maxNumberToShow = 9; |
| | 12 | |
|
| | 13 | | private IChatController chatController; |
| | 14 | | private string currentUserId; |
| | 15 | | private int currentUnreadMessagesValue; |
| | 16 | | private bool isInitialized; |
| | 17 | |
|
| | 18 | | public int CurrentUnreadMessages |
| | 19 | | { |
| 0 | 20 | | get => currentUnreadMessagesValue; |
| | 21 | | set |
| | 22 | | { |
| 130 | 23 | | currentUnreadMessagesValue = value; |
| | 24 | |
|
| 130 | 25 | | if (currentUnreadMessagesValue > 0) |
| | 26 | | { |
| 27 | 27 | | notificationContainer.SetActive(true); |
| 27 | 28 | | notificationText.text = currentUnreadMessagesValue <= maxNumberToShow ? currentUnreadMessagesValue.ToStr |
| 27 | 29 | | } |
| | 30 | | else |
| | 31 | | { |
| 103 | 32 | | notificationContainer.SetActive(false); |
| | 33 | | } |
| 103 | 34 | | } |
| | 35 | | } |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// Prepares the notification badge for listening to a specific user |
| | 39 | | /// </summary> |
| | 40 | | /// <param name="chatController">Chat Controlled to be listened</param> |
| | 41 | | /// <param name="userId">User ID to listen to</param> |
| | 42 | | public void Initialize(IChatController chatController, string userId) |
| | 43 | | { |
| 95 | 44 | | if (chatController == null) |
| 12 | 45 | | return; |
| | 46 | |
|
| 83 | 47 | | this.chatController = chatController; |
| 83 | 48 | | currentUserId = userId; |
| | 49 | |
|
| 83 | 50 | | UpdateUnreadMessages(); |
| | 51 | |
|
| 83 | 52 | | chatController.OnUserUnseenMessagesUpdated -= HandleUnseenMessagesUpdated; |
| 83 | 53 | | chatController.OnUserUnseenMessagesUpdated += HandleUnseenMessagesUpdated; |
| | 54 | |
|
| 83 | 55 | | chatController.OnChannelUnseenMessagesUpdated -= HandleChannelUnseenMessagesUpdated; |
| 83 | 56 | | chatController.OnChannelUnseenMessagesUpdated += HandleChannelUnseenMessagesUpdated; |
| | 57 | |
|
| 83 | 58 | | isInitialized = true; |
| 83 | 59 | | } |
| | 60 | |
|
| | 61 | | private void HandleUnseenMessagesUpdated(string userId, int unseenMessages) |
| | 62 | | { |
| 18 | 63 | | if (userId != currentUserId) return; |
| 14 | 64 | | CurrentUnreadMessages = unseenMessages; |
| 14 | 65 | | } |
| | 66 | |
|
| | 67 | | private void HandleChannelUnseenMessagesUpdated(string channelId, int unseenMessages) |
| | 68 | | { |
| 18 | 69 | | if (channelId != currentUserId) return; |
| 14 | 70 | | CurrentUnreadMessages = unseenMessages; |
| 14 | 71 | | } |
| | 72 | |
|
| | 73 | | private void OnEnable() |
| | 74 | | { |
| 253 | 75 | | if (!isInitialized) return; |
| 19 | 76 | | UpdateUnreadMessages(); |
| 19 | 77 | | } |
| | 78 | |
|
| | 79 | | private void OnDestroy() |
| | 80 | | { |
| 66 | 81 | | if (chatController != null) |
| | 82 | | { |
| 52 | 83 | | chatController.OnUserUnseenMessagesUpdated -= HandleUnseenMessagesUpdated; |
| 52 | 84 | | chatController.OnChannelUnseenMessagesUpdated -= HandleChannelUnseenMessagesUpdated; |
| | 85 | | } |
| 66 | 86 | | } |
| | 87 | |
|
| | 88 | | private void UpdateUnreadMessages() => |
| 102 | 89 | | CurrentUnreadMessages = chatController.GetAllocatedUnseenMessages(currentUserId) + chatController.GetAllocatedUn |
| | 90 | | } |