| | 1 | | using TMPro; |
| | 2 | | using UnityEngine; |
| | 3 | | using UnityEngine.UI; |
| | 4 | | using System; |
| | 5 | | using System.Collections; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using System.Text; |
| | 8 | | using DCL.Helpers; |
| | 9 | | using DG.Tweening; |
| | 10 | | using System.Threading; |
| | 11 | | using Cysharp.Threading.Tasks; |
| | 12 | |
|
| | 13 | | public class ChatNotificationMessageComponentView : BaseComponentView, IChatNotificationMessageComponentView, IComponent |
| | 14 | | { |
| | 15 | | private const string NEAR_BY_CHANNEL = "~nearby"; |
| | 16 | |
|
| | 17 | | [Header("Prefab References")] |
| | 18 | | [SerializeField] internal Button button; |
| | 19 | | [SerializeField] public TMP_Text notificationMessage; |
| | 20 | | [SerializeField] internal TMP_Text notificationHeader; |
| | 21 | | [SerializeField] internal TMP_Text notificationSender; |
| | 22 | | [SerializeField] internal TMP_Text notificationTimestamp; |
| | 23 | | [SerializeField] internal ImageComponentView image; |
| | 24 | | [SerializeField] internal GameObject imageBackground; |
| | 25 | | [SerializeField] internal GameObject multiNotificationBackground; |
| | 26 | | [SerializeField] internal GameObject firstSeparator; |
| | 27 | | [SerializeField] internal GameObject secondSeparator; |
| | 28 | | [SerializeField] internal bool isPrivate; |
| | 29 | | [SerializeField] internal RectTransform backgroundTransform; |
| | 30 | | [SerializeField] internal RectTransform messageContainerTransform; |
| | 31 | | [SerializeField] internal RectTransform header; |
| | 32 | | [SerializeField] internal RectTransform content; |
| | 33 | |
|
| | 34 | | [Header("Configuration")] |
| | 35 | | [SerializeField] internal ChatNotificationMessageComponentModel model; |
| | 36 | | [SerializeField] private Color privateColor; |
| | 37 | | [SerializeField] private Color publicColor; |
| | 38 | | [SerializeField] private Color standardColor; |
| | 39 | | [SerializeField] private Color[] channelColors; |
| | 40 | |
|
| | 41 | | public event Action<string> OnClickedNotification; |
| 17 | 42 | | public bool shouldAnimateFocus = true; |
| | 43 | | public string notificationTargetId; |
| | 44 | | private int maxContentCharacters, maxHeaderCharacters, maxSenderCharacters; |
| | 45 | | private float startingXPosition; |
| | 46 | | private Image backgroundImage; |
| | 47 | |
|
| | 48 | | public void Configure(ChatNotificationMessageComponentModel newModel) |
| | 49 | | { |
| 2 | 50 | | model = newModel; |
| 2 | 51 | | RefreshControl(); |
| 2 | 52 | | } |
| | 53 | |
|
| | 54 | | public override void Awake() |
| | 55 | | { |
| 16 | 56 | | base.Awake(); |
| 17 | 57 | | button?.onClick.AddListener(()=>OnClickedNotification?.Invoke(notificationTargetId)); |
| 16 | 58 | | startingXPosition = messageContainerTransform.anchoredPosition.x; |
| 16 | 59 | | backgroundImage = imageBackground.GetComponent<Image>(); |
| 16 | 60 | | RefreshControl(); |
| 16 | 61 | | } |
| | 62 | |
|
| | 63 | | public override void Show(bool instant = false) |
| | 64 | | { |
| 0 | 65 | | showHideAnimator.animSpeedFactor = 0.7f; |
| 0 | 66 | | base.Show(instant); |
| 0 | 67 | | ForceUIRefresh(); |
| 0 | 68 | | } |
| | 69 | |
|
| | 70 | | public override void OnFocus() |
| | 71 | | { |
| 0 | 72 | | base.OnFocus(); |
| 0 | 73 | | if(shouldAnimateFocus) |
| 0 | 74 | | messageContainerTransform.anchoredPosition = new Vector2(startingXPosition + 5, messageContainerTransform.an |
| 0 | 75 | | } |
| | 76 | |
|
| | 77 | | public override void OnLoseFocus() |
| | 78 | | { |
| 19 | 79 | | base.OnLoseFocus(); |
| 19 | 80 | | if(shouldAnimateFocus) |
| 19 | 81 | | messageContainerTransform.anchoredPosition = new Vector2(startingXPosition, messageContainerTransform.anchor |
| 19 | 82 | | } |
| | 83 | |
|
| | 84 | | public override void Hide(bool instant = false) |
| | 85 | | { |
| 0 | 86 | | showHideAnimator.animSpeedFactor = 0.05f; |
| 0 | 87 | | base.Hide(instant); |
| 0 | 88 | | } |
| | 89 | |
|
| | 90 | | public override void RefreshControl() |
| | 91 | | { |
| 21 | 92 | | if (model == null) |
| 0 | 93 | | return; |
| | 94 | |
|
| 21 | 95 | | SetMaxContentCharacters(model.maxContentCharacters); |
| 21 | 96 | | SetMaxHeaderCharacters(model.maxHeaderCharacters); |
| 21 | 97 | | SetMaxSenderCharacters(model.maxSenderCharacters); |
| 21 | 98 | | SetNotificationSender(model.messageSender); |
| 21 | 99 | | SetMessage(model.message); |
| 21 | 100 | | SetTimestamp(model.time); |
| 21 | 101 | | SetIsPrivate(model.isPrivate); |
| 21 | 102 | | SetNotificationHeader(model.messageHeader); |
| 21 | 103 | | SetImage(model.imageUri); |
| 21 | 104 | | } |
| | 105 | |
|
| | 106 | | public void SetMessage(string message) |
| | 107 | | { |
| 26 | 108 | | model.message = message; |
| 26 | 109 | | if (message.Length <= maxContentCharacters) |
| 23 | 110 | | notificationMessage.text = message; |
| | 111 | | else |
| 3 | 112 | | notificationMessage.text = $"{message.Substring(0, maxContentCharacters)}..."; |
| | 113 | |
|
| 26 | 114 | | ForceUIRefresh(); |
| 26 | 115 | | } |
| | 116 | |
|
| | 117 | | public void SetTimestamp(string timestamp) |
| | 118 | | { |
| 25 | 119 | | model.time = timestamp; |
| 25 | 120 | | notificationTimestamp.text = timestamp; |
| | 121 | |
|
| 25 | 122 | | ForceUIRefresh(); |
| 25 | 123 | | } |
| | 124 | |
|
| | 125 | | public void SetNotificationHeader(string header) |
| | 126 | | { |
| 26 | 127 | | if (!isPrivate) |
| | 128 | | { |
| 5 | 129 | | if(header == NEAR_BY_CHANNEL) |
| 2 | 130 | | notificationHeader.color = publicColor; |
| | 131 | | else |
| 3 | 132 | | SetRandomColorForChannel(header); |
| | 133 | | } |
| | 134 | |
|
| 26 | 135 | | model.messageHeader = header; |
| 26 | 136 | | if(header.Length <= maxHeaderCharacters) |
| 23 | 137 | | notificationHeader.text = header; |
| | 138 | | else |
| 3 | 139 | | notificationHeader.text = $"{header.Substring(0, maxHeaderCharacters)}..."; |
| | 140 | |
|
| 26 | 141 | | ForceUIRefresh(); |
| 26 | 142 | | } |
| | 143 | |
|
| | 144 | |
|
| | 145 | | private void SetRandomColorForChannel(string header) |
| | 146 | | { |
| 3 | 147 | | int seed = 0; |
| 3 | 148 | | byte[] ASCIIvalues = Encoding.ASCII.GetBytes(header); |
| 66 | 149 | | foreach (var value in ASCIIvalues) |
| | 150 | | { |
| 30 | 151 | | seed += (int)value; |
| | 152 | | } |
| 3 | 153 | | System.Random rand1 = new System.Random(seed); |
| 3 | 154 | | notificationHeader.color = channelColors[rand1.Next(0, channelColors.Length)]; |
| 3 | 155 | | } |
| | 156 | |
|
| | 157 | | public void SetNotificationSender(string sender) |
| | 158 | | { |
| 24 | 159 | | model.messageSender = sender; |
| 24 | 160 | | if (sender.Length <= maxSenderCharacters) |
| 16 | 161 | | notificationSender.text = $"{sender}"; |
| | 162 | | else |
| 8 | 163 | | notificationSender.text = $"{sender.Substring(0, maxSenderCharacters)}:"; |
| | 164 | |
|
| 24 | 165 | | ForceUIRefresh(); |
| 24 | 166 | | } |
| | 167 | |
|
| | 168 | | public void SetIsMultipleNotifications() |
| | 169 | | { |
| 0 | 170 | | imageBackground.SetActive(false); |
| 0 | 171 | | multiNotificationBackground.SetActive(true); |
| 0 | 172 | | notificationHeader.color = standardColor; |
| 0 | 173 | | ForceUIRefresh(); |
| 0 | 174 | | } |
| | 175 | |
|
| | 176 | | public void SetIsPrivate(bool isPrivate) |
| | 177 | | { |
| 26 | 178 | | model.isPrivate = isPrivate; |
| 26 | 179 | | this.isPrivate = isPrivate; |
| 26 | 180 | | imageBackground.SetActive(isPrivate); |
| 26 | 181 | | firstSeparator.SetActive(isPrivate); |
| 26 | 182 | | secondSeparator.SetActive(isPrivate); |
| 26 | 183 | | if(multiNotificationBackground != null) |
| 0 | 184 | | multiNotificationBackground.SetActive(false); |
| | 185 | |
|
| 26 | 186 | | if (isPrivate) |
| 20 | 187 | | notificationHeader.color = privateColor; |
| | 188 | | else |
| 6 | 189 | | notificationHeader.color = publicColor; |
| 26 | 190 | | ForceUIRefresh(); |
| 26 | 191 | | } |
| | 192 | |
|
| | 193 | | public void SetImage(string uri) |
| | 194 | | { |
| 21 | 195 | | if (!isPrivate) |
| 3 | 196 | | return; |
| | 197 | |
|
| 18 | 198 | | image.SetImage((Sprite)null); |
| 18 | 199 | | model.imageUri = uri; |
| 18 | 200 | | image.SetImage(uri); |
| 18 | 201 | | ForceUIRefresh(); |
| 18 | 202 | | } |
| | 203 | |
|
| | 204 | | public void SetPositionOffset(float xPosHeader, float xPosContent) |
| | 205 | | { |
| 0 | 206 | | if(header != null) |
| 0 | 207 | | header.anchoredPosition = new Vector2(xPosHeader, header.anchoredPosition.y); |
| 0 | 208 | | if(content != null) |
| 0 | 209 | | content.anchoredPosition = new Vector2(xPosContent, content.anchoredPosition.y); |
| | 210 | |
|
| 0 | 211 | | ForceUIRefresh(); |
| 0 | 212 | | } |
| | 213 | |
|
| | 214 | | public void SetMaxContentCharacters(int maxContentCharacters) |
| | 215 | | { |
| 23 | 216 | | model.maxContentCharacters = maxContentCharacters; |
| 23 | 217 | | this.maxContentCharacters = maxContentCharacters; |
| 23 | 218 | | } |
| | 219 | |
|
| | 220 | | public void SetMaxHeaderCharacters(int maxHeaderCharacters) |
| | 221 | | { |
| 23 | 222 | | model.maxHeaderCharacters = maxHeaderCharacters; |
| 23 | 223 | | this.maxHeaderCharacters = maxHeaderCharacters; |
| 23 | 224 | | } |
| | 225 | |
|
| | 226 | | public void SetMaxSenderCharacters(int maxSenderCharacters) |
| | 227 | | { |
| 21 | 228 | | model.maxSenderCharacters = maxSenderCharacters; |
| 21 | 229 | | this.maxSenderCharacters = maxSenderCharacters; |
| 21 | 230 | | } |
| | 231 | |
|
| | 232 | | public void SetNotificationTargetId(string notificationTargetId) |
| | 233 | | { |
| 3 | 234 | | model.notificationTargetId = notificationTargetId; |
| 3 | 235 | | this.notificationTargetId = notificationTargetId; |
| 3 | 236 | | } |
| | 237 | |
|
| | 238 | | private void ForceUIRefresh() |
| | 239 | | { |
| 145 | 240 | | Utils.ForceRebuildLayoutImmediate(backgroundTransform); |
| 145 | 241 | | } |
| | 242 | | } |