EventManager.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using System;
  2. using System.Collections.Generic;
  3. using TapSDK.UI;
  4. namespace TapSDK.Core.Internal.Utils
  5. {
  6. public sealed class EventManager : Singleton<EventManager>
  7. {
  8. public const string OnApplicationPause = "OnApplicationPause";
  9. public const string OnApplicationQuit = "OnApplicationQuit";
  10. public const string OnComplianceUserChanged = "OnComplianceUserChanged";
  11. public const string OnTapUserChanged = "OnTapUserChanged";
  12. public const string IsLaunchedFromTapTapPCFinished = "IsLaunchedFromTapTapPCFinished";
  13. private Dictionary<string, Action<object>> eventRegistries = new Dictionary<string, Action<object>>();
  14. public static void AddListener(string eventName, Action<object> listener) {
  15. if (listener == null) return;
  16. if (string.IsNullOrEmpty(eventName)) return;
  17. Action<object> thisEvent;
  18. if (Instance.eventRegistries.TryGetValue(eventName, out thisEvent)) {
  19. thisEvent += listener;
  20. Instance.eventRegistries[eventName] = thisEvent;
  21. } else {
  22. thisEvent += listener;
  23. Instance.eventRegistries.Add(eventName, thisEvent);
  24. }
  25. }
  26. public static void RemoveListener(string eventName, Action<object> listener) {
  27. if (listener == null) return;
  28. if (string.IsNullOrEmpty(eventName)) return;
  29. Action<object> thisEvent;
  30. if (Instance.eventRegistries.TryGetValue(eventName, out thisEvent)) {
  31. thisEvent -= listener;
  32. Instance.eventRegistries[eventName] = thisEvent;
  33. }
  34. }
  35. public static void TriggerEvent(string eventName, object message) {
  36. Action<object> thisEvent = null;
  37. if (Instance.eventRegistries.TryGetValue(eventName, out thisEvent)) {
  38. thisEvent.Invoke(message);
  39. }
  40. }
  41. }
  42. }