Solian/lib/widgets/attachments/attachment_item.dart

321 lines
9.3 KiB
Dart
Raw Normal View History

2024-06-01 13:39:28 +00:00
import 'package:cached_network_image/cached_network_image.dart';
2024-05-18 14:23:36 +00:00
import 'package:flutter/material.dart';
import 'package:flutter_animate/flutter_animate.dart';
2024-05-20 15:39:23 +00:00
import 'package:get/get.dart';
2024-08-19 11:36:01 +00:00
import 'package:media_kit/media_kit.dart';
import 'package:media_kit_video/media_kit_video.dart';
2024-05-18 14:23:36 +00:00
import 'package:solian/models/attachment.dart';
2024-06-01 13:39:28 +00:00
import 'package:solian/platform.dart';
2024-05-18 14:23:36 +00:00
import 'package:solian/services.dart';
import 'package:solian/widgets/sized_container.dart';
2024-05-27 15:07:01 +00:00
import 'package:url_launcher/url_launcher_string.dart';
2024-05-18 14:23:36 +00:00
2024-05-27 15:07:01 +00:00
class AttachmentItem extends StatefulWidget {
2024-05-25 16:11:00 +00:00
final String parentId;
2024-05-18 14:23:36 +00:00
final Attachment item;
2024-05-20 16:02:39 +00:00
final bool showBadge;
final bool showHideButton;
2024-08-16 13:06:50 +00:00
final bool autoload;
2024-05-20 16:02:39 +00:00
final BoxFit fit;
2024-05-20 15:39:23 +00:00
final String? badge;
2024-05-20 16:02:39 +00:00
final Function? onHide;
2024-05-18 14:23:36 +00:00
2024-05-20 15:39:23 +00:00
const AttachmentItem({
super.key,
2024-05-25 16:11:00 +00:00
required this.parentId,
2024-05-20 15:39:23 +00:00
required this.item,
this.badge,
2024-05-20 16:02:39 +00:00
this.fit = BoxFit.cover,
this.showBadge = true,
this.showHideButton = true,
2024-08-16 13:06:50 +00:00
this.autoload = false,
2024-05-20 16:02:39 +00:00
this.onHide,
2024-05-20 15:39:23 +00:00
});
2024-05-18 14:23:36 +00:00
2024-05-27 15:07:01 +00:00
@override
State<AttachmentItem> createState() => _AttachmentItemState();
}
class _AttachmentItemState extends State<AttachmentItem> {
2024-05-18 14:23:36 +00:00
@override
Widget build(BuildContext context) {
2024-05-27 15:07:01 +00:00
switch (widget.item.mimetype.split('/').first) {
case 'image':
2024-07-06 10:35:43 +00:00
return _AttachmentItemImage(
parentId: widget.parentId,
item: widget.item,
badge: widget.badge,
fit: widget.fit,
showBadge: widget.showBadge,
showHideButton: widget.showHideButton,
onHide: widget.onHide,
2024-05-27 15:07:01 +00:00
);
case 'video':
2024-08-16 13:06:50 +00:00
return _AttachmentItemVideo(
item: widget.item,
autoload: widget.autoload,
);
2024-05-27 15:07:01 +00:00
default:
return Center(
child: Container(
constraints: const BoxConstraints(
maxWidth: 280,
2024-05-20 15:39:23 +00:00
),
2024-05-27 15:07:01 +00:00
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Icon(Icons.file_present, size: 32),
const SizedBox(height: 6),
Text(
widget.item.mimetype,
style: TextStyle(
fontSize: 12,
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
textAlign: TextAlign.center,
),
const SizedBox(height: 2),
Text(
widget.item.alt,
style: const TextStyle(fontSize: 13),
textAlign: TextAlign.center,
),
const SizedBox(height: 12),
TextButton.icon(
icon: const Icon(Icons.launch),
label: Text('openInBrowser'.tr),
style: const ButtonStyle(
visualDensity: VisualDensity(vertical: -2, horizontal: -4),
),
2024-05-20 16:02:39 +00:00
onPressed: () {
2024-05-27 15:07:01 +00:00
launchUrlString(
ServiceFinder.buildUrl(
'files',
'/attachments/${widget.item.rid}',
),
2024-05-27 15:07:01 +00:00
);
2024-05-20 16:02:39 +00:00
},
2024-05-20 15:39:23 +00:00
),
2024-05-27 15:07:01 +00:00
],
2024-05-20 15:39:23 +00:00
),
2024-05-27 15:07:01 +00:00
),
);
}
}
2024-07-06 10:35:43 +00:00
}
class _AttachmentItemImage extends StatelessWidget {
final String parentId;
final Attachment item;
final bool showBadge;
final bool showHideButton;
final BoxFit fit;
final String? badge;
final Function? onHide;
const _AttachmentItemImage({
required this.parentId,
required this.item,
required this.showBadge,
required this.showHideButton,
required this.fit,
this.badge,
this.onHide,
});
@override
Widget build(BuildContext context) {
return Hero(
tag: Key('a${item.uuid}p$parentId'),
2024-07-06 10:35:43 +00:00
child: Stack(
fit: StackFit.expand,
children: [
if (PlatformInfo.canCacheImage)
CachedNetworkImage(
fit: fit,
imageUrl: ServiceFinder.buildUrl(
'files',
'/attachments/${item.rid}',
2024-07-06 10:35:43 +00:00
),
progressIndicatorBuilder: (context, url, downloadProgress) {
return Center(
child: CircularProgressIndicator(
value: downloadProgress.progress,
),
);
},
errorWidget: (context, url, error) {
return Material(
color: Theme.of(context).colorScheme.surface,
2024-08-24 03:47:40 +00:00
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(Icons.close, size: 32)
.animate(onPlay: (e) => e.repeat(reverse: true))
.fade(duration: 500.ms),
Text(error.toString()),
],
),
);
},
2024-07-06 10:35:43 +00:00
)
else
Image.network(
2024-08-24 03:47:40 +00:00
ServiceFinder.buildUrl('files', '/attachments/${item.rid}'),
2024-07-06 10:35:43 +00:00
fit: fit,
loadingBuilder: (BuildContext context, Widget child,
ImageChunkEvent? loadingProgress) {
if (loadingProgress == null) return child;
return Center(
child: CircularProgressIndicator(
value: loadingProgress.expectedTotalBytes != null
? loadingProgress.cumulativeBytesLoaded /
loadingProgress.expectedTotalBytes!
: null,
),
);
},
errorBuilder: (context, error, stackTrace) {
return Material(
color: Theme.of(context).colorScheme.surface,
2024-08-24 03:47:40 +00:00
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(Icons.close, size: 32)
.animate(onPlay: (e) => e.repeat(reverse: true))
.fade(duration: 500.ms),
Text(error.toString()),
],
),
);
},
2024-07-06 10:35:43 +00:00
),
if (showBadge && badge != null)
Positioned(
right: 12,
bottom: 8,
child: Material(
color: Colors.transparent,
child: Chip(label: Text(badge!)),
),
),
if (showHideButton && item.isMature)
Positioned(
top: 8,
left: 12,
child: Material(
color: Colors.transparent,
child: ActionChip(
visualDensity:
const VisualDensity(vertical: -4, horizontal: -4),
avatar: Icon(
Icons.visibility_off,
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
label: Text('hide'.tr),
onPressed: () {
if (onHide != null) onHide!();
},
),
),
),
],
),
);
}
}
class _AttachmentItemVideo extends StatefulWidget {
final Attachment item;
final bool autoload;
2024-07-06 10:35:43 +00:00
const _AttachmentItemVideo({
required this.item,
this.autoload = false,
});
2024-07-06 10:35:43 +00:00
@override
State<_AttachmentItemVideo> createState() => _AttachmentItemVideoState();
}
class _AttachmentItemVideoState extends State<_AttachmentItemVideo> {
2024-08-19 11:36:01 +00:00
late final _player = Player(
configuration: const PlayerConfiguration(
logLevel: MPVLogLevel.error,
),
);
late final _controller = VideoController(_player);
2024-07-06 10:35:43 +00:00
bool _showContent = false;
2024-08-17 10:44:20 +00:00
Future<void> _startLoad() async {
2024-08-19 11:36:01 +00:00
await _player.open(
Media(ServiceFinder.buildUrl('files', '/attachments/${widget.item.rid}')),
play: false,
2024-07-06 10:35:43 +00:00
);
setState(() => _showContent = true);
}
@override
void initState() {
super.initState();
2024-08-17 10:44:20 +00:00
if (widget.autoload) {
_startLoad();
}
2024-07-06 10:35:43 +00:00
}
@override
Widget build(BuildContext context) {
final ratio = widget.item.metadata?['ratio'] ?? 16 / 9;
2024-08-19 11:36:01 +00:00
if (!_showContent) {
return GestureDetector(
child: AspectRatio(
aspectRatio: ratio,
child: CenteredContainer(
maxWidth: 280,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(
Icons.not_started,
color: Colors.white,
size: 32,
),
const SizedBox(height: 8),
Text(
'attachmentUnload'.tr,
style: const TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 16,
),
),
Text(
'attachmentUnloadCaption'.tr,
style: const TextStyle(color: Colors.white),
textAlign: TextAlign.center,
),
],
),
),
),
onTap: () {
_startLoad();
},
);
}
2024-08-19 11:36:01 +00:00
return Video(
aspectRatio: ratio,
controller: _controller,
2024-07-06 11:07:46 +00:00
);
2024-07-06 10:35:43 +00:00
}
2024-08-19 11:36:01 +00:00
@override
void dispose() {
_player.dispose();
super.dispose();
}
2024-05-20 15:39:23 +00:00
}