mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-20 14:52:27 +01:00
Flutter's Linux embedder manages ~20fps at 3840x2160 -- measured on a stock `flutter create` counter app, so it is the toolkit rather than this code -- where the same Dart rendered by Chrome sustains ~144fps (evidence in ~/todo/docs/desktop-performance-findings.md). The desktop target therefore becomes a Flutter *web* build; Android is untouched. `dart:io` does not fail a web *compile* -- it resolves to a stub that throws at runtime -- so every platform edge moves behind a conditional export rather than being discovered later as a blank white window: - storage: `document_store*.dart`, files on Android and IndexedDB in the browser (mirrored to the desktop wrapper, which is what makes a wiped Chrome profile recoverable). Document names stay the exact pre-web filenames so an installed phone keeps reading its existing log. - photos: `blob_store*.dart` plus `attached_image*.dart`, since `Image.file` cannot resolve an IndexedDB key. The picker itself has web support; what changed is the storage and display model. Camera *capture* is genuinely lost on desktop -- and photos never crossed devices anyway, `imagePath` being stripped before push. - scheduling: `background_tasks*.dart`. A browser has no WorkManager equivalent -- a closed tab runs nothing -- so the desktop runs an in-page 5-minute check while its window is open. That loses nothing on the PC, where `diet-guard-gate.timer` already locks the screen on an unlogged slot, which is strictly stronger than a notification. - notifications: `notification_backend*.dart`, the browser Notifications API in place of flutter_local_notifications. - the GitHub token: `token_vault*.dart`. On web it is *not* held by the browser at all -- flutter_secure_storage's web backend is WebCrypto-wrapped localStorage with the wrap key beside the ciphertext, which is obfuscation, not encryption at rest -- so the desktop wrapper owns it and proxies the API (`github_client_factory*.dart`). `Platform.isAndroid` itself throws on web, so every platform branch now tests `kIsWeb` first; the battery-optimization row (permission_handler, no web implementation) is hidden off Android. The desktop pushes under device id `desktop`, not `phone`: sharing an id would have made the two devices overwrite each other's pushed log on every tick. The Python side enumerates device directories generically, so it needs no change. Tests move onto the same seams -- the file-backed stores keep the atomicity and unreadable-file guarantees where they belong. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
139 lines
4.3 KiB
Dart
139 lines
4.3 KiB
Dart
/// Shared attach/preview/remove control for a meal entry's optional photo.
|
|
library;
|
|
|
|
import 'package:diet_guard_app/screens/photo_viewer_screen.dart';
|
|
import 'package:diet_guard_app/services/photo_attach_service.dart';
|
|
import 'package:diet_guard_app/widgets/attached_image.dart';
|
|
import 'package:flutter/foundation.dart' show kIsWeb;
|
|
import 'package:flutter/material.dart';
|
|
import 'package:image_picker/image_picker.dart';
|
|
|
|
/// Shows an "Attach photo" button when [imagePath] is null, or a tappable
|
|
/// thumbnail (opens [PhotoViewerScreen] full-screen) plus a "Remove photo"
|
|
/// action once one is set.
|
|
///
|
|
/// Used identically by the single-item and composite-meal logging screens,
|
|
/// so the attach/preview/remove behavior only needs to be implemented once.
|
|
class PhotoAttachField extends StatelessWidget {
|
|
/// Creates a [PhotoAttachField].
|
|
const PhotoAttachField({
|
|
required this.imagePath,
|
|
required this.onChanged,
|
|
this.compact = false,
|
|
super.key,
|
|
});
|
|
|
|
/// The currently attached photo's local path, or null if none.
|
|
final String? imagePath;
|
|
|
|
/// Called with the new path after a successful pick, or null after the
|
|
/// user removes the current photo.
|
|
final ValueChanged<String?> onChanged;
|
|
|
|
/// Whether to render an icon-only button and a small thumbnail badge
|
|
/// instead of the default text button and 64x64 preview.
|
|
final bool compact;
|
|
|
|
Future<void> _attach(BuildContext context) async {
|
|
final source = await showModalBottomSheet<ImageSource>(
|
|
context: context,
|
|
builder: (sheetContext) => SafeArea(
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
// No camera row on web: the browser's picker is a file input,
|
|
// and `ImageSource.camera` silently falls back to the same file
|
|
// dialog -- an option that lies about what it does is worse than
|
|
// no option.
|
|
if (!kIsWeb)
|
|
ListTile(
|
|
leading: const Icon(Icons.photo_camera),
|
|
title: const Text('Take a photo'),
|
|
onTap: () => Navigator.of(sheetContext).pop(ImageSource.camera),
|
|
),
|
|
ListTile(
|
|
leading: const Icon(Icons.photo_library),
|
|
title: const Text(
|
|
kIsWeb ? 'Choose a file' : 'Choose from gallery',
|
|
),
|
|
onTap: () => Navigator.of(sheetContext).pop(ImageSource.gallery),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
if (source == null) return;
|
|
final path = await PhotoAttachService.instance.pickAndStore(source);
|
|
if (path != null) onChanged(path);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final path = imagePath;
|
|
if (path == null) {
|
|
if (compact) {
|
|
return Tooltip(
|
|
message: 'Attach photo',
|
|
child: IconButton(
|
|
onPressed: () => _attach(context),
|
|
icon: const Icon(Icons.add_a_photo),
|
|
),
|
|
);
|
|
}
|
|
return OutlinedButton.icon(
|
|
onPressed: () => _attach(context),
|
|
icon: const Icon(Icons.add_a_photo),
|
|
label: const Text('Attach photo'),
|
|
);
|
|
}
|
|
final thumbnailSize = compact ? 32.0 : 64.0;
|
|
final thumbnail = GestureDetector(
|
|
onTap: () => Navigator.of(context).push<void>(
|
|
MaterialPageRoute(builder: (_) => PhotoViewerScreen(path: path)),
|
|
),
|
|
child: ClipRRect(
|
|
borderRadius: BorderRadius.circular(8),
|
|
child: AttachedImage(
|
|
path: path,
|
|
width: thumbnailSize,
|
|
height: thumbnailSize,
|
|
fit: BoxFit.cover,
|
|
),
|
|
),
|
|
);
|
|
if (compact) {
|
|
return Stack(
|
|
clipBehavior: Clip.none,
|
|
children: [
|
|
thumbnail,
|
|
Positioned(
|
|
top: -6,
|
|
right: -6,
|
|
child: Tooltip(
|
|
message: 'Remove photo',
|
|
child: InkWell(
|
|
onTap: () => onChanged(null),
|
|
customBorder: const CircleBorder(),
|
|
child: const CircleAvatar(
|
|
radius: 9,
|
|
child: Icon(Icons.close, size: 12),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
return Row(
|
|
children: [
|
|
thumbnail,
|
|
const SizedBox(width: 8),
|
|
TextButton(
|
|
onPressed: () => onChanged(null),
|
|
child: const Text('Remove photo'),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
}
|