PinEntryServiceImpl.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. namespace SRDebugger.Services.Implementation
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using Internal;
  7. using SRF;
  8. using SRF.Service;
  9. using UI.Controls;
  10. using UnityEngine;
  11. [Service(typeof (IPinEntryService))]
  12. public class PinEntryServiceImpl : SRServiceBase<IPinEntryService>, IPinEntryService
  13. {
  14. private PinEntryCompleteCallback _callback;
  15. private bool _isVisible;
  16. private PinEntryControl _pinControl;
  17. private readonly List<int> _requiredPin = new List<int>(4);
  18. public bool IsShowingKeypad
  19. {
  20. get { return _isVisible; }
  21. }
  22. public void ShowPinEntry(IReadOnlyList<int> requiredPin, string message, PinEntryCompleteCallback callback,
  23. bool allowCancel = true)
  24. {
  25. if (_isVisible)
  26. {
  27. throw new InvalidOperationException("Pin entry is already in progress");
  28. }
  29. VerifyPin(requiredPin);
  30. if (_pinControl == null)
  31. {
  32. Load();
  33. }
  34. if (_pinControl == null)
  35. {
  36. Debug.LogWarning("[PinEntry] Pin entry failed loading, executing callback with fail result");
  37. callback(false);
  38. return;
  39. }
  40. _pinControl.Clear();
  41. _pinControl.PromptText.text = message;
  42. _pinControl.CanCancel = allowCancel;
  43. _callback = callback;
  44. _requiredPin.Clear();
  45. _requiredPin.AddRange(requiredPin);
  46. _pinControl.Show();
  47. _isVisible = true;
  48. SRDebuggerUtil.EnsureEventSystemExists();
  49. }
  50. protected override void Awake()
  51. {
  52. base.Awake();
  53. CachedTransform.SetParent(Hierarchy.Get("SRDebugger"));
  54. }
  55. private void Load()
  56. {
  57. var prefab = Resources.Load<PinEntryControl>(SRDebugPaths.PinEntryPrefabPath);
  58. if (prefab == null)
  59. {
  60. Debug.LogError("[PinEntry] Unable to load pin entry prefab");
  61. return;
  62. }
  63. _pinControl = SRInstantiate.Instantiate(prefab);
  64. _pinControl.CachedTransform.SetParent(CachedTransform, false);
  65. _pinControl.Hide();
  66. _pinControl.Complete += PinControlOnComplete;
  67. }
  68. private void PinControlOnComplete(IList<int> result, bool didCancel)
  69. {
  70. var isValid = _requiredPin.SequenceEqual(result);
  71. if (!didCancel && !isValid)
  72. {
  73. _pinControl.Clear();
  74. _pinControl.PlayInvalidCodeAnimation();
  75. return;
  76. }
  77. _isVisible = false;
  78. _pinControl.Hide();
  79. if (didCancel)
  80. {
  81. _callback(false);
  82. return;
  83. }
  84. _callback(isValid);
  85. }
  86. private void VerifyPin(IReadOnlyList<int> pin)
  87. {
  88. if (pin.Count != 4)
  89. {
  90. throw new ArgumentException("Pin list must have 4 elements");
  91. }
  92. for (var i = 0; i < pin.Count; i++)
  93. {
  94. if (pin[i] < 0 || pin[i] > 9)
  95. {
  96. throw new ArgumentException("Pin numbers must be >= 0 && <= 9");
  97. }
  98. }
  99. }
  100. }
  101. }