This commit is contained in:
otsmr 2025-06-03 13:16:41 +02:00
parent 324b8a9758
commit b8aeb0e6ed
3 changed files with 67 additions and 55 deletions

View file

@ -7,19 +7,23 @@ import 'package:twonly/src/services/api/media_send.dart';
import 'dart:typed_data'; import 'dart:typed_data';
import 'package:twonly/src/utils/misc.dart'; import 'package:twonly/src/utils/misc.dart';
import 'package:twonly/src/utils/storage.dart';
class SaveToGalleryButton extends StatefulWidget { class SaveToGalleryButton extends StatefulWidget {
final Future<Uint8List?> Function() getMergedImage; final Future<Uint8List?> Function() getMergedImage;
final String? sendNextMediaToUserName; final String? sendNextMediaToUserName;
final File? videoFilePath; final File? videoFilePath;
final int? mediaUploadId; final int? mediaUploadId;
final bool isLoading;
const SaveToGalleryButton( const SaveToGalleryButton({
{super.key, super.key,
required this.getMergedImage, required this.getMergedImage,
this.sendNextMediaToUserName, required this.isLoading,
this.mediaUploadId, this.sendNextMediaToUserName,
this.videoFilePath}); this.mediaUploadId,
this.videoFilePath,
});
@override @override
State<SaveToGalleryButton> createState() => SaveToGalleryButtonState(); State<SaveToGalleryButton> createState() => SaveToGalleryButtonState();
@ -40,53 +44,62 @@ class SaveToGalleryButtonState extends State<SaveToGalleryButton> {
? Theme.of(context).colorScheme.outline ? Theme.of(context).colorScheme.outline
: Theme.of(context).colorScheme.primary, : Theme.of(context).colorScheme.primary,
), ),
onPressed: () async { onPressed: (widget.isLoading)
setState(() { ? null
_imageSaving = true; : () async {
}); setState(() {
_imageSaving = true;
});
String? res; String? res;
String memoryPath = await getMediaBaseFilePath("memories"); String memoryPath = await getMediaBaseFilePath("memories");
if (widget.mediaUploadId != null) { if (widget.mediaUploadId != null) {
memoryPath = join(memoryPath, "${widget.mediaUploadId!}"); memoryPath = join(memoryPath, "${widget.mediaUploadId!}");
} else { } else {
final Random random = Random(); final Random random = Random();
String token = uint8ListToHex( String token = uint8ListToHex(
List<int>.generate(32, (i) => random.nextInt(256))); List<int>.generate(32, (i) => random.nextInt(256)));
memoryPath = join(memoryPath, token); memoryPath = join(memoryPath, token);
} }
final user = await getUser();
if (user != null && (user.storeMediaFilesInGallery ?? true)) {}
bool storeToGallery = user?.storeMediaFilesInGallery ?? true;
if (widget.videoFilePath != null) { if (widget.videoFilePath != null) {
memoryPath += ".mp4"; memoryPath += ".mp4";
await File(widget.videoFilePath!.path).copy(memoryPath); await File(widget.videoFilePath!.path).copy(memoryPath);
res = await saveVideoToGallery(widget.videoFilePath!.path); if (storeToGallery) {
} else { res = await saveVideoToGallery(widget.videoFilePath!.path);
memoryPath += ".png"; }
Uint8List? imageBytes = await widget.getMergedImage(); } else {
if (imageBytes == null || !mounted) return; memoryPath += ".png";
await File(memoryPath).writeAsBytes(imageBytes); Uint8List? imageBytes = await widget.getMergedImage();
res = await saveImageToGallery(imageBytes); if (imageBytes == null || !mounted) return;
} await File(memoryPath).writeAsBytes(imageBytes);
if (res == null) { if (storeToGallery) {
setState(() { res = await saveImageToGallery(imageBytes);
_imageSaved = true; }
}); }
} else if (mounted && context.mounted) { if (res == null) {
ScaffoldMessenger.of(context).showSnackBar( setState(() {
SnackBar( _imageSaved = true;
content: Text(res), });
duration: Duration(seconds: 3), } else if (mounted && context.mounted) {
), ScaffoldMessenger.of(context).showSnackBar(
); SnackBar(
} content: Text(res),
setState(() { duration: Duration(seconds: 3),
_imageSaving = false; ),
}); );
}, }
setState(() {
_imageSaving = false;
});
},
child: Row( child: Row(
children: [ children: [
_imageSaving (_imageSaving || widget.isLoading)
? SizedBox( ? SizedBox(
width: 12, width: 12,
height: 12, height: 12,

View file

@ -535,11 +535,11 @@ class _ShareImageEditorView extends State<ShareImageEditorView> {
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: [ children: [
SaveToGalleryButton( SaveToGalleryButton(
getMergedImage: getMergedImage, getMergedImage: getMergedImage,
mediaUploadId: mediaUploadId, mediaUploadId: mediaUploadId,
videoFilePath: widget.videoFilePath, videoFilePath: widget.videoFilePath,
sendNextMediaToUserName: sendNextMediaToUserName, sendNextMediaToUserName: sendNextMediaToUserName,
), isLoading: sendingOrLoadingImage),
if (sendNextMediaToUserName != null) SizedBox(width: 10), if (sendNextMediaToUserName != null) SizedBox(width: 10),
if (sendNextMediaToUserName != null) if (sendNextMediaToUserName != null)
OutlinedButton( OutlinedButton(

View file

@ -277,7 +277,7 @@ class _MediaViewerViewState extends State<MediaViewerView> {
}); });
} }
startTimer() { void startTimer() {
nextMediaTimer?.cancel(); nextMediaTimer?.cancel();
progressTimer?.cancel(); progressTimer?.cancel();
nextMediaTimer = Timer(canBeSeenUntil!.difference(DateTime.now()), () { nextMediaTimer = Timer(canBeSeenUntil!.difference(DateTime.now()), () {
@ -323,7 +323,6 @@ class _MediaViewerViewState extends State<MediaViewerView> {
imageSaved = true; imageSaved = true;
}); });
final user = await getUser(); final user = await getUser();
if (user != null && (user.storeMediaFilesInGallery ?? true)) { if (user != null && (user.storeMediaFilesInGallery ?? true)) {
if (videoPath != null) { if (videoPath != null) {
await saveVideoToGallery(videoPath!); await saveVideoToGallery(videoPath!);