From cbd4847394531769666d02bb6a930beeaf539d2f Mon Sep 17 00:00:00 2001 From: Dang Dat Date: Mon, 6 Jul 2026 10:09:27 +0700 Subject: [PATCH] fixup! TF-4661 Improve drive link UI --- core/lib/utils/html/html_utils.dart | 42 +++++++++++ .../presentation/composer_bindings.dart | 2 +- .../manager/drive_attachment_handler.dart | 75 +++++++------------ ...achment_handler_insert_link_html_test.dart | 3 +- .../drive_attachment_handler_test.dart | 3 +- 5 files changed, 76 insertions(+), 49 deletions(-) diff --git a/core/lib/utils/html/html_utils.dart b/core/lib/utils/html/html_utils.dart index 0706baf1c..80f1d83cb 100644 --- a/core/lib/utils/html/html_utils.dart +++ b/core/lib/utils/html/html_utils.dart @@ -1060,4 +1060,46 @@ class HtmlUtils { var values = List.generate(len, (i) => random.nextInt(255)); return base64UrlEncode(values); } + + static String buildFileLinkCard({ + required String href, + required String title, + required String actionLabel, + required String iconZoneHtml, + int cardWidthPx = 183, + int cardMinHeightPx = 151, + }) { + return '' + '$iconZoneHtml' + '
' + '
$title
' + '
$actionLabel ↗
' + '
' + '
'; + } + + static String buildFileCardIconZone({ + String? iconBase64Png, + int iconZoneHeightPx = 94, + int iconSizePx = 60, + }) { + final content = iconBase64Png != null + ? '' + : ''; + return '
' + '$content' + '
'; + } + + static String wrapFileCardsHtml(String cardsHtml) { + if (cardsHtml.isEmpty) return ''; + return '
$cardsHtml

'; + } } diff --git a/lib/features/composer/presentation/composer_bindings.dart b/lib/features/composer/presentation/composer_bindings.dart index 4cd7009cd..34aaeed5a 100644 --- a/lib/features/composer/presentation/composer_bindings.dart +++ b/lib/features/composer/presentation/composer_bindings.dart @@ -344,7 +344,7 @@ abstract class ComposerBindings extends BaseBindings { @override void bindingsController() { - Get.lazyPut(() => const DriveAttachmentHandler(), tag: composerId); + Get.lazyPut(() => DriveAttachmentHandler(fileUtils: Get.find()), tag: composerId); bindPlatformRichTextController(); Get.lazyPut( () => UploadController(Get.find(tag: composerId)), diff --git a/lib/features/composer/presentation/manager/drive_attachment_handler.dart b/lib/features/composer/presentation/manager/drive_attachment_handler.dart index 03a9ddee7..153ae363a 100644 --- a/lib/features/composer/presentation/manager/drive_attachment_handler.dart +++ b/lib/features/composer/presentation/manager/drive_attachment_handler.dart @@ -6,17 +6,16 @@ import 'package:core/presentation/resources/image_paths.dart'; import 'package:core/utils/app_logger.dart'; import 'package:core/utils/build_utils.dart'; import 'package:core/utils/file_utils.dart'; +import 'package:core/utils/html/html_utils.dart'; import 'package:http_parser/http_parser.dart'; import 'package:tmail_ui_user/main/localizations/app_localizations.dart'; import 'package:workplace/domain/entity/drive_document.dart'; class DriveAttachmentHandler { - const DriveAttachmentHandler(); + DriveAttachmentHandler({required this.fileUtils}); + + final FileUtils fileUtils; - static const _cardWidthPx = 183; - static const _cardMinHeightPx = 151; - static const _iconZoneHeightPx = 94; - static const _iconSizePx = 60; static const _fallbackOpenInDriveLabel = 'Open in drive'; static Map categoryPngPaths(ImagePaths imagePaths) => { @@ -54,13 +53,13 @@ class DriveAttachmentHandler { ); } - static Future buildDriveLinksHtml( + Future buildDriveLinksHtml( List docs, { required ImagePaths imagePaths, AppLocalizations? appLocalizations, bool requireHttps = BuildUtils.isReleaseMode, }) async { - final base64PngCache = {}; + final base64PngCache = >{}; final cards = await Future.wait( docs.map( (doc) => _driveFileCard( @@ -72,16 +71,14 @@ class DriveAttachmentHandler { ), ), ); - final nonNullCards = cards.nonNulls.join(); - if (nonNullCards.isEmpty) return ''; - return '
$nonNullCards

'; + return HtmlUtils.wrapFileCardsHtml(cards.nonNulls.join()); } - static Future _driveFileCard( + Future _driveFileCard( DriveDocument doc, { required bool requireHttps, required ImagePaths imagePath, - required Map base64PngCache, + required Map> base64PngCache, AppLocalizations? appLocalizations, }) async { final link = doc.sharingLink; @@ -97,24 +94,22 @@ class DriveAttachmentHandler { ); final category = _resolveCategory(doc); - return '' - '${await _iconZoneHtml(category, imagePaths: imagePath, base64PngCache: base64PngCache)}' - '
' - '
$name
' - '
$openInDriveLabel ↗
' - '
' - '
'; + return HtmlUtils.buildFileLinkCard( + href: href, + title: name, + actionLabel: openInDriveLabel, + iconZoneHtml: await _iconZoneHtml( + category, + imagePaths: imagePath, + base64PngCache: base64PngCache, + ), + ); } - static Future _iconZoneHtml( + Future _iconZoneHtml( FileCategory category, { required ImagePaths imagePaths, - required Map base64PngCache, + required Map> base64PngCache, }) async { final base64Png = await _cachedCategoryPngBase64( @@ -127,40 +122,28 @@ class DriveAttachmentHandler { imagePaths: imagePaths, base64PngCache: base64PngCache, ); - final content = base64Png != null - ? '' - : ''; - return '
' - '$content' - '
'; + return HtmlUtils.buildFileCardIconZone(iconBase64Png: base64Png); } - static Future _cachedCategoryPngBase64( + Future _cachedCategoryPngBase64( FileCategory category, { required ImagePaths imagePaths, - required Map base64PngCache, - }) async { - if (base64PngCache.containsKey(category)) { - return base64PngCache[category]; - } - final base64Png = await _loadCategoryPngBase64( + required Map> base64PngCache, + }) { + return base64PngCache.putIfAbsent( category, - imagePaths: imagePaths, + () => _loadCategoryPngBase64(category, imagePaths: imagePaths), ); - base64PngCache[category] = base64Png; - return base64Png; } - static Future _loadCategoryPngBase64( + Future _loadCategoryPngBase64( FileCategory category, { required ImagePaths imagePaths, }) async { final path = categoryPngPaths(imagePaths)[category]; if (path == null) return null; try { - final base64Data = await FileUtils().convertImageAssetToBase64(path); + final base64Data = await fileUtils.convertImageAssetToBase64(path); return base64Data.isNotEmpty ? base64Data : null; } catch (e) { logWarning( diff --git a/test/features/composer/presentation/manager/drive_attachment_handler_insert_link_html_test.dart b/test/features/composer/presentation/manager/drive_attachment_handler_insert_link_html_test.dart index 57cd30eb8..36c5d7230 100644 --- a/test/features/composer/presentation/manager/drive_attachment_handler_insert_link_html_test.dart +++ b/test/features/composer/presentation/manager/drive_attachment_handler_insert_link_html_test.dart @@ -1,6 +1,7 @@ import 'dart:convert'; import 'package:core/presentation/resources/image_paths.dart'; +import 'package:core/utils/file_utils.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:tmail_ui_user/features/composer/presentation/manager/drive_attachment_handler.dart'; @@ -31,7 +32,7 @@ void main() { setUp(() { insertedHtml = []; - handler = const DriveAttachmentHandler(); + handler = DriveAttachmentHandler(fileUtils: FileUtils()); }); tearDown(() { diff --git a/test/features/composer/presentation/manager/drive_attachment_handler_test.dart b/test/features/composer/presentation/manager/drive_attachment_handler_test.dart index 8af0ab790..f3af67af3 100644 --- a/test/features/composer/presentation/manager/drive_attachment_handler_test.dart +++ b/test/features/composer/presentation/manager/drive_attachment_handler_test.dart @@ -1,4 +1,5 @@ import 'package:core/presentation/resources/image_paths.dart'; +import 'package:core/utils/file_utils.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:tmail_ui_user/features/composer/presentation/manager/drive_attachment_handler.dart'; import 'package:tmail_ui_user/main/localizations/app_localizations.dart'; @@ -12,7 +13,7 @@ void main() { setUp(() { insertedHtml = []; - handler = const DriveAttachmentHandler(); + handler = DriveAttachmentHandler(fileUtils: FileUtils()); }); group('DriveAttachmentHandler::handleDrivePickResult::', () {