Skip to content

Commit c545a2d

Browse files
committed
Using webview_flutter instead of flutter_inappwebview to render HTML documentation
1 parent ed10771 commit c545a2d

File tree

9 files changed

+39
-134
lines changed

9 files changed

+39
-134
lines changed

ios/Runner.xcodeproj/project.pbxproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@
173173
isa = PBXProject;
174174
attributes = {
175175
BuildIndependentTargetsInParallel = YES;
176-
LastUpgradeCheck = 1510;
176+
LastUpgradeCheck = 1540;
177177
ORGANIZATIONNAME = "";
178178
TargetAttributes = {
179179
97C146ED1CF9000F007C117D = {

ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "1510"
3+
LastUpgradeVersion = "1540"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

lib/info.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Future<String> getHumanFriendlyVersionString() async {
5959
return "$version ($osName)";
6060
}
6161

62-
/// Return marketing version string, e.g. "1.4.0", "1.3.3 dbg"
62+
/// Return marketing version string, e.g. "1.4.1", "1.3.3 dbg"
6363
Future<String> getMarketingVersion() async {
6464
final packageInfo = await PackageInfo.fromPlatform();
6565
if (kDebugMode == true) {

lib/main.dart

-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import './prefs.dart' show Prefs;
3434
import './session.dart' show SessionRoute;
3535
import './theme.dart' show lightThemeData, darkThemeData;
3636
import './hotword.dart' show HotwordDetector;
37-
import './web.dart' show preloadHTMLDocuments;
3837
// import './util.dart' show readServerAPIKey;
3938

4039
void main() async {
@@ -85,7 +84,6 @@ void main() async {
8584

8685
// Init/preload these to prevent any lag after launching app
8786
await preloadAnimationFrames();
88-
//await preloadHTMLDocuments();
8987
await EmblaSession.prepare();
9088

9189
// Initialize singleton, loading hotword-related assets into memory

lib/session.dart

+2-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,8 @@ class SessionRouteState extends State<SessionRoute> with SingleTickerProviderSta
196196
}
197197

198198
Future<bool> isConnectedToInternet() async {
199-
return (await Connectivity().checkConnectivity() != ConnectivityResult.none);
199+
final List<ConnectivityResult> connectivityResult = await Connectivity().checkConnectivity();
200+
return !connectivityResult.contains(ConnectivityResult.none);
200201
}
201202

202203
// Set text field string (and optionally, an associated image)

lib/web.dart

+22-103
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,16 @@
1818

1919
// Documentation web views.
2020

21-
import 'package:flutter/foundation.dart';
2221
import 'package:flutter/material.dart';
23-
import 'package:flutter/services.dart' show rootBundle;
2422

25-
// import 'package:flutter_inappwebview/flutter_inappwebview.dart';
26-
import 'package:url_launcher/url_launcher.dart' show launchUrl, LaunchMode;
23+
import 'package:webview_flutter/webview_flutter.dart';
2724

2825
import './theme.dart' show standardAppBar;
29-
import './common.dart' show dlog;
3026

3127
const String kDocsDir = 'docs';
3228
const String kLoadingHTMLFilePath = "$kDocsDir/loading.html";
3329
const String kLoadingDarkHTMLFilePath = "$kDocsDir/loading_dark.html";
3430

35-
// late InAppWebViewInitialData loadingHTMLData;
36-
// late InAppWebViewInitialData loadingDarkHTMLData;
37-
38-
// /// Preloads the "loading" HTML documents to prevent any initial
39-
// /// lag when showing loading indicator for documentation pages.
40-
// Future<void> preloadHTMLDocuments() async {
41-
// dlog("Preloading HTML loading documents");
42-
// loadingHTMLData =
43-
// InAppWebViewInitialData(data: await rootBundle.loadString(kLoadingHTMLFilePath));
44-
// loadingHTMLData.baseUrl = Uri.parse("file:///");
45-
// loadingDarkHTMLData =
46-
// InAppWebViewInitialData(data: await rootBundle.loadString(kLoadingDarkHTMLFilePath));
47-
// loadingDarkHTMLData.baseUrl = Uri.parse("file:///");
48-
// }
49-
5031
/// Standard web view route used for displaying HTML documentation files.
5132
class WebViewRoute extends StatefulWidget {
5233
final String initialURL;
@@ -58,96 +39,34 @@ class WebViewRoute extends StatefulWidget {
5839
}
5940

6041
class WebViewRouteState extends State<WebViewRoute> {
61-
/// Path to local asset with same filename as remote HTML document.
62-
String _fallbackAssetForURL(String url) {
63-
final Uri uri = Uri.parse(url);
64-
return "$kDocsDir/${uri.pathSegments.last}";
65-
}
66-
6742
/// Add dark=1 query parameter to URL.
6843
/// This param is used to style the HTML document for dark mode via JS.
6944
String _darkURLForURL(String url) {
7045
return "$url?dark=1";
7146
}
7247

73-
// /// Fall back to local HTML document if error comes
74-
// /// up when fetching document from remote server.
75-
// void errHandler(InAppWebViewController controller, Uri? url, int errCode, String desc) async {
76-
// dlog("Page load error for $url: $errCode, $desc");
77-
// final String path = _fallbackAssetForURL(url.toString());
78-
// dlog("Falling back to local asset $path");
79-
// setState(() {
80-
// controller.loadFile(assetFilePath: path);
81-
// });
82-
// }
83-
84-
// /// Handle clicks on links in HTML documentation.
85-
// /// These links should be opened in an external browser to
86-
// /// avoid screwing with the navigation stack of the app.
87-
// Future<NavigationActionPolicy> urlClickHandler(
88-
// InAppWebViewController controller, NavigationAction action) async {
89-
// final URLRequest req = action.request;
90-
// final String urlStr = req.url.toString();
91-
// final String fallbackFilename = _fallbackAssetForURL(urlStr);
92-
93-
// if (urlStr.startsWith(widget.initialURL) == false &&
94-
// urlStr.endsWith(fallbackFilename) == false) {
95-
// // It's not a local URL, so open it in an external browser
96-
// dlog("Opening external URL: ${req.url}");
97-
// await launchUrl(req.url!, mode: LaunchMode.externalApplication);
98-
// return NavigationActionPolicy.CANCEL;
99-
// }
100-
101-
// return NavigationActionPolicy.ALLOW;
102-
// }
103-
104-
// /// Create web view that initially presents a "loading" document with
105-
// /// a progress indicator. Then immediately fetch the actual remote document.
106-
// /// Falls back to loading a local bundled HTML document with the same name
107-
// /// on network error. This ensures that at least *some* version of the
108-
// /// document can be viewed even when the device is offline.
109-
// InAppWebView _buildWebView(BuildContext context) {
110-
// final darkMode = (MediaQuery.platformBrightnessOf(context) == Brightness.dark);
111-
// final loadingURL = darkMode ? kLoadingDarkHTMLFilePath : kLoadingHTMLFilePath;
112-
// final finalURL = darkMode ? _darkURLForURL(widget.initialURL) : widget.initialURL;
113-
// final initialData = darkMode ? loadingDarkHTMLData : loadingHTMLData;
114-
115-
// final webViewOpts = InAppWebViewGroupOptions(
116-
// crossPlatform: InAppWebViewOptions(
117-
// clearCache: kDebugMode,
118-
// useShouldOverrideUrlLoading: true,
119-
// transparentBackground: true,
120-
// ));
121-
122-
// // Create and configure web view
123-
// return InAppWebView(
124-
// initialData: initialData,
125-
// initialUrlRequest: URLRequest(url: Uri.parse(finalURL)),
126-
// initialOptions: webViewOpts,
127-
// onLoadStart: (InAppWebViewController controller, Uri? uri) {
128-
// dlog("Loading URL ${uri.toString()}");
129-
// },
130-
// onLoadStop: (InAppWebViewController controller, Uri? uri) async {
131-
// final String urlStr = uri.toString();
132-
// if (urlStr.endsWith(loadingURL) || urlStr == 'about:blank' || urlStr == 'file:///') {
133-
// // Loading of initial "loading" document is complete.
134-
// // Now load the actual remote document.
135-
// setState(() {
136-
// controller.loadUrl(urlRequest: URLRequest(url: Uri.parse(finalURL)));
137-
// });
138-
// }
139-
// },
140-
// onLoadError: errHandler,
141-
// onLoadHttpError: errHandler,
142-
// shouldOverrideUrlLoading: urlClickHandler,
143-
// onConsoleMessage: (InAppWebViewController controller, ConsoleMessage msg) {
144-
// dlog("Web View Console message: ${msg.message}");
145-
// });
146-
// }
147-
14848
@override
14949
Widget build(BuildContext context) {
150-
return Scaffold(appBar: standardAppBar, body: standardAppBar //buildWebView(context),
151-
);
50+
final darkMode = (MediaQuery.platformBrightnessOf(context) == Brightness.dark);
51+
final finalURL = darkMode ? _darkURLForURL(widget.initialURL) : widget.initialURL;
52+
53+
var controller = WebViewController()
54+
..setJavaScriptMode(JavaScriptMode.unrestricted)
55+
..setBackgroundColor(const Color(0x00000000))
56+
..setNavigationDelegate(
57+
NavigationDelegate(
58+
onProgress: (int progress) {},
59+
onPageStarted: (String url) {},
60+
onPageFinished: (String url) {},
61+
onHttpError: (HttpResponseError error) {},
62+
onWebResourceError: (WebResourceError error) {},
63+
onNavigationRequest: (NavigationRequest request) {
64+
return NavigationDecision.navigate;
65+
},
66+
),
67+
)
68+
..loadRequest(Uri.parse(finalURL));
69+
70+
return Scaffold(appBar: standardAppBar, body: WebViewWidget(controller: controller));
15271
}
15372
}

pubspec.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: embla
22
description: Cross-platform mobile Icelandic-language voice assistant.
33
publish_to: 'none'
4-
version: 1.4.1+0
4+
version: 1.4.1+1
55
homepage: https://embla.is
66
repository: https://github.com/mideind/EmblaFlutterApp
77

@@ -24,7 +24,7 @@ dependencies:
2424
url: https://github.com/tommysolsen/platform_device_id.git
2525
path: platform_device_id
2626
connectivity_plus: '>=5.0.2'
27-
# flutter_inappwebview: '^6.0.0'
27+
webview_flutter: ^4.8.0
2828
path_provider: '^2.1.4'
2929
wakelock_plus: '^1.2.7'
3030
permission_handler: ^11.3.1

test/classes_test.dart

+10-10
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import 'package:flutter/material.dart';
55

66
import 'package:embla/animations.dart';
77
import 'package:embla/hotword.dart';
8-
import 'package:embla/jsexec.dart';
8+
// import 'package:embla/jsexec.dart';
99
import 'package:embla/loc.dart';
1010
import 'package:embla/prefs.dart';
1111

@@ -14,7 +14,7 @@ void main() {
1414

1515
testAnimations();
1616
testHotword();
17-
testJSExec();
17+
//testJSExec();
1818
testLocationTracker();
1919
testPrefs();
2020
}
@@ -36,15 +36,15 @@ void testHotword() async {
3636
}
3737

3838
// jsexec.dart
39-
void testJSExec() async {
40-
test("JSExecutor should be singleton", () {
41-
expect(JSExecutor() == JSExecutor(), true);
42-
});
43-
// test("JSExecutor should run JavaScript code without issue", () async {
44-
// JSExecutor executor = JSExecutor();
45-
// expect(await executor.run("2+2"), 4);
39+
// void testJSExec() async {
40+
// test("JSExecutor should be singleton", () {
41+
// expect(JSExecutor() == JSExecutor(), true);
4642
// });
47-
}
43+
// // test("JSExecutor should run JavaScript code without issue", () async {
44+
// // JSExecutor executor = JSExecutor();
45+
// // expect(await executor.run("2+2"), 4);
46+
// // });
47+
// }
4848

4949
// loc.dart
5050
void testLocationTracker() async {

test/widget_test.dart

-13
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import 'package:embla/menu.dart';
99
import 'package:embla/settings.dart';
1010
import 'package:embla/info.dart';
1111
import 'package:embla/voices.dart';
12-
import 'package:embla/web.dart';
13-
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
1412

1513
void main() {
1614
// main.dart
@@ -75,15 +73,4 @@ void main() {
7573
expect(find.byType(ListView), findsOneWidget);
7674
expect(find.byType(ListTile), findsAtLeastNWidgets(2));
7775
});
78-
79-
// web.dart
80-
testWidgets('WebViewRoute contains InAppWebView', (tester) async {
81-
preloadHTMLDocuments();
82-
await tester.pumpWidget(
83-
const MaterialApp(
84-
home: WebViewRoute(initialURL: "https://mideind.is"),
85-
),
86-
);
87-
expect(find.byType(InAppWebView), findsOneWidget);
88-
});
8976
}

0 commit comments

Comments
 (0)