Skip to content

armgit / ente

Public Imported from GitHub
0

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

Polish Locker document scanning flow

laurenspriem0de216afcc7d
diff --git a/mobile/apps/locker/lib/ui/pages/scanner/scanner_capture_page.dart b/mobile/apps/locker/lib/ui/pages/scanner/scanner_capture_page.dart
index f5bb19f611..1e1f98a38f 100644
--- a/mobile/apps/locker/lib/ui/pages/scanner/scanner_capture_page.dart
+++ b/mobile/apps/locker/lib/ui/pages/scanner/scanner_capture_page.dart
@@ -49,6 +49,14 @@ class _ScannerCapturePageState extends State<ScannerCapturePage>
super.initState();
WidgetsBinding.instance.addObserver(this);
_session.addListener(_onSessionChanged);
+ unawaited(_initialize());
+ }
+
+ Future<void> _initialize() async {
+ await SystemChrome.setPreferredOrientations(const [
+ DeviceOrientation.portraitUp,
+ ]);
+ if (!mounted) return;
unawaited(_initScanner());
unawaited(_startCamera());
}
@@ -69,6 +77,9 @@ class _ScannerCapturePageState extends State<ScannerCapturePage>
_session.removeListener(_onSessionChanged);
unawaited(_camera?.dispose());
unawaited(_session.disposeSession());
+ unawaited(
+ SystemChrome.setPreferredOrientations(const <DeviceOrientation>[]),
+ );
super.dispose();
}
@@ -122,16 +133,24 @@ class _ScannerCapturePageState extends State<ScannerCapturePage>
: ImageFormatGroup.yuv420,
);
await controller.initialize();
- if (!mounted) {
- await controller.dispose();
- return;
+ var retainedController = false;
+ try {
+ if (!mounted) return;
+ await controller.lockCaptureOrientation(DeviceOrientation.portraitUp);
+ if (!mounted) return;
+ await controller.startImageStream(_onFrame);
+ if (!mounted) return;
+ _torchOn = false;
+ setState(() {
+ _camera = controller;
+ _status = _CameraStatus.ready;
+ });
+ retainedController = true;
+ } finally {
+ if (!retainedController) {
+ await controller.dispose();
+ }
}
- await controller.startImageStream(_onFrame);
- _torchOn = false;
- setState(() {
- _camera = controller;
- _status = _CameraStatus.ready;
- });
} on CameraException catch (e) {
_logger.warning('Camera unavailable: ${e.code}', e);
if (!mounted) return;
diff --git a/mobile/apps/locker/lib/ui/pages/scanner/scanner_review_page.dart b/mobile/apps/locker/lib/ui/pages/scanner/scanner_review_page.dart
index 7b223dcba2..a710044913 100644
--- a/mobile/apps/locker/lib/ui/pages/scanner/scanner_review_page.dart
+++ b/mobile/apps/locker/lib/ui/pages/scanner/scanner_review_page.dart
@@ -33,6 +33,7 @@ class _ScannerReviewPageState extends State<ScannerReviewPage> {
late final PageController _pageController;
int _index = 0;
+ bool _operationInFlight = false;
@override
void initState() {
@@ -82,7 +83,17 @@ class _ScannerReviewPageState extends State<ScannerReviewPage> {
return widget.session.buildPdf();
}
- Future<void> _saveToEnte() async {
+ Future<void> _runExclusive(Future<void> Function() operation) async {
+ if (_operationInFlight) return;
+ setState(() => _operationInFlight = true);
+ try {
+ await operation();
+ } finally {
+ if (mounted) setState(() => _operationInFlight = false);
+ }
+ }
+
+ Future<void> _saveToEnte() => _runExclusive(() async {
final pdf = await _buildPdf();
if (!mounted) return;
final didUpload = await widget.onUploadFiles([pdf]);
@@ -91,12 +102,12 @@ class _ScannerReviewPageState extends State<ScannerReviewPage> {
showShortToast(context, context.strings.scanSaved);
Navigator.of(context).pop(true);
}
- }
+ });
- Future<void> _share() async {
+ Future<void> _share() => _runExclusive(() async {
final pdf = await _buildPdf();
await SharePlus.instance.share(ShareParams(files: [XFile(pdf.path)]));
- }
+ });
Future<void> _rename() async {
final l10n = context.strings;
@@ -114,7 +125,7 @@ class _ScannerReviewPageState extends State<ScannerReviewPage> {
);
}
- Future<void> _adjustCrop() async {
+ Future<void> _adjustCrop() => _runExclusive(() async {
final page = _currentPage;
if (page == null) return;
await Navigator.of(context).push(
@@ -123,19 +134,19 @@ class _ScannerReviewPageState extends State<ScannerReviewPage> {
ScannerCropPage(session: widget.session, pageId: page.id),
),
);
- }
+ });
- Future<void> _rotate() async {
+ Future<void> _rotate() => _runExclusive(() async {
final page = _currentPage;
if (page == null) return;
await widget.session.rotatePageClockwise(page.id);
- }
+ });
- Future<void> _delete() async {
+ Future<void> _delete() => _runExclusive(() async {
final page = _currentPage;
if (page == null) return;
await widget.session.deletePage(page.id);
- }
+ });
void _jumpToPage(int index) {
_pageController.animateToPage(
@@ -263,142 +274,151 @@ class _ScannerReviewPageState extends State<ScannerReviewPage> {
return Scaffold(
backgroundColor: colors.backgroundBase,
body: SafeArea(
- child: ListenableBuilder(
- listenable: widget.session,
- builder: (context, _) {
- final pages = widget.session.pages;
- final pageCount = pages.length;
- final current = pageCount == 0 ? 0 : _index.clamp(0, pageCount - 1);
- return Column(
- children: [
- Padding(
- padding: const EdgeInsets.symmetric(
- horizontal: Spacing.lg,
- vertical: Spacing.sm,
+ child: AbsorbPointer(
+ absorbing: _operationInFlight,
+ child: ListenableBuilder(
+ listenable: widget.session,
+ builder: (context, _) {
+ final pages = widget.session.pages;
+ final pageCount = pages.length;
+ final current = pageCount == 0
+ ? 0
+ : _index.clamp(0, pageCount - 1);
+ return Column(
+ children: [
+ Padding(
+ padding: const EdgeInsets.symmetric(
+ horizontal: Spacing.lg,
+ vertical: Spacing.sm,
+ ),
+ child: Row(
+ children: [
+ IconButtonComponent(
+ icon: HugeIcon(
+ icon: HugeIcons.strokeRoundedArrowLeft01,
+ color: colors.textBase,
+ ),
+ variant: IconButtonComponentVariant.unfilled,
+ onTap: () => Navigator.of(context).pop(false),
+ tooltip: l10n.addPage,
+ ),
+ Expanded(
+ child: Center(child: _buildFileNameField(colors)),
+ ),
+ IconButtonComponent(
+ icon: HugeIcon(
+ icon: HugeIcons.strokeRoundedCameraAdd01,
+ color: colors.textBase,
+ ),
+ variant: IconButtonComponentVariant.unfilled,
+ onTap: () => Navigator.of(context).pop(false),
+ tooltip: l10n.addPage,
+ ),
+ ],
+ ),
),
- child: Row(
+ Expanded(
+ child: pageCount == 0
+ ? const Center(child: CircularProgressIndicator())
+ : PageView.builder(
+ controller: _pageController,
+ itemCount: pageCount,
+ onPageChanged: (index) =>
+ setState(() => _index = index),
+ itemBuilder: (context, index) {
+ final page = pages[index];
+ return Padding(
+ padding: const EdgeInsets.symmetric(
+ horizontal: Spacing.lg,
+ vertical: Spacing.sm,
+ ),
+ child: Center(
+ child: ClipRRect(
+ borderRadius: BorderRadius.circular(
+ Radii.sm,
+ ),
+ child: Image.file(
+ page.processedJpeg,
+ key: ValueKey(page.processedJpeg.path),
+ fit: BoxFit.contain,
+ ),
+ ),
+ ),
+ );
+ },
+ ),
+ ),
+ if (pageCount > 0)
+ Text(
+ l10n.scanPageOfTotal(
+ current: current + 1,
+ total: pageCount,
+ ),
+ style: TextStyles.mini.copyWith(
+ color: colors.textLighter,
+ ),
+ ),
+ const SizedBox(height: Spacing.sm),
+ if (pageCount > 1) ...[
+ _buildThumbnailStrip(pages, current, colors),
+ const SizedBox(height: Spacing.sm),
+ ],
+ Row(
+ mainAxisAlignment: MainAxisAlignment.center,
children: [
IconButtonComponent(
icon: HugeIcon(
- icon: HugeIcons.strokeRoundedArrowLeft01,
+ icon: HugeIcons.strokeRoundedCrop,
color: colors.textBase,
),
- variant: IconButtonComponentVariant.unfilled,
- onTap: () => Navigator.of(context).pop(false),
- tooltip: l10n.addPage,
- ),
- Expanded(
- child: Center(child: _buildFileNameField(colors)),
+ onTap: pageCount == 0 ? null : _adjustCrop,
+ tooltip: l10n.adjustCrop,
),
+ const SizedBox(width: Spacing.lg),
IconButtonComponent(
icon: HugeIcon(
- icon: HugeIcons.strokeRoundedCameraAdd01,
+ icon: HugeIcons.strokeRoundedRotateClockwise,
color: colors.textBase,
),
- variant: IconButtonComponentVariant.unfilled,
- onTap: () => Navigator.of(context).pop(false),
- tooltip: l10n.addPage,
+ onTap: pageCount == 0 ? null : _rotate,
+ tooltip: l10n.rotate,
+ ),
+ const SizedBox(width: Spacing.lg),
+ IconButtonComponent(
+ icon: HugeIcon(
+ icon: HugeIcons.strokeRoundedDelete02,
+ color: colors.warning,
+ ),
+ onTap: pageCount == 0 ? null : _delete,
+ tooltip: l10n.delete,
),
],
),
- ),
- Expanded(
- child: pageCount == 0
- ? const Center(child: CircularProgressIndicator())
- : PageView.builder(
- controller: _pageController,
- itemCount: pageCount,
- onPageChanged: (index) =>
- setState(() => _index = index),
- itemBuilder: (context, index) {
- final page = pages[index];
- return Padding(
- padding: const EdgeInsets.symmetric(
- horizontal: Spacing.lg,
- vertical: Spacing.sm,
- ),
- child: Center(
- child: ClipRRect(
- borderRadius: BorderRadius.circular(Radii.sm),
- child: Image.file(
- page.processedJpeg,
- key: ValueKey(page.processedJpeg.path),
- fit: BoxFit.contain,
- ),
- ),
- ),
- );
- },
+ Padding(
+ padding: const EdgeInsets.all(Spacing.xl),
+ child: Column(
+ children: [
+ ButtonComponent(
+ label: l10n.saveToEnte,
+ onTap: pageCount == 0 && !widget.session.isProcessing
+ ? null
+ : _saveToEnte,
),
- ),
- if (pageCount > 0)
- Text(
- l10n.scanPageOfTotal(
- current: current + 1,
- total: pageCount,
+ const SizedBox(height: Spacing.md),
+ ButtonComponent(
+ label: l10n.share,
+ variant: ButtonComponentVariant.secondary,
+ onTap: pageCount == 0 && !widget.session.isProcessing
+ ? null
+ : _share,
+ ),
+ ],
),
- style: TextStyles.mini.copyWith(color: colors.textLighter),
),
- const SizedBox(height: Spacing.sm),
- if (pageCount > 1) ...[
- _buildThumbnailStrip(pages, current, colors),
- const SizedBox(height: Spacing.sm),
],
- Row(
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- IconButtonComponent(
- icon: HugeIcon(
- icon: HugeIcons.strokeRoundedCrop,
- color: colors.textBase,
- ),
- onTap: pageCount == 0 ? null : _adjustCrop,
- tooltip: l10n.adjustCrop,
- ),
- const SizedBox(width: Spacing.lg),
- IconButtonComponent(
- icon: HugeIcon(
- icon: HugeIcons.strokeRoundedRotateClockwise,
- color: colors.textBase,
- ),
- onTap: pageCount == 0 ? null : _rotate,
- tooltip: l10n.rotate,
- ),
- const SizedBox(width: Spacing.lg),
- IconButtonComponent(
- icon: HugeIcon(
- icon: HugeIcons.strokeRoundedDelete02,
- color: colors.warning,
- ),
- onTap: pageCount == 0 ? null : _delete,
- tooltip: l10n.delete,
- ),
- ],
- ),
- Padding(
- padding: const EdgeInsets.all(Spacing.xl),
- child: Column(
- children: [
- ButtonComponent(
- label: l10n.saveToEnte,
- onTap: pageCount == 0 && !widget.session.isProcessing
- ? null
- : _saveToEnte,
- ),
- const SizedBox(height: Spacing.md),
- ButtonComponent(
- label: l10n.share,
- variant: ButtonComponentVariant.secondary,
- onTap: pageCount == 0 && !widget.session.isProcessing
- ? null
- : _share,
- ),
- ],
- ),
- ),
- ],
- );
- },
+ );
+ },
+ ),
),
),
);
diff --git a/mobile/apps/locker/lib/ui/pages/uploader_page.dart b/mobile/apps/locker/lib/ui/pages/uploader_page.dart
index 07286600a3..5b76bf2442 100644
--- a/mobile/apps/locker/lib/ui/pages/uploader_page.dart
+++ b/mobile/apps/locker/lib/ui/pages/uploader_page.dart
@@ -24,6 +24,8 @@ abstract class UploaderPage extends BaseHomePage {
const UploaderPage({super.key});
}
+enum _UploadFilesOutcome { cancelled, succeeded, failed }
+
abstract class UploaderPageState<T extends UploaderPage> extends State<T> {
final _logger = Logger('UploaderPage');
@@ -48,16 +50,21 @@ abstract class UploaderPageState<T extends UploaderPage> extends State<T> {
.toList();
if (selectedFiles.isNotEmpty) {
- return await uploadFiles(selectedFiles);
+ final outcome = await _uploadFiles(selectedFiles);
+ return outcome != _UploadFilesOutcome.cancelled;
}
}
return false;
}
- Future<bool> uploadFiles(List<File> files) async {
- var didUpload = false;
+ Future<bool> uploadFiles(List<File> files) async =>
+ await _uploadFiles(files) == _UploadFilesOutcome.succeeded;
+
+ Future<_UploadFilesOutcome> _uploadFiles(List<File> files) async {
+ var outcome = _UploadFilesOutcome.cancelled;
var hasUploadError = false;
+ var completedPrimaryUploads = 0;
var didShowDialog = false;
final l10n = context.strings;
ProgressDialog? progressDialog;
@@ -69,7 +76,7 @@ abstract class UploaderPageState<T extends UploaderPage> extends State<T> {
.getCollectionsForUI();
if (!mounted) {
- return false;
+ return _UploadFilesOutcome.cancelled;
}
final uploadResult = await Navigator.of(context)
@@ -89,7 +96,7 @@ abstract class UploaderPageState<T extends UploaderPage> extends State<T> {
uploadResult != null && uploadResult.selectedCollections.isNotEmpty;
if (isUncategorizedUpload || isRegularUpload) {
- didUpload = true;
+ outcome = _UploadFilesOutcome.failed;
if (isUncategorizedUpload) {
final uncategorizedCollection = await CollectionService.instance
.getOrCreateUncategorizedCollection();
@@ -114,6 +121,7 @@ abstract class UploaderPageState<T extends UploaderPage> extends State<T> {
futures.add(
fileUploadFuture.then<void>(
(enteFile) async {
+ completedPrimaryUploads++;
completedUploads++;
if (didShowDialog &&
mounted &&
@@ -190,8 +198,16 @@ abstract class UploaderPageState<T extends UploaderPage> extends State<T> {
_logger.warning('Background sync failed after upload', e);
});
}
+ if (!hasUploadError) {
+ outcome = _UploadFilesOutcome.succeeded;
+ }
}
} catch (e, s) {
+ final didUploadAllFiles =
+ files.isNotEmpty && completedPrimaryUploads == files.length;
+ outcome = didUploadAllFiles
+ ? _UploadFilesOutcome.succeeded
+ : _UploadFilesOutcome.failed;
_logger.severe('Failed to complete file upload', e, s);
if (didShowDialog && progressDialog?.isShowing() == true) {
await progressDialog?.hide();
@@ -207,7 +223,7 @@ abstract class UploaderPageState<T extends UploaderPage> extends State<T> {
}
}
- return didUpload;
+ return outcome;
}
Future<void> _showUploadFailureError(Object error) async {
diff --git a/rust/crates/ml/tests/scan.rs b/rust/crates/ml/tests/scan.rs
deleted file mode 100644
index 9852434f84..0000000000
--- a/rust/crates/ml/tests/scan.rs
+++ /dev/null
@@ -1,174 +0,0 @@
-use std::io::Cursor;
-
-use ente_ml::scan::{ColorMode, PlaneLayout, Quad, ReprocessOptions, ScanOptions, ScannerSession};
-use image::{ImageFormat, Rgb, RgbImage};
-
-const MODEL_ENV: &str = "ENTE_DOC_SEGMENTATION_MODEL";
-
-fn session() -> Option<ScannerSession> {
- let Ok(path) = std::env::var(MODEL_ENV) else {
- eprintln!("skipping: {MODEL_ENV} is not set");
- return None;
- };
- Some(ScannerSession::new(&path).expect("model should load"))
-}
-
-fn synthetic_document() -> RgbImage {
- let (width, height) = (640u32, 480u32);
- let mut img = RgbImage::from_pixel(width, height, Rgb([40, 45, 50]));
- let (x0, y0, x1, y1) = (120u32, 80u32, 520u32, 400u32);
- for y in y0..y1 {
- for x in x0..x1 {
- img.put_pixel(x, y, Rgb([235, 233, 228]));
- }
- }
- for line in 0..8u32 {
- let ly = y0 + 30 + line * 36;
- for y in ly..ly + 6 {
- for x in x0 + 30..x1 - 30 {
- img.put_pixel(x, y, Rgb([60, 60, 65]));
- }
- }
- }
- img
-}
-
-fn encode(img: &RgbImage, format: ImageFormat) -> Vec<u8> {
- let mut bytes = Cursor::new(Vec::new());
- img.write_to(&mut bytes, format).expect("encode");
- bytes.into_inner()
-}
-
-fn assert_document_result(result: &ente_ml::scan::ScanResult) {
- let quad = result.quad.expect("the synthetic document should be found");
- for p in quad.corners() {
- assert!(p.x >= 0.0 && p.x <= result.source_width as f64, "{quad:?}");
- assert!(p.y >= 0.0 && p.y <= result.source_height as f64, "{quad:?}");
- }
- assert_eq!(result.source_width, 640);
- assert_eq!(result.source_height, 480);
- assert!(result.output_width > 100 && result.output_height > 100);
- assert_jpeg(&result.processed_image);
-}
-
-fn assert_jpeg(bytes: &[u8]) {
- assert_eq!(
- image::guess_format(bytes).expect("recognizable output"),
- ImageFormat::Jpeg
- );
-}
-
-#[test]
-fn processes_a_png_capture() {
- let Some(session) = session() else { return };
- let bytes = encode(&synthetic_document(), ImageFormat::Png);
-
- let result = session
- .process_capture(&bytes, &ScanOptions::default())
- .expect("process_capture");
- assert_document_result(&result);
-
- let decoded = image::load_from_memory(&result.processed_image).expect("valid JPEG output");
- assert_eq!(decoded.width(), result.output_width);
- assert_eq!(decoded.height(), result.output_height);
-}
-
-#[test]
-fn processes_a_jpeg_capture_and_reprocesses_it() {
- let Some(session) = session() else { return };
- let bytes = encode(&synthetic_document(), ImageFormat::Jpeg);
-
- let result = session
- .process_capture(&bytes, &ScanOptions::default())
- .expect("process_capture");
- assert_document_result(&result);
- image::load_from_memory(&result.processed_image).expect("valid JPEG output");
-
- let reprocessed = session
- .reprocess(
- &bytes,
- &ReprocessOptions {
- quad: result.quad.expect("quad"),
- rotation_degrees: 90,
- color_mode: ColorMode::Grayscale,
- max_pixels: Some(500_000),
- jpeg_quality: Some(60),
- },
- )
- .expect("reprocess");
- assert_eq!(reprocessed.color_mode, ColorMode::Grayscale);
- assert_jpeg(&reprocessed.processed_image);
- assert!(reprocessed.output_width < reprocessed.output_height);
- image::load_from_memory(&reprocessed.processed_image).expect("valid JPEG output");
-}
-
-#[test]
-fn live_detect_rgba_and_yuv_agree_on_a_grayscale_frame() {
- let Some(session) = session() else { return };
- let doc = synthetic_document();
- let (width, height) = (doc.width() as i32, doc.height() as i32);
-
- let mut rgba = Vec::with_capacity((width * height * 4) as usize);
- let mut y_plane = Vec::with_capacity((width * height) as usize);
- for px in doc.pixels() {
- let y = 16 + (px.0[0] as i32 * 219 / 255) as u8;
- let value = (((y as i32 - 16) * 1164 + 500) / 1000).clamp(0, 255) as u8;
- rgba.extend_from_slice(&[value, value, value, 255]);
- y_plane.push(y);
- }
- let chroma_len = ((width + 1) / 2 * (height + 1) / 2) as usize;
- let u_plane = vec![128u8; chroma_len];
- let v_plane = vec![128u8; chroma_len];
-
- let from_rgba = session
- .live_detect_rgba(&rgba, width as u32, height as u32, 0)
- .expect("live_detect_rgba")
- .expect("quad from RGBA");
- let from_yuv = session
- .live_detect_yuv420(
- &y_plane,
- &u_plane,
- &v_plane,
- PlaneLayout {
- width,
- height,
- y_row_stride: width,
- uv_row_stride: (width + 1) / 2,
- uv_pixel_stride: 1,
- },
- 0,
- )
- .expect("live_detect_yuv420")
- .expect("quad from YUV");
-
- let max_delta = quad_max_delta(&from_rgba, &from_yuv);
- assert!(
- max_delta < 2.0,
- "RGBA and YUV quads diverge by {max_delta} in mask space:\n{from_rgba:?}\n{from_yuv:?}"
- );
-
- let rotated = session
- .live_detect_rgba(&rgba, width as u32, height as u32, 180)
- .expect("live_detect_rgba rotated")
- .expect("quad");
- for p in rotated.corners() {
- assert!((0.0..=256.0).contains(&p.x) && (0.0..=256.0).contains(&p.y));
- }
-}
-
-#[test]
-fn rejects_undecodable_bytes_with_a_codec_error() {
- let Some(session) = session() else { return };
- let error = session
- .process_capture(b"not an image", &ScanOptions::default())
- .expect_err("garbage must not decode");
- assert!(matches!(error, ente_ml::scan::ScanError::Codec(_)));
-}
-
-fn quad_max_delta(a: &Quad, b: &Quad) -> f64 {
- a.corners()
- .iter()
- .zip(b.corners())
- .map(|(p, q)| (p.x - q.x).hypot(p.y - q.y))
- .fold(0.0, f64::max)
-}