todo-app/lib/ui/image_attach.dart
Krzysztof kuhy Rudnicki 3a747c2e51
Some checks failed
dependency freshness / dependencies on newest stable (push) Has been cancelled
file-length / file length <= 250 lines (push) Has been cancelled
md-naming / markdown naming convention (push) Has been cancelled
release-apk / release (APK) (push) Has been cancelled
repo-contract / repo contract (push) Has been cancelled
Report errors and slow waits to the board
crdt_sync_flutter 0.9.0's installAppErrors in main(): uncaught errors and
every spinner shown over 1 s (WaitWatch: sync.run, mirror.test) upload to
RTDB /todo/errors/<device> with the versionCode, where the PC app-errors
ingest files board tickets (kuhyx/utils DOCS-app-errors.md).

The app moves to material_ui first: sync_settings_ui 0.8.x and
design_system 0.4.x sit on it, and Flutter 3.50 deprecates
flutter/material. Pins: crdt_sync 0.14.1, csf 0.9.0, ssui 0.8.3,
gda 0.5.1, design_system 0.4.1, image 4.13.1, mcp_dart 2.4.4.
file_picker stays 13.3.3, held by data_transfer_flutter 0.2.2
(allowlisted, self-clearing).

appRoute takes a route name so each report names its screen; the
release build passes APP_BUILD/APP_VERSION. Nothing new on screen.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 23:07:58 +01:00

115 lines
3.7 KiB
Dart

/// Turning pasted image bytes and picked or dropped files into links in the
/// open note.
///
/// Shared by the capture and detail screens: each owns one
/// [ImageAttachController], hands it to its [NoteEditor] (which binds the
/// insert-at-cursor), and routes pastes through [attachImages] and picks and
/// drops through [attachFiles].
library;
import 'dart:typed_data';
import 'package:material_ui/material_ui.dart';
import 'package:todo/attachments/image_store_api.dart';
import 'package:todo/attachments/image_store_scope.dart';
import 'package:todo/images/attachment_ref.dart';
import 'package:todo/ui/cue_snack.dart';
/// Lets a screen insert lines into whichever editor is currently mounted.
class ImageAttachController {
void Function(List<String> lines)? _insert;
/// Called by the editor on mount with its insert-at-cursor function;
/// returns the call that undoes it (a no-op once another editor bound).
VoidCallback bind(void Function(List<String> lines) insert) {
_insert = insert;
return () {
if (identical(_insert, insert)) _insert = null;
};
}
/// Inserts [lines] at the editor's cursor; false when no editor is bound.
bool insert(List<String> lines) {
final insert = _insert;
if (insert == null || lines.isEmpty) return false;
insert(lines);
return true;
}
}
/// Stores each of [images] for the note [noteId] names and inserts their
/// links, in order, through [target].
///
/// [noteId] is a callback because the capture screen's draft only gets an id
/// once there is something to save; asking for it is what reserves one.
/// Reserving writes nothing, so a rejected paste still never creates an
/// empty note. [rejected] counts inputs already known
/// not to be images (e.g. a pasted PDF), so one message covers both.
Future<void> attachImages(
BuildContext context, {
required String Function() noteId,
required ImageAttachController target,
required List<Uint8List> images,
int rejected = 0,
}) async {
final store = ImageStoreScope.read(context);
final refs = <AttachmentRef>[];
var notImages = rejected;
String? failure;
for (final bytes in images) {
try {
final ref = await store.attach(noteId(), bytes);
if (ref == null) {
notImages++;
} else {
refs.add(ref);
}
} on ImageStoreException catch (e) {
failure = e.message;
break;
}
}
target.insert([for (final ref in refs) ref.imageMarkdown]);
if (!context.mounted) return;
if (failure != null) {
showCue(context, Icons.cloud_off, failure, error: true);
} else if (notImages > 0) {
// A broken-image glyph plus how many were refused.
showCue(
context,
Icons.broken_image_outlined,
notImages == 1 ? 'Not an image' : '$notImages files were not images',
error: true,
count: notImages,
);
}
}
/// Stores each of [files] (picked or dropped, any type and size) for the note
/// [noteId] names and inserts their links, in order, through [target]:
/// images inline, anything else as a file link. Stops at the first file that
/// cannot be stored, keeping the ones before it.
Future<void> attachFiles(
BuildContext context, {
required String Function() noteId,
required ImageAttachController target,
required List<XFile> files,
}) async {
final store = ImageStoreScope.read(context);
final attached = <Attachment>[];
String? failure;
for (final file in files) {
try {
attached.add(await store.attachFile(noteId(), file));
} on ImageStoreException catch (e) {
failure = e.message;
break;
}
}
target.insert([for (final attachment in attached) attachment.markdown]);
if (failure != null && context.mounted) {
showCue(context, Icons.cloud_off, failure, error: true);
}
}