UniWebViewBackForwardList.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System.Collections.Generic;
  2. using UniWebViewExternal;
  3. /// <summary>
  4. /// Represents the back-forward navigation history list of a UniWebView instance.
  5. /// This class manages the browsing history and provides access to previous and next pages.
  6. ///
  7. /// You do not create an instance of this class directly. Instead, you get an instance of this class to represent the
  8. /// back-forward list of a UniWebView instance by calling the `CopyBackForwardList` method of the `UniWebView` class.
  9. ///
  10. /// The content of this class is read-only and fixed when the instance is created. It does not get updated with the
  11. /// web view's navigation history automatically. If you need the latest navigation history, you should call the method
  12. /// `CopyBackForwardList` again to get a new instance of this class.
  13. /// </summary>
  14. public class UniWebViewBackForwardList {
  15. private readonly List<UniWebViewBackForwardItem> items;
  16. private readonly int currentIndex;
  17. public UniWebViewBackForwardList(string json) {
  18. var dict = Json.Deserialize(json) as Dictionary<string, object>;
  19. if (dict.TryGetValue("currentIndex", out object currentIndexObj) &&
  20. int.TryParse(currentIndexObj.ToString(), out int parsedIndex))
  21. {
  22. currentIndex = parsedIndex;
  23. }
  24. items = new List<UniWebViewBackForwardItem>();
  25. if (!(dict["items"] is List<object>)) {
  26. UniWebViewLogger.Instance.Critical("Invalid items in back-forward list: " + dict["items"]);
  27. return;
  28. }
  29. var itemsArray = (List<object>)dict["items"];
  30. foreach (var item in itemsArray) {
  31. if (item is Dictionary<string, object> itemDict) {
  32. items.Add(new UniWebViewBackForwardItem(itemDict));
  33. } else {
  34. UniWebViewLogger.Instance.Critical("Invalid item in back-forward list: " + item);
  35. }
  36. }
  37. }
  38. /// <summary>
  39. /// Gets all items in the back-forward navigation history list.
  40. /// </summary>
  41. public List<UniWebViewBackForwardItem> AllItems => items;
  42. /// <summary>
  43. /// Gets the current page item in the navigation history.
  44. ///
  45. /// It is the page that is currently displayed in the list. If there is no item in the list, it will return null.
  46. /// </summary>
  47. public UniWebViewBackForwardItem CurrentItem => ItemAtIndex(currentIndex);
  48. /// <summary>
  49. /// Gets the previous (back) page item in the navigation history.
  50. ///
  51. /// Returns null if there is no previous page.
  52. /// </summary>
  53. public UniWebViewBackForwardItem BackItem => ItemAtIndex(currentIndex - 1);
  54. /// <summary>
  55. /// Gets the next (forward) page item in the navigation history.
  56. ///
  57. /// Returns null if there is no next page.
  58. /// </summary>
  59. public UniWebViewBackForwardItem ForwardItem => ItemAtIndex(currentIndex + 1);
  60. /// <summary>
  61. /// Gets the index of current page in the navigation history.
  62. ///
  63. /// The index is zero-based in the list. If there is no item in the list, it will return -1.
  64. /// </summary>
  65. public int CurrentIndex => currentIndex;
  66. /// <summary>
  67. /// Gets the item at the specified index in the navigation history.
  68. /// </summary>
  69. /// <param name="index">The zero-based index of the item to retrieve.</param>
  70. /// <returns>The navigation item at the specified index, or null if the index is out of range.</returns>
  71. public UniWebViewBackForwardItem ItemAtIndex(int index) {
  72. if (index >= 0 && index < items.Count) {
  73. return items[index];
  74. }
  75. return null;
  76. }
  77. /// <summary>
  78. /// Gets the total number of items in the navigation history.
  79. /// </summary>
  80. public int Size => items.Count;
  81. }