From ab7b1b68ad0a3269370208f4a83632c73662f132 Mon Sep 17 00:00:00 2001 From: pichillilorenzo Date: Mon, 29 Oct 2018 22:02:59 +0100 Subject: [PATCH] added and class, added method for and --- .idea/workspace.xml | 97 +++++++++++-------- CHANGELOG.md | 5 + README.md | 8 +- .../flutter_inappbrowser/FlutterWebView.java | 5 +- .../InAppBrowserActivity.java | 7 ++ .../InAppBrowserFlutterPlugin.java | 10 ++ .../InAppWebView/InAppWebView.java | 31 ++++++ example/lib/main.dart | 6 ++ .../InAppBrowserWebViewController.swift | 26 +++++ ios/Classes/SwiftFlutterPlugin.swift | 10 ++ lib/flutter_inappbrowser.dart | 48 +++++++++ pubspec.yaml | 5 +- 12 files changed, 216 insertions(+), 42 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index de513aca..98ddc1bf 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -15,7 +15,18 @@ + + + + + + + + + + + @@ -35,8 +46,8 @@ - - + + @@ -47,17 +58,13 @@ - - - - - - - - - - - + + + + + + + @@ -65,8 +72,8 @@ - - + + @@ -74,6 +81,15 @@ + + + + + + + + + @@ -85,7 +101,6 @@ - _controller canGoBack getOp onLoadError @@ -115,6 +130,7 @@ WebView print throw + InAppWeb activity.getPreferences(0) @@ -157,17 +173,17 @@ - @@ -362,12 +378,12 @@ - + - + @@ -376,13 +392,13 @@ - + - + @@ -635,13 +651,6 @@ - - - - - - - @@ -651,22 +660,34 @@ - - + + - - + + + + + + + + + + + + + + - - + + @@ -675,8 +696,8 @@ - - + + diff --git a/CHANGELOG.md b/CHANGELOG.md index efc659aa..3cb4157b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.5.4 + +- added `WebHistory` and `WebHistoryItem` class +- added `getCopyBackForwardList` method for `InAppWebView` and `InAppBrowser` + ## 0.5.3 - added `CookieManager` class diff --git a/README.md b/README.md index 9fb52577..410728c8 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # Flutter InAppBrowser Plugin [![Share on Twitter](https://img.shields.io/twitter/url/http/shields.io.svg?style=social)](https://twitter.com/intent/tweet?text=Flutter%20InAppBrowser%20plugin!&url=https://github.com/pichillilorenzo/flutter_inappbrowser&hashtags=flutter,flutterio,dart,dartlang,webview) [![Share on Facebook](https://img.shields.io/badge/share-facebook-blue.svg?longCache=true&style=flat&colorB=%234267b2)](https://www.facebook.com/sharer/sharer.php?u=https%3A//github.com/pichillilorenzo/flutter_inappbrowser) [![Pub](https://img.shields.io/pub/v/flutter_inappbrowser.svg)](https://pub.dartlang.org/packages/flutter_inappbrowser) +[![Awesome Flutter](https://img.shields.io/badge/Awesome-Flutter-blue.svg?longCache=true&style=flat-square)](https://stackoverflow.com/questions/tagged/flutter?sort=votes) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](/LICENSE) [![Donate to this project using Paypal](https://img.shields.io/badge/paypal-donate-yellow.svg)](https://www.paypal.me/LorenzoPichilli) @@ -9,6 +10,10 @@ A Flutter plugin that allows you to add an inline webview or open an in-app browser window. This plugin is inspired by the popular [cordova-plugin-inappbrowser](https://github.com/apache/cordova-plugin-inappbrowser)! +### Requirements +- Dart sdk: ">=2.1.0-dev.7.1 <3.0.0" +- Flutter: ">=0.10.1 <2.0.0" + ### IMPORTANT Note for iOS To be able to use this plugin on iOS, you need to create the Flutter App with `flutter create -i swift` (see [flutter/flutter#13422 (comment)](https://github.com/flutter/flutter/issues/13422#issuecomment-392133780)), otherwise, you will get this message: ``` @@ -29,7 +34,7 @@ First, add `flutter_inappbrowser` as a [dependency in your pubspec.yaml file](ht ## Usage Classes: -- [InAppWebView](#inappwebview-class): Flutter Widget for adding an **inline native WebView** integrated in the flutter widget tree. [**Available only for Android** ([AndroidView](https://docs.flutter.io/flutter/widgets/AndroidView-class.html)) at this moment]. +- [InAppWebView](#inappwebview-class): Flutter Widget for adding an **inline native WebView** integrated in the flutter widget tree. [**Available only for Android** ([AndroidView](https://docs.flutter.io/flutter/widgets/AndroidView-class.html)) at this moment. For iOS, it will be available as soon as the Flutter team will release the corresponding dart class.]. - [InAppBrowser](#inappbrowser-class): In-App Browser using native WebView. - [ChromeSafariBrowser](#chromesafaribrowser-class): In-App Browser using [Chrome Custom Tabs](https://developer.android.com/reference/android/support/customtabs/package-summary) on Android / [SFSafariViewController](https://developer.apple.com/documentation/safariservices/sfsafariviewcontroller) on iOS. - [InAppLocalhostServer](#inapplocalhostserver-class): This class allows you to create a simple server on `http://localhost:[port]/`. The default `port` value is `8080`. @@ -42,6 +47,7 @@ Flutter Widget for adding an **inline native WebView** integrated in the flutter So, if you want use it, you can but you will have some limitation such as the inability to use the keyboard! **Available only for Android** ([AndroidView](https://docs.flutter.io/flutter/widgets/AndroidView-class.html)) at this moment. +For iOS, it will be available as soon as the Flutter team will release the corresponding dart class. Use `InAppWebViewController` to control the WebView instance. Example: 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 1dd633f3..5a98ce23 100644 --- a/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/FlutterWebView.java +++ b/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/FlutterWebView.java @@ -4,7 +4,6 @@ import android.app.Activity; import android.content.Context; import android.util.Log; import android.view.View; -import android.view.ViewGroup; import android.webkit.WebChromeClient; import android.webkit.WebView; import android.webkit.WebViewClient; @@ -186,6 +185,9 @@ public class FlutterWebView implements PlatformView, MethodCallHandler { case "getOptions": result.success((webView != null) ? webView.getOptions() : null); break; + case "getCopyBackForwardList": + result.success((webView != null) ? webView.getCopyBackForwardList() : null); + break; case "dispose": dispose(); result.success(true); @@ -201,6 +203,7 @@ public class FlutterWebView implements PlatformView, MethodCallHandler { webView.setWebChromeClient(new WebChromeClient()); webView.setWebViewClient(new WebViewClient() { public void onPageFinished(WebView view, String url) { + webView.destroy(); webView = null; } }); 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 febbabf0..9b33e915 100644 --- a/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppBrowserActivity.java +++ b/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppBrowserActivity.java @@ -381,4 +381,11 @@ public class InAppBrowserActivity extends AppCompatActivity { else result.success(""); } + + public HashMap getCopyBackForwardList() { + if (webView != null) + return webView.getCopyBackForwardList(); + return null; + } + } 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 a7e091db..c70e5f4b 100644 --- a/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppBrowserFlutterPlugin.java +++ b/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppBrowserFlutterPlugin.java @@ -265,6 +265,9 @@ public class InAppBrowserFlutterPlugin implements MethodCallHandler { case "getOptions": result.success(getOptions(uuid)); break; + case "getCopyBackForwardList": + result.success(getCopyBackForwardList(uuid)); + break; default: result.notImplemented(); } @@ -556,4 +559,11 @@ public class InAppBrowserFlutterPlugin implements MethodCallHandler { return null; } + public HashMap getCopyBackForwardList(String uuid) { + InAppBrowserActivity inAppBrowserActivity = webViewActivities.get(uuid); + if (inAppBrowserActivity != null) + return inAppBrowserActivity.getCopyBackForwardList(); + return null; + } + } 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 4b476c50..5b36463c 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 @@ -11,6 +11,8 @@ import android.util.JsonToken; import android.util.Log; import android.webkit.CookieManager; import android.webkit.ValueCallback; +import android.webkit.WebBackForwardList; +import android.webkit.WebHistoryItem; import android.webkit.WebSettings; import android.webkit.WebView; @@ -22,7 +24,9 @@ import com.pichillilorenzo.flutter_inappbrowser.Util; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.StringReader; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import io.flutter.plugin.common.MethodChannel; @@ -394,4 +398,31 @@ public class InAppWebView extends WebView { } }); } + + public HashMap getCopyBackForwardList() { + WebBackForwardList currentList = copyBackForwardList(); + int currentSize = currentList.getSize(); + int currentIndex = currentList.getCurrentIndex(); + + List> history = new ArrayList>(); + + for(int i = 0; i < currentSize; i++) { + WebHistoryItem historyItem = currentList.getItemAtIndex(i); + HashMap historyItemMap = new HashMap<>(); + + historyItemMap.put("originalUrl", historyItem.getOriginalUrl()); + historyItemMap.put("title", historyItem.getTitle()); + historyItemMap.put("url", historyItem.getUrl()); + + history.add(historyItemMap); + } + + HashMap result = new HashMap<>(); + + result.put("history", history); + result.put("currentIndex", currentIndex); + + return result; + } + } diff --git a/example/lib/main.dart b/example/lib/main.dart index 9f3c1980..90813e3e 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -16,6 +16,12 @@ 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); + } // print(await this.webViewController.canGoBack()); // print(await this.webViewController.canGoForward()); diff --git a/ios/Classes/InAppBrowserWebViewController.swift b/ios/Classes/InAppBrowserWebViewController.swift index cb1daef3..45952fce 100644 --- a/ios/Classes/InAppBrowserWebViewController.swift +++ b/ios/Classes/InAppBrowserWebViewController.swift @@ -925,4 +925,30 @@ class InAppBrowserWebViewController: UIViewController, WKUIDelegate, WKNavigatio self.navigationDelegate?.onProgressChanged(uuid: self.uuid, webView: self.webView, progress: progress) } } + + func getCopyBackForwardList() -> [String: Any] { + let currentList = self.webView.backForwardList + let currentIndex = currentList.backList.count + var completeList = currentList.backList + if currentList.currentItem != nil { + completeList.append(currentList.currentItem!) + } + completeList.append(contentsOf: currentList.forwardList) + + var history: [[String: String]] = [] + + for historyItem in completeList { + var historyItemMap: [String: String] = [:] + historyItemMap["originalUrl"] = historyItem.initialURL.absoluteString + historyItemMap["title"] = historyItem.title + historyItemMap["url"] = historyItem.url.absoluteString + history.append(historyItemMap) + } + + var result: [String: Any] = [:] + result["history"] = history + result["currentIndex"] = currentIndex + + return result; + } } diff --git a/ios/Classes/SwiftFlutterPlugin.swift b/ios/Classes/SwiftFlutterPlugin.swift index 24c20636..3e6c5223 100644 --- a/ios/Classes/SwiftFlutterPlugin.swift +++ b/ios/Classes/SwiftFlutterPlugin.swift @@ -193,6 +193,9 @@ public class SwiftFlutterPlugin: NSObject, FlutterPlugin { case "getOptions": result(self.getOptions(uuid: uuid)) break + case "getCopyBackForwardList": + result(self.getCopyBackForwardList(uuid: uuid)); + break; default: result(FlutterMethodNotImplemented) break @@ -708,4 +711,11 @@ public class SwiftFlutterPlugin: NSObject, FlutterPlugin { return nil } + func getCopyBackForwardList(uuid: String) -> [String: Any]? { + if let webViewController = self.webViewControllers[uuid] { + return webViewController!.getCopyBackForwardList() + } + return nil + } + } diff --git a/lib/flutter_inappbrowser.dart b/lib/flutter_inappbrowser.dart index dd281d8d..464adcd5 100644 --- a/lib/flutter_inappbrowser.dart +++ b/lib/flutter_inappbrowser.dart @@ -658,6 +658,7 @@ class InAppWebViewController { String _inAppBrowserUuid; InAppBrowser _inAppBrowser; + InAppWebViewController(int id, InAppWebView widget) { _id = id; _channel = MethodChannel('com.pichillilorenzo/flutter_inappwebview_$id'); @@ -1030,12 +1031,59 @@ class InAppWebViewController { return options; } + ///Gets the WebHistory for this WebView. This contains the back/forward list for use in querying each item in the history stack. + ///This contains only a snapshot of the current state. + ///Multiple calls to this method may return different objects. + ///The object returned from this method will not be updated to reflect any new state. + Future getCopyBackForwardList() async { + Map args = {}; + if (_inAppBrowserUuid != null) { + _inAppBrowser._throwIsNotOpened(); + args.putIfAbsent('uuid', () => _inAppBrowserUuid); + } + Map result = await _channel.invokeMethod('getCopyBackForwardList', args); + result = result.cast(); + + List historyListMap = result["history"]; + historyListMap = historyListMap.cast>(); + + int currentIndex = result["currentIndex"]; + + List historyList = List(); + for(LinkedHashMap historyItem in historyListMap) { + historyList.add(WebHistoryItem(historyItem["originalUrl"], historyItem["title"], historyItem["url"])); + } + return WebHistory(historyList, currentIndex); + } + Future _dispose() async { await _channel.invokeMethod('dispose'); } } +///WebHistory class. +/// +///This class contains a snapshot of the current back/forward list for a WebView. +class WebHistory { + List _list; + List get list => _list; + int currentIndex; + + WebHistory(this._list, this.currentIndex); +} + +///WebHistoryItem class. +/// +///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 { + String originalUrl; + String title; + String url; + + WebHistoryItem(this.originalUrl, this.title, this.url); +} + ///InAppLocalhostServer class. /// ///This class allows you to create a simple server on `http://localhost:[port]/` in order to be able to load your assets file on a server. The default [port] value is `8080`. diff --git a/pubspec.yaml b/pubspec.yaml index 5dc82525..c646ded4 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,11 +1,12 @@ name: flutter_inappbrowser description: A Flutter plugin that allows you to add an inline webview or open an in-app browser window. (inspired by the popular cordova-plugin-inappbrowser). -version: 0.5.3 +version: 0.5.4 author: Lorenzo Pichilli homepage: https://github.com/pichillilorenzo/flutter_inappbrowser environment: - sdk: ">=2.0.0-dev.68.0 <3.0.0" + sdk: ">=2.1.0-dev.7.1 <3.0.0" + flutter: ">=0.10.1 <2.0.0" dependencies: flutter: