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