🎨 Continued to rearrange core folders content
This commit is contained in:
122
lib/embeds/embed_list.dart
Normal file
122
lib/embeds/embed_list.dart
Normal file
@@ -0,0 +1,122 @@
|
||||
import 'package:easy_localization/easy_localization.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:island/polls/polls_widgets/poll/poll_submit.dart';
|
||||
import 'package:island/posts/posts_models/embed.dart';
|
||||
import 'package:island/embeds/link.dart';
|
||||
import 'package:island/wallets/wallet_widgets/wallet/fund_envelope.dart';
|
||||
import 'package:material_symbols_icons/material_symbols_icons.dart';
|
||||
|
||||
class EmbedListWidget extends StatelessWidget {
|
||||
final List<dynamic> embeds;
|
||||
final bool isInteractive;
|
||||
final bool isFullPost;
|
||||
final EdgeInsets renderingPadding;
|
||||
final double? maxWidth;
|
||||
|
||||
const EmbedListWidget({
|
||||
super.key,
|
||||
required this.embeds,
|
||||
this.isInteractive = true,
|
||||
this.isFullPost = false,
|
||||
this.renderingPadding = EdgeInsets.zero,
|
||||
this.maxWidth,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final linkEmbeds = embeds.where((e) => e['type'] == 'link').toList();
|
||||
final otherEmbeds = embeds.where((e) => e['type'] != 'link').toList();
|
||||
|
||||
return Column(
|
||||
children: [
|
||||
if (linkEmbeds.isNotEmpty)
|
||||
Container(
|
||||
margin: EdgeInsets.only(
|
||||
top: 8,
|
||||
left: renderingPadding.horizontal,
|
||||
right: renderingPadding.horizontal,
|
||||
),
|
||||
decoration: BoxDecoration(
|
||||
border: Border.all(
|
||||
color: Theme.of(context).dividerColor.withOpacity(0.5),
|
||||
),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Theme(
|
||||
data: Theme.of(
|
||||
context,
|
||||
).copyWith(dividerColor: Colors.transparent),
|
||||
child: ExpansionTile(
|
||||
initiallyExpanded: true,
|
||||
dense: true,
|
||||
leading: const Icon(Symbols.link),
|
||||
title: Text('embedLinks'.plural(linkEmbeds.length)),
|
||||
children: [
|
||||
Container(
|
||||
width: double.infinity,
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8),
|
||||
child: linkEmbeds.length == 1
|
||||
? EmbedLinkWidget(
|
||||
link: SnScrappedLink.fromJson(linkEmbeds.first),
|
||||
)
|
||||
: SingleChildScrollView(
|
||||
scrollDirection: Axis.horizontal,
|
||||
child: Row(
|
||||
children: linkEmbeds
|
||||
.map(
|
||||
(embedData) => EmbedLinkWidget(
|
||||
link: SnScrappedLink.fromJson(embedData),
|
||||
maxWidth:
|
||||
200, // Fixed width for horizontal scroll
|
||||
margin: const EdgeInsets.symmetric(
|
||||
horizontal: 4,
|
||||
),
|
||||
),
|
||||
)
|
||||
.toList(),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
...otherEmbeds.map(
|
||||
(embedData) => switch (embedData['type']) {
|
||||
'poll' => Card(
|
||||
margin: EdgeInsets.symmetric(
|
||||
horizontal: renderingPadding.horizontal,
|
||||
vertical: 8,
|
||||
),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 16,
|
||||
vertical: 12,
|
||||
),
|
||||
child: embedData['id'] == null
|
||||
? const Text('Poll was unavailable...')
|
||||
: PollSubmit(
|
||||
pollId: embedData['id'],
|
||||
onSubmit: (_) {},
|
||||
isReadonly: !isInteractive,
|
||||
isInitiallyExpanded: isFullPost,
|
||||
),
|
||||
),
|
||||
),
|
||||
'fund' =>
|
||||
embedData['id'] == null
|
||||
? const Text('Fund envelope was unavailable...')
|
||||
: FundEnvelopeWidget(
|
||||
fundId: embedData['id'],
|
||||
margin: EdgeInsets.symmetric(
|
||||
horizontal: renderingPadding.horizontal,
|
||||
vertical: 8,
|
||||
),
|
||||
),
|
||||
_ => Text('Unable show embed: ${embedData['type']}'),
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
313
lib/embeds/link.dart
Normal file
313
lib/embeds/link.dart
Normal file
@@ -0,0 +1,313 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:cached_network_image/cached_network_image.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:gap/gap.dart';
|
||||
import 'package:island/posts/posts_models/embed.dart';
|
||||
import 'package:island/drive/content/image.dart';
|
||||
import 'package:material_symbols_icons/symbols.dart';
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
|
||||
class EmbedLinkWidget extends StatefulWidget {
|
||||
final SnScrappedLink link;
|
||||
final double? maxWidth;
|
||||
final EdgeInsetsGeometry? margin;
|
||||
|
||||
const EmbedLinkWidget({
|
||||
super.key,
|
||||
required this.link,
|
||||
this.maxWidth,
|
||||
this.margin,
|
||||
});
|
||||
|
||||
@override
|
||||
State<EmbedLinkWidget> createState() => _EmbedLinkWidgetState();
|
||||
}
|
||||
|
||||
class _EmbedLinkWidgetState extends State<EmbedLinkWidget> {
|
||||
bool? _isSquare;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_checkIfSquare();
|
||||
}
|
||||
|
||||
Future<void> _checkIfSquare() async {
|
||||
if (widget.link.imageUrl == null ||
|
||||
widget.link.imageUrl!.isEmpty ||
|
||||
widget.link.imageUrl == widget.link.faviconUrl) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
final image = CachedNetworkImageProvider(widget.link.imageUrl!);
|
||||
final ImageStream stream = image.resolve(ImageConfiguration.empty);
|
||||
final completer = Completer<ImageInfo>();
|
||||
final listener = ImageStreamListener((
|
||||
ImageInfo info,
|
||||
bool synchronousCall,
|
||||
) {
|
||||
completer.complete(info);
|
||||
});
|
||||
stream.addListener(listener);
|
||||
final info = await completer.future;
|
||||
stream.removeListener(listener);
|
||||
|
||||
final aspectRatio = info.image.width / info.image.height;
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_isSquare = aspectRatio >= 0.9 && aspectRatio <= 1.1;
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
// If error, assume not square
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_isSquare = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _launchUrl() async {
|
||||
final uri = Uri.parse(widget.link.url);
|
||||
if (await canLaunchUrl(uri)) {
|
||||
await launchUrl(uri, mode: LaunchMode.externalApplication);
|
||||
}
|
||||
}
|
||||
|
||||
String _getBaseUrl(String url) {
|
||||
final uri = Uri.parse(url);
|
||||
final port = uri.port;
|
||||
final defaultPort = uri.scheme == 'https' ? 443 : 80;
|
||||
final portString = port != defaultPort ? ':$port' : '';
|
||||
return '${uri.scheme}://${uri.host}$portString';
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
final colorScheme = theme.colorScheme;
|
||||
|
||||
return Container(
|
||||
width: widget.maxWidth,
|
||||
margin: widget.margin ?? const EdgeInsets.symmetric(vertical: 8),
|
||||
child: Card(
|
||||
margin: EdgeInsets.zero,
|
||||
clipBehavior: Clip.antiAlias,
|
||||
child: InkWell(
|
||||
onTap: _launchUrl,
|
||||
child: Row(
|
||||
children: [
|
||||
// Sqaure open graph image
|
||||
if (_isSquare == true) ...[
|
||||
ConstrainedBox(
|
||||
constraints: const BoxConstraints(maxHeight: 120),
|
||||
child: AspectRatio(
|
||||
aspectRatio: 1,
|
||||
child: UniversalImage(
|
||||
uri: widget.link.imageUrl!,
|
||||
fit: BoxFit.cover,
|
||||
),
|
||||
),
|
||||
),
|
||||
const Gap(8),
|
||||
],
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// Preview Image
|
||||
if (widget.link.imageUrl != null &&
|
||||
widget.link.imageUrl!.isNotEmpty &&
|
||||
widget.link.imageUrl != widget.link.faviconUrl &&
|
||||
_isSquare != true)
|
||||
Container(
|
||||
width: double.infinity,
|
||||
color: Theme.of(
|
||||
context,
|
||||
).colorScheme.surfaceContainerHigh,
|
||||
child: IntrinsicHeight(
|
||||
child: UniversalImage(
|
||||
uri: widget.link.imageUrl!,
|
||||
fit: BoxFit.cover,
|
||||
useFallbackImage: false,
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
// Content
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// Site info row
|
||||
Row(
|
||||
children: [
|
||||
if (widget.link.faviconUrl?.isNotEmpty ??
|
||||
false) ...[
|
||||
ClipRRect(
|
||||
borderRadius: BorderRadius.circular(4),
|
||||
child: UniversalImage(
|
||||
uri:
|
||||
widget.link.faviconUrl!.startsWith('//')
|
||||
? 'https:${widget.link.faviconUrl!}'
|
||||
: widget.link.faviconUrl!.startsWith(
|
||||
'/',
|
||||
)
|
||||
? _getBaseUrl(widget.link.url) +
|
||||
widget.link.faviconUrl!
|
||||
: widget.link.faviconUrl!,
|
||||
width: 16,
|
||||
height: 16,
|
||||
fit: BoxFit.cover,
|
||||
useFallbackImage: false,
|
||||
),
|
||||
),
|
||||
const Gap(8),
|
||||
] else ...[
|
||||
Icon(
|
||||
Symbols.link,
|
||||
size: 16,
|
||||
color: colorScheme.onSurfaceVariant,
|
||||
),
|
||||
const Gap(8),
|
||||
],
|
||||
|
||||
// Site name
|
||||
Expanded(
|
||||
child: Text(
|
||||
(widget.link.siteName?.isNotEmpty ?? false)
|
||||
? widget.link.siteName!
|
||||
: Uri.parse(widget.link.url).host,
|
||||
style: theme.textTheme.bodySmall?.copyWith(
|
||||
color: colorScheme.onSurfaceVariant,
|
||||
),
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
),
|
||||
|
||||
// External link icon
|
||||
Icon(
|
||||
Symbols.open_in_new,
|
||||
size: 16,
|
||||
color: colorScheme.onSurfaceVariant,
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
const Gap(8),
|
||||
|
||||
// Title
|
||||
if (widget.link.title?.isNotEmpty ?? false) ...[
|
||||
Text(
|
||||
widget.link.title!,
|
||||
style: theme.textTheme.titleMedium?.copyWith(
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
maxLines: _isSquare == true ? 1 : 2,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
Gap(_isSquare == true ? 2 : 4),
|
||||
],
|
||||
|
||||
// Description
|
||||
if (widget.link.description != null &&
|
||||
widget.link.description!.isNotEmpty) ...[
|
||||
Text(
|
||||
widget.link.description!,
|
||||
style: theme.textTheme.bodyMedium?.copyWith(
|
||||
color: colorScheme.onSurfaceVariant,
|
||||
),
|
||||
maxLines: _isSquare == true ? 1 : 3,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
Gap(_isSquare == true ? 4 : 8),
|
||||
],
|
||||
|
||||
// URL
|
||||
Text(
|
||||
widget.link.url,
|
||||
style: theme.textTheme.bodySmall?.copyWith(
|
||||
color: colorScheme.primary,
|
||||
decoration: TextDecoration.underline,
|
||||
),
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
|
||||
// Author and publish date
|
||||
if (widget.link.author != null ||
|
||||
widget.link.publishedDate != null) ...[
|
||||
const Gap(8),
|
||||
Row(
|
||||
children: [
|
||||
if (widget.link.author != null) ...[
|
||||
Icon(
|
||||
Symbols.person,
|
||||
size: 14,
|
||||
color: colorScheme.onSurfaceVariant,
|
||||
),
|
||||
const Gap(4),
|
||||
Text(
|
||||
widget.link.author!,
|
||||
style: theme.textTheme.bodySmall?.copyWith(
|
||||
color: colorScheme.onSurfaceVariant,
|
||||
),
|
||||
),
|
||||
],
|
||||
if (widget.link.author != null &&
|
||||
widget.link.publishedDate != null)
|
||||
const Gap(16),
|
||||
if (widget.link.publishedDate != null) ...[
|
||||
Icon(
|
||||
Symbols.schedule,
|
||||
size: 14,
|
||||
color: colorScheme.onSurfaceVariant,
|
||||
),
|
||||
const Gap(4),
|
||||
Text(
|
||||
_formatDate(widget.link.publishedDate!),
|
||||
style: theme.textTheme.bodySmall?.copyWith(
|
||||
color: colorScheme.onSurfaceVariant,
|
||||
),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
String _formatDate(DateTime date) {
|
||||
try {
|
||||
final now = DateTime.now();
|
||||
final difference = now.difference(date);
|
||||
|
||||
if (difference.inDays == 0) {
|
||||
return 'Today';
|
||||
} else if (difference.inDays == 1) {
|
||||
return 'Yesterday';
|
||||
} else if (difference.inDays < 7) {
|
||||
return '${difference.inDays} days ago';
|
||||
} else {
|
||||
return '${date.day}/${date.month}/${date.year}';
|
||||
}
|
||||
} catch (e) {
|
||||
return date.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user