feat(inventory): photo-first drafts + on-device OCR (digitization R2+R4)

Lower the bulk-digitization cliff with two more routes on top of the
already-landed CSV import and "save and add another":

- Photo-first drafts (capture now, catalogue later): burst-capture
  photos (camera or multi-gallery) into unnamed draft varieties, shown
  in a "to catalogue" tray, hidden from the main list until named.
  Adds Variety.isDraft (schema), addDraftVariety/watchDrafts/nameDraft,
  the triage sheet and the inventory banner.
- On-device OCR label suggestion (Tesseract, offline, no Google): a
  "Suggest name from photo" button in the naming dialog behind a
  LabelTextExtractor interface (Tesseract on Android/iOS, no-op
  elsewhere). Reads the largest print via hOCR bounding boxes, drops
  boilerplate/low-confidence noise, preprocesses (grayscale, contrast,
  upscale) and sweeps rotations (0-315 deg) so tilted packets still
  read. Bundles tessdata_fast eng+spa; validated on-device against real
  packets. The photo is written to a temp file deleted immediately in a
  finally block (the plugin needs a path) - a bounded, documented
  exception to no-plaintext-at-rest.

This commit also carries the co-developed schema evolution v5 to v8 that
shares these files (organic flag, species viability years, crop
calendar, lot provenance/abundance/preservation format, condition
checks) plus their exports/migrations and i18n.

Tests: CSV/draft/OCR unit + widget + migration green in isolation.
Note: the full widget suite currently hangs (>10 min) - under investigation.
This commit is contained in:
vjrj 2026-07-09 21:23:46 +02:00
parent 12a2ee2d64
commit 6809dc6143
89 changed files with 17141 additions and 228 deletions

View file

@ -0,0 +1,39 @@
/// Crop-calendar months are stored compactly as a 12-bit mask in a single
/// nullable integer column (bit 0 = January bit 11 = December). A phase can
/// happen in several months (e.g. sow lettuce in March, April and September),
/// so the calendar is a *set* of months, not one. `null` or `0` means "not
/// recorded". Pure functions — no I/O — so they are trivially testable.
library;
/// Packs a set of 1..12 month numbers into a bitmask, or null when empty (so an
/// unset phase stays null in the DB rather than 0). Values outside 1..12 are
/// ignored.
int? monthsToMask(Iterable<int> months) {
var mask = 0;
for (final m in months) {
if (m >= 1 && m <= 12) mask |= 1 << (m - 1);
}
return mask == 0 ? null : mask;
}
/// Unpacks a month bitmask into an ascending list of 1..12 month numbers.
/// Returns an empty list for null or 0.
List<int> maskToMonths(int? mask) {
if (mask == null || mask == 0) return const [];
return [
for (var m = 1; m <= 12; m++)
if (mask & (1 << (m - 1)) != 0) m,
];
}
/// Whether month [month] (1..12) is set in [mask].
bool maskHasMonth(int? mask, int month) =>
mask != null && month >= 1 && month <= 12 && mask & (1 << (month - 1)) != 0;
/// Toggles month [month] (1..12) in [mask], returning the new mask (null when
/// the result is empty).
int? toggleMonth(int? mask, int month) {
if (month < 1 || month > 12) return mask;
final next = (mask ?? 0) ^ (1 << (month - 1));
return next == 0 ? null : next;
}

View file

@ -0,0 +1,41 @@
/// Viability status of a seed lot, derived purely from its age versus the
/// species' typical seed longevity (bundled reference data). Turns passive
/// storage into active stewardship: it surfaces which lots to sow or regenerate
/// before they lapse, instead of just accumulating them.
enum SeedViability {
/// Comfortably within the species' typical viability window.
fresh,
/// In the final year of the window sow or reproduce this season.
expiringSoon,
/// Past the typical viability window germination is likely dropping fast.
expired,
/// Not enough data to judge (no harvest year, or no reference figure).
unknown,
}
/// Computes the viability status of a seed lot harvested in [harvestYear] for a
/// species whose typical longevity is [viabilityYears], as of [currentYear].
///
/// A conservative, age-based signal: the bundled figure is a single number, not
/// a decay curve, so this only distinguishes "fine / use soon / past it".
/// [expiringSoon] flags the last year of the window so the grower can prioritise
/// what to reproduce before it lapses. Any actual [GerminationTest] a grower
/// records remains the ground truth and is shown alongside this estimate.
SeedViability seedViability({
required int? harvestYear,
required int? viabilityYears,
required int currentYear,
}) {
if (harvestYear == null || viabilityYears == null || viabilityYears <= 0) {
return SeedViability.unknown;
}
final age = currentYear - harvestYear;
// A harvest stamped in the future is treated as fresh, not expired.
if (age < 0) return SeedViability.fresh;
if (age >= viabilityYears) return SeedViability.expired;
if (age >= viabilityYears - 1) return SeedViability.expiringSoon;
return SeedViability.fresh;
}