BugReportSheetController.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using System;
  2. using System.Collections;
  3. using System.Linq;
  4. using SRDebugger.Internal;
  5. using SRDebugger.Services;
  6. using SRF;
  7. using SRF.Service;
  8. using UnityEngine;
  9. using UnityEngine.EventSystems;
  10. using UnityEngine.UI;
  11. namespace SRDebugger.UI.Other
  12. {
  13. public class BugReportSheetController : SRMonoBehaviourEx
  14. {
  15. public bool IsCancelButtonEnabled
  16. {
  17. get
  18. {
  19. return this.CancelButton.gameObject.activeSelf;
  20. }
  21. set
  22. {
  23. this.CancelButton.gameObject.SetActive(value);
  24. }
  25. }
  26. protected override void Start()
  27. {
  28. base.Start();
  29. this.SetLoadingSpinnerVisible(false);
  30. this.ClearErrorMessage();
  31. this.ClearForm();
  32. }
  33. public void Submit()
  34. {
  35. EventSystem.current.SetSelectedGameObject(null);
  36. this.ProgressBar.value = 0f;
  37. this.ClearErrorMessage();
  38. this.SetLoadingSpinnerVisible(true);
  39. this.SetFormEnabled(false);
  40. if (!string.IsNullOrEmpty(this.EmailField.text))
  41. {
  42. this.SetDefaultEmailFieldContents(this.EmailField.text);
  43. }
  44. base.StartCoroutine(this.SubmitCo());
  45. }
  46. public void Cancel()
  47. {
  48. if (this.CancelPressed != null)
  49. {
  50. this.CancelPressed();
  51. }
  52. }
  53. private IEnumerator SubmitCo()
  54. {
  55. if (BugReportScreenshotUtil.ScreenshotData == null)
  56. {
  57. if (this.TakingScreenshot != null)
  58. {
  59. this.TakingScreenshot();
  60. }
  61. yield return new WaitForEndOfFrame();
  62. yield return base.StartCoroutine(BugReportScreenshotUtil.ScreenshotCaptureCo());
  63. if (this.ScreenshotComplete != null)
  64. {
  65. this.ScreenshotComplete();
  66. }
  67. }
  68. IBugReportService s = SRServiceManager.GetService<IBugReportService>();
  69. BugReport r = new BugReport();
  70. r.Email = this.EmailField.text;
  71. r.UserDescription = this.DescriptionField.text;
  72. r.ConsoleLog = Service.Console.AllEntries.ToList<ConsoleEntry>();
  73. r.SystemInformation = SRServiceManager.GetService<ISystemInformationService>().CreateReport(false);
  74. r.ScreenshotData = BugReportScreenshotUtil.ScreenshotData;
  75. BugReportScreenshotUtil.ScreenshotData = null;
  76. s.SendBugReport(r, new BugReportCompleteCallback(this.OnBugReportComplete), new BugReportProgressCallback(this.OnBugReportProgress));
  77. yield break;
  78. }
  79. private void OnBugReportProgress(float progress)
  80. {
  81. this.ProgressBar.value = progress;
  82. }
  83. private void OnBugReportComplete(bool didSucceed, string errorMessage)
  84. {
  85. if (!didSucceed)
  86. {
  87. this.ShowErrorMessage("Error sending bug report", errorMessage);
  88. }
  89. else
  90. {
  91. this.ClearForm();
  92. this.ShowErrorMessage("Bug report submitted successfully", null);
  93. }
  94. this.SetLoadingSpinnerVisible(false);
  95. this.SetFormEnabled(true);
  96. if (this.SubmitComplete != null)
  97. {
  98. this.SubmitComplete(didSucceed, errorMessage);
  99. }
  100. }
  101. protected void SetLoadingSpinnerVisible(bool visible)
  102. {
  103. this.ProgressBar.gameObject.SetActive(visible);
  104. this.ButtonContainer.SetActive(!visible);
  105. }
  106. protected void ClearForm()
  107. {
  108. this.EmailField.text = this.GetDefaultEmailFieldContents();
  109. this.DescriptionField.text = string.Empty;
  110. }
  111. protected void ShowErrorMessage(string userMessage, string serverMessage = null)
  112. {
  113. string text = userMessage;
  114. if (!string.IsNullOrEmpty(serverMessage))
  115. {
  116. text += " (<b>{0}</b>)".Fmt(new object[]
  117. {
  118. serverMessage
  119. });
  120. }
  121. this.ResultMessageText.text = text;
  122. this.ResultMessageText.gameObject.SetActive(true);
  123. }
  124. protected void ClearErrorMessage()
  125. {
  126. this.ResultMessageText.text = string.Empty;
  127. this.ResultMessageText.gameObject.SetActive(false);
  128. }
  129. protected void SetFormEnabled(bool e)
  130. {
  131. this.SubmitButton.interactable = e;
  132. this.CancelButton.interactable = e;
  133. this.EmailField.interactable = e;
  134. this.DescriptionField.interactable = e;
  135. }
  136. private string GetDefaultEmailFieldContents()
  137. {
  138. return PlayerPrefs.GetString("SRDEBUGGER_BUG_REPORT_LAST_EMAIL", string.Empty);
  139. }
  140. private void SetDefaultEmailFieldContents(string value)
  141. {
  142. PlayerPrefs.SetString("SRDEBUGGER_BUG_REPORT_LAST_EMAIL", value);
  143. PlayerPrefs.Save();
  144. }
  145. [RequiredField]
  146. public GameObject ButtonContainer;
  147. [RequiredField]
  148. public Text ButtonText;
  149. [RequiredField]
  150. public Button CancelButton;
  151. public Action CancelPressed;
  152. [RequiredField]
  153. public InputField DescriptionField;
  154. [RequiredField]
  155. public InputField EmailField;
  156. [RequiredField]
  157. public Slider ProgressBar;
  158. [RequiredField]
  159. public Text ResultMessageText;
  160. public Action ScreenshotComplete;
  161. [RequiredField]
  162. public Button SubmitButton;
  163. public Action<bool, string> SubmitComplete;
  164. public Action TakingScreenshot;
  165. }
  166. }