Cookies.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using BestHTTP.Core;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. namespace BestHTTP.Examples.Helpers.Components
  8. {
  9. public class Cookies : MonoBehaviour
  10. {
  11. #pragma warning disable 0649, 0169
  12. [SerializeField]
  13. private Text _count;
  14. [SerializeField]
  15. private Text _size;
  16. [SerializeField]
  17. private Button _clear;
  18. #pragma warning restore
  19. private void Start()
  20. {
  21. PluginEventHelper.OnEvent += OnPluginEvent;
  22. UpdateLabels();
  23. }
  24. private void OnDestroy()
  25. {
  26. PluginEventHelper.OnEvent -= OnPluginEvent;
  27. }
  28. private void OnPluginEvent(PluginEventInfo @event)
  29. {
  30. #if !BESTHTTP_DISABLE_COOKIES
  31. if (@event.Event == PluginEvents.SaveCookieLibrary)
  32. UpdateLabels();
  33. #endif
  34. }
  35. private void UpdateLabels()
  36. {
  37. #if !BESTHTTP_DISABLE_COOKIES
  38. var cookies = BestHTTP.Cookies.CookieJar.GetAll();
  39. var size = cookies.Sum(c => c.GuessSize());
  40. this._count.text = cookies.Count.ToString("N0");
  41. this._size.text = size.ToString("N0");
  42. #else
  43. this._count.text = "0";
  44. this._size.text = "0";
  45. #endif
  46. }
  47. public void OnClearButtonClicked()
  48. {
  49. #if !BESTHTTP_DISABLE_COOKIES
  50. BestHTTP.Cookies.CookieJar.Clear();
  51. #endif
  52. }
  53. }
  54. }