freezer/lib/ui/external_link_route.dart

65 lines
1.8 KiB
Dart

import 'package:cookie_jar/cookie_jar.dart';
import 'package:flutter/material.dart';
import 'package:freezer/api/deezer.dart';
import 'package:freezer/settings.dart';
import 'package:get_it/get_it.dart';
import 'package:webview_flutter/webview_flutter.dart';
class ExternalLinkRoute extends StatefulWidget {
final String title;
final String target;
const ExternalLinkRoute(
{required this.title, required this.target, super.key});
@override
State<ExternalLinkRoute> createState() => _ExternalLinkRouteState();
}
class _ExternalLinkRouteState extends State<ExternalLinkRoute> {
late final WebViewController _controller;
@override
void initState() {
_controller = WebViewController();
_initStateAsync();
super.initState();
}
void _initStateAsync() async {
final uri = _resolveTarget(widget.target);
final headers = await _resolveHeaders(uri);
_controller.addJavaScriptChannel('test', onMessageReceived: print);
_controller.loadRequest(uri, headers: headers);
}
Uri _resolveTarget(String target) {
print('target: $target');
if (target == 'story/mdy' || target == '/story/mdy') {
// my deezer year redirect to iframe URL
return Uri.parse(
'https://mydeezerstory.deezer.com/inapp?campaign=mdy&lang=${settings.deezerLanguage}');
}
// resolve relative to www.deezer.com
return Uri.https('www.deezer.com', '/us').resolve(target);
}
Future<Map<String, String>> _resolveHeaders(Uri uri) async {
List<Cookie> cookies =
await GetIt.instance<CookieJar>().loadForRequest(uri);
print(cookies);
return {'Cookie': cookies.join(';')};
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(widget.title)),
body: WebViewWidget(
controller: _controller,
),
);
}
}