From 3422076fa50a94eb4269dfe1cef79b2c20392ffc Mon Sep 17 00:00:00 2001 From: kodjomoustapha <107993382+kodjodevf@users.noreply.github.com> Date: Sat, 27 Jan 2024 18:44:14 +0100 Subject: [PATCH] Upgrade flutter_inappwebview --- lib/eval/model/m_bridge.dart | 2 +- lib/ffi/torrent_server_ffi.dart | 1 - lib/main.dart | 2 +- lib/modules/anime/anime_player_view.dart | 107 ++++++++------- lib/modules/webview/webview.dart | 122 +++++++++++++++--- lib/services/cloudflare/cloudflare.dart | 4 +- lib/services/cloudflare/cookie.dart | 2 +- macos/Flutter/GeneratedPluginRegistrant.swift | 2 + pubspec.lock | 52 +++++++- pubspec.yaml | 2 +- 10 files changed, 223 insertions(+), 73 deletions(-) diff --git a/lib/eval/model/m_bridge.dart b/lib/eval/model/m_bridge.dart index 07e94e0d..4e68d751 100644 --- a/lib/eval/model/m_bridge.dart +++ b/lib/eval/model/m_bridge.dart @@ -183,7 +183,7 @@ class MBridge { headlessWebView!.dispose(); }, initialUrlRequest: URLRequest( - url: Uri.parse(url), + url: WebUri(url), ), ); headlessWebView.run(); diff --git a/lib/ffi/torrent_server_ffi.dart b/lib/ffi/torrent_server_ffi.dart index 3920e617..d4128d95 100644 --- a/lib/ffi/torrent_server_ffi.dart +++ b/lib/ffi/torrent_server_ffi.dart @@ -24,7 +24,6 @@ Future start(String mcfg) async { const String _libName = 'libmtorrentserver'; -/// The dynamic library in which the symbols for [NativeAddBindings] can be found. final DynamicLibrary _dylib = () { if (Platform.isMacOS) { return DynamicLibrary.open('$_libName.dylib'); diff --git a/lib/main.dart b/lib/main.dart index 7b9619d3..e4e3763e 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -39,7 +39,7 @@ void main(List args) async { // Override the default HTTP client. HttpOverrides.global = MyHttpoverrides(); // If running on desktop platforms and web view title bar widget is active, exit. - if (Platform.isLinux || Platform.isWindows || Platform.isMacOS) { + if (Platform.isLinux || Platform.isWindows) { if (runWebViewTitleBarWidget(args)) { return; } diff --git a/lib/modules/anime/anime_player_view.dart b/lib/modules/anime/anime_player_view.dart index 0d65ba6f..4cf44fa4 100644 --- a/lib/modules/anime/anime_player_view.dart +++ b/lib/modules/anime/anime_player_view.dart @@ -992,56 +992,65 @@ class _AnimeStreamPageState extends riv.ConsumerState { } Widget _topButtonBar(BuildContext context) { - return Row( - children: [ - BackButton( - color: Colors.white, - onPressed: () async { - if (_isDesktop) { - final isFullScreen = await windowManager.isFullScreen(); - if (isFullScreen) { - setFullScreen(value: false); - } else { - if (mounted) { - Navigator.pop(context); - } - } - } else { - SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, - overlays: SystemUiOverlay.values); - if (mounted) { - Navigator.pop(context); - } - } - }, - ), - Flexible( - child: ListTile( - dense: true, - title: SizedBox( - width: context.mediaWidth(0.8), - child: Text( - widget.episode.manga.value!.name!, - style: const TextStyle( - fontWeight: FontWeight.bold, color: Colors.white), - overflow: TextOverflow.ellipsis, - ), + return ValueListenableBuilder( + valueListenable: _enterFullScreen, + builder: (context, fullScreen, _) { + return Padding( + padding: EdgeInsets.only( + top: !_isDesktop && !fullScreen + ? MediaQuery.of(context).padding.top + : 0), + child: Row( + children: [ + BackButton( + color: Colors.white, + onPressed: () async { + if (_isDesktop) { + if (fullScreen) { + setFullScreen(value: false); + } else { + if (mounted) { + Navigator.pop(context); + } + } + } else { + SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, + overlays: SystemUiOverlay.values); + if (mounted) { + Navigator.pop(context); + } + } + }, + ), + Flexible( + child: ListTile( + dense: true, + title: SizedBox( + width: context.mediaWidth(0.8), + child: Text( + widget.episode.manga.value!.name!, + style: const TextStyle( + fontWeight: FontWeight.bold, color: Colors.white), + overflow: TextOverflow.ellipsis, + ), + ), + subtitle: SizedBox( + width: context.mediaWidth(0.8), + child: Text( + widget.episode.name!, + style: TextStyle( + fontSize: 12, + fontWeight: FontWeight.w400, + color: Colors.white.withOpacity(0.7)), + overflow: TextOverflow.ellipsis, + ), + ), + ), + ), + ], ), - subtitle: SizedBox( - width: context.mediaWidth(0.8), - child: Text( - widget.episode.name!, - style: TextStyle( - fontSize: 12, - fontWeight: FontWeight.w400, - color: Colors.white.withOpacity(0.7)), - overflow: TextOverflow.ellipsis, - ), - ), - ), - ), - ], - ); + ); + }); } void _resize(BoxFit fit) async { diff --git a/lib/modules/webview/webview.dart b/lib/modules/webview/webview.dart index 36f04bb1..9551ca55 100644 --- a/lib/modules/webview/webview.dart +++ b/lib/modules/webview/webview.dart @@ -3,6 +3,7 @@ import 'dart:convert'; import 'dart:io'; import 'package:desktop_webview_window/desktop_webview_window.dart'; +import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_inappwebview/flutter_inappwebview.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; @@ -30,10 +31,11 @@ class MangaWebView extends ConsumerStatefulWidget { class _MangaWebViewState extends ConsumerState { final GlobalKey webViewKey = GlobalKey(); - + late final MyInAppBrowser browser; double progress = 0; @override void initState() { + browser = MyInAppBrowser(context: context); if (Platform.isWindows || Platform.isLinux || Platform.isMacOS) { _runWebViewDesktop(); } else { @@ -46,17 +48,33 @@ class _MangaWebViewState extends ConsumerState { Webview? webview; _runWebViewDesktop() async { - webview = await WebviewWindow.create( - configuration: CreateConfiguration( - userDataFolderWindows: await getWebViewPath(), - ), - ); - webview! - ..setBrightness(Brightness.dark) - ..launch(widget.url) - ..onClose.whenComplete(() { - Navigator.pop(context); - }); + if (Platform.isMacOS) { + await browser.openUrlRequest( + urlRequest: URLRequest(url: WebUri(widget.url)), + settings: InAppBrowserClassSettings( + browserSettings: InAppBrowserSettings( + toolbarTopBackgroundColor: Colors.blue, + presentationStyle: ModalPresentationStyle.POPOVER), + webViewSettings: InAppWebViewSettings( + isInspectable: kDebugMode, + useShouldOverrideUrlLoading: true, + useOnLoadResource: true, + ), + ), + ); + } else { + webview = await WebviewWindow.create( + configuration: CreateConfiguration( + userDataFolderWindows: await getWebViewPath(), + ), + ); + webview! + ..setBrightness(Brightness.dark) + ..launch(widget.url) + ..onClose.whenComplete(() { + Navigator.pop(context); + }); + } } bool isNotDesktop = false; @@ -79,7 +97,14 @@ class _MangaWebViewState extends ConsumerState { ), leading: IconButton( onPressed: () { - webview!.close(); + if (Platform.isMacOS) { + if (browser.isOpened()) { + browser.close(); + } + } else { + webview!.close(); + } + Navigator.pop(context); }, icon: const Icon(Icons.close)), @@ -155,7 +180,7 @@ class _MangaWebViewState extends ConsumerState { Share.share(_url); } else if (value == 2) { await InAppBrowser.openWithSystemBrowser( - url: Uri.parse(_url)); + url: WebUri(_url)); } else if (value == 3) { CookieManager.instance().deleteAllCookies(); } @@ -228,7 +253,7 @@ class _MangaWebViewState extends ConsumerState { _canGoForward = canGoForward; }); }, - initialUrlRequest: URLRequest(url: Uri.parse(widget.url)), + initialUrlRequest: URLRequest(url: WebUri(widget.url)), ), ), ], @@ -267,3 +292,70 @@ Future decodeHtml(Webview webview, {String? sourceId}) async { return null; } } + +class MyInAppBrowser extends InAppBrowser { + MyInAppBrowser( + {super.windowId, + super.initialUserScripts, + super.pullToRefreshController, + required this.context}); + late BuildContext context; + @override + Future onBrowserCreated() async { + print("\n\nBrowser Created!\n\n"); + } + + @override + Future onLoadStart(url) async {} + + @override + Future onLoadStop(url) async {} + + @override + Future onPermissionRequest(request) async { + return PermissionResponse( + resources: request.resources, action: PermissionResponseAction.GRANT); + } + + @override + void onLoadError(url, code, message) { + pullToRefreshController?.endRefreshing(); + } + + @override + void onProgressChanged(progress) { + if (progress == 100) { + pullToRefreshController?.endRefreshing(); + } + } + + @override + void onExit() { + Navigator.pop(context); + } + + @override + Future shouldOverrideUrlLoading( + navigationAction) async { + var uri = navigationAction.request.url!; + + if (!["http", "https", "file", "chrome", "data", "javascript", "about"] + .contains(uri.scheme)) { + if (await canLaunchUrl(uri)) { + // Launch the App + await launchUrl( + uri, + ); + // and cancel the request + return NavigationActionPolicy.CANCEL; + } + } + + return NavigationActionPolicy.ALLOW; + } + + @override + void onMainWindowWillClose() { + close(); + } +} diff --git a/lib/services/cloudflare/cloudflare.dart b/lib/services/cloudflare/cloudflare.dart index cd7b6c5e..eed9ba65 100644 --- a/lib/services/cloudflare/cloudflare.dart +++ b/lib/services/cloudflare/cloudflare.dart @@ -10,7 +10,7 @@ Future cloudflareBypass( String ua = ""; bool isOk = false; String? html; - if (Platform.isWindows || Platform.isLinux || Platform.isMacOS) { + if (Platform.isWindows || Platform.isLinux) { final webview = await WebviewWindow.create( configuration: CreateConfiguration( windowHeight: 500, @@ -56,7 +56,7 @@ Future cloudflareBypass( isOk = true; headlessWebView!.dispose(); }, - initialUrlRequest: URLRequest(url: Uri.parse(url)), + initialUrlRequest: URLRequest(url: WebUri(url)), ); headlessWebView.run(); diff --git a/lib/services/cloudflare/cookie.dart b/lib/services/cloudflare/cookie.dart index bd00583b..5eed226f 100644 --- a/lib/services/cloudflare/cookie.dart +++ b/lib/services/cloudflare/cookie.dart @@ -40,7 +40,7 @@ Future addCookie(String sourceId, String url, String ua) async { flutter_inappwebview.CookieManager.instance(); final cookie = (await cookieManager.getCookie( - url: Uri.parse(url), name: "cf_clearance")); + url: flutter_inappwebview.WebUri(url), name: "cf_clearance")); if (cookie != null) { final newCookie = "${cookie.name}=${cookie.value}"; diff --git a/macos/Flutter/GeneratedPluginRegistrant.swift b/macos/Flutter/GeneratedPluginRegistrant.swift index cbc63ae8..899d7ad6 100644 --- a/macos/Flutter/GeneratedPluginRegistrant.swift +++ b/macos/Flutter/GeneratedPluginRegistrant.swift @@ -6,6 +6,7 @@ import FlutterMacOS import Foundation import desktop_webview_window +import flutter_inappwebview_macos import flutter_web_auth_2 import isar_flutter_libs import media_kit_libs_macos_video @@ -23,6 +24,7 @@ import window_to_front func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { DesktopWebviewWindowPlugin.register(with: registry.registrar(forPlugin: "DesktopWebviewWindowPlugin")) + InAppWebViewFlutterPlugin.register(with: registry.registrar(forPlugin: "InAppWebViewFlutterPlugin")) FlutterWebAuth2Plugin.register(with: registry.registrar(forPlugin: "FlutterWebAuth2Plugin")) IsarFlutterLibsPlugin.register(with: registry.registrar(forPlugin: "IsarFlutterLibsPlugin")) MediaKitLibsMacosVideoPlugin.register(with: registry.registrar(forPlugin: "MediaKitLibsMacosVideoPlugin")) diff --git a/pubspec.lock b/pubspec.lock index 1dab74dd..72556bec 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -475,10 +475,58 @@ packages: dependency: "direct main" description: name: flutter_inappwebview - sha256: d198297060d116b94048301ee6749cd2e7d03c1f2689783f52d210a6b7aba350 + sha256: "3e9a443a18ecef966fb930c3a76ca5ab6a7aafc0c7b5e14a4a850cf107b09959" url: "https://pub.dev" source: hosted - version: "5.8.0" + version: "6.0.0" + flutter_inappwebview_android: + dependency: transitive + description: + name: flutter_inappwebview_android + sha256: fd4db51e46f49b140d83a3206851432c54ea920b381137c0ba82d0cf59be1dee + url: "https://pub.dev" + source: hosted + version: "1.0.12" + flutter_inappwebview_internal_annotations: + dependency: transitive + description: + name: flutter_inappwebview_internal_annotations + sha256: "5f80fd30e208ddded7dbbcd0d569e7995f9f63d45ea3f548d8dd4c0b473fb4c8" + url: "https://pub.dev" + source: hosted + version: "1.1.1" + flutter_inappwebview_ios: + dependency: transitive + description: + name: flutter_inappwebview_ios + sha256: f363577208b97b10b319cd0c428555cd8493e88b468019a8c5635a0e4312bd0f + url: "https://pub.dev" + source: hosted + version: "1.0.13" + flutter_inappwebview_macos: + dependency: transitive + description: + name: flutter_inappwebview_macos + sha256: b55b9e506c549ce88e26580351d2c71d54f4825901666bd6cfa4be9415bb2636 + url: "https://pub.dev" + source: hosted + version: "1.0.11" + flutter_inappwebview_platform_interface: + dependency: transitive + description: + name: flutter_inappwebview_platform_interface + sha256: "545fd4c25a07d2775f7d5af05a979b2cac4fbf79393b0a7f5d33ba39ba4f6187" + url: "https://pub.dev" + source: hosted + version: "1.0.10" + flutter_inappwebview_web: + dependency: transitive + description: + name: flutter_inappwebview_web + sha256: d8c680abfb6fec71609a700199635d38a744df0febd5544c5a020bd73de8ee07 + url: "https://pub.dev" + source: hosted + version: "1.0.8" flutter_launcher_icons: dependency: "direct dev" description: diff --git a/pubspec.yaml b/pubspec.yaml index 114fecb2..438fd962 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -34,7 +34,7 @@ dependencies: url: https://github.com/kodjodevf/background_downloader.git ref: 2cc16eeb475788ec0443b64badc325c00d7dab90 permission_handler: ^11.1.0 - flutter_inappwebview: ^5.7.2+3 + flutter_inappwebview: ^6.0.0 draggable_menu: ^4.4.1 isar: 3.1.0+1 isar_flutter_libs: 3.1.0+1