2024-01-24 17:55:25 +00:00
|
|
|
import 'package:cookie_jar/cookie_jar.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:freezer/api/deezer.dart';
|
|
|
|
import 'package:freezer/settings.dart';
|
2024-01-29 00:24:04 +00:00
|
|
|
import 'package:webview_flutter/webview_flutter.dart';
|
2024-01-24 17:55:25 +00:00
|
|
|
|
2024-01-29 00:24:04 +00:00
|
|
|
class ExternalLinkRoute extends StatefulWidget {
|
2024-01-24 17:55:25 +00:00
|
|
|
final String title;
|
|
|
|
final String target;
|
|
|
|
const ExternalLinkRoute(
|
|
|
|
{required this.title, required this.target, super.key});
|
|
|
|
|
2024-01-29 00:24:04 +00:00
|
|
|
@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);
|
|
|
|
}
|
|
|
|
|
2024-01-24 17:55:25 +00:00
|
|
|
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 deezerAPI.cookieJar.loadForRequest(uri);
|
|
|
|
print(cookies);
|
|
|
|
return {'Cookie': cookies.join(';')};
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
2024-01-29 00:24:04 +00:00
|
|
|
appBar: AppBar(title: Text(widget.title)),
|
|
|
|
body: WebViewWidget(
|
|
|
|
controller: _controller,
|
2024-01-24 17:55:25 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|