[reddit] Add Support for YouTube Videos (#134)

The Reddit source supports YouTube videos now. This means if a Reddit
post contains a link to a YouTube video, we render the video in the item
details, so that a user can directly watch the video.
This commit is contained in:
Rico Berger
2024-02-10 21:04:09 +01:00
committed by GitHub
parent c0c87e2c10
commit 303f78c3bc

View File

@@ -5,6 +5,7 @@ import 'package:feeddeck/models/source.dart';
import 'package:feeddeck/widgets/item/details/utils/item_description.dart';
import 'package:feeddeck/widgets/item/details/utils/item_subtitle.dart';
import 'package:feeddeck/widgets/item/details/utils/item_title.dart';
import 'package:feeddeck/widgets/item/details/utils/item_youtube/item_youtube_video.dart';
class ItemDetailsReddit extends StatelessWidget {
const ItemDetailsReddit({
@@ -16,6 +17,60 @@ class ItemDetailsReddit extends StatelessWidget {
final FDItem item;
final FDSource source;
/// [_getYoutubeUrl] returns a YouTube url when the provided [description]
/// contains a YouTube link. If the [description] does not contain a YouTube
/// link, the function returns `null`.
String? _getYoutubeUrl(String description) {
final exp = RegExp(r'(?:(?:https?|ftp):\/\/)?[\w/\-?=%.]+\.[\w/\-?=%.]+');
final matches = exp.allMatches(description);
for (var match in matches) {
final url = description.substring(match.start, match.end);
if (url.startsWith('https://youtu.be/') ||
url.startsWith('https://www.youtube.com/watch?') ||
url.startsWith('https://m.youtube.com/watch?')) {
return url;
}
}
return null;
}
/// [_buildDescription] builds the description widget for the item. If the
/// description contains a YouTube link, we render the [ItemYoutubeVideo]
/// and the [ItemDescription] widgets. If the description does not contain a
/// YouTube link, we only render the [ItemDescription] widget.
///
/// If the description containes a YouTube link we also have to disable the
/// rendering of images within the [ItemDescription] widget.
List<Widget> _buildDescription() {
final youtubeUrl =
item.description != null ? _getYoutubeUrl(item.description!) : null;
if (youtubeUrl != null) {
return [
ItemYoutubeVideo(
item.media,
youtubeUrl,
),
ItemDescription(
itemDescription: item.description,
sourceFormat: DescriptionFormat.html,
tagetFormat: DescriptionFormat.markdown,
disableImages: true,
),
];
}
return [
ItemDescription(
itemDescription: item.description,
sourceFormat: DescriptionFormat.html,
tagetFormat: DescriptionFormat.markdown,
),
];
}
@override
Widget build(BuildContext context) {
return Column(
@@ -29,11 +84,7 @@ class ItemDetailsReddit extends StatelessWidget {
item: item,
source: source,
),
ItemDescription(
itemDescription: item.description,
sourceFormat: DescriptionFormat.html,
tagetFormat: DescriptionFormat.markdown,
),
..._buildDescription(),
],
);
}