2023-04-22 12:45:06 +00:00
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
import 'package:file_picker/file_picker.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_file_dialog/flutter_file_dialog.dart';
|
|
|
|
import 'package:path/path.dart' as p;
|
|
|
|
import 'package:path_provider/path_provider.dart';
|
|
|
|
|
2023-04-26 19:58:33 +00:00
|
|
|
import '../controls/audio_video/av_control.dart';
|
2023-04-22 12:45:06 +00:00
|
|
|
import '../controls/login_aware_cached_network_image.dart';
|
|
|
|
import '../friendica_client/friendica_client.dart';
|
|
|
|
import '../globals.dart';
|
2023-04-25 13:18:39 +00:00
|
|
|
import '../models/attachment_media_type_enum.dart';
|
2023-04-22 12:45:06 +00:00
|
|
|
import '../models/media_attachment.dart';
|
|
|
|
import '../services/auth_service.dart';
|
|
|
|
import '../utils/clipboard_utils.dart';
|
|
|
|
import '../utils/snackbar_builder.dart';
|
|
|
|
|
|
|
|
class MediaViewerScreen extends StatefulWidget {
|
|
|
|
final List<MediaAttachment> attachments;
|
|
|
|
final int initialIndex;
|
|
|
|
|
|
|
|
const MediaViewerScreen({
|
|
|
|
super.key,
|
|
|
|
required this.attachments,
|
|
|
|
this.initialIndex = 0,
|
|
|
|
});
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<MediaViewerScreen> createState() => _MediaViewerScreenState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _MediaViewerScreenState extends State<MediaViewerScreen> {
|
|
|
|
var currentIndex = 0;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
currentIndex = widget.initialIndex;
|
|
|
|
}
|
|
|
|
|
2023-04-24 12:19:43 +00:00
|
|
|
void nextAttachment() {
|
|
|
|
if (currentIndex >= widget.attachments.length - 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setState(() {
|
|
|
|
currentIndex++;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void previousAttachment() {
|
|
|
|
if (currentIndex < 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setState(() {
|
|
|
|
currentIndex--;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-04-22 12:45:06 +00:00
|
|
|
Future<void> saveImage(
|
|
|
|
BuildContext context,
|
|
|
|
MediaAttachment attachment,
|
|
|
|
) async {
|
|
|
|
buildSnackbar(context, 'Downloading full image to save locally');
|
|
|
|
final appsDir = await getApplicationDocumentsDirectory();
|
|
|
|
final filename = p.basename(attachment.fullFileUri.path);
|
|
|
|
final bytesResult =
|
|
|
|
await RemoteFileClient(getIt<AccountsService>().currentProfile)
|
|
|
|
.getFileBytes(attachment.uri);
|
|
|
|
if (bytesResult.isFailure && mounted) {
|
|
|
|
buildSnackbar(context,
|
|
|
|
'Error getting full size version of file: ${bytesResult.error}');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Platform.isAndroid || Platform.isIOS) {
|
|
|
|
final params = SaveFileDialogParams(
|
|
|
|
data: bytesResult.value,
|
|
|
|
fileName: filename,
|
|
|
|
);
|
|
|
|
await FlutterFileDialog.saveFile(params: params);
|
|
|
|
} else {
|
|
|
|
final location = await FilePicker.platform.saveFile(
|
|
|
|
dialogTitle: 'Save Image',
|
|
|
|
fileName: filename,
|
|
|
|
initialDirectory: appsDir.path,
|
|
|
|
);
|
|
|
|
if (location != null) {
|
|
|
|
await File(location).writeAsBytes(bytesResult.value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
final currentAttachment = widget.attachments[currentIndex];
|
2023-04-24 12:19:43 +00:00
|
|
|
final width = MediaQuery.of(context).size.width;
|
|
|
|
final height = MediaQuery.of(context).size.height * 0.9;
|
|
|
|
final descriptionHeightPct = widget.attachments.isEmpty ? 0.0 : 0.1;
|
|
|
|
final mediaHeight = height * (1 - descriptionHeightPct);
|
|
|
|
final descriptionHeight = height * descriptionHeightPct;
|
2023-04-25 13:18:39 +00:00
|
|
|
|
|
|
|
late final bool canSave;
|
|
|
|
late final Widget mediaViewer;
|
|
|
|
switch (currentAttachment.explicitType) {
|
|
|
|
case AttachmentMediaType.image:
|
|
|
|
canSave = true;
|
|
|
|
mediaViewer = SizedBox(
|
|
|
|
width: width,
|
|
|
|
height: mediaHeight,
|
|
|
|
child: InteractiveViewer(
|
|
|
|
maxScale: 10.0,
|
|
|
|
scaleFactor: 400,
|
|
|
|
child: LoginAwareCachedNetworkImage(
|
|
|
|
imageUrl: currentAttachment.uri.toString()),
|
|
|
|
));
|
|
|
|
break;
|
|
|
|
case AttachmentMediaType.unknown:
|
|
|
|
case AttachmentMediaType.video:
|
|
|
|
canSave = false;
|
2023-04-26 19:58:33 +00:00
|
|
|
if (Platform.isLinux) {
|
|
|
|
mediaViewer = Text(
|
|
|
|
'No media viewer for ${currentAttachment.explicitType.name} type for link ${currentAttachment.fullFileUri}');
|
|
|
|
} else {
|
|
|
|
mediaViewer = SizedBox(
|
|
|
|
width: width,
|
|
|
|
height: mediaHeight,
|
|
|
|
child: AVControl(
|
|
|
|
videoUrl: currentAttachment.fullFileUri.toString(),
|
|
|
|
description: currentAttachment.description,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2023-04-25 13:18:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-04-22 12:45:06 +00:00
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
actions: [
|
2023-04-25 13:18:39 +00:00
|
|
|
if (canSave)
|
|
|
|
IconButton(
|
|
|
|
onPressed: () => saveImage(context, currentAttachment),
|
|
|
|
icon: const Icon(Icons.download))
|
2023-04-22 12:45:06 +00:00
|
|
|
],
|
|
|
|
),
|
|
|
|
body: SafeArea(
|
|
|
|
child: Stack(
|
|
|
|
children: [
|
|
|
|
Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.start,
|
|
|
|
children: [
|
2023-04-25 13:18:39 +00:00
|
|
|
mediaViewer,
|
2023-04-22 12:45:06 +00:00
|
|
|
if (currentAttachment.description.isNotEmpty)
|
2023-04-24 12:19:43 +00:00
|
|
|
buildTextArea(currentAttachment, descriptionHeight),
|
2023-04-22 12:45:06 +00:00
|
|
|
],
|
|
|
|
),
|
|
|
|
if (widget.attachments.length > 1) ...[
|
|
|
|
Positioned(
|
2023-04-24 12:19:43 +00:00
|
|
|
bottom: mediaHeight * 0.5,
|
2023-04-22 12:45:06 +00:00
|
|
|
child: Opacity(
|
|
|
|
opacity: 0.8,
|
|
|
|
child: currentIndex < 1
|
|
|
|
? null
|
|
|
|
: Container(
|
|
|
|
color: Colors.black,
|
|
|
|
child: IconButton(
|
|
|
|
color: Colors.white,
|
2023-04-24 12:19:43 +00:00
|
|
|
onPressed: previousAttachment,
|
2023-04-22 12:45:06 +00:00
|
|
|
icon: const Icon(Icons.chevron_left),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Positioned(
|
2023-04-24 12:19:43 +00:00
|
|
|
bottom: mediaHeight * 0.5,
|
2023-04-22 12:45:06 +00:00
|
|
|
right: 0.0,
|
|
|
|
child: Opacity(
|
|
|
|
opacity: 0.8,
|
|
|
|
child: currentIndex >= widget.attachments.length - 1
|
|
|
|
? null
|
|
|
|
: Container(
|
|
|
|
color: Colors.black,
|
|
|
|
child: IconButton(
|
|
|
|
color: Colors.white,
|
2023-04-24 12:19:43 +00:00
|
|
|
onPressed: nextAttachment,
|
2023-04-22 12:45:06 +00:00
|
|
|
icon: const Icon(Icons.chevron_right),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
]
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-04-24 12:19:43 +00:00
|
|
|
Widget buildTextArea(MediaAttachment attachment, double height) {
|
2023-04-22 12:45:06 +00:00
|
|
|
return Padding(
|
2023-04-24 12:19:43 +00:00
|
|
|
padding: const EdgeInsets.only(left: 8.0, right: 8.0),
|
2023-04-22 12:45:06 +00:00
|
|
|
child: SizedBox(
|
|
|
|
height: height,
|
|
|
|
child: attachment.description.isEmpty
|
|
|
|
? null
|
|
|
|
: Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
|
|
children: [
|
|
|
|
Expanded(
|
2023-04-24 12:19:43 +00:00
|
|
|
child: Scrollbar(
|
|
|
|
controller: ScrollController(),
|
|
|
|
thumbVisibility: true,
|
|
|
|
child: SingleChildScrollView(
|
|
|
|
child: Text(attachment.description),
|
|
|
|
),
|
2023-04-22 12:45:06 +00:00
|
|
|
)),
|
|
|
|
IconButton(
|
|
|
|
onPressed: () async {
|
|
|
|
await copyToClipboard(
|
|
|
|
context: context,
|
|
|
|
text: attachment.description,
|
|
|
|
message: 'Image description copied to clipboard');
|
|
|
|
},
|
|
|
|
icon: Icon(Icons.copy),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|