using System.Collections.Generic; using UniWebViewExternal; /// /// Represents the back-forward navigation history list of a UniWebView instance. /// This class manages the browsing history and provides access to previous and next pages. /// /// You do not create an instance of this class directly. Instead, you get an instance of this class to represent the /// back-forward list of a UniWebView instance by calling the `CopyBackForwardList` method of the `UniWebView` class. /// /// The content of this class is read-only and fixed when the instance is created. It does not get updated with the /// web view's navigation history automatically. If you need the latest navigation history, you should call the method /// `CopyBackForwardList` again to get a new instance of this class. /// public class UniWebViewBackForwardList { private readonly List items; private readonly int currentIndex; public UniWebViewBackForwardList(string json) { var dict = Json.Deserialize(json) as Dictionary; if (dict.TryGetValue("currentIndex", out object currentIndexObj) && int.TryParse(currentIndexObj.ToString(), out int parsedIndex)) { currentIndex = parsedIndex; } items = new List(); if (!(dict["items"] is List)) { UniWebViewLogger.Instance.Critical("Invalid items in back-forward list: " + dict["items"]); return; } var itemsArray = (List)dict["items"]; foreach (var item in itemsArray) { if (item is Dictionary itemDict) { items.Add(new UniWebViewBackForwardItem(itemDict)); } else { UniWebViewLogger.Instance.Critical("Invalid item in back-forward list: " + item); } } } /// /// Gets all items in the back-forward navigation history list. /// public List AllItems => items; /// /// Gets the current page item in the navigation history. /// /// It is the page that is currently displayed in the list. If there is no item in the list, it will return null. /// public UniWebViewBackForwardItem CurrentItem => ItemAtIndex(currentIndex); /// /// Gets the previous (back) page item in the navigation history. /// /// Returns null if there is no previous page. /// public UniWebViewBackForwardItem BackItem => ItemAtIndex(currentIndex - 1); /// /// Gets the next (forward) page item in the navigation history. /// /// Returns null if there is no next page. /// public UniWebViewBackForwardItem ForwardItem => ItemAtIndex(currentIndex + 1); /// /// Gets the index of current page in the navigation history. /// /// The index is zero-based in the list. If there is no item in the list, it will return -1. /// public int CurrentIndex => currentIndex; /// /// Gets the item at the specified index in the navigation history. /// /// The zero-based index of the item to retrieve. /// The navigation item at the specified index, or null if the index is out of range. public UniWebViewBackForwardItem ItemAtIndex(int index) { if (index >= 0 && index < items.Count) { return items[index]; } return null; } /// /// Gets the total number of items in the navigation history. /// public int Size => items.Count; }