Merge branch 'claude/nostalgic-wright-acfc25': inline offer photos
Market offers carry a small base64 data-URI thumbnail embedded in the Nostr event (no media server); full photo stays in the encrypted inventory. Blossom alternative parked on branch parked/offer-photos-blossom. # Conflicts: # apps/app_seeds/lib/i18n/strings.g.dart
This commit is contained in:
commit
2c8869ea0d
20 changed files with 520 additions and 16 deletions
|
|
@ -174,6 +174,7 @@ class SeedLabelEntry extends Equatable {
|
|||
class ShareableLot extends Equatable {
|
||||
const ShareableLot({
|
||||
required this.lotId,
|
||||
required this.varietyId,
|
||||
required this.summary,
|
||||
required this.offerStatus,
|
||||
this.category,
|
||||
|
|
@ -183,6 +184,10 @@ class ShareableLot extends Equatable {
|
|||
|
||||
final String lotId;
|
||||
|
||||
/// The lot's variety, so the publish step can fetch its cover photo to host
|
||||
/// as the offer image. Not published itself — never leaves the device.
|
||||
final String varietyId;
|
||||
|
||||
/// What to publish: the variety label (never the full inventory).
|
||||
final String summary;
|
||||
|
||||
|
|
@ -197,7 +202,7 @@ class ShareableLot extends Equatable {
|
|||
|
||||
@override
|
||||
List<Object?> get props =>
|
||||
[lotId, summary, offerStatus, category, harvestYear, isOrganic];
|
||||
[lotId, varietyId, summary, offerStatus, category, harvestYear, isOrganic];
|
||||
}
|
||||
|
||||
/// One germination test on a lot; [rate] is derived (0..1), null when it can't
|
||||
|
|
@ -727,6 +732,12 @@ class VarietyRepository {
|
|||
return byVariety;
|
||||
}
|
||||
|
||||
/// The cover photo (lowest `sortOrder`) for a single variety, or null when it
|
||||
/// has none. Used by the publish step to host an offer's image; reuses the
|
||||
/// same "first photo" rule as the inventory avatar.
|
||||
Future<Uint8List?> coverPhotoFor(String varietyId) async =>
|
||||
(await _firstPhotosFor([varietyId]))[varietyId];
|
||||
|
||||
/// Maps each of [speciesIds] to its scientific name (one query).
|
||||
Future<Map<String, String>> _scientificNamesFor(
|
||||
Set<String> speciesIds,
|
||||
|
|
@ -1461,6 +1472,7 @@ class VarietyRepository {
|
|||
if (byId[l.varietyId] case final v?)
|
||||
ShareableLot(
|
||||
lotId: l.id,
|
||||
varietyId: v.id,
|
||||
summary: v.label,
|
||||
offerStatus: l.offerStatus,
|
||||
category: v.category,
|
||||
|
|
|
|||
|
|
@ -482,7 +482,8 @@
|
|||
"sharedBy": "Compartíu por",
|
||||
"noProfile": "Esta persona entá nun compartió'l so perfil",
|
||||
"copyId": "Copiar códigu",
|
||||
"idCopied": "Códigu copiáu"
|
||||
"idCopied": "Códigu copiáu",
|
||||
"photo": "Semeya"
|
||||
},
|
||||
"profile": {
|
||||
"title": "El to perfil",
|
||||
|
|
|
|||
|
|
@ -485,7 +485,8 @@
|
|||
"sharedBy": "Shared by",
|
||||
"noProfile": "This person hasn't shared a profile yet",
|
||||
"copyId": "Copy code",
|
||||
"idCopied": "Code copied"
|
||||
"idCopied": "Code copied",
|
||||
"photo": "Photo"
|
||||
},
|
||||
"profile": {
|
||||
"title": "Your profile",
|
||||
|
|
|
|||
|
|
@ -484,7 +484,8 @@
|
|||
"sharedBy": "Compartido por",
|
||||
"noProfile": "Esta persona aún no ha compartido su perfil",
|
||||
"copyId": "Copiar código",
|
||||
"idCopied": "Código copiado"
|
||||
"idCopied": "Código copiado",
|
||||
"photo": "Foto"
|
||||
},
|
||||
"profile": {
|
||||
"title": "Tu perfil",
|
||||
|
|
|
|||
|
|
@ -481,7 +481,8 @@
|
|||
"sharedBy": "Partilhado por",
|
||||
"noProfile": "Esta pessoa ainda não partilhou o seu perfil",
|
||||
"copyId": "Copiar código",
|
||||
"idCopied": "Código copiado"
|
||||
"idCopied": "Código copiado",
|
||||
"photo": "Foto"
|
||||
},
|
||||
"profile": {
|
||||
"title": "O teu perfil",
|
||||
|
|
|
|||
|
|
@ -4,9 +4,9 @@
|
|||
/// To regenerate, run: `dart run slang`
|
||||
///
|
||||
/// Locales: 4
|
||||
/// Strings: 1664 (416 per locale)
|
||||
/// Strings: 1668 (417 per locale)
|
||||
///
|
||||
/// Built on 2026-07-10 at 19:36 UTC
|
||||
/// Built on 2026-07-10 at 19:42 UTC
|
||||
|
||||
// coverage:ignore-file
|
||||
// ignore_for_file: type=lint, unused_import
|
||||
|
|
|
|||
|
|
@ -693,6 +693,7 @@ class _Translations$market$ast extends Translations$market$en {
|
|||
@override String get noProfile => 'Esta persona entá nun compartió\'l so perfil';
|
||||
@override String get copyId => 'Copiar códigu';
|
||||
@override String get idCopied => 'Códigu copiáu';
|
||||
@override String get photo => 'Semeya';
|
||||
}
|
||||
|
||||
// Path: profile
|
||||
|
|
@ -1505,6 +1506,7 @@ extension on TranslationsAst {
|
|||
'market.noProfile' => 'Esta persona entá nun compartió\'l so perfil',
|
||||
'market.copyId' => 'Copiar códigu',
|
||||
'market.idCopied' => 'Códigu copiáu',
|
||||
'market.photo' => 'Semeya',
|
||||
'profile.title' => 'El to perfil',
|
||||
'profile.name' => 'Nome',
|
||||
'profile.nameHint' => 'Cómo te ven los demás',
|
||||
|
|
|
|||
|
|
@ -1284,6 +1284,9 @@ class Translations$market$en {
|
|||
|
||||
/// en: 'Code copied'
|
||||
String get idCopied => 'Code copied';
|
||||
|
||||
/// en: 'Photo'
|
||||
String get photo => 'Photo';
|
||||
}
|
||||
|
||||
// Path: profile
|
||||
|
|
@ -2327,6 +2330,7 @@ extension on Translations {
|
|||
'market.noProfile' => 'This person hasn\'t shared a profile yet',
|
||||
'market.copyId' => 'Copy code',
|
||||
'market.idCopied' => 'Code copied',
|
||||
'market.photo' => 'Photo',
|
||||
'profile.title' => 'Your profile',
|
||||
'profile.name' => 'Display name',
|
||||
'profile.nameHint' => 'How others see you',
|
||||
|
|
|
|||
|
|
@ -695,6 +695,7 @@ class _Translations$market$es extends Translations$market$en {
|
|||
@override String get noProfile => 'Esta persona aún no ha compartido su perfil';
|
||||
@override String get copyId => 'Copiar código';
|
||||
@override String get idCopied => 'Código copiado';
|
||||
@override String get photo => 'Foto';
|
||||
}
|
||||
|
||||
// Path: profile
|
||||
|
|
@ -1509,6 +1510,7 @@ extension on TranslationsEs {
|
|||
'market.noProfile' => 'Esta persona aún no ha compartido su perfil',
|
||||
'market.copyId' => 'Copiar código',
|
||||
'market.idCopied' => 'Código copiado',
|
||||
'market.photo' => 'Foto',
|
||||
'profile.title' => 'Tu perfil',
|
||||
'profile.name' => 'Nombre',
|
||||
'profile.nameHint' => 'Cómo te ven los demás',
|
||||
|
|
|
|||
|
|
@ -692,6 +692,7 @@ class _Translations$market$pt extends Translations$market$en {
|
|||
@override String get noProfile => 'Esta pessoa ainda não partilhou o seu perfil';
|
||||
@override String get copyId => 'Copiar código';
|
||||
@override String get idCopied => 'Código copiado';
|
||||
@override String get photo => 'Foto';
|
||||
}
|
||||
|
||||
// Path: profile
|
||||
|
|
@ -1503,6 +1504,7 @@ extension on TranslationsPt {
|
|||
'market.noProfile' => 'Esta pessoa ainda não partilhou o seu perfil',
|
||||
'market.copyId' => 'Copiar código',
|
||||
'market.idCopied' => 'Código copiado',
|
||||
'market.photo' => 'Foto',
|
||||
'profile.title' => 'O teu perfil',
|
||||
'profile.name' => 'Nome',
|
||||
'profile.nameHint' => 'Como os outros te veem',
|
||||
|
|
|
|||
56
apps/app_seeds/lib/services/offer_thumbnail.dart
Normal file
56
apps/app_seeds/lib/services/offer_thumbnail.dart
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
import 'dart:convert';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:image/image.dart' as img;
|
||||
|
||||
/// Builds a tiny `data:image/jpeg;base64,…` thumbnail from a full photo, small
|
||||
/// enough to ride *inside* a Nostr offer event — no media server, fully
|
||||
/// decentralized. The full-resolution photo stays in the encrypted local
|
||||
/// inventory; only this shrunken preview is published so peers can see the seed.
|
||||
///
|
||||
/// Relays cap event size, so we downscale (longest edge) and re-encode until the
|
||||
/// base64 fits under [maxBytes]. Returns null when the bytes aren't a decodable
|
||||
/// image or no size small enough is reachable — the offer then publishes without
|
||||
/// a photo (graceful, never a hard failure).
|
||||
String? offerThumbnailDataUri(
|
||||
Uint8List bytes, {
|
||||
int maxBytes = 40000,
|
||||
List<int> edges = const [320, 256, 192, 128],
|
||||
int quality = 68,
|
||||
}) {
|
||||
try {
|
||||
final decoded = img.decodeImage(bytes);
|
||||
if (decoded == null) return null;
|
||||
|
||||
for (final edge in edges) {
|
||||
final resized = _fitWithin(decoded, edge);
|
||||
final jpeg = img.encodeJpg(resized, quality: quality);
|
||||
final b64 = base64.encode(jpeg);
|
||||
if (b64.length <= maxBytes) return 'data:image/jpeg;base64,$b64';
|
||||
}
|
||||
return null; // even the smallest edge didn't fit — skip the image
|
||||
} catch (_) {
|
||||
return null; // undecodable/corrupt bytes — publish without a photo
|
||||
}
|
||||
}
|
||||
|
||||
/// Extracts the raw bytes from a `data:...;base64,…` URI, or null when [uri] is
|
||||
/// not a base64 data URI. Used by the UI to render an inline thumbnail with
|
||||
/// `Image.memory` instead of a network fetch.
|
||||
Uint8List? decodeDataUri(String uri) {
|
||||
if (!uri.startsWith('data:')) return null;
|
||||
try {
|
||||
return Uri.parse(uri).data?.contentAsBytes();
|
||||
} on FormatException {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// Resizes [image] so its longest edge is at most [edge], preserving aspect
|
||||
/// ratio. Never upscales a photo that's already smaller.
|
||||
img.Image _fitWithin(img.Image image, int edge) {
|
||||
if (image.width <= edge && image.height <= edge) return image;
|
||||
return image.width >= image.height
|
||||
? img.copyResize(image, width: edge)
|
||||
: img.copyResize(image, height: edge);
|
||||
}
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
import 'dart:async';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:commons_core/commons_core.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
|
|
@ -7,6 +8,7 @@ import 'package:flutter_bloc/flutter_bloc.dart';
|
|||
import '../data/variety_repository.dart';
|
||||
import '../services/offer_mapper.dart';
|
||||
import '../services/offer_outbox.dart';
|
||||
import '../services/offer_thumbnail.dart';
|
||||
import '../services/social_service.dart';
|
||||
import '../services/social_settings.dart';
|
||||
|
||||
|
|
@ -134,12 +136,26 @@ class OffersState extends Equatable {
|
|||
/// UI stays offline-tolerant (a null transport = the social layer is unavailable
|
||||
/// and the screen degrades gracefully).
|
||||
class OffersCubit extends Cubit<OffersState> {
|
||||
OffersCubit(this._transport, {Future<void> Function()? onDispose})
|
||||
: _onDispose = onDispose,
|
||||
OffersCubit(
|
||||
this._transport, {
|
||||
Future<Uint8List?> Function(String varietyId)? coverPhoto,
|
||||
String? Function(Uint8List bytes)? thumbnail,
|
||||
Future<void> Function()? onDispose,
|
||||
}) : _coverPhoto = coverPhoto,
|
||||
_thumbnail = thumbnail,
|
||||
_onDispose = onDispose,
|
||||
super(const OffersState());
|
||||
|
||||
final OfferTransport? _transport;
|
||||
|
||||
/// Fetches a variety's cover photo bytes; null in tests or when no inventory
|
||||
/// repo is wired.
|
||||
final Future<Uint8List?> Function(String varietyId)? _coverPhoto;
|
||||
|
||||
/// Turns full photo bytes into a small `data:` thumbnail embedded in the offer
|
||||
/// (no media server). Null → offers publish without a photo.
|
||||
final String? Function(Uint8List bytes)? _thumbnail;
|
||||
|
||||
/// Closes the owning [SocialSession]/connection when the cubit is disposed
|
||||
/// (null in tests, where the transport is a fake with nothing to close).
|
||||
final Future<void> Function()? _onDispose;
|
||||
|
|
@ -269,6 +285,7 @@ class OffersCubit extends Cubit<OffersState> {
|
|||
areaGeohash: areaGeohash,
|
||||
category: lot.category,
|
||||
isOrganic: lot.isOrganic,
|
||||
imageUrl: await _coverThumbnail(lot.varietyId),
|
||||
);
|
||||
final result = await transport.publish(offer);
|
||||
if (result.accepted) accepted++;
|
||||
|
|
@ -281,6 +298,22 @@ class OffersCubit extends Cubit<OffersState> {
|
|||
return accepted;
|
||||
}
|
||||
|
||||
/// Builds a small inline `data:` thumbnail from the lot's cover photo to embed
|
||||
/// in the offer, or null when there's no photo, no thumbnailer, or it can't be
|
||||
/// shrunk to fit. Best-effort: a missing image never blocks publishing.
|
||||
Future<String?> _coverThumbnail(String varietyId) async {
|
||||
final coverPhoto = _coverPhoto;
|
||||
final thumbnail = _thumbnail;
|
||||
if (coverPhoto == null || thumbnail == null) return null;
|
||||
try {
|
||||
final Uint8List? bytes = await coverPhoto(varietyId);
|
||||
if (bytes == null || bytes.isEmpty) return null;
|
||||
return thumbnail(bytes);
|
||||
} catch (_) {
|
||||
return null; // degrade: publish the offer without a photo
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> close() async {
|
||||
_searchTimeout?.cancel();
|
||||
|
|
@ -295,13 +328,19 @@ class OffersCubit extends Cubit<OffersState> {
|
|||
/// fails), the cubit is created with a null transport and the screen still opens.
|
||||
Future<OffersCubit> createOffersCubit(
|
||||
SocialService social,
|
||||
SocialSettings settings,
|
||||
) async {
|
||||
SocialSettings settings, {
|
||||
VarietyRepository? repository,
|
||||
}) async {
|
||||
final relays = await settings.relayUrls();
|
||||
if (relays.isEmpty) return OffersCubit(null);
|
||||
try {
|
||||
final session = await social.openSession(relays);
|
||||
return OffersCubit(session.offers, onDispose: session.close);
|
||||
return OffersCubit(
|
||||
session.offers,
|
||||
coverPhoto: repository?.coverPhotoFor,
|
||||
thumbnail: offerThumbnailDataUri,
|
||||
onDispose: session.close,
|
||||
);
|
||||
} catch (_) {
|
||||
return OffersCubit(null); // offline / no relay reachable
|
||||
}
|
||||
|
|
|
|||
|
|
@ -123,6 +123,10 @@ class _MarketOfferDetailScreenState extends State<MarketOfferDetailScreen> {
|
|||
body: ListView(
|
||||
padding: const EdgeInsets.all(16),
|
||||
children: [
|
||||
if (o.imageUrl != null) ...[
|
||||
OfferHeroImage(url: o.imageUrl!, semanticLabel: t.market.photo),
|
||||
const SizedBox(height: 16),
|
||||
],
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
|
|
|
|||
|
|
@ -58,7 +58,11 @@ class _MarketScreenState extends State<MarketScreen> {
|
|||
final repo =
|
||||
widget.outbox != null ? context.read<VarietyRepository>() : null;
|
||||
setState(() => _loading = true);
|
||||
final cubit = await createOffersCubit(widget.social, widget.settings);
|
||||
final cubit = await createOffersCubit(
|
||||
widget.social,
|
||||
widget.settings,
|
||||
repository: repo,
|
||||
);
|
||||
final area = await widget.settings.areaGeohash();
|
||||
if (!mounted) {
|
||||
await cubit.close();
|
||||
|
|
@ -380,6 +384,13 @@ class _OfferCard extends StatelessWidget {
|
|||
children: [
|
||||
Row(
|
||||
children: [
|
||||
if (offer.imageUrl != null) ...[
|
||||
OfferThumbnail(
|
||||
url: offer.imageUrl!,
|
||||
semanticLabel: t.market.photo,
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
],
|
||||
Expanded(
|
||||
child: Text(
|
||||
offer.summary,
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ import 'package:commons_core/commons_core.dart';
|
|||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../i18n/strings.g.dart';
|
||||
import '../services/offer_thumbnail.dart';
|
||||
import 'theme.dart';
|
||||
|
||||
/// Human words for an offer's reciprocity mode. Shared by the market list and
|
||||
|
|
@ -14,6 +15,122 @@ String offerTypeLabel(Translations t, OfferType type) => switch (type) {
|
|||
OfferType.lend => t.share.exchange,
|
||||
};
|
||||
|
||||
/// A small square thumbnail of an offer's photo for the market list card.
|
||||
/// The image usually rides inline in the offer as a `data:` URI (decentralized,
|
||||
/// no server), so it renders straight from memory; an `http(s)` URL is still
|
||||
/// supported for interop and degrades gracefully (spinner, then placeholder).
|
||||
class OfferThumbnail extends StatelessWidget {
|
||||
const OfferThumbnail({
|
||||
required this.url,
|
||||
required this.semanticLabel,
|
||||
this.size = 56,
|
||||
super.key,
|
||||
});
|
||||
|
||||
final String url;
|
||||
final String semanticLabel;
|
||||
final double size;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ClipRRect(
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
child: _offerImage(
|
||||
url,
|
||||
semanticLabel: semanticLabel,
|
||||
width: size,
|
||||
height: size,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Full-width "hero" image atop the offer detail page. Same source handling as
|
||||
/// [OfferThumbnail]; hidden entirely when the offer has no photo.
|
||||
class OfferHeroImage extends StatelessWidget {
|
||||
const OfferHeroImage({
|
||||
required this.url,
|
||||
required this.semanticLabel,
|
||||
super.key,
|
||||
});
|
||||
|
||||
final String url;
|
||||
final String semanticLabel;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ClipRRect(
|
||||
borderRadius: BorderRadius.circular(14),
|
||||
child: AspectRatio(
|
||||
aspectRatio: 4 / 3,
|
||||
child: _offerImage(url, semanticLabel: semanticLabel),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Renders an offer image from either an inline `data:` URI (via memory) or a
|
||||
/// remote URL (via network), always cropping to fill and falling back to a
|
||||
/// neutral placeholder — a broken image must never block the card.
|
||||
Widget _offerImage(
|
||||
String url, {
|
||||
required String semanticLabel,
|
||||
double? width,
|
||||
double? height,
|
||||
}) {
|
||||
final inline = decodeDataUri(url);
|
||||
if (inline != null) {
|
||||
return Image.memory(
|
||||
inline,
|
||||
width: width,
|
||||
height: height,
|
||||
fit: BoxFit.cover,
|
||||
semanticLabel: semanticLabel,
|
||||
errorBuilder: (context, _, _) =>
|
||||
_OfferImagePlaceholder(width: width, height: height),
|
||||
);
|
||||
}
|
||||
return Image.network(
|
||||
url,
|
||||
width: width,
|
||||
height: height,
|
||||
fit: BoxFit.cover,
|
||||
semanticLabel: semanticLabel,
|
||||
loadingBuilder: (context, child, progress) => progress == null
|
||||
? child
|
||||
: _OfferImagePlaceholder(width: width, height: height, loading: true),
|
||||
errorBuilder: (context, _, _) =>
|
||||
_OfferImagePlaceholder(width: width, height: height),
|
||||
);
|
||||
}
|
||||
|
||||
/// Neutral fill shown while a remote offer image loads or when it can't be
|
||||
/// decoded/fetched — a marketplace is useless if a broken image blocks the card.
|
||||
class _OfferImagePlaceholder extends StatelessWidget {
|
||||
const _OfferImagePlaceholder({this.width, this.height, this.loading = false});
|
||||
|
||||
final double? width;
|
||||
final double? height;
|
||||
final bool loading;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
width: width,
|
||||
height: height,
|
||||
color: seedPrimaryContainer.withValues(alpha: 0.4),
|
||||
alignment: Alignment.center,
|
||||
child: loading
|
||||
? const SizedBox(
|
||||
width: 20,
|
||||
height: 20,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
)
|
||||
: const Icon(Icons.image_not_supported_outlined, color: seedMuted),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// The small pill showing an offer's reciprocity mode (give/swap/sell/wanted).
|
||||
class OfferTypeChip extends StatelessWidget {
|
||||
const OfferTypeChip({required this.label, super.key});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue