Skip to content

armgit / ente

Public Imported from GitHub
0

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

Replace QR overlay button with long-press gesture detection

Amrithesh Kakkoth Ente44f66b209e5d
diff --git a/mobile/apps/photos/lib/ui/viewer/file/detail_page.dart b/mobile/apps/photos/lib/ui/viewer/file/detail_page.dart
index 59389a1e53..f2c80e5217 100644
--- a/mobile/apps/photos/lib/ui/viewer/file/detail_page.dart
+++ b/mobile/apps/photos/lib/ui/viewer/file/detail_page.dart
@@ -15,6 +15,7 @@ import "package:photos/generated/l10n.dart";
import "package:photos/models/file/extensions/file_props.dart";
import 'package:photos/models/file/file.dart';
import "package:photos/models/file/file_type.dart";
+import "package:photos/models/file/trash_file.dart";
import "package:photos/service_locator.dart";
import "package:photos/services/collections_service.dart";
import "package:photos/services/local_authentication_service.dart";
@@ -27,7 +28,8 @@ import "package:photos/ui/viewer/file/file_app_bar.dart";
import "package:photos/ui/viewer/file/file_bottom_bar.dart";
import 'package:photos/ui/viewer/file/file_widget.dart';
import "package:photos/ui/viewer/file/panorama_viewer_screen.dart";
-import "package:photos/ui/viewer/file/qr_code_overlay_button.dart";
+import "package:photos/ui/viewer/file/qr_code_content_sheet.dart";
+import "package:photos/ui/viewer/file/qr_code_detection_helper.dart";
import "package:photos/ui/viewer/file/text_detection_overlay_button.dart";
import 'package:photos/ui/viewer/gallery/gallery.dart';
import 'package:photos/utils/dialog_util.dart';
@@ -130,6 +132,8 @@ class _BodyState extends State<_Body> {
bool isGuestView = false;
bool swipeLocked = false;
late final StreamSubscription<GuestViewEvent> _guestViewEventSubscription;
+ QrCodeDetectionHelper? _qrHelper;
+ Timer? _longPressTimer;
@override
void initState() {
@@ -145,11 +149,15 @@ class _BodyState extends State<_Body> {
swipeLocked = event.swipeLocked;
});
});
+ if (flagService.qrFeatureEnabled) {
+ _qrHelper = QrCodeDetectionHelper();
+ }
// Update shared collection state after first frame
WidgetsBinding.instance.addPostFrameCallback((_) {
if (!mounted) return;
_updateSharedCollectionState(_files![_selectedIndexNotifier.value]);
+ _qrHelper?.evaluateFile(_files![_selectedIndexNotifier.value]);
widget.config.onPageReady?.call(context);
});
}
@@ -159,6 +167,8 @@ class _BodyState extends State<_Body> {
_guestViewEventSubscription.cancel();
_pageController.dispose();
_selectedIndexNotifier.dispose();
+ _longPressTimer?.cancel();
+ _qrHelper?.dispose();
super.dispose();
SystemChrome.setSystemUIOverlayStyle(
@@ -256,21 +266,6 @@ class _BodyState extends State<_Body> {
);
},
),
- if (flagService.qrFeatureEnabled)
- ValueListenableBuilder(
- valueListenable: _selectedIndexNotifier,
- builder: (BuildContext context, int selectedIndex, _) {
- return widget.config.mode == DetailPageMode.minimalistic
- ? const SizedBox.shrink()
- : QrCodeOverlayButton(
- file: _files![selectedIndex],
- enableFullScreenNotifier:
- InheritedDetailPageState.of(context)
- .enableFullScreenNotifier,
- isGuestView: isGuestView,
- );
- },
- ),
ValueListenableBuilder(
valueListenable: _selectedIndexNotifier,
builder: (BuildContext context, int selectedIndex, _) {
@@ -367,13 +362,30 @@ class _BodyState extends State<_Body> {
},
backgroundDecoration: const BoxDecoration(color: Colors.black),
);
- return GestureDetector(
- onTap: () {
- file.fileType != FileType.video
- ? InheritedDetailPageState.of(context).toggleFullScreenByUser()
- : null;
+ return Listener(
+ onPointerDown: (_) {
+ _longPressTimer?.cancel();
+ _longPressTimer = Timer(
+ const Duration(milliseconds: 500),
+ () => _onLongPress(context, file),
+ );
+ },
+ onPointerUp: (_) => _longPressTimer?.cancel(),
+ onPointerCancel: (_) => _longPressTimer?.cancel(),
+ onPointerMove: (event) {
+ if (event.delta.distance > 5) {
+ _longPressTimer?.cancel();
+ }
},
- child: fileContent,
+ child: GestureDetector(
+ onTap: () {
+ file.fileType != FileType.video
+ ? InheritedDetailPageState.of(context)
+ .toggleFullScreenByUser()
+ : null;
+ },
+ child: fileContent,
+ ),
);
},
onPageChanged: (index) {
@@ -390,6 +402,7 @@ class _BodyState extends State<_Body> {
}
Bus.instance.fire(GuestViewEvent(isGuestView, swipeLocked));
_updateSharedCollectionState(_files![index]);
+ _qrHelper?.evaluateFile(_files![index]);
},
physics: _shouldDisableScroll || swipeLocked
? const NeverScrollableScrollPhysics()
@@ -517,6 +530,20 @@ class _BodyState extends State<_Body> {
);
}
+ Future<void> _onLongPress(BuildContext context, EnteFile file) async {
+ if (file.fileType != FileType.image) return;
+ if (widget.config.mode == DetailPageMode.minimalistic) return;
+ if (isGuestView) return;
+ if (file is TrashFile) return;
+
+ final content = _qrHelper?.qrContentNotifier.value;
+ if (content == null || content.isEmpty) return;
+
+ await HapticFeedback.lightImpact();
+ if (!mounted) return;
+ await showQrCodeContentSheet(context, content: content);
+ }
+
Future<void> _updateSharedCollectionState(EnteFile file) async {
final fileID = file.uploadedFileID;
final notifier =
diff --git a/mobile/apps/photos/lib/ui/viewer/file/qr_code_detection_helper.dart b/mobile/apps/photos/lib/ui/viewer/file/qr_code_detection_helper.dart
new file mode 100644
index 0000000000..6d1816f7e7
--- /dev/null
+++ b/mobile/apps/photos/lib/ui/viewer/file/qr_code_detection_helper.dart
@@ -0,0 +1,102 @@
+import "dart:io";
+
+import "package:ente_qr/ente_qr.dart";
+import "package:flutter/foundation.dart";
+import "package:logging/logging.dart";
+import "package:photos/models/file/file.dart";
+import "package:photos/models/file/file_type.dart";
+import "package:photos/utils/file_util.dart";
+
+class QrCodeDetectionHelper {
+ static final Map<String, _DetectionResult> _cache = {};
+ final Logger _logger = Logger("QrCodeDetectionHelper");
+ final EnteQr _enteQr = EnteQr();
+
+ final ValueNotifier<String?> qrContentNotifier = ValueNotifier<String?>(null);
+
+ int _requestId = 0;
+ bool _disposed = false;
+
+ Future<void> evaluateFile(EnteFile file) async {
+ final bool isEligible = _isFileEligible(file);
+ final int requestId = ++_requestId;
+
+ if (!isEligible) {
+ qrContentNotifier.value = null;
+ return;
+ }
+
+ final String cacheKey = _cacheKey(file);
+ final _DetectionResult? cachedResult = _cache[cacheKey];
+ if (cachedResult != null) {
+ if (_disposed || requestId != _requestId) return;
+ qrContentNotifier.value = cachedResult.content;
+ return;
+ }
+
+ qrContentNotifier.value = null;
+
+ try {
+ final File? localFile = await getFile(file);
+ if (_disposed || requestId != _requestId) return;
+ if (localFile == null || !localFile.existsSync()) {
+ _cache[cacheKey] = const _DetectionResult(hasQr: false);
+ qrContentNotifier.value = null;
+ return;
+ }
+
+ bool hasQr = false;
+ String? qrContent;
+ try {
+ final result = await _enteQr.scanQrFromImage(localFile.path);
+ if (result.success && result.content != null) {
+ hasQr = true;
+ qrContent = result.content;
+ }
+ } catch (error, stackTrace) {
+ _logger.severe("Failed to scan QR code", error, stackTrace);
+ }
+
+ if (_disposed || requestId != _requestId) return;
+
+ final detectionResult = _DetectionResult(
+ hasQr: hasQr,
+ content: hasQr ? qrContent : null,
+ );
+ _cache[cacheKey] = detectionResult;
+ qrContentNotifier.value = detectionResult.content;
+ } catch (error, stackTrace) {
+ _logger.severe("QR code detection failed", error, stackTrace);
+ if (_disposed || requestId != _requestId) return;
+ _cache[cacheKey] = const _DetectionResult(hasQr: false);
+ qrContentNotifier.value = null;
+ }
+ }
+
+ bool _isFileEligible(EnteFile file) {
+ return file.fileType == FileType.image ||
+ file.fileType == FileType.livePhoto;
+ }
+
+ String _cacheKey(EnteFile file) {
+ if (file.uploadedFileID != null) {
+ return "qr_uploaded_${file.uploadedFileID}";
+ }
+ if (file.localID != null) {
+ return "qr_local_${file.localID}";
+ }
+ return "qr_generated_${file.generatedID}";
+ }
+
+ void dispose() {
+ _disposed = true;
+ qrContentNotifier.dispose();
+ }
+}
+
+class _DetectionResult {
+ final bool hasQr;
+ final String? content;
+
+ const _DetectionResult({required this.hasQr, this.content});
+}
diff --git a/mobile/apps/photos/lib/ui/viewer/file/qr_code_overlay_button.dart b/mobile/apps/photos/lib/ui/viewer/file/qr_code_overlay_button.dart
deleted file mode 100644
index 2fc8c1b6d4..0000000000
--- a/mobile/apps/photos/lib/ui/viewer/file/qr_code_overlay_button.dart
+++ /dev/null
@@ -1,286 +0,0 @@
-import "dart:io";
-
-import "package:ente_qr/ente_qr.dart";
-import "package:flutter/foundation.dart";
-import "package:flutter/material.dart";
-import "package:logging/logging.dart";
-import "package:photos/generated/l10n.dart";
-import "package:photos/models/file/file.dart";
-import "package:photos/models/file/file_type.dart";
-import "package:photos/models/file/trash_file.dart";
-import "package:photos/theme/ente_theme.dart";
-import "package:photos/ui/viewer/file/qr_code_content_sheet.dart";
-import "package:photos/utils/dialog_util.dart";
-import "package:photos/utils/file_util.dart";
-
-class QrCodeOverlayButton extends StatefulWidget {
- final EnteFile file;
- final ValueListenable<bool> enableFullScreenNotifier;
- final bool isGuestView;
-
- const QrCodeOverlayButton({
- required this.file,
- required this.enableFullScreenNotifier,
- required this.isGuestView,
- super.key,
- });
-
- @override
- State<QrCodeOverlayButton> createState() => _QrCodeOverlayButtonState();
-}
-
-class _QrCodeOverlayButtonState extends State<QrCodeOverlayButton> {
- static final Map<String, _DetectionResult> _cache = {};
- final Logger _logger = Logger("QrCodeOverlayButton");
- final EnteQr _enteQr = EnteQr();
-
- bool _isEligible = false;
- bool _hasQr = false;
- bool _isChecking = false;
- String? _qrContent;
- int _requestId = 0;
-
- @override
- void initState() {
- super.initState();
- _evaluateFile();
- }
-
- @override
- void didUpdateWidget(covariant QrCodeOverlayButton oldWidget) {
- super.didUpdateWidget(oldWidget);
- if (_didFileChange(oldWidget.file, widget.file)) {
- _evaluateFile();
- }
- }
-
- bool _didFileChange(EnteFile oldFile, EnteFile newFile) {
- if (oldFile.generatedID != newFile.generatedID) {
- return true;
- }
- if (oldFile.uploadedFileID != newFile.uploadedFileID) {
- return true;
- }
- if (oldFile.localID != newFile.localID) {
- return true;
- }
- return false;
- }
-
- Future<void> _evaluateFile() async {
- final bool isEligible = _isFileEligible(widget.file);
- final int requestId = ++_requestId;
-
- if (!isEligible) {
- setState(() {
- _isEligible = false;
- _hasQr = false;
- _qrContent = null;
- _isChecking = false;
- });
- return;
- }
-
- final String cacheKey = _cacheKey(widget.file);
- final _DetectionResult? cachedResult = _cache[cacheKey];
- if (cachedResult != null) {
- if (!mounted || requestId != _requestId) {
- return;
- }
- setState(() {
- _isEligible = true;
- _hasQr = cachedResult.hasQr;
- _qrContent = cachedResult.content;
- _isChecking = false;
- });
- return;
- }
-
- setState(() {
- _isEligible = true;
- _hasQr = false;
- _qrContent = null;
- _isChecking = true;
- });
-
- try {
- final File? localFile = await getFile(widget.file);
- if (!mounted || requestId != _requestId) {
- return;
- }
- if (localFile == null || !localFile.existsSync()) {
- _cache[cacheKey] = const _DetectionResult(hasQr: false);
- setState(() {
- _hasQr = false;
- _qrContent = null;
- _isChecking = false;
- });
- return;
- }
-
- bool hasQr = false;
- String? qrContent;
- try {
- final result = await _enteQr.scanQrFromImage(localFile.path);
- if (result.success && result.content != null) {
- hasQr = true;
- qrContent = result.content;
- }
- } catch (error, stackTrace) {
- _logger.severe("Failed to scan QR code", error, stackTrace);
- }
-
- if (!mounted || requestId != _requestId) {
- return;
- }
-
- final _DetectionResult result = _DetectionResult(
- hasQr: hasQr,
- content: hasQr ? qrContent : null,
- );
- _cache[cacheKey] = result;
- setState(() {
- _hasQr = result.hasQr;
- _qrContent = result.content;
- _isChecking = false;
- });
- } catch (error, stackTrace) {
- _logger.severe("QR code detection failed", error, stackTrace);
- if (!mounted || requestId != _requestId) {
- return;
- }
- _cache[cacheKey] = const _DetectionResult(hasQr: false);
- setState(() {
- _hasQr = false;
- _qrContent = null;
- _isChecking = false;
- });
- }
- }
-
- bool _isFileEligible(EnteFile file) {
- return file.fileType == FileType.image ||
- file.fileType == FileType.livePhoto;
- }
-
- String _cacheKey(EnteFile file) {
- if (file.uploadedFileID != null) {
- return "qr_uploaded_${file.uploadedFileID}";
- }
- if (file.localID != null) {
- return "qr_local_${file.localID}";
- }
- return "qr_generated_${file.generatedID}";
- }
-
- @override
- Widget build(BuildContext context) {
- if (!_isEligible || _isChecking || !_hasQr) {
- return const SizedBox.shrink();
- }
-
- return ValueListenableBuilder<bool>(
- valueListenable: widget.enableFullScreenNotifier,
- builder: (context, isFullScreen, _) {
- final bool shouldHide =
- isFullScreen || widget.isGuestView || widget.file is TrashFile;
- if (shouldHide) {
- return const SizedBox.shrink();
- }
-
- double bottomOffset = MediaQuery.paddingOf(context).bottom + 72.0;
-
- final caption = widget.file.caption;
- if (caption != null && caption.trim().isNotEmpty) {
- final textPainter = TextPainter(
- text: TextSpan(
- text: caption.trim(),
- style: getEnteTextTheme(context).mini,
- ),
- textDirection: TextDirection.ltr,
- textScaler: MediaQuery.textScalerOf(context),
- maxLines: 3,
- );
-
- final double maxWidth = MediaQuery.sizeOf(context).width - 16.0;
- textPainter.layout(maxWidth: maxWidth);
-
- bottomOffset += textPainter.height + 24.0;
- }
-
- // Position above the text detection button
- bottomOffset += 44.0;
-
- return Positioned(
- bottom: bottomOffset,
- left: 0,
- right: 0,
- child: Center(
- child: GestureDetector(
- onTap: _onPressed,
- child: Container(
- padding: const EdgeInsets.symmetric(
- horizontal: 12,
- vertical: 8,
- ),
- decoration: BoxDecoration(
- color: Colors.black.withAlpha(160),
- borderRadius: BorderRadius.circular(60),
- border: Border.all(
- color: Colors.white.withAlpha(60),
- width: 0.5,
- ),
- ),
- child: Row(
- mainAxisSize: MainAxisSize.min,
- children: [
- Container(
- width: 8,
- height: 8,
- decoration: BoxDecoration(
- color: getEnteColorScheme(context).primary700,
- shape: BoxShape.circle,
- ),
- ),
- const SizedBox(width: 8),
- Text(
- AppLocalizations.of(context).readQr,
- style: getEnteTextTheme(context).mini.copyWith(
- color: Colors.white,
- ),
- ),
- ],
- ),
- ),
- ),
- ),
- );
- },
- );
- }
-
- Future<void> _onPressed() async {
- try {
- final String? content = _qrContent;
- if (content == null || content.isEmpty) {
- throw Exception("QR code content is empty");
- }
- if (!mounted) {
- return;
- }
- await showQrCodeContentSheet(context, content: content);
- } catch (error, stackTrace) {
- _logger.severe("Failed to show QR content", error, stackTrace);
- if (mounted) {
- await showGenericErrorDialog(context: context, error: error);
- }
- }
- }
-}
-
-class _DetectionResult {
- final bool hasQr;
- final String? content;
-
- const _DetectionResult({required this.hasQr, this.content});
-}