Skip to content

Commit

Permalink
fixup! feat: preview pdf on web client
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian KOUNE committed Oct 5, 2023
1 parent 1e6484f commit 60913c8
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 22 deletions.
5 changes: 4 additions & 1 deletion assets/l10n/intl_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -2754,5 +2754,8 @@
"workIdentitiesInfo": "WORK IDENTITIES INFO",
"editWorkIdentitiesDescriptions": "Edit your work identity settings such as Matrix ID, email or company name.",
"copiedMatrixIdToClipboard": "Copied Matrix ID to clipboard.",
"changeProfilePhoto": "Change profile photo"
"changeProfilePhoto": "Change profile photo",
"roomCreationFailed": "Room creation failed",
"errorGettingPdf": "Error getting PDF",
"errorPreviewingFile": "Error previewing file"
}
48 changes: 36 additions & 12 deletions lib/domain/model/extensions/mime_type_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,26 @@ extension MediaTypeExtension on Event {
'AttachmentExtension::getIcon(): mediaType: $mimeType || fileType: $fileType',
);
if (mimeType?.isEmpty == true || fileType == null) {
return ImagePaths.icFileUnKnow;
return ImagePaths.icFileUnknown;
}
if (isDocFile()) {
return ImagePaths.icFileDocx;
} else if (isExcelFile()) {
return ImagePaths.icFileXlsx;
} else if (isPowerPointFile()) {
return ImagePaths.icFilePptx;
} else if (isPdfFile()) {
return ImagePaths.icFilePdf;
} else if (isZipFile()) {
return ImagePaths.icFileZip;

switch (getSupportedPreviewFileType()) {
case SupportedPreviewFileTypesEnum.image:
return ImagePaths.icFileUnknown;
case SupportedPreviewFileTypesEnum.doc:
return ImagePaths.icFileDocx;
case SupportedPreviewFileTypesEnum.excel:
return ImagePaths.icFileXlsx;
case SupportedPreviewFileTypesEnum.powerPoint:
return ImagePaths.icFilePptx;
case SupportedPreviewFileTypesEnum.pdf:
return ImagePaths.icFilePdf;
case SupportedPreviewFileTypesEnum.zip:
return ImagePaths.icFileZip;
case SupportedPreviewFileTypesEnum.unknown:
default:
return ImagePaths.icFileUnknown;
}
return ImagePaths.icFileUnKnow;
}

String getFileType(BuildContext context) {
Expand All @@ -70,4 +76,22 @@ extension MediaTypeExtension on Event {
return L10n.of(context)!.file.toUpperCase();
}
}

SupportedPreviewFileTypesEnum getSupportedPreviewFileType() {
if (isImageFile()) {
return SupportedPreviewFileTypesEnum.image;
} else if (isDocFile()) {
return SupportedPreviewFileTypesEnum.doc;
} else if (isExcelFile()) {
return SupportedPreviewFileTypesEnum.excel;
} else if (isPowerPointFile()) {
return SupportedPreviewFileTypesEnum.powerPoint;
} else if (isPdfFile()) {
return SupportedPreviewFileTypesEnum.pdf;
} else if (isZipFile()) {
return SupportedPreviewFileTypesEnum.zip;
} else {
return SupportedPreviewFileTypesEnum.unknown;
}
}
}
10 changes: 10 additions & 0 deletions lib/domain/model/preview_file/supported_preview_file_types.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
enum SupportedPreviewFileTypesEnum {
image,
doc,
excel,
powerPoint,
pdf,
zip,
unknown,
}

class SupportedPreviewFileTypes {
static const imageMimeTypes = [
'image/bmp',
Expand Down
22 changes: 15 additions & 7 deletions lib/pages/chat/chat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -575,10 +575,16 @@ class ChatController extends State<Chat>
}

void _handlePreviewWeb({required Event event}) async {
if (event.isPdfFile()) {
downloadAndPreviewPdfOnWeb(context, event);
} else {
downloadFileAction(context, event);
if (!event.hasAttachment) {
Fluttertoast.showToast(msg: L10n.of(context)!.errorPreviewingFile);
return;
}

switch (event.getSupportedPreviewFileType()) {
case SupportedPreviewFileTypesEnum.pdf:
return previewPdfWeb(context, event);
default:
downloadFileAction(context, event);
}
}

Expand Down Expand Up @@ -1494,10 +1500,12 @@ class ChatController extends State<Chat>
Future<String?> downloadFileAction(BuildContext context, Event event) async =>
await event.saveFile(context);

void downloadAndPreviewPdfOnWeb(BuildContext context, Event event) async {
void previewPdfWeb(BuildContext context, Event event) async {
final pdf = await event.getFile(context);

if (pdf.result == null) return;
if (pdf.result == null || event.sizeString != pdf.result?.sizeString) {
Fluttertoast.showToast(msg: L10n.of(context)!.errorGettingPdf);
return;
}

final blob = html.Blob([pdf.result!.bytes], 'application/pdf');
final url = html.Url.createObjectUrlFromBlob(blob);
Expand Down
3 changes: 2 additions & 1 deletion lib/pages/chat_draft/draft_chat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import 'package:linagora_design_flutter/images_picker/images_picker.dart'
hide ImagePicker;
import 'package:matrix/matrix.dart';
import 'package:scroll_to_index/scroll_to_index.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';

typedef OnRoomCreatedSuccess = FutureOr<void> Function(Room room)?;
typedef OnRoomCreatedFailed = FutureOr<void> Function()?;
Expand Down Expand Up @@ -238,7 +239,7 @@ class DraftChatController extends State<DraftChat>
void onInputBarSubmitted(_) {
sendText(
onCreateRoomFailed: () {
Fluttertoast.showToast(msg: 'Create room failed');
Fluttertoast.showToast(msg: L10n.of(context)!.roomCreationFailed);
FocusScope.of(context).requestFocus(inputFocus);
},
);
Expand Down
2 changes: 1 addition & 1 deletion lib/resource/image_paths.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ImagePaths {
static String get icFilePdf => _getImagePath('ic_file_pdf.svg');
static String get icFilePptx => _getImagePath('ic_file_ppt.svg');
static String get icFileFolder => _getImagePath('ic_file_folder.svg');
static String get icFileUnKnow => _getImagePath('ic_file_unknow.svg');
static String get icFileUnknown => _getImagePath('ic_file_unknow.svg');
static String get icTwakeImageLogoDark =>
_getImagePath('ic_twake_image_logo_dark.svg');
static String get icApplicationGrid =>
Expand Down

0 comments on commit 60913c8

Please sign in to comment.