tane/apps/app_seeds/lib/domain/crop_calendar.dart
vjrj 6809dc6143 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.
2026-07-09 21:23:46 +02:00

39 lines
1.5 KiB
Dart

/// 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;
}