Skip to content

Commit

Permalink
Switched to flutter_inappwebview
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolfteam committed Jun 22, 2021
1 parent 1e1a1ee commit d71f257
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 172 deletions.
54 changes: 9 additions & 45 deletions lib/presentation/daily_check_in/daily_check_in_page.dart
@@ -1,19 +1,16 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
import 'package:genshindb/application/bloc.dart';
import 'package:genshindb/generated/l10n.dart';
import 'package:genshindb/presentation/shared/app_webview.dart';
import 'package:genshindb/presentation/shared/loading.dart';
import 'package:genshindb/presentation/shared/page_message.dart';

class DailyCheckInPage extends StatefulWidget {
@override
_DailyCheckInPageState createState() => _DailyCheckInPageState();
}

class _DailyCheckInPageState extends State<DailyCheckInPage> {
final flutterWebViewPlugin = FlutterWebviewPlugin();

final script = '''
function removeButtons() {
const backButton = document.querySelectorAll('div[class*="back"]');
Expand All @@ -24,44 +21,19 @@ class _DailyCheckInPageState extends State<DailyCheckInPage> {
shareButton[0].remove();
}
}
setTimeout(removeButtons, 300);
setTimeout(removeButtons, 1200);
setTimeout(removeButtons, 3000);
setTimeout(removeButtons, 6000);
''';

@override
void initState() {
super.initState();
flutterWebViewPlugin.onStateChanged.listen((WebViewStateChanged state) {
if (mounted) {
if (state.type == WebViewState.finishLoad) {
debugPrint('loaded...');
_onPageLoaded();
} else if (state.type == WebViewState.abortLoad) {
// if there is a problem with loading the url
debugPrint('there is a problem...');
} else if (state.type == WebViewState.startLoad) {
// if the url started loading
debugPrint('start loading...');
}
}
});
}

@override
void didChangeDependencies() {
super.didChangeDependencies();
context.read<UrlPageBloc>().add(const UrlPageEvent.init(loadMap: false, loadWishSimulator: false, loadDailyCheckIn: true));
}

@override
void dispose() {
super.dispose();
flutterWebViewPlugin.dispose();
}

@override
Widget build(BuildContext context) {
final s = S.of(context);
Expand All @@ -70,24 +42,16 @@ class _DailyCheckInPageState extends State<DailyCheckInPage> {
builder: (context, state) {
return state.map(
loading: (_) => const Loading(),
loaded: (state) {
if (state.hasInternetConnection) {
return WebviewScaffold(
appBar: AppBar(title: Text(s.dailyCheckIn)),
url: state.dailyCheckInUrl,
hidden: true,
initialChild: const Loading(useScaffold: false),
);
}
return PageMessage(text: s.noInternetConnection);
},
loaded: (state) => AppWebView(
appBar: AppBar(title: Text(s.dailyCheckIn)),
url: state.dailyCheckInUrl,
userAgent: state.userAgent,
hasInternetConnection: state.hasInternetConnection,
script: script,
),
);
},
),
);
}

void _onPageLoaded() {
flutterWebViewPlugin.evalJavascript(script);
}
}
5 changes: 3 additions & 2 deletions lib/presentation/home/widgets/sliver_daily_check_in_card.dart
Expand Up @@ -16,7 +16,7 @@ class SliverDailyCheckInCard extends StatelessWidget {
final theme = Theme.of(context);
final s = S.of(context);
return SliverCardItem(
onClick: _gotoSettingsPage,
onClick: _goToPageDailyCheckInPage,
iconToTheLeft: iconToTheLeft,
icon: Icon(Icons.rotate_right_sharp, size: 60, color: theme.accentColor),
children: [
Expand All @@ -25,8 +25,9 @@ class SliverDailyCheckInCard extends StatelessWidget {
);
}

Future<void> _gotoSettingsPage(BuildContext context) async {
Future<void> _goToPageDailyCheckInPage(BuildContext context) async {
final route = MaterialPageRoute(builder: (c) => DailyCheckInPage());
await Navigator.push(context, route);
await route.completed;
}
}
Expand Up @@ -30,5 +30,6 @@ class SliverWishSimulatorCard extends StatelessWidget {
Future<void> _gotoWishSimulatorPage(BuildContext context) async {
final route = MaterialPageRoute(builder: (c) => WishSimulatorPage());
await Navigator.push(context, route);
await route.completed;
}
}
70 changes: 7 additions & 63 deletions lib/presentation/map/map_page.dart
@@ -1,19 +1,15 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
import 'package:genshindb/application/bloc.dart';
import 'package:genshindb/generated/l10n.dart';
import 'package:genshindb/presentation/shared/app_webview.dart';
import 'package:genshindb/presentation/shared/loading.dart';
import 'package:genshindb/presentation/shared/page_message.dart';

class MapPage extends StatefulWidget {
@override
_MapPageState createState() => _MapPageState();
}

class _MapPageState extends State<MapPage> {
final flutterWebviewPlugin = FlutterWebviewPlugin();

final String script = '''
let wasRemoved = false;
function removeAds(){
Expand Down Expand Up @@ -45,78 +41,26 @@ class _MapPageState extends State<MapPage> {
setTimeout(removeAds, 3500);
''';

@override
void initState() {
super.initState();
flutterWebviewPlugin.onStateChanged.listen((WebViewStateChanged state) async {
if (mounted) {
if (state.type == WebViewState.finishLoad && !state.url.contains('google')) {
debugPrint('loaded...');
await _onPageLoaded();
} else if (state.type == WebViewState.abortLoad) {
// if there is a problem with loading the url
debugPrint('there is a problem...');
} else if (state.type == WebViewState.startLoad) {
// if the url started loading
debugPrint('start loading...');
}
}
});
}

@override
void didChangeDependencies() {
super.didChangeDependencies();
context.read<UrlPageBloc>().add(const UrlPageEvent.init(loadMap: true, loadWishSimulator: false, loadDailyCheckIn: false));
}

@override
void dispose() {
super.dispose();
flutterWebviewPlugin.dispose();
}

@override
Widget build(BuildContext context) {
final s = S.of(context);
return BlocBuilder<UrlPageBloc, UrlPageState>(
builder: (context, state) {
return state.map(
loading: (_) => const Loading(),
loaded: (state) {
if (state.hasInternetConnection) {
return WebviewScaffold(
url: state.mapUrl,
ignoreSSLErrors: true,
appCacheEnabled: true,
hidden: true,
initialChild: const Loading(useScaffold: false),
);
}
return PageMessage(text: s.noInternetConnection);
},
loaded: (state) => AppWebView(
url: state.mapUrl,
userAgent: state.userAgent,
hasInternetConnection: state.hasInternetConnection,
script: script,
),
);
},
);
}

Rect _buildRect() {
const statusBarHeight = 24;
final mediaQuery = MediaQuery.of(context);
final topPadding = mediaQuery.padding.top;
final top = topPadding;
var height = mediaQuery.size.height - top;
height -= 56.0 + mediaQuery.padding.bottom;

if (height < 0.0) {
height = 0.0;
}

return Rect.fromLTWH(0.0, top + statusBarHeight, mediaQuery.size.width, height - statusBarHeight);
}

Future<void> _onPageLoaded() async {
await flutterWebviewPlugin.resize(_buildRect());
await flutterWebviewPlugin.evalJavascript(script);
}
}
73 changes: 73 additions & 0 deletions lib/presentation/shared/app_webview.dart
@@ -0,0 +1,73 @@
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:genshindb/generated/l10n.dart';
import 'package:genshindb/presentation/shared/page_message.dart';

import 'loading.dart';

class AppWebView extends StatefulWidget {
final String url;
final String userAgent;
final bool hasInternetConnection;
final bool isLoading;
final String? script;
final AppBar? appBar;

const AppWebView({
Key? key,
required this.url,
required this.userAgent,
required this.hasInternetConnection,
this.isLoading = false,
this.script,
this.appBar,
}) : super(key: key);

@override
_AppWebViewState createState() => _AppWebViewState();
}

class _AppWebViewState extends State<AppWebView> {
bool _loading = true;

@override
Widget build(BuildContext context) {
if (!widget.hasInternetConnection) {
final s = S.of(context);
return PageMessage(text: s.noInternetConnection);
}
if (widget.isLoading) {
return const Loading();
}

return Stack(
children: [
Scaffold(
appBar: widget.appBar,
body: InAppWebView(
initialUrlRequest: URLRequest(url: Uri.parse(widget.url)),
initialOptions: InAppWebViewGroupOptions(
android: AndroidInAppWebViewOptions(
useHybridComposition: true,
),
crossPlatform: InAppWebViewOptions(
preferredContentMode: UserPreferredContentMode.MOBILE,
userAgent: widget.userAgent,
transparentBackground: true,
),
),
onLoadStop: (controller, url) async {
if (widget.script != null) {
await controller.evaluateJavascript(source: widget.script!);
}
setState(() {
_loading = false;
});
},
),
),
if (_loading) const Loading(),
],
);
}
}
54 changes: 8 additions & 46 deletions lib/presentation/wish_simulator/wish_simulator_page.dart
@@ -1,19 +1,16 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
import 'package:genshindb/application/bloc.dart';
import 'package:genshindb/generated/l10n.dart';
import 'package:genshindb/presentation/shared/app_webview.dart';
import 'package:genshindb/presentation/shared/loading.dart';
import 'package:genshindb/presentation/shared/page_message.dart';

class WishSimulatorPage extends StatefulWidget {
@override
_WishSimulatorPageState createState() => _WishSimulatorPageState();
}

class _WishSimulatorPageState extends State<WishSimulatorPage> {
final flutterWebviewPlugin = FlutterWebviewPlugin();

final script = '''
function closeModal(){
if (!document.getElementsByClassName("modal-container"))
Expand All @@ -34,63 +31,28 @@ class _WishSimulatorPageState extends State<WishSimulatorPage> {
setTimeout(closeModal, 6000);
''';

@override
void initState() {
super.initState();
flutterWebviewPlugin.onStateChanged.listen((WebViewStateChanged state) {
if (mounted) {
if (state.type == WebViewState.finishLoad) {
debugPrint('loaded...');
_onPageLoaded();
} else if (state.type == WebViewState.abortLoad) {
// if there is a problem with loading the url
debugPrint('there is a problem...');
} else if (state.type == WebViewState.startLoad) {
// if the url started loading
debugPrint('start loading...');
}
}
});
}

@override
void didChangeDependencies() {
super.didChangeDependencies();
context.read<UrlPageBloc>().add(const UrlPageEvent.init(loadMap: false, loadWishSimulator: true, loadDailyCheckIn: false));
}

@override
void dispose() {
super.dispose();
flutterWebviewPlugin.dispose();
}

@override
Widget build(BuildContext context) {
final s = S.of(context);
return SafeArea(child: BlocBuilder<UrlPageBloc, UrlPageState>(
builder: (context, state) {
return state.map(
loading: (_) => const Loading(),
loaded: (state) {
if (state.hasInternetConnection) {
return WebviewScaffold(
appBar: AppBar(title: Text(s.wishSimulator)),
url: state.wishSimulatorUrl,
hidden: true,
clearCache: true,
clearCookies: true,
initialChild: const Loading(useScaffold: false),
);
}
return PageMessage(text: s.noInternetConnection);
},
loaded: (state) => AppWebView(
appBar: AppBar(title: Text(s.wishSimulator)),
url: state.wishSimulatorUrl,
userAgent: state.userAgent,
hasInternetConnection: state.hasInternetConnection,
script: script,
),
);
},
));
}

void _onPageLoaded() {
flutterWebviewPlugin.evalJavascript(script);
}
}

0 comments on commit d71f257

Please sign in to comment.