| | 1 | | using TMPro; |
| | 2 | | using UnityEngine; |
| | 3 | |
|
| | 4 | | public class ReceivedFriendRequestsNotificationBadge : MonoBehaviour |
| | 5 | | { |
| | 6 | | [SerializeField] private TextMeshProUGUI notificationText; |
| | 7 | | [SerializeField] private GameObject notificationContainer; |
| 54 | 8 | | [SerializeField] private int maxNumberToShow = 9; |
| | 9 | |
|
| | 10 | | private IFriendsController friendsController; |
| | 11 | | private int currentUnreadMessagesValue; |
| | 12 | |
|
| | 13 | | public int CurrentUnreadMessages |
| | 14 | | { |
| 0 | 15 | | get => currentUnreadMessagesValue; |
| | 16 | | set |
| | 17 | | { |
| 33 | 18 | | currentUnreadMessagesValue = value; |
| | 19 | |
|
| 33 | 20 | | if (currentUnreadMessagesValue > 0) |
| | 21 | | { |
| 0 | 22 | | notificationContainer.SetActive(true); |
| 0 | 23 | | notificationText.text = currentUnreadMessagesValue <= maxNumberToShow |
| | 24 | | ? currentUnreadMessagesValue.ToString() |
| | 25 | | : $"+{maxNumberToShow}"; |
| 0 | 26 | | } |
| | 27 | | else |
| | 28 | | { |
| 33 | 29 | | notificationContainer.SetActive(false); |
| | 30 | | } |
| 33 | 31 | | } |
| | 32 | | } |
| | 33 | |
|
| | 34 | | private void Start() |
| | 35 | | { |
| 32 | 36 | | Initialize(FriendsController.i); |
| 32 | 37 | | } |
| | 38 | |
|
| | 39 | | private void OnEnable() |
| | 40 | | { |
| 38 | 41 | | UpdateReceivedRequests(); |
| 38 | 42 | | } |
| | 43 | |
|
| | 44 | | public void Initialize(IFriendsController friendsController) |
| | 45 | | { |
| 32 | 46 | | this.friendsController = friendsController; |
| 32 | 47 | | friendsController.OnTotalFriendRequestUpdated += UpdateReceivedRequests; |
| 32 | 48 | | UpdateReceivedRequests(); |
| 32 | 49 | | } |
| | 50 | |
|
| | 51 | | private void OnDestroy() |
| | 52 | | { |
| 34 | 53 | | if (friendsController != null) |
| 32 | 54 | | friendsController.OnTotalFriendRequestUpdated -= UpdateReceivedRequests; |
| 34 | 55 | | } |
| | 56 | |
|
| | 57 | | private void UpdateReceivedRequests(int totalReceivedFriendRequests, int totalSentFriendRequests) => |
| 0 | 58 | | CurrentUnreadMessages = totalReceivedFriendRequests; |
| | 59 | |
|
| | 60 | | private void UpdateReceivedRequests() |
| | 61 | | { |
| 107 | 62 | | if (friendsController == null) return; |
| 33 | 63 | | CurrentUnreadMessages = friendsController.TotalReceivedFriendRequestCount; |
| 33 | 64 | | } |
| | 65 | | } |