Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[mob][locker] Add in-app image viewer behind the internal flag (#13769)

aman-pilot8568150062ae
diff --git a/mobile/apps/locker/changes/image-viewer.md b/mobile/apps/locker/changes/image-viewer.md
new file mode 100644
index 0000000000..ea25010078
--- /dev/null
+++ b/mobile/apps/locker/changes/image-viewer.md
@@ -0,0 +1 @@
+- (i) Added a built-in image viewer with pinch and double-tap zoom.
diff --git a/mobile/apps/locker/lib/services/feature_flag_service.dart b/mobile/apps/locker/lib/services/feature_flag_service.dart
index 51f547655c..019e2d60eb 100644
--- a/mobile/apps/locker/lib/services/feature_flag_service.dart
+++ b/mobile/apps/locker/lib/services/feature_flag_service.dart
@@ -18,6 +18,7 @@ class FeatureFlagService {
bool get internalUser => _flagService.internalUser;
bool get multipartUpload => internalUser;
bool get pdfViewer => internalUser;
+ bool get imageViewer => internalUser;
bool get isInternalUserDisabled =>
_preferences.getBool(_internalUserDisabled) ?? false;
diff --git a/mobile/apps/locker/lib/ui/pages/image_viewer_page.dart b/mobile/apps/locker/lib/ui/pages/image_viewer_page.dart
new file mode 100644
index 0000000000..bf56071abd
--- /dev/null
+++ b/mobile/apps/locker/lib/ui/pages/image_viewer_page.dart
@@ -0,0 +1,305 @@
+import "dart:async";
+import "dart:io";
+
+import "package:ente_components/ente_components.dart";
+import "package:ente_strings/ente_strings.dart";
+import "package:flutter/material.dart";
+import "package:hugeicons/hugeicons.dart";
+import "package:logging/logging.dart";
+
+typedef ImageViewerAction = Future<void> Function(BuildContext);
+
+class ImageViewerPage extends StatefulWidget {
+ const ImageViewerPage({
+ super.key,
+ required this.localFile,
+ required this.fileName,
+ required this.onOpenExternally,
+ this.onDownload,
+ this.onShare,
+ });
+
+ final File localFile;
+ final String fileName;
+ final ImageViewerAction onOpenExternally;
+ final ImageViewerAction? onDownload;
+ final ImageViewerAction? onShare;
+
+ @override
+ State<ImageViewerPage> createState() => _ImageViewerPageState();
+}
+
+class _ImageViewerPageState extends State<ImageViewerPage> {
+ static final _logger = Logger("ImageViewerPage");
+ static const _maxScale = 3.0;
+ static const _doubleTapScale = 2.5;
+ static const _maxDimension = 3072;
+
+ final _transformation = TransformationController();
+ late final ImageProvider _image = ResizeImage(
+ FileImage(widget.localFile),
+ width: _maxDimension,
+ height: _maxDimension,
+ policy: ResizeImagePolicy.fit,
+ );
+ bool _failed = false;
+ bool _toolbarVisible = true;
+ Offset _doubleTapAt = Offset.zero;
+ Size _viewport = Size.zero;
+
+ @override
+ void dispose() {
+ _transformation.dispose();
+ unawaited(_image.evict());
+ super.dispose();
+ }
+
+ void _markFailed(Object error, StackTrace? stack) {
+ if (_failed) return;
+ _failed = true;
+ _logger.warning("Failed to decode image", error, stack);
+ WidgetsBinding.instance.addPostFrameCallback((_) {
+ if (mounted) setState(() {});
+ });
+ }
+
+ void _toggleZoom() {
+ final zoomedIn = _transformation.value.getMaxScaleOnAxis() > 1.01;
+ final scale = zoomedIn ? 1.0 : _doubleTapScale;
+ final scene = _transformation.toScene(_doubleTapAt);
+ final x = (_doubleTapAt.dx - scene.dx * scale).clamp(
+ _viewport.width * (1 - scale),
+ 0.0,
+ );
+ final y = (_doubleTapAt.dy - scene.dy * scale).clamp(
+ _viewport.height * (1 - scale),
+ 0.0,
+ );
+ _transformation.value = Matrix4.translationValues(x, y, 0)
+ ..scaleByDouble(scale, scale, 1, 1);
+ }
+
+ @override
+ Widget build(BuildContext context) {
+ final canHideToolbar =
+ !_failed && !MediaQuery.accessibleNavigationOf(context);
+ return Theme(
+ data: ComponentTheme.darkTheme(app: ComponentApp.locker),
+ child: Scaffold(
+ backgroundColor: Colors.black,
+ extendBodyBehindAppBar: true,
+ appBar: _Toolbar(
+ visible: _toolbarVisible || !canHideToolbar,
+ fileName: widget.fileName,
+ onShare: widget.onShare,
+ onDownload: widget.onDownload,
+ onOpenExternally: widget.onOpenExternally,
+ ),
+ body: GestureDetector(
+ behavior: HitTestBehavior.opaque,
+ excludeFromSemantics: true,
+ onTap: canHideToolbar
+ ? () => setState(() => _toolbarVisible = !_toolbarVisible)
+ : null,
+ onDoubleTapDown: (details) => _doubleTapAt = details.localPosition,
+ onDoubleTap: _failed ? null : _toggleZoom,
+ child: _failed
+ ? _ErrorMessage(onOpenExternally: widget.onOpenExternally)
+ : _buildImage(),
+ ),
+ ),
+ );
+ }
+
+ Widget _buildImage() {
+ return LayoutBuilder(
+ builder: (context, constraints) {
+ if (_viewport != Size.zero && _viewport != constraints.biggest) {
+ WidgetsBinding.instance.addPostFrameCallback((_) {
+ if (mounted) _transformation.value = Matrix4.identity();
+ });
+ }
+ _viewport = constraints.biggest;
+ return InteractiveViewer(
+ transformationController: _transformation,
+ minScale: 1,
+ maxScale: _maxScale,
+ child: Image(
+ image: _image,
+ width: _viewport.width,
+ height: _viewport.height,
+ fit: BoxFit.contain,
+ semanticLabel: widget.fileName,
+ frameBuilder: (context, child, frame, loadedSynchronously) {
+ if (loadedSynchronously) return child;
+ return Stack(
+ alignment: Alignment.center,
+ children: [
+ if (frame == null) const LoadingComponent(),
+ AnimatedOpacity(
+ opacity: frame == null ? 0 : 1,
+ duration: const Duration(milliseconds: 200),
+ curve: Curves.easeOut,
+ child: child,
+ ),
+ ],
+ );
+ },
+ errorBuilder: (context, error, stack) {
+ _markFailed(error, stack);
+ return const SizedBox.shrink();
+ },
+ ),
+ );
+ },
+ );
+ }
+}
+
+class _ErrorMessage extends StatelessWidget {
+ const _ErrorMessage({required this.onOpenExternally});
+
+ final ImageViewerAction onOpenExternally;
+
+ @override
+ Widget build(BuildContext context) {
+ final l10n = context.strings;
+ return Center(
+ child: SingleChildScrollView(
+ padding: EdgeInsets.fromLTRB(
+ Spacing.xxl,
+ MediaQuery.paddingOf(context).top + kToolbarHeight + Spacing.xxl,
+ Spacing.xxl,
+ Spacing.xxl,
+ ),
+ child: Column(
+ mainAxisSize: MainAxisSize.min,
+ crossAxisAlignment: CrossAxisAlignment.stretch,
+ children: [
+ Text(
+ l10n.errorOpeningFile,
+ textAlign: TextAlign.center,
+ style: TextStyles.body.copyWith(
+ color: context.componentColors.textBase,
+ ),
+ ),
+ const SizedBox(height: Spacing.xl),
+ ButtonComponent(
+ label: l10n.openInAnotherApp,
+ onTap: () => onOpenExternally(context),
+ ),
+ ],
+ ),
+ ),
+ );
+ }
+}
+
+class _Toolbar extends StatelessWidget implements PreferredSizeWidget {
+ const _Toolbar({
+ required this.visible,
+ required this.fileName,
+ required this.onOpenExternally,
+ this.onShare,
+ this.onDownload,
+ });
+
+ final bool visible;
+ final String fileName;
+ final ImageViewerAction onOpenExternally;
+ final ImageViewerAction? onShare;
+ final ImageViewerAction? onDownload;
+
+ @override
+ Size get preferredSize => const Size.fromHeight(kToolbarHeight);
+
+ @override
+ Widget build(BuildContext context) {
+ final colors = context.componentColors;
+ final l10n = context.strings;
+ return IgnorePointer(
+ ignoring: !visible,
+ child: AnimatedOpacity(
+ opacity: visible ? 1 : 0,
+ duration: MediaQuery.disableAnimationsOf(context)
+ ? Duration.zero
+ : const Duration(milliseconds: 150),
+ child: DecoratedBox(
+ decoration: BoxDecoration(
+ gradient: LinearGradient(
+ begin: Alignment.topCenter,
+ end: Alignment.bottomCenter,
+ colors: [
+ Colors.black.withValues(alpha: 0.72),
+ Colors.black.withValues(alpha: 0.6),
+ Colors.black.withValues(alpha: 0.05),
+ Colors.black.withValues(alpha: 0.025),
+ Colors.transparent,
+ ],
+ stops: const [0, 0.2, 0.9, 0.95, 1],
+ ),
+ ),
+ child: SafeArea(
+ bottom: false,
+ child: AppBar(
+ backgroundColor: Colors.transparent,
+ scrolledUnderElevation: 0,
+ centerTitle: false,
+ actionsPadding: const EdgeInsetsDirectional.only(end: Spacing.sm),
+ leading: IconButtonComponent(
+ icon: const HugeIcon(icon: HugeIcons.strokeRoundedArrowLeft01),
+ variant: IconButtonComponentVariant.unfilled,
+ tooltip: MaterialLocalizations.of(context).backButtonTooltip,
+ onTap: () => Navigator.maybePop(context),
+ ),
+ title: Text(
+ fileName,
+ maxLines: 1,
+ overflow: TextOverflow.ellipsis,
+ style: TextStyles.display3.copyWith(color: colors.textBase),
+ ),
+ actions: [
+ if (onShare != null)
+ IconButtonComponent(
+ icon: const HugeIcon(icon: HugeIcons.strokeRoundedShare08),
+ variant: IconButtonComponentVariant.unfilled,
+ tooltip: l10n.shareLink,
+ shouldSurfaceExecutionStates: false,
+ onTap: () => onShare!(context),
+ ),
+ if (onShare != null) const SizedBox(width: Spacing.md),
+ Builder(
+ builder: (buttonContext) => IconButtonComponent(
+ icon: const HugeIcon(
+ icon: HugeIcons.strokeRoundedMoreVertical,
+ ),
+ variant: IconButtonComponentVariant.unfilled,
+ tooltip: l10n.more,
+ shouldSurfaceExecutionStates: false,
+ onTap: () => _showMenu(buttonContext),
+ ),
+ ),
+ ],
+ ),
+ ),
+ ),
+ ),
+ );
+ }
+
+ Future<void> _showMenu(BuildContext context) async {
+ final l10n = context.strings;
+ final action = await showEntePopupMenu<ImageViewerAction>(
+ context: context,
+ options: [
+ if (onDownload != null)
+ EntePopupMenuOption(value: onDownload!, label: l10n.download),
+ EntePopupMenuOption(
+ value: onOpenExternally,
+ label: l10n.openInAnotherApp,
+ ),
+ ],
+ );
+ if (action != null && context.mounted) await action(context);
+ }
+}
diff --git a/mobile/apps/locker/lib/utils/file_util.dart b/mobile/apps/locker/lib/utils/file_util.dart
index 17b0818390..046b6d8702 100644
--- a/mobile/apps/locker/lib/utils/file_util.dart
+++ b/mobile/apps/locker/lib/utils/file_util.dart
@@ -21,6 +21,7 @@ import 'package:locker/ui/components/locker_progress_dialog.dart';
import "package:locker/ui/pages/account_credentials_page.dart";
import "package:locker/ui/pages/base_info_page.dart";
import "package:locker/ui/pages/emergency_contact_page.dart";
+import "package:locker/ui/pages/image_viewer_page.dart";
import "package:locker/ui/pages/pdf_viewer_page.dart";
import "package:locker/ui/pages/personal_note_page.dart";
import "package:locker/ui/pages/physical_records_page.dart";
@@ -399,32 +400,51 @@ class FileUtil {
file,
fallbackPath: localFile.path,
).toLowerCase();
- if (context == null ||
- !context.mounted ||
- extension != ".pdf" ||
- !FeatureFlagService.instance.pdfViewer) {
+ final flags = FeatureFlagService.instance;
+ final isPdf = extension == ".pdf" && flags.pdfViewer;
+ final isImage = _imageExtensions.contains(extension) && flags.imageViewer;
+ if (context == null || !context.mounted || !(isPdf || isImage)) {
return _openExternally(context, localFile, file);
}
final canShare =
file is! TrashFile &&
file.ownerID == Configuration.instance.getUserID();
+ Future<void> openExternally(BuildContext viewerContext) =>
+ _openExternally(viewerContext, localFile, file);
+ Future<void> download(BuildContext viewerContext) =>
+ downloadFile(viewerContext, file);
+ Future<void> share(BuildContext viewerContext) =>
+ FileActions.shareFileLink(viewerContext, file);
await Navigator.of(context).push(
MaterialPageRoute(
- builder: (_) => PdfViewerPage(
- localFile: localFile,
- fileName: file.displayName,
- onOpenExternally: (viewerContext) =>
- _openExternally(viewerContext, localFile, file),
- onDownload: (viewerContext) => downloadFile(viewerContext, file),
- onShare: canShare
- ? (viewerContext) =>
- FileActions.shareFileLink(viewerContext, file)
- : null,
- ),
+ builder: (_) => isPdf
+ ? PdfViewerPage(
+ localFile: localFile,
+ fileName: file.displayName,
+ onOpenExternally: openExternally,
+ onDownload: download,
+ onShare: canShare ? share : null,
+ )
+ : ImageViewerPage(
+ localFile: localFile,
+ fileName: file.displayName,
+ onOpenExternally: openExternally,
+ onDownload: download,
+ onShare: canShare ? share : null,
+ ),
),
);
}
+ static const _imageExtensions = {
+ ".jpg",
+ ".jpeg",
+ ".png",
+ ".webp",
+ ".heic",
+ ".heif",
+ };
+
static Future<void> _openExternally(
BuildContext? context,
File localFile,