diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 98ddc1bf7..9a0e9194a 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -17,16 +17,15 @@ - + - @@ -46,8 +45,8 @@ - - + + @@ -58,8 +57,8 @@ - - + + @@ -72,8 +71,8 @@ - - + + @@ -84,8 +83,8 @@ - - + + @@ -173,9 +172,9 @@ @@ -383,16 +382,16 @@ - + - + - + - + @@ -658,46 +657,46 @@ - + - - + + - + - - + + - - - + - + - - + + + + + + + - + - - - - - + + - - + + diff --git a/CHANGELOG.md b/CHANGELOG.md index 3cb4157bc..18e333d4b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ ## 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 diff --git a/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/FlutterWebView.java b/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/FlutterWebView.java index 5a98ce236..c82b1c234 100644 --- a/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/FlutterWebView.java +++ b/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/FlutterWebView.java @@ -162,6 +162,14 @@ public void onMethodCall(MethodCall call, Result result) { 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(); diff --git a/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppBrowserActivity.java b/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppBrowserActivity.java index 9b33e915a..7feeb4505 100644 --- a/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppBrowserActivity.java +++ b/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppBrowserActivity.java @@ -238,17 +238,32 @@ public void goBack() { 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 boolean canGoForward() { - return webView.canGoForward(); + 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() { diff --git a/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppBrowserFlutterPlugin.java b/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppBrowserFlutterPlugin.java index c70e5f4bf..d224f2077 100644 --- a/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppBrowserFlutterPlugin.java +++ b/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppBrowserFlutterPlugin.java @@ -233,6 +233,13 @@ public void run() { 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 boolean canGoForward(String uuid) { 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); diff --git a/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppWebView/InAppWebView.java b/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppWebView/InAppWebView.java index 5b36463cb..1854de09b 100644 --- a/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppWebView/InAppWebView.java +++ b/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppWebView/InAppWebView.java @@ -97,6 +97,12 @@ else if (obj instanceof FlutterWebView) this.options = options; } + @Override + public void reload() { + super.reload(); + Log.d(LOG_TAG, "RELOAD"); + } + public void prepare() { boolean isFromInAppBrowserActivity = inAppBrowserActivity != null; diff --git a/example/lib/main.dart b/example/lib/main.dart index 90813e3e1..cb69bfbef 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -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()); diff --git a/ios/Classes/InAppBrowserWebViewController.swift b/ios/Classes/InAppBrowserWebViewController.swift index 45952fce7..1e9804dd1 100644 --- a/ios/Classes/InAppBrowserWebViewController.swift +++ b/ios/Classes/InAppBrowserWebViewController.swift @@ -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 } diff --git a/ios/Classes/InAppWebView.swift b/ios/Classes/InAppWebView.swift index 4f533130e..1dede39db 100644 --- a/ios/Classes/InAppWebView.swift +++ b/ios/Classes/InAppWebView.swift @@ -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 + } + } diff --git a/ios/Classes/SwiftFlutterPlugin.swift b/ios/Classes/SwiftFlutterPlugin.swift index 3e6c52238..063f98d34 100644 --- a/ios/Classes/SwiftFlutterPlugin.swift +++ b/ios/Classes/SwiftFlutterPlugin.swift @@ -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) diff --git a/lib/flutter_inappbrowser.dart b/lib/flutter_inappbrowser.dart index 464adcd54..eed213222 100644 --- a/lib/flutter_inappbrowser.dart +++ b/lib/flutter_inappbrowser.dart @@ -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 goBackOrForward(int steps) async { + assert(steps != null); + + Map args = {}; + 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 canGoBackOrForward(int steps) async { + assert(steps != null); + + Map args = {}; + 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 goTo(WebHistoryItem historyItem) async { + await goBackOrForward(historyItem.offset); + } + ///Check if the Web View of the [InAppWebView] instance is in a loading state. Future isLoading() async { Map args = {}; @@ -1050,8 +1081,9 @@ class InAppWebViewController { int currentIndex = result["currentIndex"]; List historyList = List(); - for(LinkedHashMap historyItem in historyListMap) { - historyList.add(WebHistoryItem(historyItem["originalUrl"], historyItem["title"], historyItem["url"])); + for(var i = 0; i < historyListMap.length; i++) { + LinkedHashMap 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 _list; + ///List of all [WebHistoryItem]s. List 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.