feat: Add svg support and better image handling
This commit is contained in:
parent
7cadcb710d
commit
31aa4c41d4
6 changed files with 289 additions and 126 deletions
|
|
@ -2,6 +2,7 @@ import 'package:famedlysdk/famedlysdk.dart';
|
|||
import 'package:fluffychat/components/dialogs/simple_dialogs.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
|
||||
import 'matrix_file_extension.dart';
|
||||
import 'app_route.dart';
|
||||
import '../views/image_view.dart';
|
||||
|
|
@ -19,7 +20,7 @@ extension LocalizedBody on Event {
|
|||
}
|
||||
final MatrixFile matrixFile =
|
||||
await SimpleDialogs(context).tryRequestWithLoadingDialog(
|
||||
downloadAndDecryptAttachment(),
|
||||
downloadAndDecryptAttachmentCached(),
|
||||
);
|
||||
matrixFile.open();
|
||||
}
|
||||
|
|
@ -39,17 +40,18 @@ extension LocalizedBody on Event {
|
|||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
bool get showThumbnail =>
|
||||
[MessageTypes.Image, MessageTypes.Sticker].contains(messageType) &&
|
||||
(kIsWeb ||
|
||||
(content['info'] is Map &&
|
||||
content['info']['size'] is int &&
|
||||
content['info']['size'] < room.client.database.maxFileSize) ||
|
||||
(hasThumbnail &&
|
||||
content['info']['thumbnail_info'] is Map &&
|
||||
content['info']['thumbnail_info']['size'] is int &&
|
||||
content['info']['thumbnail_info']['size'] <
|
||||
room.client.database.maxFileSize) ||
|
||||
isAttachmentSmallEnough ||
|
||||
isThumbnailSmallEnough ||
|
||||
(content['url'] is String));
|
||||
|
||||
String get sizeString {
|
||||
|
|
@ -73,4 +75,36 @@ extension LocalizedBody on Event {
|
|||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue