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