PACKET v1.0.0 - Initial release

App móvil Flutter para capturar contenido multimedia, etiquetarlo con hashes y enviarlo a backends configurables.

Features:
- Captura de fotos, audio, video y archivos
- Sistema de etiquetas con bibliotecas externas (HST)
- Packs de etiquetas predefinidos
- Cola de reintentos (hasta 20 contenedores)
- Soporte GPS
- Hash SHA-256 auto-generado por contenedor
- Persistencia SQLite local
- Múltiples destinos configurables

Stack: Flutter 3.38.5, flutter_bloc, sqflite, dio

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
tzzrgit
2025-12-21 18:10:27 +01:00
commit dac0c51483
163 changed files with 8603 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
import 'package:flutter_bloc/flutter_bloc.dart';
import '../../../data/repositories/config_repository.dart';
import '../../../domain/entities/destino.dart';
import 'app_state.dart';
class AppCubit extends Cubit<AppState> {
final ConfigRepository _configRepo = ConfigRepository();
AppCubit() : super(const AppState());
Future<void> init() async {
emit(state.copyWith(isLoading: true));
final destinos = await _configRepo.getDestinos();
final activo = await _configRepo.getDestinoActivo();
emit(state.copyWith(
destinos: destinos,
destinoActivo: activo,
isLoading: false,
));
}
void setIndex(int index) {
emit(state.copyWith(currentIndex: index));
}
Future<void> setDestinoActivo(Destino destino) async {
if (destino.id != null) {
await _configRepo.setDestinoActivo(destino.id!);
emit(state.copyWith(destinoActivo: destino));
}
}
Future<void> addDestino(Destino destino) async {
final id = await _configRepo.insertDestino(destino);
final newDestino = destino.copyWith(id: id);
final destinos = [...state.destinos, newDestino];
emit(state.copyWith(destinos: destinos));
if (state.destinoActivo == null) {
await setDestinoActivo(newDestino);
}
}
Future<void> removeDestino(int id) async {
await _configRepo.deleteDestino(id);
final destinos = state.destinos.where((d) => d.id != id).toList();
emit(state.copyWith(destinos: destinos));
if (state.destinoActivo?.id == id) {
emit(state.copyWith(
destinoActivo: destinos.isNotEmpty ? destinos.first : null,
));
}
}
}

View File

@@ -0,0 +1,32 @@
import 'package:equatable/equatable.dart';
import '../../../domain/entities/destino.dart';
class AppState extends Equatable {
final int currentIndex;
final Destino? destinoActivo;
final List<Destino> destinos;
final bool isLoading;
const AppState({
this.currentIndex = 0,
this.destinoActivo,
this.destinos = const [],
this.isLoading = false,
});
AppState copyWith({
int? currentIndex,
Destino? destinoActivo,
List<Destino>? destinos,
bool? isLoading,
}) =>
AppState(
currentIndex: currentIndex ?? this.currentIndex,
destinoActivo: destinoActivo ?? this.destinoActivo,
destinos: destinos ?? this.destinos,
isLoading: isLoading ?? this.isLoading,
);
@override
List<Object?> get props => [currentIndex, destinoActivo, destinos, isLoading];
}

View File

@@ -0,0 +1,191 @@
import 'dart:typed_data';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:geolocator/geolocator.dart';
import 'package:image_picker/image_picker.dart';
import 'package:file_picker/file_picker.dart';
import '../../../core/utils/hash_utils.dart';
import '../../../data/repositories/contenedor_repository.dart';
import '../../../domain/entities/archivo_adjunto.dart';
import '../../../domain/entities/contenedor.dart';
import '../../../domain/entities/destino.dart';
import '../../../domain/entities/gps_location.dart';
import 'captura_state.dart';
class CapturaCubit extends Cubit<CapturaState> {
final ContenedorRepository _repo = ContenedorRepository();
final ImagePicker _picker = ImagePicker();
CapturaCubit()
: super(CapturaState(
contenedor: Contenedor(hash: HashUtils.generateHash()),
));
void regenerateHash() {
emit(state.copyWith(
contenedor: state.contenedor.copyWith(hash: HashUtils.generateHash()),
));
}
void setTitulo(String titulo) {
emit(state.copyWith(
contenedor: state.contenedor.copyWith(titulo: titulo.isEmpty ? null : titulo),
));
}
void setDescripcion(String descripcion) {
emit(state.copyWith(
contenedor: state.contenedor.copyWith(
descripcion: descripcion.isEmpty ? null : descripcion,
),
));
}
Future<void> capturePhoto() async {
try {
final image = await _picker.pickImage(source: ImageSource.camera);
if (image == null) return;
final bytes = await image.readAsBytes();
final archivo = ArchivoAdjunto(
nombre: image.name,
mimeType: 'image/jpeg',
bytes: bytes,
tipo: ArchivoTipo.image,
hash: HashUtils.hashFromBytes(bytes),
);
emit(state.addArchivo(archivo));
} catch (e) {
emit(state.copyWith(
status: CapturaStatus.error,
errorMessage: 'Error al capturar foto: $e',
));
}
}
Future<void> captureVideo() async {
try {
final video = await _picker.pickVideo(source: ImageSource.camera);
if (video == null) return;
final bytes = await video.readAsBytes();
final archivo = ArchivoAdjunto(
nombre: video.name,
mimeType: 'video/mp4',
bytes: bytes,
tipo: ArchivoTipo.video,
hash: HashUtils.hashFromBytes(bytes),
);
emit(state.addArchivo(archivo));
} catch (e) {
emit(state.copyWith(
status: CapturaStatus.error,
errorMessage: 'Error al capturar video: $e',
));
}
}
Future<void> pickFile() async {
try {
final result = await FilePicker.platform.pickFiles(withData: true);
if (result == null || result.files.isEmpty) return;
final file = result.files.first;
if (file.bytes == null) return;
final archivo = ArchivoAdjunto(
nombre: file.name,
mimeType: _getMimeType(file.extension),
bytes: file.bytes!,
tipo: ArchivoTipo.document,
hash: HashUtils.hashFromBytes(file.bytes!),
);
emit(state.addArchivo(archivo));
} catch (e) {
emit(state.copyWith(
status: CapturaStatus.error,
errorMessage: 'Error al seleccionar archivo: $e',
));
}
}
void addAudioFile(Uint8List bytes, String nombre) {
final archivo = ArchivoAdjunto(
nombre: nombre,
mimeType: 'audio/m4a',
bytes: bytes,
tipo: ArchivoTipo.audio,
hash: HashUtils.hashFromBytes(bytes),
);
emit(state.addArchivo(archivo));
}
void removeArchivo(int index) {
emit(state.removeArchivo(index));
}
Future<void> captureGps() async {
try {
final permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
await Geolocator.requestPermission();
}
final position = await Geolocator.getCurrentPosition();
final gps = GpsLocation(lat: position.latitude, long: position.longitude);
emit(state.setGps(gps));
} catch (e) {
emit(state.copyWith(
status: CapturaStatus.error,
errorMessage: 'Error al obtener GPS: $e',
));
}
}
void clearGps() {
emit(state.setGps(null));
}
void setEtiquetas(List<String> etiquetas) {
emit(state.setEtiquetas(etiquetas));
}
Future<void> enviar(Destino destino) async {
emit(state.copyWith(status: CapturaStatus.sending));
try {
await _repo.enviar(state.contenedor, destino);
emit(state.copyWith(status: CapturaStatus.success));
reset();
} catch (e) {
emit(state.copyWith(
status: CapturaStatus.error,
errorMessage: 'Error al enviar: $e',
));
}
}
void reset() {
emit(CapturaState(
contenedor: Contenedor(hash: HashUtils.generateHash()),
));
}
String _getMimeType(String? extension) {
switch (extension?.toLowerCase()) {
case 'pdf':
return 'application/pdf';
case 'doc':
case 'docx':
return 'application/msword';
case 'xls':
case 'xlsx':
return 'application/vnd.ms-excel';
case 'png':
return 'image/png';
case 'jpg':
case 'jpeg':
return 'image/jpeg';
default:
return 'application/octet-stream';
}
}
}

View File

@@ -0,0 +1,63 @@
import 'package:equatable/equatable.dart';
import '../../../domain/entities/contenedor.dart';
import '../../../domain/entities/archivo_adjunto.dart';
import '../../../domain/entities/gps_location.dart';
enum CapturaStatus { idle, capturing, sending, success, error }
class CapturaState extends Equatable {
final Contenedor contenedor;
final CapturaStatus status;
final String? errorMessage;
final bool isRecording;
const CapturaState({
required this.contenedor,
this.status = CapturaStatus.idle,
this.errorMessage,
this.isRecording = false,
});
CapturaState copyWith({
Contenedor? contenedor,
CapturaStatus? status,
String? errorMessage,
bool? isRecording,
}) =>
CapturaState(
contenedor: contenedor ?? this.contenedor,
status: status ?? this.status,
errorMessage: errorMessage,
isRecording: isRecording ?? this.isRecording,
);
CapturaState addArchivo(ArchivoAdjunto archivo) {
return copyWith(
contenedor: contenedor.copyWith(
archivos: [...contenedor.archivos, archivo],
),
);
}
CapturaState removeArchivo(int index) {
final archivos = [...contenedor.archivos]..removeAt(index);
return copyWith(
contenedor: contenedor.copyWith(archivos: archivos),
);
}
CapturaState setGps(GpsLocation? gps) {
return copyWith(
contenedor: contenedor.copyWith(gps: gps),
);
}
CapturaState setEtiquetas(List<String> etiquetas) {
return copyWith(
contenedor: contenedor.copyWith(etiquetas: etiquetas),
);
}
@override
List<Object?> get props => [contenedor, status, errorMessage, isRecording];
}

View File

@@ -0,0 +1,105 @@
import 'package:flutter_bloc/flutter_bloc.dart';
import '../../../core/utils/hash_utils.dart';
import '../../../data/repositories/config_repository.dart';
import '../../../data/repositories/etiqueta_repository.dart';
import 'etiquetas_state.dart';
class EtiquetasCubit extends Cubit<EtiquetasState> {
final ConfigRepository _configRepo = ConfigRepository();
final EtiquetaRepository _etiquetaRepo = EtiquetaRepository();
EtiquetasCubit() : super(const EtiquetasState());
Future<void> init() async {
emit(state.copyWith(isLoading: true));
try {
final bibliotecas = await _configRepo.getBibliotecas();
final etiquetasPorBiblioteca = <int, List<dynamic>>{};
for (final bib in bibliotecas) {
if (bib.id != null) {
final etiquetas = await _etiquetaRepo.getEtiquetasByBiblioteca(bib.id!);
etiquetasPorBiblioteca[bib.id!] = etiquetas;
}
}
emit(state.copyWith(
bibliotecas: bibliotecas,
etiquetasPorBiblioteca: etiquetasPorBiblioteca.cast(),
isLoading: false,
));
} catch (e) {
emit(state.copyWith(
isLoading: false,
error: 'Error al cargar etiquetas: $e',
));
}
}
Future<void> syncBiblioteca(int bibliotecaId) async {
emit(state.copyWith(isLoading: true));
try {
final biblioteca = state.bibliotecas.firstWhere((b) => b.id == bibliotecaId);
await _etiquetaRepo.syncBiblioteca(biblioteca);
final etiquetas = await _etiquetaRepo.getEtiquetasByBiblioteca(bibliotecaId);
final newMap = Map<int, List<dynamic>>.from(state.etiquetasPorBiblioteca);
newMap[bibliotecaId] = etiquetas;
emit(state.copyWith(
etiquetasPorBiblioteca: newMap.cast(),
isLoading: false,
));
} catch (e) {
emit(state.copyWith(
isLoading: false,
error: 'Error al sincronizar: $e',
));
}
}
void toggleEtiqueta(String hash) {
final seleccionadas = List<String>.from(state.seleccionadas);
if (seleccionadas.contains(hash)) {
seleccionadas.remove(hash);
} else {
seleccionadas.add(hash);
}
emit(state.copyWith(seleccionadas: seleccionadas));
}
void removeEtiqueta(String hash) {
final seleccionadas = List<String>.from(state.seleccionadas)..remove(hash);
emit(state.copyWith(seleccionadas: seleccionadas));
}
void clearSeleccion() {
emit(state.copyWith(seleccionadas: []));
}
Future<void> addHashExterno(String input) async {
final hashes = HashUtils.extractHashes(input);
if (hashes.isEmpty) return;
final seleccionadas = List<String>.from(state.seleccionadas);
final resolvedHashes = Map<String, dynamic>.from(state.resolvedHashes);
for (final hash in hashes) {
if (!seleccionadas.contains(hash)) {
seleccionadas.add(hash);
// Try to resolve in background
final etiqueta = await _etiquetaRepo.resolveHash(hash);
resolvedHashes[hash] = etiqueta;
}
}
emit(state.copyWith(
seleccionadas: seleccionadas,
resolvedHashes: resolvedHashes.cast(),
));
}
void setSeleccionadas(List<String> etiquetas) {
emit(state.copyWith(seleccionadas: etiquetas));
}
}

View File

@@ -0,0 +1,50 @@
import 'package:equatable/equatable.dart';
import '../../../domain/entities/etiqueta.dart';
import '../../../domain/entities/biblioteca.dart';
class EtiquetasState extends Equatable {
final List<Biblioteca> bibliotecas;
final Map<int, List<Etiqueta>> etiquetasPorBiblioteca;
final List<String> seleccionadas;
final Map<String, Etiqueta?> resolvedHashes;
final bool isLoading;
final String? error;
const EtiquetasState({
this.bibliotecas = const [],
this.etiquetasPorBiblioteca = const {},
this.seleccionadas = const [],
this.resolvedHashes = const {},
this.isLoading = false,
this.error,
});
EtiquetasState copyWith({
List<Biblioteca>? bibliotecas,
Map<int, List<Etiqueta>>? etiquetasPorBiblioteca,
List<String>? seleccionadas,
Map<String, Etiqueta?>? resolvedHashes,
bool? isLoading,
String? error,
}) =>
EtiquetasState(
bibliotecas: bibliotecas ?? this.bibliotecas,
etiquetasPorBiblioteca: etiquetasPorBiblioteca ?? this.etiquetasPorBiblioteca,
seleccionadas: seleccionadas ?? this.seleccionadas,
resolvedHashes: resolvedHashes ?? this.resolvedHashes,
isLoading: isLoading ?? this.isLoading,
error: error,
);
bool isSelected(String hash) => seleccionadas.contains(hash);
@override
List<Object?> get props => [
bibliotecas,
etiquetasPorBiblioteca,
seleccionadas,
resolvedHashes,
isLoading,
error,
];
}

View File

@@ -0,0 +1,42 @@
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:equatable/equatable.dart';
import '../../../data/repositories/etiqueta_repository.dart';
import '../../../domain/entities/pack.dart';
class PacksState extends Equatable {
final List<Pack> packs;
final bool isLoading;
const PacksState({this.packs = const [], this.isLoading = false});
PacksState copyWith({List<Pack>? packs, bool? isLoading}) => PacksState(
packs: packs ?? this.packs,
isLoading: isLoading ?? this.isLoading,
);
@override
List<Object?> get props => [packs, isLoading];
}
class PacksCubit extends Cubit<PacksState> {
final EtiquetaRepository _repo = EtiquetaRepository();
PacksCubit() : super(const PacksState());
Future<void> load() async {
emit(state.copyWith(isLoading: true));
final packs = await _repo.getPacks();
emit(state.copyWith(packs: packs, isLoading: false));
}
Future<void> addPack(String nombre, List<String> tags) async {
final pack = Pack(nombre: nombre, tags: tags);
await _repo.insertPack(pack);
await load();
}
Future<void> deletePack(int id) async {
await _repo.deletePack(id);
await load();
}
}

View File

@@ -0,0 +1,66 @@
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:equatable/equatable.dart';
import '../../../data/repositories/contenedor_repository.dart';
import '../../../domain/entities/pendiente.dart';
import '../../../domain/entities/destino.dart';
class PendientesState extends Equatable {
final List<Pendiente> pendientes;
final bool isLoading;
final bool queueFull;
const PendientesState({
this.pendientes = const [],
this.isLoading = false,
this.queueFull = false,
});
PendientesState copyWith({
List<Pendiente>? pendientes,
bool? isLoading,
bool? queueFull,
}) =>
PendientesState(
pendientes: pendientes ?? this.pendientes,
isLoading: isLoading ?? this.isLoading,
queueFull: queueFull ?? this.queueFull,
);
@override
List<Object?> get props => [pendientes, isLoading, queueFull];
}
class PendientesCubit extends Cubit<PendientesState> {
final ContenedorRepository _repo = ContenedorRepository();
PendientesCubit() : super(const PendientesState());
Future<void> load() async {
emit(state.copyWith(isLoading: true));
final pendientes = await _repo.getPendientes();
emit(state.copyWith(
pendientes: pendientes,
isLoading: false,
queueFull: pendientes.length >= 20,
));
}
Future<void> reintentar(Pendiente pendiente, Destino destino) async {
emit(state.copyWith(isLoading: true));
await _repo.reintentar(pendiente, destino);
await load();
}
Future<void> eliminar(String hash) async {
await _repo.eliminarPendiente(hash);
await load();
}
Future<void> reintentarTodos(Destino destino) async {
emit(state.copyWith(isLoading: true));
for (final p in state.pendientes.where((p) => p.puedeReintentar)) {
await _repo.reintentar(p, destino);
}
await load();
}
}