fluffychat/lib/utils/matrix_sdk_extensions.dart/event_extension.dart

103 lines
3.2 KiB
Dart
Raw Normal View History

import 'package:matrix/matrix.dart';
2020-12-25 09:58:34 +01:00
import 'package:future_loading_dialog/future_loading_dialog.dart';
2020-05-07 11:19:29 +02:00
import 'package:flutter/foundation.dart';
2020-01-19 15:07:42 +01:00
import 'package:flutter/material.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
2020-05-16 08:02:33 +02:00
import 'matrix_file_extension.dart';
2020-01-19 15:07:42 +01:00
extension LocalizedBody on Event {
2021-07-11 17:12:56 +02:00
void saveFile(BuildContext context) async {
2020-12-25 09:58:34 +01:00
final matrixFile = await showFutureLoadingDialog(
context: context,
future: () => downloadAndDecryptAttachmentCached(),
2020-05-16 08:02:33 +02:00
);
2021-07-11 17:12:56 +02:00
matrixFile.result?.save(context);
2020-05-16 08:02:33 +02:00
}
2020-03-13 20:09:32 +01:00
IconData get statusIcon {
2021-10-25 10:46:58 +02:00
switch (status.intValue) {
2020-03-13 20:09:32 +01:00
case -1:
return Icons.error_outline;
case 0:
2020-12-06 10:31:35 +01:00
return Icons.timer_outlined;
2020-03-13 20:09:32 +01:00
case 1:
2020-12-06 10:31:35 +01:00
return Icons.done_outlined;
2020-03-13 20:09:32 +01:00
case 2:
2020-12-06 10:31:35 +01:00
return Icons.done_all_outlined;
2020-03-13 20:09:32 +01:00
default:
2020-12-06 10:31:35 +01:00
return Icons.done_outlined;
2020-03-13 20:09:32 +01:00
}
}
2020-03-13 21:58:48 +01: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 11:19:29 +02:00
bool get showThumbnail =>
2021-08-08 17:55:00 +02:00
[MessageTypes.Image, MessageTypes.Sticker, MessageTypes.Video]
.contains(messageType) &&
(kIsWeb ||
isAttachmentSmallEnough ||
isThumbnailSmallEnough ||
2020-09-03 12:58:54 +02:00
(content['url'] is String));
2020-05-07 11:19:29 +02:00
2020-03-13 21:58:48 +01:00
String get sizeString {
2020-05-13 15:58:59 +02:00
if (content['info'] is Map<String, dynamic> &&
content['info'].containsKey('size')) {
num size = content['info']['size'];
2020-03-13 21:58:48 +01:00
if (size < 1000000) {
size = size / 1000;
2020-05-16 08:09:07 +02:00
size = (size * 10).round() / 10;
return '${size.toString()} KB';
2020-03-13 21:58:48 +01:00
} else if (size < 1000000000) {
size = size / 1000000;
2020-05-16 08:09:07 +02:00
size = (size * 10).round() / 10;
return '${size.toString()} MB';
2020-03-13 21:58:48 +01:00
} else {
size = size / 1000000000;
2020-05-16 08:09:07 +02:00
size = (size * 10).round() / 10;
return '${size.toString()} GB';
2020-03-13 21:58:48 +01:00
}
2020-03-14 11:05:28 +01:00
} else {
2020-03-13 21:58:48 +01:00
return null;
2020-03-14 11:05:28 +01:00
}
2020-03-13 21:58:48 +01: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
2021-08-26 19:03:08 +02:00
final url = mxcUrl.getDownloadLink(room.client);
2021-04-21 14:19:54 +02:00
final file = await DefaultCacheManager().getFileFromCache(url.toString());
return file != null;
}
Future<MatrixFile> downloadAndDecryptAttachmentCached(
{bool getThumbnail = false}) async {
2021-08-29 15:18:02 +02:00
final mxcUrl =
attachmentOrThumbnailMxcUrl(getThumbnail: getThumbnail).toString();
_downloadAndDecryptFutures[mxcUrl] ??= downloadAndDecryptAttachment(
getThumbnail: getThumbnail,
2021-04-21 14:19:54 +02:00
downloadCallback: (Uri url) async {
final file = await DefaultCacheManager().getSingleFile(url.toString());
return await file.readAsBytes();
},
);
final res = await _downloadAndDecryptFutures[mxcUrl];
return res;
}
2020-01-19 15:07:42 +01:00
}