2024-12-07 16:25:53 +00:00
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
import 'package:dio/dio.dart';
|
2024-11-10 09:21:57 +00:00
|
|
|
import 'package:dismissible_page/dismissible_page.dart';
|
2024-12-01 15:56:56 +00:00
|
|
|
import 'package:easy_localization/easy_localization.dart';
|
2024-12-13 16:14:23 +00:00
|
|
|
import 'package:file_saver/file_saver.dart';
|
2024-12-07 16:25:53 +00:00
|
|
|
import 'package:flutter/foundation.dart';
|
2024-11-10 09:21:57 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2024-12-07 16:25:53 +00:00
|
|
|
import 'package:gal/gal.dart';
|
2024-12-01 15:56:56 +00:00
|
|
|
import 'package:gap/gap.dart';
|
|
|
|
import 'package:google_fonts/google_fonts.dart';
|
2024-12-07 16:25:53 +00:00
|
|
|
import 'package:material_symbols_icons/symbols.dart';
|
|
|
|
import 'package:path/path.dart' show extension;
|
2024-11-10 09:21:57 +00:00
|
|
|
import 'package:photo_view/photo_view.dart';
|
2024-11-25 14:41:15 +00:00
|
|
|
import 'package:photo_view/photo_view_gallery.dart';
|
2024-11-10 09:21:57 +00:00
|
|
|
import 'package:provider/provider.dart';
|
2024-12-01 15:56:56 +00:00
|
|
|
import 'package:styled_widget/styled_widget.dart';
|
2024-11-10 09:21:57 +00:00
|
|
|
import 'package:surface/providers/sn_network.dart';
|
2024-12-01 15:56:56 +00:00
|
|
|
import 'package:surface/providers/user_directory.dart';
|
2024-11-10 09:21:57 +00:00
|
|
|
import 'package:surface/types/attachment.dart';
|
2024-12-01 15:56:56 +00:00
|
|
|
import 'package:surface/widgets/account/account_image.dart';
|
2024-12-07 16:25:53 +00:00
|
|
|
import 'package:surface/widgets/dialog.dart';
|
2024-11-10 09:21:57 +00:00
|
|
|
import 'package:surface/widgets/universal_image.dart';
|
2024-12-07 16:25:53 +00:00
|
|
|
import 'package:url_launcher/url_launcher_string.dart';
|
2024-11-10 09:21:57 +00:00
|
|
|
import 'package:uuid/uuid.dart';
|
|
|
|
|
2024-11-25 14:41:15 +00:00
|
|
|
class AttachmentZoomView extends StatefulWidget {
|
|
|
|
final Iterable<SnAttachment> data;
|
|
|
|
final int? initialIndex;
|
|
|
|
final List<String?>? heroTags;
|
2024-12-07 16:25:53 +00:00
|
|
|
|
2024-11-25 14:41:15 +00:00
|
|
|
const AttachmentZoomView({
|
|
|
|
super.key,
|
|
|
|
required this.data,
|
|
|
|
this.initialIndex,
|
|
|
|
this.heroTags,
|
|
|
|
});
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<AttachmentZoomView> createState() => _AttachmentZoomViewState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _AttachmentZoomViewState extends State<AttachmentZoomView> {
|
2024-12-07 16:25:53 +00:00
|
|
|
late final PageController _pageController = PageController(initialPage: widget.initialIndex ?? 0);
|
2024-11-25 14:41:15 +00:00
|
|
|
|
2024-12-01 15:56:56 +00:00
|
|
|
void _updatePage() {
|
2024-12-07 16:25:53 +00:00
|
|
|
setState(() {
|
|
|
|
if (_isCompletedDownload) {
|
|
|
|
setState(() => _isCompletedDownload = false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool _isDownloading = false;
|
|
|
|
bool _isCompletedDownload = false;
|
|
|
|
double? _progressOfDownload = 0;
|
|
|
|
|
|
|
|
Future<void> _saveToAlbum(int idx) async {
|
|
|
|
final sn = context.read<SnNetworkProvider>();
|
|
|
|
final item = widget.data.elementAt(idx);
|
|
|
|
final url = sn.getAttachmentUrl(item.rid);
|
|
|
|
|
|
|
|
if (kIsWeb || Platform.isLinux) {
|
|
|
|
await launchUrlString(url);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setState(() => _isDownloading = true);
|
|
|
|
|
|
|
|
var extName = extension(item.name);
|
|
|
|
if (extName.isEmpty) extName = '.png';
|
|
|
|
final imagePath = '${Directory.systemTemp.path}/${item.uuid}$extName';
|
|
|
|
await Dio().download(
|
|
|
|
url,
|
|
|
|
imagePath,
|
|
|
|
onReceiveProgress: (count, total) {
|
|
|
|
setState(() => _progressOfDownload = count / total);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
bool isSuccess = false;
|
|
|
|
try {
|
2024-12-13 16:14:23 +00:00
|
|
|
if (!kIsWeb && (Platform.isAndroid || Platform.isIOS)) {
|
2024-12-13 17:32:13 +00:00
|
|
|
if (!await Gal.hasAccess(toAlbum: true)) {
|
|
|
|
if (!await Gal.requestAccess(toAlbum: true)) return;
|
|
|
|
}
|
2024-12-13 16:14:23 +00:00
|
|
|
await Gal.putImage(imagePath, album: 'Solar Network');
|
|
|
|
} else {
|
|
|
|
await FileSaver.instance.saveFile(
|
|
|
|
name: item.name,
|
|
|
|
file: File(imagePath),
|
|
|
|
);
|
|
|
|
}
|
2024-12-07 16:25:53 +00:00
|
|
|
setState(() {
|
|
|
|
isSuccess = true;
|
|
|
|
_isDownloading = false;
|
|
|
|
_isCompletedDownload = isSuccess;
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
if (!mounted) return;
|
|
|
|
context.showErrorDialog(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mounted) return;
|
|
|
|
context.showSnackbar(
|
2024-12-13 17:32:13 +00:00
|
|
|
(!kIsWeb && (Platform.isIOS || Platform.isAndroid)) ? 'attachmentSaved'.tr() : 'attachmentSavedDesktop'.tr(),
|
|
|
|
action: (!kIsWeb && (Platform.isIOS || Platform.isAndroid))
|
|
|
|
? SnackBarAction(
|
|
|
|
label: 'openInAlbum'.tr(),
|
|
|
|
onPressed: () async => Gal.open(),
|
|
|
|
)
|
|
|
|
: null,
|
2024-12-07 16:25:53 +00:00
|
|
|
);
|
2024-12-01 15:56:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
_pageController.addListener(_updatePage);
|
2024-12-01 16:40:27 +00:00
|
|
|
Future.delayed(const Duration(milliseconds: 100), _updatePage);
|
2024-12-01 15:56:56 +00:00
|
|
|
}
|
|
|
|
|
2024-11-25 14:41:15 +00:00
|
|
|
@override
|
|
|
|
void dispose() {
|
2024-12-01 15:56:56 +00:00
|
|
|
_pageController.removeListener(_updatePage);
|
2024-11-25 14:41:15 +00:00
|
|
|
_pageController.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
2024-11-10 09:21:57 +00:00
|
|
|
|
2024-12-07 16:25:53 +00:00
|
|
|
Color get _unFocusColor => Theme.of(context).colorScheme.onSurface.withOpacity(0.75);
|
2024-12-01 15:56:56 +00:00
|
|
|
|
2024-11-10 09:21:57 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
final sn = context.read<SnNetworkProvider>();
|
|
|
|
final uuid = Uuid();
|
|
|
|
|
2024-12-01 15:56:56 +00:00
|
|
|
final metaTextStyle = GoogleFonts.roboto(
|
|
|
|
fontSize: 12,
|
|
|
|
color: _unFocusColor,
|
|
|
|
height: 1,
|
|
|
|
);
|
|
|
|
|
2024-11-10 09:21:57 +00:00
|
|
|
return DismissiblePage(
|
|
|
|
onDismissed: () {
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
},
|
|
|
|
direction: DismissiblePageDismissDirection.down,
|
|
|
|
backgroundColor: Colors.transparent,
|
|
|
|
isFullScreen: true,
|
2024-12-07 16:25:53 +00:00
|
|
|
child: Scaffold(
|
|
|
|
body: Stack(
|
|
|
|
children: [
|
|
|
|
Builder(builder: (context) {
|
|
|
|
if (widget.data.length == 1) {
|
|
|
|
final heroTag = widget.heroTags?.first ?? uuid.v4();
|
|
|
|
return Hero(
|
|
|
|
tag: 'attachment-${widget.data.first.rid}-$heroTag',
|
|
|
|
child: PhotoView(
|
|
|
|
key: Key('attachment-detail-${widget.data.first.rid}-$heroTag'),
|
|
|
|
backgroundDecoration: BoxDecoration(color: Colors.transparent),
|
|
|
|
imageProvider: UniversalImage.provider(
|
|
|
|
sn.getAttachmentUrl(widget.data.first.rid),
|
|
|
|
),
|
2024-12-01 15:56:56 +00:00
|
|
|
),
|
|
|
|
);
|
2024-12-07 16:25:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return PhotoViewGallery.builder(
|
|
|
|
pageController: _pageController,
|
|
|
|
scrollPhysics: const BouncingScrollPhysics(),
|
|
|
|
builder: (context, idx) {
|
|
|
|
final heroTag = widget.heroTags?.elementAt(idx) ?? uuid.v4();
|
|
|
|
return PhotoViewGalleryPageOptions(
|
|
|
|
imageProvider: UniversalImage.provider(
|
|
|
|
sn.getAttachmentUrl(widget.data.elementAt(idx).rid),
|
|
|
|
),
|
|
|
|
heroAttributes: PhotoViewHeroAttributes(
|
|
|
|
tag: 'attachment-${widget.data.first.rid}-$heroTag',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
itemCount: widget.data.length,
|
|
|
|
loadingBuilder: (context, event) => Center(
|
|
|
|
child: SizedBox(
|
|
|
|
width: 20.0,
|
|
|
|
height: 20.0,
|
|
|
|
child: CircularProgressIndicator(
|
|
|
|
value: event == null ? 0 : event.cumulativeBytesLoaded / (event.expectedTotalBytes ?? 1),
|
|
|
|
),
|
2024-12-01 15:56:56 +00:00
|
|
|
),
|
|
|
|
),
|
2024-12-07 16:25:53 +00:00
|
|
|
backgroundDecoration: BoxDecoration(color: Colors.transparent),
|
|
|
|
);
|
|
|
|
}),
|
|
|
|
Align(
|
|
|
|
alignment: Alignment.bottomCenter,
|
|
|
|
child: IgnorePointer(
|
|
|
|
child: Container(
|
|
|
|
height: 300,
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
gradient: LinearGradient(
|
|
|
|
begin: Alignment.bottomCenter,
|
|
|
|
end: Alignment.topCenter,
|
|
|
|
colors: [
|
|
|
|
Theme.of(context).colorScheme.surface,
|
|
|
|
Colors.transparent,
|
|
|
|
],
|
|
|
|
),
|
2024-12-01 15:56:56 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2024-12-07 16:25:53 +00:00
|
|
|
Positioned(
|
|
|
|
left: 16,
|
|
|
|
right: 16,
|
|
|
|
bottom: 16 + MediaQuery.of(context).padding.bottom,
|
|
|
|
child: Material(
|
|
|
|
color: Colors.transparent,
|
|
|
|
child: Builder(builder: (context) {
|
|
|
|
final ud = context.read<UserDirectoryProvider>();
|
|
|
|
final item = widget.data.elementAt(
|
|
|
|
widget.data.length > 1 ? _pageController.page?.round() ?? 0 : 0,
|
|
|
|
);
|
|
|
|
final account = ud.getAccountFromCache(item.accountId);
|
2024-12-01 15:56:56 +00:00
|
|
|
|
2024-12-07 16:25:53 +00:00
|
|
|
return Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
|
|
|
if (item.accountId > 0)
|
|
|
|
Row(
|
|
|
|
children: [
|
|
|
|
IgnorePointer(
|
|
|
|
child: AccountImage(
|
|
|
|
content: account!.avatar,
|
|
|
|
radius: 19,
|
|
|
|
),
|
2024-12-01 15:56:56 +00:00
|
|
|
),
|
2024-12-07 16:25:53 +00:00
|
|
|
const Gap(8),
|
|
|
|
Expanded(
|
|
|
|
child: IgnorePointer(
|
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
|
|
|
Text(
|
|
|
|
'attachmentUploadBy'.tr(),
|
|
|
|
style: Theme.of(context).textTheme.bodySmall,
|
|
|
|
),
|
|
|
|
Text(
|
|
|
|
account.nick,
|
|
|
|
style: Theme.of(context).textTheme.bodyMedium,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
2024-12-01 15:56:56 +00:00
|
|
|
),
|
|
|
|
),
|
2024-12-07 16:25:53 +00:00
|
|
|
if (widget.data.length > 1)
|
|
|
|
IgnorePointer(
|
|
|
|
child: Text(
|
|
|
|
'${(_pageController.page?.round() ?? 0) + 1}/${widget.data.length}',
|
|
|
|
style: GoogleFonts.robotoMono(fontSize: 13),
|
|
|
|
).padding(right: 8),
|
|
|
|
),
|
|
|
|
InkWell(
|
2024-12-13 17:32:13 +00:00
|
|
|
borderRadius: const BorderRadius.all(Radius.circular(16)),
|
2024-12-07 16:25:53 +00:00
|
|
|
onTap: _isDownloading
|
|
|
|
? null
|
|
|
|
: () => _saveToAlbum(widget.data.length > 1 ? _pageController.page?.round() ?? 0 : 0),
|
|
|
|
child: Container(
|
|
|
|
padding: const EdgeInsets.all(6),
|
|
|
|
child: !_isDownloading
|
|
|
|
? !_isCompletedDownload
|
|
|
|
? const Icon(Symbols.save_alt)
|
|
|
|
: const Icon(Symbols.download_done)
|
|
|
|
: SizedBox(
|
|
|
|
width: 24,
|
|
|
|
height: 24,
|
|
|
|
child: CircularProgressIndicator(
|
|
|
|
value: _progressOfDownload,
|
|
|
|
strokeWidth: 3,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2024-12-02 14:01:02 +00:00
|
|
|
),
|
2024-12-07 16:25:53 +00:00
|
|
|
],
|
|
|
|
),
|
|
|
|
const Gap(4),
|
|
|
|
IgnorePointer(
|
|
|
|
child: Text(
|
|
|
|
item.alt,
|
|
|
|
maxLines: 2,
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
style: const TextStyle(
|
|
|
|
fontSize: 15,
|
|
|
|
fontWeight: FontWeight.w500,
|
|
|
|
),
|
2024-12-01 15:56:56 +00:00
|
|
|
),
|
|
|
|
),
|
2024-12-07 16:25:53 +00:00
|
|
|
const Gap(2),
|
|
|
|
IgnorePointer(
|
|
|
|
child: Wrap(
|
|
|
|
spacing: 6,
|
|
|
|
children: [
|
|
|
|
if (item.metadata['exif'] == null)
|
|
|
|
Text(
|
|
|
|
'#${item.rid}',
|
|
|
|
style: metaTextStyle,
|
|
|
|
),
|
|
|
|
if (item.metadata['exif']?['Model'] != null)
|
|
|
|
Text(
|
|
|
|
'attachmentShotOn'.tr(args: [
|
|
|
|
item.metadata['exif']?['Model'],
|
|
|
|
]),
|
|
|
|
style: metaTextStyle,
|
|
|
|
).padding(right: 2),
|
|
|
|
if (item.metadata['exif']?['ShutterSpeed'] != null)
|
|
|
|
Text(
|
|
|
|
item.metadata['exif']?['ShutterSpeed'],
|
|
|
|
style: metaTextStyle,
|
|
|
|
).padding(right: 2),
|
|
|
|
if (item.metadata['exif']?['ISO'] != null)
|
|
|
|
Text(
|
|
|
|
'ISO${item.metadata['exif']?['ISO']}',
|
|
|
|
style: metaTextStyle,
|
|
|
|
).padding(right: 2),
|
|
|
|
if (item.metadata['exif']?['Aperture'] != null)
|
|
|
|
Text(
|
|
|
|
'f/${item.metadata['exif']?['Aperture']}',
|
|
|
|
style: metaTextStyle,
|
|
|
|
).padding(right: 2),
|
|
|
|
if (item.metadata['exif']?['Megapixels'] != null && item.metadata['exif']?['Model'] != null)
|
|
|
|
Text(
|
|
|
|
'${item.metadata['exif']?['Megapixels']}MP',
|
|
|
|
style: metaTextStyle,
|
|
|
|
)
|
|
|
|
else
|
|
|
|
Text(
|
|
|
|
'${item.size} Bytes',
|
|
|
|
style: metaTextStyle,
|
|
|
|
),
|
2024-12-13 17:32:13 +00:00
|
|
|
if (item.metadata['width'] != null && item.metadata['height'] != null)
|
|
|
|
Text(
|
|
|
|
'${item.metadata['width']}x${item.metadata['height']}',
|
|
|
|
style: metaTextStyle,
|
|
|
|
),
|
2024-12-07 16:25:53 +00:00
|
|
|
if (item.metadata['ratio'] != null)
|
|
|
|
Text(
|
|
|
|
(item.metadata['ratio'] as num).toStringAsFixed(2),
|
|
|
|
style: metaTextStyle,
|
|
|
|
),
|
2024-12-02 14:01:02 +00:00
|
|
|
Text(
|
2024-12-07 16:25:53 +00:00
|
|
|
item.mimetype,
|
2024-12-02 14:01:02 +00:00
|
|
|
style: metaTextStyle,
|
|
|
|
),
|
2024-12-07 16:25:53 +00:00
|
|
|
],
|
|
|
|
),
|
2024-12-01 15:56:56 +00:00
|
|
|
),
|
2024-12-07 16:25:53 +00:00
|
|
|
],
|
|
|
|
);
|
|
|
|
}),
|
|
|
|
),
|
2024-11-25 14:41:15 +00:00
|
|
|
),
|
2024-12-07 16:25:53 +00:00
|
|
|
],
|
|
|
|
),
|
2024-12-01 15:56:56 +00:00
|
|
|
),
|
2024-11-10 09:21:57 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|