Skip to content

armgit / ente

Public Imported from GitHub
0

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

Replace QR overlay with auto-popup detection

Amrithesh Kakkoth Ente71eef15e2c26
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 51a63fe0bf..488f265f2b 100644
--- a/mobile/apps/photos/lib/ui/viewer/file/detail_page.dart
+++ b/mobile/apps/photos/lib/ui/viewer/file/detail_page.dart
@@ -15,7 +15,6 @@ 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";
@@ -28,6 +27,7 @@ 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_content_sheet.dart";
import "package:photos/ui/viewer/file/qr_code_detection_helper.dart";
import "package:photos/ui/viewer/file/qr_code_highlight_overlay.dart";
import "package:photos/ui/viewer/file/text_detection_overlay_button.dart";
@@ -133,6 +133,7 @@ class _BodyState extends State<_Body> {
bool swipeLocked = false;
late final StreamSubscription<GuestViewEvent> _guestViewEventSubscription;
QrCodeDetectionHelper? _qrHelper;
+ bool _qrSheetOpen = false;
@override
void initState() {
@@ -150,6 +151,7 @@ class _BodyState extends State<_Body> {
});
if (flagService.qrFeatureEnabled) {
_qrHelper = QrCodeDetectionHelper();
+ _qrHelper!.qrContentNotifier.addListener(_onQrContentChanged);
}
// Update shared collection state after first frame
@@ -161,8 +163,25 @@ class _BodyState extends State<_Body> {
});
}
+ void _onQrContentChanged() {
+ if (!mounted || _qrSheetOpen) return;
+ final content = _qrHelper?.qrContentNotifier.value;
+ if (content == null) return;
+ _qrSheetOpen = true;
+ WidgetsBinding.instance.addPostFrameCallback((_) {
+ if (!mounted) {
+ _qrSheetOpen = false;
+ return;
+ }
+ showQrContentSheet(context, content: content).whenComplete(() {
+ _qrSheetOpen = false;
+ });
+ });
+ }
+
@override
void dispose() {
+ _qrHelper?.qrContentNotifier.removeListener(_onQrContentChanged);
_guestViewEventSubscription.cancel();
_pageController.dispose();
_selectedIndexNotifier.dispose();
@@ -264,29 +283,7 @@ class _BodyState extends State<_Body> {
);
},
),
- if (_qrHelper != null)
- ValueListenableBuilder(
- valueListenable: _selectedIndexNotifier,
- builder: (BuildContext context, int selectedIndex, _) {
- if (widget.config.mode == DetailPageMode.minimalistic ||
- isGuestView ||
- _files![selectedIndex] is TrashFile) {
- return const SizedBox.shrink();
- }
- return ValueListenableBuilder(
- valueListenable: _qrHelper!.qrDetectionsNotifier,
- builder: (context, detections, _) {
- return QrCodeHighlightOverlay(
- detections: detections,
- file: _files![selectedIndex],
- enableFullScreenNotifier:
- InheritedDetailPageState.of(context)
- .enableFullScreenNotifier,
- );
- },
- );
- },
- ),
+ if (_qrHelper != null) const QrCodeDetectionOverlay(),
ValueListenableBuilder(
valueListenable: _selectedIndexNotifier,
builder: (BuildContext context, int selectedIndex, _) {
diff --git a/mobile/apps/photos/lib/ui/viewer/file/qr_code_content_sheet.dart b/mobile/apps/photos/lib/ui/viewer/file/qr_code_content_sheet.dart
index 3c7850648e..3fefe538f1 100644
--- a/mobile/apps/photos/lib/ui/viewer/file/qr_code_content_sheet.dart
+++ b/mobile/apps/photos/lib/ui/viewer/file/qr_code_content_sheet.dart
@@ -29,6 +29,17 @@ Future<void> showQrCodeContentSheet(
);
}
+Future<void> showQrContentSheet(
+ BuildContext context, {
+ required String content,
+}) {
+ return showBaseBottomSheet(
+ context,
+ title: AppLocalizations.of(context).qrCode,
+ child: _QrContentEntry(content: content),
+ );
+}
+
class _QrContentEntry extends StatelessWidget {
final String content;
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
index c8b6232184..4adea8b521 100644
--- 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
@@ -9,14 +9,14 @@ import "package:photos/models/file/file_type.dart";
import "package:photos/utils/file_util.dart";
class QrCodeDetectionHelper {
- static final Map<String, _DetectionResult> _cache = {};
- static const _debounceDuration = Duration(milliseconds: 500);
+ static final Map<String, String?> _cache = {};
+ static const _debounceDuration = Duration(milliseconds: 1500);
final Logger _logger = Logger("QrCodeDetectionHelper");
final EnteQr _enteQr = EnteQr();
- final ValueNotifier<List<QrDetection>> qrDetectionsNotifier =
- ValueNotifier<List<QrDetection>>(const []);
+ /// null = no QR found, non-null = QR content string
+ final ValueNotifier<String?> qrContentNotifier = ValueNotifier<String?>(null);
int _requestId = 0;
bool _disposed = false;
@@ -29,20 +29,19 @@ class QrCodeDetectionHelper {
final int requestId = ++_requestId;
if (!isEligible) {
- qrDetectionsNotifier.value = const [];
+ qrContentNotifier.value = null;
return;
}
// Return cached results immediately without debounce
final String cacheKey = _cacheKey(file);
- final _DetectionResult? cachedResult = _cache[cacheKey];
- if (cachedResult != null) {
+ if (_cache.containsKey(cacheKey)) {
if (_disposed || requestId != _requestId) return;
- qrDetectionsNotifier.value = cachedResult.detections;
+ qrContentNotifier.value = _cache[cacheKey];
return;
}
- qrDetectionsNotifier.value = const [];
+ qrContentNotifier.value = null;
// Debounce uncached scans so swiping doesn't trigger work
_debounceTimer = Timer(_debounceDuration, () {
@@ -61,31 +60,30 @@ class QrCodeDetectionHelper {
final File? localFile = await getFile(file);
if (_disposed || requestId != _requestId) return;
if (localFile == null || !localFile.existsSync()) {
- _cache[cacheKey] = const _DetectionResult(detections: []);
- qrDetectionsNotifier.value = const [];
+ _cache[cacheKey] = null;
+ qrContentNotifier.value = null;
return;
}
- List<QrDetection> detections = const [];
+ String? content;
try {
- final result = await _enteQr.scanAllQrFromImage(localFile.path);
- if (result.success && result.detections.isNotEmpty) {
- detections = result.detections;
+ final result = await _enteQr.scanQrFromImage(localFile.path);
+ if (result.success && result.content != null) {
+ content = result.content;
}
} catch (error, stackTrace) {
- _logger.severe("Failed to scan QR codes", error, stackTrace);
+ _logger.severe("Failed to scan QR code", error, stackTrace);
}
if (_disposed || requestId != _requestId) return;
- final detectionResult = _DetectionResult(detections: detections);
- _cache[cacheKey] = detectionResult;
- qrDetectionsNotifier.value = detectionResult.detections;
+ _cache[cacheKey] = content;
+ qrContentNotifier.value = content;
} catch (error, stackTrace) {
_logger.severe("QR code detection failed", error, stackTrace);
if (_disposed || requestId != _requestId) return;
- _cache[cacheKey] = const _DetectionResult(detections: []);
- qrDetectionsNotifier.value = const [];
+ _cache[cacheKey] = null;
+ qrContentNotifier.value = null;
}
}
@@ -107,12 +105,6 @@ class QrCodeDetectionHelper {
void dispose() {
_disposed = true;
_debounceTimer?.cancel();
- qrDetectionsNotifier.dispose();
+ qrContentNotifier.dispose();
}
}
-
-class _DetectionResult {
- final List<QrDetection> detections;
-
- const _DetectionResult({required this.detections});
-}
diff --git a/mobile/apps/photos/lib/ui/viewer/file/qr_code_highlight_overlay.dart b/mobile/apps/photos/lib/ui/viewer/file/qr_code_highlight_overlay.dart
index ef64f2a275..ff13052533 100644
--- a/mobile/apps/photos/lib/ui/viewer/file/qr_code_highlight_overlay.dart
+++ b/mobile/apps/photos/lib/ui/viewer/file/qr_code_highlight_overlay.dart
@@ -1,173 +1,18 @@
-import "dart:async";
-
-import "package:ente_qr/ente_qr.dart";
-import "package:flutter/foundation.dart";
import "package:flutter/material.dart";
-import "package:flutter/services.dart";
-import "package:photos/models/file/file.dart";
-import "package:photos/ui/viewer/file/qr_code_content_sheet.dart";
-import "package:url_launcher/url_launcher.dart";
-
-class QrCodeHighlightOverlay extends StatelessWidget {
- final List<QrDetection> detections;
- final EnteFile file;
- final ValueListenable<bool> enableFullScreenNotifier;
- const QrCodeHighlightOverlay({
- required this.detections,
- required this.file,
- required this.enableFullScreenNotifier,
- super.key,
- });
+class QrCodeDetectionOverlay extends StatelessWidget {
+ const QrCodeDetectionOverlay({super.key});
@override
Widget build(BuildContext context) {
- if (detections.isEmpty || !file.hasDimensions) {
- return const SizedBox.shrink();
- }
-
- return ValueListenableBuilder<bool>(
- valueListenable: enableFullScreenNotifier,
- builder: (context, isFullScreen, _) {
- if (isFullScreen) {
- return const SizedBox.shrink();
- }
-
- return LayoutBuilder(
- builder: (context, constraints) {
- final screenWidth = constraints.maxWidth;
- final screenHeight = constraints.maxHeight;
- final imageAspect = file.width / file.height;
- final screenAspect = screenWidth / screenHeight;
-
- double displayWidth;
- double displayHeight;
- if (imageAspect > screenAspect) {
- displayWidth = screenWidth;
- displayHeight = screenWidth / imageAspect;
- } else {
- displayHeight = screenHeight;
- displayWidth = screenHeight * imageAspect;
- }
-
- final offsetX = (screenWidth - displayWidth) / 2;
- final offsetY = (screenHeight - displayHeight) / 2;
-
- return Stack(
- children: [
- for (final detection in detections)
- _QrTapRegion(
- detection: detection,
- offsetX: offsetX,
- offsetY: offsetY,
- displayWidth: displayWidth,
- displayHeight: displayHeight,
- ),
- ],
- );
- },
- );
- },
- );
- }
-}
-
-class _QrTapRegion extends StatefulWidget {
- final QrDetection detection;
- final double offsetX;
- final double offsetY;
- final double displayWidth;
- final double displayHeight;
-
- const _QrTapRegion({
- required this.detection,
- required this.offsetX,
- required this.offsetY,
- required this.displayWidth,
- required this.displayHeight,
- });
-
- @override
- State<_QrTapRegion> createState() => _QrTapRegionState();
-}
-
-class _QrTapRegionState extends State<_QrTapRegion> {
- static const _longPressDuration = Duration(milliseconds: 500);
- static const _moveThreshold = 20.0;
- Timer? _timer;
- Offset? _downPosition;
-
- @override
- void dispose() {
- _timer?.cancel();
- super.dispose();
- }
-
- Future<void> _onLongPress() async {
- await HapticFeedback.lightImpact();
- final uri = Uri.tryParse(widget.detection.content);
- final isUpi = uri != null && uri.scheme == "upi";
- if (isUpi) {
- try {
- final launched =
- await launchUrl(uri, mode: LaunchMode.externalApplication);
- if (!launched && mounted) {
- await showQrCodeContentSheet(
- context,
- detections: [widget.detection],
- );
- }
- } catch (_) {
- if (mounted) {
- await showQrCodeContentSheet(
- context,
- detections: [widget.detection],
- );
- }
- }
- } else {
- await showQrCodeContentSheet(
- context,
- detections: [widget.detection],
- );
- }
- }
-
- @override
- Widget build(BuildContext context) {
- final rawW = widget.detection.width * widget.displayWidth;
- final rawH = widget.detection.height * widget.displayHeight;
- const double minTapTarget = 48.0;
- final screenW = rawW < minTapTarget ? minTapTarget : rawW;
- final screenH = rawH < minTapTarget ? minTapTarget : rawH;
- final screenX = widget.offsetX +
- widget.detection.x * widget.displayWidth -
- (screenW - rawW) / 2;
- final screenY = widget.offsetY +
- widget.detection.y * widget.displayHeight -
- (screenH - rawH) / 2;
-
- return Positioned(
- left: screenX,
- top: screenY,
- width: screenW,
- height: screenH,
- child: Listener(
- behavior: HitTestBehavior.translucent,
- onPointerDown: (event) {
- _downPosition = event.position;
- _timer?.cancel();
- _timer = Timer(_longPressDuration, _onLongPress);
- },
- onPointerMove: (event) {
- if (_downPosition != null &&
- (event.position - _downPosition!).distance > _moveThreshold) {
- _timer?.cancel();
- }
- },
- onPointerUp: (_) => _timer?.cancel(),
- onPointerCancel: (_) => _timer?.cancel(),
- child: const SizedBox.expand(),
+ return const Center(
+ child: SizedBox(
+ width: 48,
+ height: 48,
+ child: CircularProgressIndicator(
+ strokeWidth: 3,
+ color: Colors.white,
+ ),
),
);
}