FileItem.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using LitJson;
  5. using UnityEngine;
  6. using UnityEngine.EventSystems;
  7. using UnityEngine.UI;
  8. public class FileItem : MonoBehaviour
  9. {
  10. public Toggle mainToggle;
  11. public Text nameText;
  12. public Image iconImage;
  13. private FileConfig fileConfig;
  14. public Sprite picIcon;
  15. public Sprite mp4Icon;
  16. public Sprite pdfIcon;
  17. public Sprite zipIcon;
  18. public Sprite unkownIcon;
  19. void Start()
  20. {
  21. mainToggle.onValueChanged.AddListener(ClickOnMainToggle);
  22. RoomFile.ClickOnPrefabAction += ClickOnPrefab;
  23. RoomFile.HidefileChooseAction += HideFileChoose;
  24. WSHandler.Rtc.OnDeleteFileSucess += DeleteFileSucess;
  25. WSHandler.Rtc.OnDeleteFile += NotifyDeleteFile;
  26. }
  27. private void HideFileChoose(FileConfig fileConfig)
  28. {
  29. if (this.fileConfig != null)
  30. {
  31. if (this.fileConfig == fileConfig)
  32. {
  33. mainToggle.isOn = false;
  34. }
  35. }
  36. }
  37. private void ClickOnPrefab(FileConfig fileConfig)
  38. {
  39. if (this.fileConfig != null)
  40. {
  41. if (this.fileConfig == fileConfig)
  42. {
  43. mainToggle.isOn = true;
  44. }
  45. else
  46. {
  47. mainToggle.isOn = false;
  48. }
  49. }
  50. }
  51. private void OnDestroy()
  52. {
  53. RoomFile.ClickOnPrefabAction -= ClickOnPrefab;
  54. RoomFile.HidefileChooseAction -= HideFileChoose;
  55. WSHandler.Rtc.OnDeleteFileSucess -= DeleteFileSucess;
  56. WSHandler.Rtc.OnDeleteFile -= NotifyDeleteFile;
  57. }
  58. private void DeleteFileSucess(JsonData data)
  59. {
  60. if (data["data"]["code"].ToString() == "200")
  61. {
  62. string path = data["data"]["data"]["path"].ToString();
  63. if (!string.IsNullOrEmpty(path))
  64. {
  65. if (this.fileConfig != null && this.fileConfig.Path == path)
  66. {
  67. Destroy(this.gameObject);
  68. if (FileList.Instance)
  69. {
  70. FileList.Instance.RemoveItem(this.fileConfig);
  71. }
  72. RoomFile.Instance.DeleteSucess(this.fileConfig);
  73. }
  74. }
  75. }
  76. }
  77. private void NotifyDeleteFile(JsonData data)
  78. {
  79. if (this.fileConfig != null)
  80. {
  81. if (data["data"]["path"].ToString() == this.fileConfig.Path)
  82. {
  83. Destroy(this.gameObject);
  84. if (FileList.Instance)
  85. {
  86. FileList.Instance.RemoveItem(this.fileConfig);
  87. }
  88. }
  89. }
  90. }
  91. private void ClickOnMainToggle(bool isOn)
  92. {
  93. if (isOn)
  94. {
  95. if (fileConfig != null)
  96. {
  97. RoomFile.ClickFileItemAction?.Invoke(fileConfig);
  98. }
  99. }
  100. else
  101. {
  102. if (fileConfig != null)
  103. {
  104. RoomFile.HideChooseKuangAction?.Invoke(fileConfig);
  105. }
  106. }
  107. }
  108. public void Init(FileConfig fileConfig)
  109. {
  110. mainToggle.group = transform.parent.GetComponent<ToggleGroup>();
  111. if (fileConfig != null)
  112. {
  113. this.fileConfig = fileConfig;
  114. if (fileConfig.FileName.Length <= 8)
  115. {
  116. nameText.text = fileConfig.FileName;
  117. }
  118. else
  119. {
  120. string str1 = fileConfig.FileName.Substring(0, 8);
  121. nameText.text = str1+"...";
  122. }
  123. switch (fileConfig.FileType)
  124. {
  125. case RoomFileType.png:
  126. iconImage.sprite = picIcon;
  127. break;
  128. case RoomFileType.jpg:
  129. iconImage.sprite = picIcon;
  130. break;
  131. case RoomFileType.jpeg:
  132. iconImage.sprite = picIcon;
  133. break;
  134. case RoomFileType.mp4:
  135. iconImage.sprite = mp4Icon;
  136. break;
  137. case RoomFileType.zip:
  138. iconImage.sprite = zipIcon;
  139. break;
  140. case RoomFileType.pdf:
  141. iconImage.sprite = pdfIcon;
  142. break;
  143. case RoomFileType.unknown:
  144. iconImage.sprite = unkownIcon;
  145. break;
  146. }
  147. }
  148. }
  149. }