| | 1 | | using DCL.Chat; |
| | 2 | | using TMPro; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | /// <summary> |
| | 6 | | /// Shows the number of unread messages from a the world chat. |
| | 7 | | /// </summary> |
| | 8 | | public class UnreadWorldNotificationBadge : MonoBehaviour |
| | 9 | | { |
| | 10 | | public TextMeshProUGUI notificationText; |
| | 11 | | public GameObject notificationContainer; |
| 10 | 12 | | public int maxNumberToShow = 9; |
| | 13 | |
|
| | 14 | | private int currentUnreadMessagesValue; |
| | 15 | | private IChatController chatController; |
| | 16 | |
|
| | 17 | | public int CurrentUnreadMessages |
| | 18 | | { |
| 0 | 19 | | get => currentUnreadMessagesValue; |
| | 20 | | set |
| | 21 | | { |
| 18 | 22 | | currentUnreadMessagesValue = value; |
| 18 | 23 | | if (currentUnreadMessagesValue > 0) |
| | 24 | | { |
| 12 | 25 | | notificationContainer.SetActive(true); |
| 12 | 26 | | notificationText.text = currentUnreadMessagesValue <= maxNumberToShow |
| | 27 | | ? currentUnreadMessagesValue.ToString() |
| | 28 | | : $"+{maxNumberToShow}"; |
| 12 | 29 | | } |
| | 30 | | else |
| | 31 | | { |
| 6 | 32 | | notificationContainer.SetActive(false); |
| | 33 | | } |
| 6 | 34 | | } |
| | 35 | | } |
| | 36 | |
|
| | 37 | | private void Start() |
| | 38 | | { |
| 2 | 39 | | Initialize(ChatController.i); |
| 2 | 40 | | } |
| | 41 | |
|
| | 42 | | private void OnDestroy() |
| | 43 | | { |
| 7 | 44 | | if (chatController != null) |
| 5 | 45 | | chatController.OnTotalUnseenMessagesUpdated -= UpdateTotalUnseenMessages; |
| 7 | 46 | | } |
| | 47 | |
|
| 9 | 48 | | private void OnEnable() => UpdateTotalUnseenMessages(); |
| | 49 | |
|
| | 50 | | /// <summary> |
| | 51 | | /// Prepares the notification badge for listening to the world chat |
| | 52 | | /// </summary> |
| | 53 | | /// <param name="chatController">Chat Controlled to be listened</param> |
| | 54 | | public void Initialize(IChatController chatController) |
| | 55 | | { |
| 5 | 56 | | if (chatController == null) return; |
| 5 | 57 | | this.chatController = chatController; |
| 5 | 58 | | chatController.OnTotalUnseenMessagesUpdated += UpdateTotalUnseenMessages; |
| 5 | 59 | | UpdateTotalUnseenMessages(); |
| 5 | 60 | | } |
| | 61 | |
|
| | 62 | | private void UpdateTotalUnseenMessages(int totalUnseenMessages) => |
| 13 | 63 | | CurrentUnreadMessages = totalUnseenMessages; |
| | 64 | |
|
| | 65 | | private void UpdateTotalUnseenMessages() |
| | 66 | | { |
| 23 | 67 | | if (chatController == null) return; |
| 5 | 68 | | CurrentUnreadMessages = chatController.TotalUnseenMessages; |
| 5 | 69 | | } |
| | 70 | | } |