⚡ Optimized attachments
This commit is contained in:
parent
90daff5b97
commit
7d087af4cd
@ -33,102 +33,21 @@ class AttachmentItem extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _AttachmentItemState extends State<AttachmentItem> {
|
class _AttachmentItemState extends State<AttachmentItem> {
|
||||||
VideoPlayerController? _videoPlayerController;
|
|
||||||
ChewieController? _chewieController;
|
|
||||||
|
|
||||||
void ensureInitVideo() {
|
|
||||||
if (_videoPlayerController != null) return;
|
|
||||||
|
|
||||||
_videoPlayerController = VideoPlayerController.networkUrl(Uri.parse(
|
|
||||||
'${ServiceFinder.services['paperclip']}/api/attachments/${widget.item.id}',
|
|
||||||
));
|
|
||||||
_videoPlayerController!.initialize();
|
|
||||||
_chewieController = ChewieController(
|
|
||||||
aspectRatio: widget.item.metadata?['ratio'] ?? 16 / 9,
|
|
||||||
videoPlayerController: _videoPlayerController!,
|
|
||||||
customControls: PlatformInfo.isMobile
|
|
||||||
? const MaterialControls()
|
|
||||||
: const MaterialDesktopControls(),
|
|
||||||
materialProgressColors: ChewieProgressColors(
|
|
||||||
playedColor: Theme.of(context).colorScheme.primary,
|
|
||||||
handleColor: Theme.of(context).colorScheme.primary,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
switch (widget.item.mimetype.split('/').first) {
|
switch (widget.item.mimetype.split('/').first) {
|
||||||
case 'image':
|
case 'image':
|
||||||
return Hero(
|
return _AttachmentItemImage(
|
||||||
tag: Key('a${widget.item.uuid}p${widget.parentId}'),
|
parentId: widget.parentId,
|
||||||
child: Stack(
|
item: widget.item,
|
||||||
fit: StackFit.expand,
|
badge: widget.badge,
|
||||||
children: [
|
|
||||||
if (PlatformInfo.canCacheImage)
|
|
||||||
CachedNetworkImage(
|
|
||||||
fit: widget.fit,
|
fit: widget.fit,
|
||||||
imageUrl:
|
showBadge: widget.showBadge,
|
||||||
'${ServiceFinder.services['paperclip']}/api/attachments/${widget.item.id}',
|
showHideButton: widget.showHideButton,
|
||||||
progressIndicatorBuilder: (context, url, downloadProgress) =>
|
onHide: widget.onHide,
|
||||||
Center(
|
|
||||||
child: CircularProgressIndicator(
|
|
||||||
value: downloadProgress.progress,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
else
|
|
||||||
Image.network(
|
|
||||||
'${ServiceFinder.services['paperclip']}/api/attachments/${widget.item.id}',
|
|
||||||
fit: widget.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,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
},
|
|
||||||
),
|
|
||||||
if (widget.showBadge && widget.badge != null)
|
|
||||||
Positioned(
|
|
||||||
right: 12,
|
|
||||||
bottom: 8,
|
|
||||||
child: Material(
|
|
||||||
color: Colors.transparent,
|
|
||||||
child: Chip(label: Text(widget.badge!)),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
if (widget.showHideButton && widget.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 (widget.onHide != null) widget.onHide!();
|
|
||||||
},
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
case 'video':
|
case 'video':
|
||||||
ensureInitVideo();
|
return _AttachmentItemVideo(item: widget.item);
|
||||||
return Chewie(controller: _chewieController!);
|
|
||||||
default:
|
default:
|
||||||
return Center(
|
return Center(
|
||||||
child: Container(
|
child: Container(
|
||||||
@ -173,6 +92,136 @@ class _AttachmentItemState extends State<AttachmentItem> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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({
|
||||||
|
super.key,
|
||||||
|
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}'),
|
||||||
|
child: Stack(
|
||||||
|
fit: StackFit.expand,
|
||||||
|
children: [
|
||||||
|
if (PlatformInfo.canCacheImage)
|
||||||
|
CachedNetworkImage(
|
||||||
|
fit: fit,
|
||||||
|
imageUrl:
|
||||||
|
'${ServiceFinder.services['paperclip']}/api/attachments/${item.id}',
|
||||||
|
progressIndicatorBuilder: (context, url, downloadProgress) =>
|
||||||
|
Center(
|
||||||
|
child: CircularProgressIndicator(
|
||||||
|
value: downloadProgress.progress,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
else
|
||||||
|
Image.network(
|
||||||
|
'${ServiceFinder.services['paperclip']}/api/attachments/${item.id}',
|
||||||
|
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,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
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;
|
||||||
|
|
||||||
|
const _AttachmentItemVideo({super.key, required this.item});
|
||||||
|
|
||||||
|
@override
|
||||||
|
State<_AttachmentItemVideo> createState() => _AttachmentItemVideoState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _AttachmentItemVideoState extends State<_AttachmentItemVideo> {
|
||||||
|
VideoPlayerController? _videoPlayerController;
|
||||||
|
ChewieController? _chewieController;
|
||||||
|
|
||||||
|
void ensureInitVideo() {
|
||||||
|
if (_videoPlayerController != null) return;
|
||||||
|
_videoPlayerController = VideoPlayerController.networkUrl(Uri.parse(
|
||||||
|
'${ServiceFinder.services['paperclip']}/api/attachments/${widget.item.id}',
|
||||||
|
));
|
||||||
|
_videoPlayerController!.initialize();
|
||||||
|
_chewieController = ChewieController(
|
||||||
|
aspectRatio: widget.item.metadata?['ratio'] ?? 16 / 9,
|
||||||
|
videoPlayerController: _videoPlayerController!,
|
||||||
|
customControls: PlatformInfo.isMobile
|
||||||
|
? const MaterialControls()
|
||||||
|
: const MaterialDesktopControls(),
|
||||||
|
materialProgressColors: ChewieProgressColors(
|
||||||
|
playedColor: Theme.of(context).colorScheme.primary,
|
||||||
|
handleColor: Theme.of(context).colorScheme.primary,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
ensureInitVideo();
|
||||||
|
return Chewie(controller: _chewieController!);
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
|
Loading…
Reference in New Issue
Block a user