fluffychat/lib/utils/matrix_sdk_extensions/matrix_file_extension.dart
2023-01-26 13:18:42 +01:00

50 lines
1.4 KiB
Dart

import 'package:flutter/material.dart';
import 'package:file_picker_cross/file_picker_cross.dart';
import 'package:image_picker/image_picker.dart';
import 'package:matrix/matrix.dart';
import 'package:share_plus/share_plus.dart';
import 'package:fluffychat/utils/platform_infos.dart';
import 'package:fluffychat/utils/size_string.dart';
extension MatrixFileExtension on MatrixFile {
void save(BuildContext context) async {
if (PlatformInfos.isIOS) {
return share(context);
}
final fileName = name.split('/').last;
final file = FilePickerCross(bytes);
await file.exportToStorage(fileName: fileName, share: false);
}
void share(BuildContext context) async {
// Workaround for iPad from
// https://github.com/fluttercommunity/plus_plugins/tree/main/packages/share_plus/share_plus#ipad
final box = context.findRenderObject() as RenderBox?;
await Share.shareXFiles(
[XFile.fromData(bytes)],
sharePositionOrigin:
box == null ? null : box.localToGlobal(Offset.zero) & box.size,
);
return;
}
MatrixFile get detectFileType {
if (msgType == MessageTypes.Image) {
return MatrixImageFile(bytes: bytes, name: name);
}
if (msgType == MessageTypes.Video) {
return MatrixVideoFile(bytes: bytes, name: name);
}
if (msgType == MessageTypes.Audio) {
return MatrixAudioFile(bytes: bytes, name: name);
}
return this;
}
String get sizeString => size.sizeString;
}