fixup! TF-4661 Improve drive link UI

This commit is contained in:
Dang Dat committed 2026-07-06 10:09:27 +07:00
1 parent d86b69846b
commit cbd4847394
5 files changed
+76 -49

No files matched your search

+42
View File
@@ -1060,4 +1060,46 @@ class HtmlUtils {
var values = List<int>.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 '<a href="$href" style="display:inline-block;vertical-align:top;width:${cardWidthPx}px;'
'min-height:${cardMinHeightPx}px;margin:0 8px 8px 0;border:1px solid #E5E7EB;'
'border-radius:10px;overflow:hidden;background:#FFFFFF;color:inherit;text-decoration:none;">'
'$iconZoneHtml'
'<div style="padding:10px 12px;">'
'<div style="font-size:14px;font-weight:500;color:#1F2937;white-space:nowrap;'
'overflow:hidden;text-overflow:ellipsis;" title="$title">$title</div>'
'<div style="display:block;margin-top:6px;font-size:12px;'
'color:#0A84FF;white-space:nowrap;overflow:hidden;'
'text-overflow:ellipsis;">$actionLabel ↗</div>'
'</div>'
'</a>';
}
static String buildFileCardIconZone({
String? iconBase64Png,
int iconZoneHeightPx = 94,
int iconSizePx = 60,
}) {
final content = iconBase64Png != null
? '<img src="data:image/png;base64,$iconBase64Png" width="$iconSizePx" height="$iconSizePx" '
'style="display:inline-block;vertical-align:middle;" />'
: '';
return '<div style="height:${iconZoneHeightPx}px;line-height:${iconZoneHeightPx}px;'
'text-align:center;background:#F5F6F8;border-bottom:1px solid #E5E7EB;">'
'$content'
'</div>';
}
static String wrapFileCardsHtml(String cardsHtml) {
if (cardsHtml.isEmpty) return '';
return '<div style="display:block;max-width:100%;">$cardsHtml</div><br>';
}
}
@@ -344,7 +344,7 @@ abstract class ComposerBindings extends BaseBindings {
@override
void bindingsController() {
Get.lazyPut(() => const DriveAttachmentHandler(), tag: composerId);
Get.lazyPut(() => DriveAttachmentHandler(fileUtils: Get.find<FileUtils>()), tag: composerId);
bindPlatformRichTextController();
Get.lazyPut(
() => UploadController(Get.find<UploadAttachmentInteractor>(tag: composerId)),
@@ -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<FileCategory, String> categoryPngPaths(ImagePaths imagePaths) => {
@@ -54,13 +53,13 @@ class DriveAttachmentHandler {
);
}
static Future<String> buildDriveLinksHtml(
Future<String> buildDriveLinksHtml(
List<DriveDocument> docs, {
required ImagePaths imagePaths,
AppLocalizations? appLocalizations,
bool requireHttps = BuildUtils.isReleaseMode,
}) async {
final base64PngCache = <FileCategory, String?>{};
final base64PngCache = <FileCategory, Future<String?>>{};
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 '<div style="display:block;max-width:100%;">$nonNullCards</div><br>';
return HtmlUtils.wrapFileCardsHtml(cards.nonNulls.join());
}
static Future<String?> _driveFileCard(
Future<String?> _driveFileCard(
DriveDocument doc, {
required bool requireHttps,
required ImagePaths imagePath,
required Map<FileCategory, String?> base64PngCache,
required Map<FileCategory, Future<String?>> base64PngCache,
AppLocalizations? appLocalizations,
}) async {
final link = doc.sharingLink;
@@ -97,24 +94,22 @@ class DriveAttachmentHandler {
);
final category = _resolveCategory(doc);
return '<a href="$href" style="display:inline-block;vertical-align:top;width:${_cardWidthPx}px;'
'min-height:${_cardMinHeightPx}px;margin:0 8px 8px 0;border:1px solid #E5E7EB;'
'border-radius:10px;overflow:hidden;background:#FFFFFF;color:inherit;text-decoration:none;">'
'${await _iconZoneHtml(category, imagePaths: imagePath, base64PngCache: base64PngCache)}'
'<div style="padding:10px 12px;">'
'<div style="font-size:14px;font-weight:500;color:#1F2937;white-space:nowrap;'
'overflow:hidden;text-overflow:ellipsis;" title="$name">$name</div>'
'<div style="display:block;margin-top:6px;font-size:12px;'
'color:#0A84FF;white-space:nowrap;overflow:hidden;'
'text-overflow:ellipsis;">$openInDriveLabel ↗</div>'
'</div>'
'</a>';
return HtmlUtils.buildFileLinkCard(
href: href,
title: name,
actionLabel: openInDriveLabel,
iconZoneHtml: await _iconZoneHtml(
category,
imagePaths: imagePath,
base64PngCache: base64PngCache,
),
);
}
static Future<String> _iconZoneHtml(
Future<String> _iconZoneHtml(
FileCategory category, {
required ImagePaths imagePaths,
required Map<FileCategory, String?> base64PngCache,
required Map<FileCategory, Future<String?>> base64PngCache,
}) async {
final base64Png =
await _cachedCategoryPngBase64(
@@ -127,40 +122,28 @@ class DriveAttachmentHandler {
imagePaths: imagePaths,
base64PngCache: base64PngCache,
);
final content = base64Png != null
? '<img src="data:image/png;base64,$base64Png" width="$_iconSizePx" height="$_iconSizePx" '
'style="display:inline-block;vertical-align:middle;" />'
: '';
return '<div style="height:${_iconZoneHeightPx}px;line-height:${_iconZoneHeightPx}px;'
'text-align:center;background:#F5F6F8;border-bottom:1px solid #E5E7EB;">'
'$content'
'</div>';
return HtmlUtils.buildFileCardIconZone(iconBase64Png: base64Png);
}
static Future<String?> _cachedCategoryPngBase64(
Future<String?> _cachedCategoryPngBase64(
FileCategory category, {
required ImagePaths imagePaths,
required Map<FileCategory, String?> base64PngCache,
}) async {
if (base64PngCache.containsKey(category)) {
return base64PngCache[category];
}
final base64Png = await _loadCategoryPngBase64(
required Map<FileCategory, Future<String?>> base64PngCache,
}) {
return base64PngCache.putIfAbsent(
category,
imagePaths: imagePaths,
() => _loadCategoryPngBase64(category, imagePaths: imagePaths),
);
base64PngCache[category] = base64Png;
return base64Png;
}
static Future<String?> _loadCategoryPngBase64(
Future<String?> _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(
@@ -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(() {
@@ -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::', () {