TweetePrefabeControler.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using System.Collections.Generic;
  6. struct TweetBufferData
  7. {
  8. public string messageText;
  9. public string senderName;
  10. public string userName;
  11. public string hastags;
  12. public string followersTxt;
  13. public Sprite avatarImage;
  14. }
  15. public class TweetePrefabeControler : GameNotification
  16. {
  17. public static event Action AddTweetToPhone;
  18. [SerializeField] private Image SenderAvatar;
  19. [SerializeField] private Text TweetText;
  20. [SerializeField] private Text SenderName;
  21. [SerializeField] private Text UserName;
  22. [SerializeField] private Text HashTags;
  23. [SerializeField] private Text NumberOfFollowers;
  24. [SerializeField] private Animator animator;
  25. [SerializeField] private GameObject AcceptBTN;
  26. [SerializeField] private GameObject Followers;
  27. string TweedID;
  28. private List<TweetBufferData> tweetBuffer;
  29. public void SetTweet(string _tweetText, string _senderName, string _userName, string _hashtags, int _numberOfFollow, Sprite _face = null, bool inQueue = false)
  30. {
  31. Debug.LogError("SetTweet");
  32. SenderName.text = _senderName;
  33. UserName.text = _userName;
  34. HashTags.text = _hashtags;
  35. NumberOfFollowers.text = "";
  36. if (NumberOfFollowers.transform.parent.gameObject.activeSelf)
  37. {
  38. NumberOfFollowers.transform.parent.gameObject.SetActive(false);
  39. }
  40. bool hasFollowers = _numberOfFollow != 0;
  41. string follow_t = "";
  42. if (hasFollowers)//To show correct number of new followers if it negative
  43. {
  44. NumberOfFollowers.text = follow_t = ((_numberOfFollow < 0) ? "" : "+") + _numberOfFollow.ToString();
  45. if (!NumberOfFollowers.transform.parent.gameObject.activeSelf)
  46. {
  47. NumberOfFollowers.transform.parent.gameObject.SetActive(true);
  48. }
  49. }
  50. SenderAvatar.sprite = _face;
  51. TweetText.text = ChangeTweet(_tweetText);
  52. if(!inQueue)
  53. {
  54. RegisterNotification();
  55. SaveToBuffer(ChangeTweet(_tweetText), _senderName, _userName, _hashtags, follow_t, _face);
  56. }
  57. }
  58. private void SaveToBuffer(string txt, string sender, string name, string htags, string follow, Sprite ava)
  59. {
  60. TweetBufferData tbd = new TweetBufferData();
  61. tbd.messageText = txt;
  62. tbd.senderName = sender;
  63. tbd.userName = name;
  64. tbd.followersTxt = follow;
  65. tbd.hastags = htags;
  66. tbd.avatarImage = ava;
  67. if (tweetBuffer == null)
  68. {
  69. tweetBuffer = new List<TweetBufferData>();
  70. }
  71. tweetBuffer.Add(tbd);
  72. }
  73. private void ReadFirstInBuffer()
  74. {
  75. if (tweetBuffer != null)
  76. {
  77. if (tweetBuffer.Count > 0)
  78. {
  79. TweetBufferData tbd = tweetBuffer[0];
  80. SenderAvatar.sprite = tbd.avatarImage;
  81. SenderName.text = tbd.senderName;
  82. UserName.text = tbd.userName;
  83. HashTags.text = tbd.hastags;
  84. TweetText.text = tbd.messageText;
  85. NumberOfFollowers.text = tbd.followersTxt;
  86. NumberOfFollowers.transform.parent.gameObject.SetActive(tbd.followersTxt.Length>0);
  87. tweetBuffer.RemoveAt(0);
  88. }
  89. else
  90. {
  91. Debug.LogWarning("Tweet buffer empty when reading");
  92. }
  93. }
  94. }
  95. public string ChangeTweet(string _text)
  96. {
  97. string text = _text;
  98. if (_text.Contains("1 hours"))
  99. text = _text.Replace("1 hours", "1 hour");
  100. text = ChangeStrings.ChangeText(text);
  101. return text;
  102. }
  103. public void TurnTweetOff()
  104. {
  105. gameObject.SetActive(false);
  106. }
  107. public void PauseAnimation()
  108. {
  109. if (animator != null)
  110. {
  111. animator.enabled = false;
  112. StartCoroutine(ResumeAnimAfterDelay(8.0f));
  113. }
  114. }
  115. public void ResumeAnimation()
  116. {
  117. if (animator != null)
  118. animator.enabled = true;
  119. }
  120. private bool PhoneIsntActive()
  121. {
  122. bool result = !WebTools.Phone.PanelPhoneBehaviour.Instance.gameObject.activeSelf;
  123. return result;
  124. }
  125. IEnumerator ResumeAnimAfterDelay(float delay)
  126. {
  127. yield return new WaitUntil(PhoneIsntActive);
  128. yield return new WaitForSeconds(delay);
  129. if (animator != null)
  130. {
  131. if(!animator.enabled)
  132. {
  133. animator.enabled = true;
  134. }
  135. }
  136. }
  137. }