fluffychat/lib/utils/event_extension.dart

108 lines
3.5 KiB
Dart
Raw Normal View History

2020-01-19 14:07:42 +00:00
import 'package:famedlysdk/famedlysdk.dart';
2020-12-25 08:58:34 +00:00
import 'package:future_loading_dialog/future_loading_dialog.dart';
2020-05-07 09:19:29 +00:00
import 'package:flutter/foundation.dart';
2020-01-19 14:07:42 +00:00
import 'package:flutter/material.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
2020-05-16 06:02:33 +00:00
import 'matrix_file_extension.dart';
import '../views/image_viewer.dart';
2020-01-19 14:07:42 +00:00
extension LocalizedBody on Event {
2020-09-03 10:58:54 +00:00
void openFile(BuildContext context, {bool downloadOnly = false}) async {
if (!downloadOnly &&
[MessageTypes.Image, MessageTypes.Sticker].contains(messageType)) {
2021-02-24 11:17:23 +00:00
await Navigator.of(context, rootNavigator: false).push(
2021-04-10 07:06:24 +00:00
MaterialPageRoute(builder: (_) => ImageViewer(this)),
2020-09-03 10:58:54 +00:00
);
return;
}
2020-12-25 08:58:34 +00:00
final matrixFile = await showFutureLoadingDialog(
context: context,
future: () => downloadAndDecryptAttachmentCached(),
2020-05-16 06:02:33 +00:00
);
2020-12-25 08:58:34 +00:00
matrixFile.result?.open();
2020-05-16 06:02:33 +00:00
}
2020-03-13 19:09:32 +00:00
IconData get statusIcon {
2020-05-13 13:58:59 +00:00
switch (status) {
2020-03-13 19:09:32 +00:00
case -1:
return Icons.error_outline;
case 0:
2020-12-06 09:31:35 +00:00
return Icons.timer_outlined;
2020-03-13 19:09:32 +00:00
case 1:
2020-12-06 09:31:35 +00:00
return Icons.done_outlined;
2020-03-13 19:09:32 +00:00
case 2:
2020-12-06 09:31:35 +00:00
return Icons.done_all_outlined;
2020-03-13 19:09:32 +00:00
default:
2020-12-06 09:31:35 +00:00
return Icons.done_outlined;
2020-03-13 19:09:32 +00:00
}
}
2020-03-13 20:58:48 +00:00
bool get isAttachmentSmallEnough =>
infoMap['size'] is int &&
infoMap['size'] < room.client.database.maxFileSize;
bool get isThumbnailSmallEnough =>
thumbnailInfoMap['size'] is int &&
thumbnailInfoMap['size'] < room.client.database.maxFileSize;
2020-05-07 09:19:29 +00:00
bool get showThumbnail =>
2020-05-10 11:26:52 +00:00
[MessageTypes.Image, MessageTypes.Sticker].contains(messageType) &&
(kIsWeb ||
isAttachmentSmallEnough ||
isThumbnailSmallEnough ||
2020-09-03 10:58:54 +00:00
(content['url'] is String));
2020-05-07 09:19:29 +00:00
2020-03-13 20:58:48 +00:00
String get sizeString {
2020-05-13 13:58:59 +00:00
if (content['info'] is Map<String, dynamic> &&
content['info'].containsKey('size')) {
num size = content['info']['size'];
2020-03-13 20:58:48 +00:00
if (size < 1000000) {
size = size / 1000;
2020-05-16 06:09:07 +00:00
size = (size * 10).round() / 10;
return '${size.toString()} KB';
2020-03-13 20:58:48 +00:00
} else if (size < 1000000000) {
size = size / 1000000;
2020-05-16 06:09:07 +00:00
size = (size * 10).round() / 10;
return '${size.toString()} MB';
2020-03-13 20:58:48 +00:00
} else {
size = size / 1000000000;
2020-05-16 06:09:07 +00:00
size = (size * 10).round() / 10;
return '${size.toString()} GB';
2020-03-13 20:58:48 +00:00
}
2020-03-14 10:05:28 +00:00
} else {
2020-03-13 20:58:48 +00:00
return null;
2020-03-14 10:05:28 +00:00
}
2020-03-13 20:58:48 +00:00
}
static final _downloadAndDecryptFutures = <String, Future<MatrixFile>>{};
Future<bool> isAttachmentCached({bool getThumbnail = false}) async {
final mxcUrl = attachmentOrThumbnailMxcUrl(getThumbnail: getThumbnail);
// check if we have it in-memory
if (_downloadAndDecryptFutures.containsKey(mxcUrl)) {
return true;
}
// check if it is stored
if (await isAttachmentInLocalStore(getThumbnail: getThumbnail)) {
return true;
}
// check if the url is cached
final url = Uri.parse(mxcUrl).getDownloadLink(room.client);
final file = await DefaultCacheManager().getFileFromCache(url);
return file != null;
}
Future<MatrixFile> downloadAndDecryptAttachmentCached(
{bool getThumbnail = false}) async {
final mxcUrl = attachmentOrThumbnailMxcUrl(getThumbnail: getThumbnail);
_downloadAndDecryptFutures[mxcUrl] ??= downloadAndDecryptAttachment(
getThumbnail: getThumbnail,
downloadCallback: (String url) async {
final file = await DefaultCacheManager().getSingleFile(url);
return await file.readAsBytes();
},
);
final res = await _downloadAndDecryptFutures[mxcUrl];
return res;
}
2020-01-19 14:07:42 +00:00
}