Able to upload from share

This commit is contained in:
2025-11-25 00:00:04 +08:00
parent aa91e376ca
commit 68be4db160
2 changed files with 102 additions and 18 deletions

View File

@@ -233,6 +233,9 @@
"pickFile": "Pick a file", "pickFile": "Pick a file",
"uploading": "Uploading", "uploading": "Uploading",
"uploadingProgress": "Uploading {} of {}", "uploadingProgress": "Uploading {} of {}",
"upload": "Upload",
"uploadSuccess": "Upload successful!",
"wouldYouLikeToViewFile": "Would you like to view the file?",
"uploadAll": "Upload All", "uploadAll": "Upload All",
"stickerCopyPlaceholder": "Copy Placeholder", "stickerCopyPlaceholder": "Copy Placeholder",
"realmSelection": "Select a Realm", "realmSelection": "Select a Realm",
@@ -1110,7 +1113,6 @@
"deleteRecycledFiles": "Delete Recycled Files", "deleteRecycledFiles": "Delete Recycled Files",
"recycledFilesDeleted": "Recycled files deleted successfully", "recycledFilesDeleted": "Recycled files deleted successfully",
"failedToDeleteRecycledFiles": "Failed to delete recycled files", "failedToDeleteRecycledFiles": "Failed to delete recycled files",
"upload": "Upload",
"updateAvailable": "Update available", "updateAvailable": "Update available",
"noChangelogProvided": "No changelog provided.", "noChangelogProvided": "No changelog provided.",
"useSecondarySourceForDownload": "Use secondary source for download", "useSecondarySourceForDownload": "Use secondary source for download",

View File

@@ -285,23 +285,10 @@ class _ShareSheetState extends ConsumerState<ShareSheet> {
); );
// Show navigation prompt // Show navigation prompt
final shouldNavigate = await showDialog<bool>( final shouldNavigate = await showConfirmAlert(
context: context, 'wouldYouLikeToGoToChat'.tr(),
builder: 'shareSuccess'.tr(),
(context) => AlertDialog( icon: Symbols.check_circle,
title: Text('shareSuccess'.tr()),
content: Text('wouldYouLikeToGoToChat'.tr()),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(false),
child: Text('no'.tr()),
),
TextButton(
onPressed: () => Navigator.of(context).pop(true),
child: Text('yes'.tr()),
),
],
),
); );
// Close the share sheet // Close the share sheet
@@ -367,6 +354,92 @@ class _ShareSheetState extends ConsumerState<ShareSheet> {
} }
} }
Future<void> _uploadFiles() async {
if (widget.content.files == null || widget.content.files!.isEmpty) return;
setState(() => _isLoading = true);
try {
final universalFiles =
widget.content.files!.map((file) {
UniversalFileType fileType;
if (file.mimeType?.startsWith('image/') == true) {
fileType = UniversalFileType.image;
} else if (file.mimeType?.startsWith('video/') == true) {
fileType = UniversalFileType.video;
} else if (file.mimeType?.startsWith('audio/') == true) {
fileType = UniversalFileType.audio;
} else {
fileType = UniversalFileType.file;
}
return UniversalFile(data: file, type: fileType);
}).toList();
// Initialize progress tracking
final messageId = DateTime.now().millisecondsSinceEpoch.toString();
_fileUploadProgress[messageId] = List.filled(universalFiles.length, 0.0);
List<SnCloudFile> uploadedFiles = [];
// Upload each file
for (var idx = 0; idx < universalFiles.length; idx++) {
final file = universalFiles[idx];
final cloudFile =
await FileUploader.createCloudFile(
ref: ref,
fileData: file,
onProgress: (progress, _) {
if (mounted) {
setState(() {
_fileUploadProgress[messageId]?[idx] = progress ?? 0.0;
});
}
},
).future;
if (cloudFile == null) {
throw Exception('Failed to upload file: ${file.data.name}');
}
uploadedFiles.add(cloudFile);
}
if (mounted) {
// Show success message
showSnackBar('uploadSuccess'.tr());
// If single file, ask to view details
if (uploadedFiles.length == 1) {
final shouldView = await showConfirmAlert(
'wouldYouLikeToViewFile'.tr(),
'uploadSuccess'.tr(),
icon: Symbols.check_circle,
);
if (mounted) {
Navigator.of(context).pop(); // Close share sheet
if (shouldView == true) {
context.pushNamed(
'fileDetail',
pathParameters: {'id': uploadedFiles.first.id},
extra: uploadedFiles.first,
);
}
}
} else {
// Just close for multiple files
Navigator.of(context).pop();
}
}
} catch (e) {
if (mounted) {
showErrorAlert(e);
}
} finally {
if (mounted) {
setState(() => _isLoading = false);
}
}
}
Future<void> _copyToClipboard() async { Future<void> _copyToClipboard() async {
try { try {
String textToCopy = ''; String textToCopy = '';
@@ -456,6 +529,15 @@ class _ShareSheetState extends ConsumerState<ShareSheet> {
onTap: _isLoading ? null : _shareToPost, onTap: _isLoading ? null : _shareToPost,
), ),
const SizedBox(width: 12), const SizedBox(width: 12),
if (widget.content.type ==
ShareContentType.file) ...[
_CompactShareOption(
icon: Symbols.cloud_upload,
title: 'upload'.tr(),
onTap: _isLoading ? null : _uploadFiles,
),
const SizedBox(width: 12),
],
_CompactShareOption( _CompactShareOption(
icon: Symbols.content_copy, icon: Symbols.content_copy,
title: 'copy'.tr(), title: 'copy'.tr(),