import 'dart:typed_data'; import 'package:commons_core/commons_core.dart'; import 'package:equatable/equatable.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import '../data/variety_repository.dart'; /// Form state for the quick-add sheet. Only [label] is required; everything /// else is progressive disclosure behind [expanded]. class QuickAddState extends Equatable { const QuickAddState({ this.label = '', this.quantityKind, this.photoBytes, this.expanded = false, this.submitting = false, this.submitted = false, this.showLabelError = false, }); final String label; final QuantityKind? quantityKind; final Uint8List? photoBytes; final bool expanded; final bool submitting; final bool submitted; final bool showLabelError; bool get hasValidLabel => label.trim().isNotEmpty; QuickAddState copyWith({ String? label, QuantityKind? quantityKind, Uint8List? photoBytes, bool? expanded, bool? submitting, bool? submitted, bool? showLabelError, }) { return QuickAddState( label: label ?? this.label, quantityKind: quantityKind ?? this.quantityKind, photoBytes: photoBytes ?? this.photoBytes, expanded: expanded ?? this.expanded, submitting: submitting ?? this.submitting, submitted: submitted ?? this.submitted, showLabelError: showLabelError ?? this.showLabelError, ); } @override List get props => [ label, quantityKind, photoBytes, expanded, submitting, submitted, showLabelError, ]; } /// Drives the 20-second quick-add flow and persists via [VarietyRepository]. class QuickAddCubit extends Cubit { QuickAddCubit(this._repo) : super(const QuickAddState()); final VarietyRepository _repo; void labelChanged(String value) => emit(state.copyWith(label: value, showLabelError: false)); void selectQuantity(QuantityKind kind) => emit(state.copyWith(quantityKind: kind)); void photoPicked(Uint8List bytes) => emit(state.copyWith(photoBytes: bytes)); void toggleExpanded() => emit(state.copyWith(expanded: !state.expanded)); /// Validates and saves. No-op (sets [showLabelError]) if the label is empty. Future submit() async { if (!state.hasValidLabel) { emit(state.copyWith(showLabelError: true)); return; } if (state.submitting) return; emit(state.copyWith(submitting: true)); await _repo.addQuickVariety( label: state.label.trim(), quantity: state.quantityKind == null ? null : Quantity(kind: state.quantityKind!), photoBytes: state.photoBytes, ); emit(state.copyWith(submitting: false, submitted: true)); } }