BugReportApiService.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System;
  2. using SRDebugger.Internal;
  3. using SRF;
  4. using SRF.Service;
  5. using UnityEngine;
  6. namespace SRDebugger.Services.Implementation
  7. {
  8. [Service(typeof(IBugReportService))]
  9. public class BugReportApiService : SRServiceBase<IBugReportService>, IBugReportService
  10. {
  11. public void SendBugReport(BugReport report, BugReportCompleteCallback completeHandler, BugReportProgressCallback progressCallback = null)
  12. {
  13. if (report == null)
  14. {
  15. throw new ArgumentNullException("report");
  16. }
  17. if (completeHandler == null)
  18. {
  19. throw new ArgumentNullException("completeHandler");
  20. }
  21. if (this._isBusy)
  22. {
  23. completeHandler(false, "BugReportApiService is already sending a bug report");
  24. return;
  25. }
  26. if (Application.internetReachability == NetworkReachability.NotReachable)
  27. {
  28. completeHandler(false, "No Internet Connection");
  29. return;
  30. }
  31. this._errorMessage = string.Empty;
  32. base.enabled = true;
  33. this._isBusy = true;
  34. this._completeCallback = completeHandler;
  35. this._progressCallback = progressCallback;
  36. this._reportApi = new BugReportApi(report, Settings.Instance.ApiKey);
  37. base.StartCoroutine(this._reportApi.Submit());
  38. }
  39. protected override void Awake()
  40. {
  41. base.Awake();
  42. base.CachedTransform.SetParent(Hierarchy.Get("SRDebugger"));
  43. }
  44. private void OnProgress(float progress)
  45. {
  46. if (this._progressCallback != null)
  47. {
  48. this._progressCallback(progress);
  49. }
  50. }
  51. private void OnComplete()
  52. {
  53. this._isBusy = false;
  54. base.enabled = false;
  55. this._completeCallback(this._reportApi.WasSuccessful, (!string.IsNullOrEmpty(this._reportApi.ErrorMessage)) ? this._reportApi.ErrorMessage : this._errorMessage);
  56. this._completeCallback = null;
  57. }
  58. protected override void Update()
  59. {
  60. base.Update();
  61. if (!this._isBusy)
  62. {
  63. return;
  64. }
  65. if (this._reportApi == null)
  66. {
  67. this._isBusy = false;
  68. }
  69. if (this._reportApi.IsComplete)
  70. {
  71. this.OnComplete();
  72. return;
  73. }
  74. if (this._previousProgress != this._reportApi.Progress)
  75. {
  76. this.OnProgress(this._reportApi.Progress);
  77. this._previousProgress = this._reportApi.Progress;
  78. }
  79. }
  80. public const float Timeout = 12f;
  81. private BugReportCompleteCallback _completeCallback;
  82. private string _errorMessage;
  83. private bool _isBusy;
  84. private float _previousProgress;
  85. private BugReportProgressCallback _progressCallback;
  86. private BugReportApi _reportApi;
  87. }
  88. }