freezer/lib/ui/external_link_route.dart

52 lines
1.7 KiB
Dart

import 'package:cookie_jar/cookie_jar.dart';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart' as fwv;
import 'package:freezer/api/deezer.dart';
import 'package:freezer/settings.dart';
class ExternalLinkRoute extends StatelessWidget {
final String title;
final String target;
const ExternalLinkRoute(
{required this.title, required this.target, super.key});
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) {
Uri uriTarget = _resolveTarget(target);
return Scaffold(
appBar: AppBar(title: Text(title)),
body: fwv.InAppWebView(
onWebViewCreated: (controller) async {
final uri = _resolveTarget(target);
final headers = await _resolveHeaders(uri);
controller.addWebMessageListener(fwv.WebMessageListener(
jsObjectName: 'jsObjectName',
onPostMessage: (message, sourceOrigin, isMainFrame, replyProxy) =>
print('message: $message, sourceOrigin: $sourceOrigin'),
));
controller.loadUrl(
urlRequest: fwv.URLRequest(url: uri, headers: headers));
},
),
);
}
}