Commit 305c7262 authored by pichillilorenzo's avatar pichillilorenzo

added WebHistory and WebHistoryItem class, added getCopyBackForwardList,...

added WebHistory and WebHistoryItem class, added getCopyBackForwardList, goBackOrForward, canGoBackOrForward and goTo methods for InAppWebView and InAppBrowser, updated docs
parent ab7b1b68
This diff is collapsed.
## 0.5.4
- added `WebHistory` and `WebHistoryItem` class
- added `getCopyBackForwardList` method for `InAppWebView` and `InAppBrowser`
- added `getCopyBackForwardList`, `goBackOrForward`, `canGoBackOrForward` and `goTo` methods for `InAppWebView` and `InAppBrowser`
## 0.5.3
......
......@@ -162,6 +162,14 @@ public class FlutterWebView implements PlatformView, MethodCallHandler {
case "canGoForward":
result.success((webView != null) && webView.canGoForward());
break;
case "goBackOrForward":
if (webView != null)
webView.goBackOrForward((Integer) call.argument("steps"));
result.success(true);
break;
case "canGoBackOrForward":
result.success((webView != null) && webView.canGoBackOrForward((Integer) call.argument("steps")));
break;
case "stopLoading":
if (webView != null)
webView.stopLoading();
......
......@@ -238,17 +238,32 @@ public class InAppBrowserActivity extends AppCompatActivity {
webView.goBack();
}
public boolean canGoBack() {
if (webView != null)
return webView.canGoBack();
return false;
}
public void goForward() {
if (webView != null && canGoForward())
webView.goForward();
}
public boolean canGoBack() {
return webView.canGoBack();
}
public boolean canGoForward() {
if (webView != null)
return webView.canGoForward();
return false;
}
public void goBackOrForward(int steps) {
if (webView != null && canGoBackOrForward(steps))
webView.goBackOrForward(steps);
}
public boolean canGoBackOrForward(int steps) {
if (webView != null)
return webView.canGoBackOrForward(steps);
return false;
}
public void hide() {
......
......@@ -233,6 +233,13 @@ public class InAppBrowserFlutterPlugin implements MethodCallHandler {
case "canGoForward":
result.success(canGoForward(uuid));
break;
case "goBackOrForward":
goBackOrForward(uuid, (Integer) call.argument("steps"));
result.success(true);
break;
case "canGoBackOrForward":
result.success(canGoBackOrForward(uuid, (Integer) call.argument("steps")));
break;
case "stopLoading":
stopLoading(uuid);
result.success(true);
......@@ -497,6 +504,18 @@ public class InAppBrowserFlutterPlugin implements MethodCallHandler {
return false;
}
public void goBackOrForward(String uuid, int steps) {
InAppBrowserActivity inAppBrowserActivity = webViewActivities.get(uuid);
if (inAppBrowserActivity != null)
inAppBrowserActivity.goBackOrForward(steps);
}
public boolean canGoBackOrForward(String uuid, int steps) {
InAppBrowserActivity inAppBrowserActivity = webViewActivities.get(uuid);
if (inAppBrowserActivity != null)
return inAppBrowserActivity.canGoBackOrForward(steps);
return false;
}
public static void close(final String uuid, final Result result) {
final InAppBrowserActivity inAppBrowserActivity = webViewActivities.get(uuid);
......
......@@ -97,6 +97,12 @@ public class InAppWebView extends WebView {
this.options = options;
}
@Override
public void reload() {
super.reload();
Log.d(LOG_TAG, "RELOAD");
}
public void prepare() {
boolean isFromInAppBrowserActivity = inAppBrowserActivity != null;
......
......@@ -16,12 +16,21 @@ class MyInAppBrowser extends InAppBrowser {
Future onLoadStop(String url) async {
print("\n\nStopped $url\n\n");
WebHistory history = await this.webViewController.getCopyBackForwardList();
print(history.list.length);
print(history.list[history.currentIndex].url);
for(WebHistoryItem item in history.list) {
print(item.title);
}
// WebHistory history = await this.webViewController.getCopyBackForwardList();
// print(history.list.length);
// print(history.currentIndex);
// print(history.list[history.currentIndex].url);
// for(WebHistoryItem item in history.list) {
// print(item.title);
// }
//
// print(await this.webViewController.canGoBackOrForward(1));
// if (await this.webViewController.canGoBackOrForward(-2)) {
// this.webViewController.goTo(history.list[0]);
// }
// await this.webViewController.goBackOrForward(-1);
// print(await this.webViewController.canGoBack());
// print(await this.webViewController.canGoForward());
......
......@@ -481,10 +481,6 @@ class InAppBrowserWebViewController: UIViewController, WKUIDelegate, WKNavigatio
})
}
func canGoBack() -> Bool {
return webView.canGoBack
}
@objc func goBack() {
if canGoBack() {
webView.goBack()
......@@ -492,8 +488,8 @@ class InAppBrowserWebViewController: UIViewController, WKUIDelegate, WKNavigatio
}
}
func canGoForward() -> Bool {
return webView.canGoForward
func canGoBack() -> Bool {
return webView.canGoBack
}
@objc func goForward() {
......@@ -503,6 +499,19 @@ class InAppBrowserWebViewController: UIViewController, WKUIDelegate, WKNavigatio
}
}
func canGoForward() -> Bool {
return webView.canGoForward
}
@objc func goBackOrForward(steps: Int) {
webView.goBackOrForward(steps: steps)
updateUrlTextField(url: (webView?.url?.absoluteString)!)
}
func canGoBackOrForward(steps: Int) -> Bool {
return webView.canGoBackOrForward(steps: steps)
}
func updateUrlTextField(url: String) {
urlField.text = url
}
......
......@@ -10,4 +10,25 @@ import WebKit
public class InAppWebView: WKWebView {
public func goBackOrForward(steps: Int) {
if canGoBackOrForward(steps: steps) {
if (steps > 0) {
let index = steps - 1
go(to: self.backForwardList.forwardList[index])
}
else if (steps < 0){
let backListLength = self.backForwardList.backList.count
let index = backListLength + steps
go(to: self.backForwardList.backList[index])
}
}
}
public func canGoBackOrForward(steps: Int) -> Bool {
let currentIndex = self.backForwardList.backList.count
return (steps >= 0)
? steps <= self.backForwardList.forwardList.count
: currentIndex + steps >= 0
}
}
......@@ -129,6 +129,22 @@ public class SwiftFlutterPlugin: NSObject, FlutterPlugin {
result(false)
}
break
case "goBackOrForward":
if let webViewController = self.webViewControllers[uuid] {
let steps = arguments!["steps"] as! Int
webViewController?.goBackOrForward(steps: steps)
}
result(true)
break
case "canGoBackOrForward":
if let webViewController = self.webViewControllers[uuid] {
let steps = arguments!["steps"] as! Int
result(webViewController?.canGoBackOrForward(steps: steps) ?? false)
}
else {
result(false)
}
break
case "isLoading":
if let webViewController = self.webViewControllers[uuid] {
result((webViewController?.webView.isLoading ?? false) == true)
......
......@@ -903,6 +903,37 @@ class InAppWebViewController {
return await _channel.invokeMethod('canGoForward', args);
}
///Goes to the history item that is the number of steps away from the current item. Steps is negative if backward and positive if forward.
Future<void> goBackOrForward(int steps) async {
assert(steps != null);
Map<String, dynamic> args = <String, dynamic>{};
if (_inAppBrowserUuid != null) {
_inAppBrowser._throwIsNotOpened();
args.putIfAbsent('uuid', () => _inAppBrowserUuid);
}
args.putIfAbsent('steps', () => steps);
await _channel.invokeMethod('goBackOrForward', args);
}
///Gets whether the page can go back or forward the given number of steps.
Future<bool> canGoBackOrForward(int steps) async {
assert(steps != null);
Map<String, dynamic> args = <String, dynamic>{};
if (_inAppBrowserUuid != null) {
_inAppBrowser._throwIsNotOpened();
args.putIfAbsent('uuid', () => _inAppBrowserUuid);
}
args.putIfAbsent('steps', () => steps);
return await _channel.invokeMethod('canGoBackOrForward', args);
}
///Navigates to an item from the back-forward list and sets it as the current item.
Future<void> goTo(WebHistoryItem historyItem) async {
await goBackOrForward(historyItem.offset);
}
///Check if the Web View of the [InAppWebView] instance is in a loading state.
Future<bool> isLoading() async {
Map<String, dynamic> args = <String, dynamic>{};
......@@ -1050,8 +1081,9 @@ class InAppWebViewController {
int currentIndex = result["currentIndex"];
List<WebHistoryItem> historyList = List();
for(LinkedHashMap<dynamic, dynamic> historyItem in historyListMap) {
historyList.add(WebHistoryItem(historyItem["originalUrl"], historyItem["title"], historyItem["url"]));
for(var i = 0; i < historyListMap.length; i++) {
LinkedHashMap<dynamic, dynamic> historyItem = historyListMap[i];
historyList.add(WebHistoryItem(historyItem["originalUrl"], historyItem["title"], historyItem["url"], i, i - currentIndex));
}
return WebHistory(historyList, currentIndex);
}
......@@ -1067,7 +1099,9 @@ class InAppWebViewController {
///This class contains a snapshot of the current back/forward list for a WebView.
class WebHistory {
List<WebHistoryItem> _list;
///List of all [WebHistoryItem]s.
List<WebHistoryItem> get list => _list;
///Index of the current [WebHistoryItem].
int currentIndex;
WebHistory(this._list, this.currentIndex);
......@@ -1077,11 +1111,18 @@ class WebHistory {
///
///A convenience class for accessing fields in an entry in the back/forward list of a WebView. Each WebHistoryItem is a snapshot of the requested history item.
class WebHistoryItem {
///Original url of this history item.
String originalUrl;
///Document title of this history item.
String title;
///Url of this history item.
String url;
///0-based position index in the back-forward [WebHistory.list].
int index;
///Position offset respect to the currentIndex of the back-forward [WebHistory.list].
int offset;
WebHistoryItem(this.originalUrl, this.title, this.url);
WebHistoryItem(this.originalUrl, this.title, this.url, this.index, this.offset);
}
///InAppLocalhostServer class.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment