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:
147
lib/presentation/app.dart
Normal file
147
lib/presentation/app.dart
Normal file
@@ -0,0 +1,147 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import '../core/theme/app_theme.dart';
|
||||
import 'bloc/app/app_cubit.dart';
|
||||
import 'bloc/app/app_state.dart';
|
||||
import 'bloc/captura/captura_cubit.dart';
|
||||
import 'bloc/etiquetas/etiquetas_cubit.dart';
|
||||
import 'bloc/packs/packs_cubit.dart';
|
||||
import 'bloc/pendientes/pendientes_cubit.dart';
|
||||
import 'pages/captura_page.dart';
|
||||
import 'pages/etiquetas_page.dart';
|
||||
import 'pages/packs_page.dart';
|
||||
import 'pages/pendientes_page.dart';
|
||||
import 'pages/config_page.dart';
|
||||
|
||||
class PacketApp extends StatelessWidget {
|
||||
const PacketApp({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MultiBlocProvider(
|
||||
providers: [
|
||||
BlocProvider(create: (_) => AppCubit()..init()),
|
||||
BlocProvider(create: (_) => CapturaCubit()),
|
||||
BlocProvider(create: (_) => EtiquetasCubit()..init()),
|
||||
BlocProvider(create: (_) => PacksCubit()..load()),
|
||||
BlocProvider(create: (_) => PendientesCubit()..load()),
|
||||
],
|
||||
child: MaterialApp(
|
||||
title: 'Packet',
|
||||
theme: AppTheme.lightTheme,
|
||||
darkTheme: AppTheme.darkTheme,
|
||||
themeMode: ThemeMode.system,
|
||||
home: const MainScreen(),
|
||||
debugShowCheckedModeBanner: false,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class MainScreen extends StatelessWidget {
|
||||
const MainScreen({super.key});
|
||||
|
||||
static const _pages = [
|
||||
CapturaPage(),
|
||||
EtiquetasPage(),
|
||||
PacksPage(),
|
||||
PendientesPage(),
|
||||
ConfigPage(),
|
||||
];
|
||||
|
||||
static const _titles = [
|
||||
'Captura',
|
||||
'Etiquetas',
|
||||
'Packs',
|
||||
'Pendientes',
|
||||
'Config',
|
||||
];
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<AppCubit, AppState>(
|
||||
builder: (context, state) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(_titles[state.currentIndex]),
|
||||
actions: [
|
||||
// Destino selector
|
||||
if (state.destinos.isNotEmpty)
|
||||
PopupMenuButton<int>(
|
||||
onSelected: (id) {
|
||||
final destino = state.destinos.firstWhere((d) => d.id == id);
|
||||
context.read<AppCubit>().setDestinoActivo(destino);
|
||||
},
|
||||
itemBuilder: (_) => state.destinos
|
||||
.map((d) => PopupMenuItem(
|
||||
value: d.id,
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(
|
||||
state.destinoActivo?.id == d.id
|
||||
? Icons.check
|
||||
: Icons.cloud,
|
||||
size: 18,
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Text(d.nombre),
|
||||
],
|
||||
),
|
||||
))
|
||||
.toList(),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Text(
|
||||
state.destinoActivo?.nombre ?? 'Sin destino',
|
||||
style: Theme.of(context).textTheme.labelLarge,
|
||||
),
|
||||
const Icon(Icons.arrow_drop_down),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
body: IndexedStack(
|
||||
index: state.currentIndex,
|
||||
children: _pages,
|
||||
),
|
||||
bottomNavigationBar: NavigationBar(
|
||||
selectedIndex: state.currentIndex,
|
||||
onDestinationSelected: (i) => context.read<AppCubit>().setIndex(i),
|
||||
destinations: const [
|
||||
NavigationDestination(
|
||||
icon: Icon(Icons.camera_alt_outlined),
|
||||
selectedIcon: Icon(Icons.camera_alt),
|
||||
label: 'Captura',
|
||||
),
|
||||
NavigationDestination(
|
||||
icon: Icon(Icons.label_outline),
|
||||
selectedIcon: Icon(Icons.label),
|
||||
label: 'Tags',
|
||||
),
|
||||
NavigationDestination(
|
||||
icon: Icon(Icons.inventory_2_outlined),
|
||||
selectedIcon: Icon(Icons.inventory_2),
|
||||
label: 'Packs',
|
||||
),
|
||||
NavigationDestination(
|
||||
icon: Icon(Icons.pending_outlined),
|
||||
selectedIcon: Icon(Icons.pending),
|
||||
label: 'Pend.',
|
||||
),
|
||||
NavigationDestination(
|
||||
icon: Icon(Icons.settings_outlined),
|
||||
selectedIcon: Icon(Icons.settings),
|
||||
label: 'Config',
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
53
lib/presentation/bloc/app/app_cubit.dart
Normal file
53
lib/presentation/bloc/app/app_cubit.dart
Normal 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,
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
32
lib/presentation/bloc/app/app_state.dart
Normal file
32
lib/presentation/bloc/app/app_state.dart
Normal 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];
|
||||
}
|
||||
191
lib/presentation/bloc/captura/captura_cubit.dart
Normal file
191
lib/presentation/bloc/captura/captura_cubit.dart
Normal 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';
|
||||
}
|
||||
}
|
||||
}
|
||||
63
lib/presentation/bloc/captura/captura_state.dart
Normal file
63
lib/presentation/bloc/captura/captura_state.dart
Normal 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];
|
||||
}
|
||||
105
lib/presentation/bloc/etiquetas/etiquetas_cubit.dart
Normal file
105
lib/presentation/bloc/etiquetas/etiquetas_cubit.dart
Normal 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));
|
||||
}
|
||||
}
|
||||
50
lib/presentation/bloc/etiquetas/etiquetas_state.dart
Normal file
50
lib/presentation/bloc/etiquetas/etiquetas_state.dart
Normal 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,
|
||||
];
|
||||
}
|
||||
42
lib/presentation/bloc/packs/packs_cubit.dart
Normal file
42
lib/presentation/bloc/packs/packs_cubit.dart
Normal 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();
|
||||
}
|
||||
}
|
||||
66
lib/presentation/bloc/pendientes/pendientes_cubit.dart
Normal file
66
lib/presentation/bloc/pendientes/pendientes_cubit.dart
Normal 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();
|
||||
}
|
||||
}
|
||||
311
lib/presentation/pages/captura_page.dart
Normal file
311
lib/presentation/pages/captura_page.dart
Normal file
@@ -0,0 +1,311 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import '../bloc/captura/captura_cubit.dart';
|
||||
import '../bloc/captura/captura_state.dart';
|
||||
import '../bloc/etiquetas/etiquetas_cubit.dart';
|
||||
import '../bloc/app/app_cubit.dart';
|
||||
import '../bloc/app/app_state.dart';
|
||||
import '../widgets/audio_recorder.dart';
|
||||
import '../../core/utils/hash_utils.dart';
|
||||
|
||||
class CapturaPage extends StatelessWidget {
|
||||
const CapturaPage({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<CapturaCubit, CapturaState>(
|
||||
builder: (context, state) {
|
||||
return Scaffold(
|
||||
body: SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
// Hash section
|
||||
Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
const Icon(Icons.tag, size: 20),
|
||||
const SizedBox(width: 8),
|
||||
const Text('Hash', style: TextStyle(fontWeight: FontWeight.bold)),
|
||||
const Spacer(),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.copy, size: 20),
|
||||
onPressed: () {
|
||||
Clipboard.setData(ClipboardData(text: state.contenedor.hash));
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('Hash copiado')),
|
||||
);
|
||||
},
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.refresh, size: 20),
|
||||
onPressed: () => context.read<CapturaCubit>().regenerateHash(),
|
||||
),
|
||||
],
|
||||
),
|
||||
Text(
|
||||
HashUtils.truncateHash(state.contenedor.hash, length: 32),
|
||||
style: Theme.of(context).textTheme.bodySmall?.copyWith(
|
||||
fontFamily: 'monospace',
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// Title & Description
|
||||
TextField(
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Título (opcional)',
|
||||
prefixIcon: Icon(Icons.title),
|
||||
),
|
||||
onChanged: (v) => context.read<CapturaCubit>().setTitulo(v),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
TextField(
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Descripción (opcional)',
|
||||
prefixIcon: Icon(Icons.description),
|
||||
),
|
||||
maxLines: 2,
|
||||
onChanged: (v) => context.read<CapturaCubit>().setDescripcion(v),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// Capture buttons
|
||||
Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text('Capturar', style: TextStyle(fontWeight: FontWeight.bold)),
|
||||
const SizedBox(height: 12),
|
||||
Wrap(
|
||||
spacing: 12,
|
||||
runSpacing: 12,
|
||||
children: [
|
||||
_CaptureButton(
|
||||
icon: Icons.camera_alt,
|
||||
label: 'Foto',
|
||||
onPressed: () => context.read<CapturaCubit>().capturePhoto(),
|
||||
),
|
||||
AudioRecorderButton(
|
||||
onRecorded: (bytes, name) {
|
||||
context.read<CapturaCubit>().addAudioFile(bytes, name);
|
||||
},
|
||||
),
|
||||
_CaptureButton(
|
||||
icon: Icons.videocam,
|
||||
label: 'Video',
|
||||
onPressed: () => context.read<CapturaCubit>().captureVideo(),
|
||||
),
|
||||
_CaptureButton(
|
||||
icon: Icons.attach_file,
|
||||
label: 'Archivo',
|
||||
onPressed: () => context.read<CapturaCubit>().pickFile(),
|
||||
),
|
||||
_CaptureButton(
|
||||
icon: state.contenedor.gps != null
|
||||
? Icons.location_on
|
||||
: Icons.location_off,
|
||||
label: 'GPS',
|
||||
onPressed: () {
|
||||
if (state.contenedor.gps != null) {
|
||||
context.read<CapturaCubit>().clearGps();
|
||||
} else {
|
||||
context.read<CapturaCubit>().captureGps();
|
||||
}
|
||||
},
|
||||
selected: state.contenedor.gps != null,
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// Attached files
|
||||
if (state.contenedor.archivos.isNotEmpty) ...[
|
||||
Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'Archivos (${state.contenedor.archivos.length})',
|
||||
style: const TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
...state.contenedor.archivos.asMap().entries.map((e) {
|
||||
final archivo = e.value;
|
||||
return ListTile(
|
||||
leading: Icon(_getFileIcon(archivo.tipo)),
|
||||
title: Text(archivo.nombre),
|
||||
subtitle: Text(archivo.sizeFormatted),
|
||||
trailing: IconButton(
|
||||
icon: const Icon(Icons.close),
|
||||
onPressed: () =>
|
||||
context.read<CapturaCubit>().removeArchivo(e.key),
|
||||
),
|
||||
);
|
||||
}),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
],
|
||||
|
||||
// Etiquetas summary
|
||||
BlocBuilder<EtiquetasCubit, dynamic>(
|
||||
builder: (context, etState) {
|
||||
final seleccionadas = (etState as dynamic).seleccionadas as List<String>;
|
||||
if (seleccionadas.isEmpty) {
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
return Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'Etiquetas (${seleccionadas.length})',
|
||||
style: const TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Wrap(
|
||||
spacing: 8,
|
||||
runSpacing: 8,
|
||||
children: seleccionadas
|
||||
.map((h) => Chip(
|
||||
label: Text(HashUtils.truncateHash(h)),
|
||||
onDeleted: () =>
|
||||
context.read<EtiquetasCubit>().removeEtiqueta(h),
|
||||
))
|
||||
.toList(),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
|
||||
// GPS info
|
||||
if (state.contenedor.gps != null) ...[
|
||||
const SizedBox(height: 16),
|
||||
Card(
|
||||
child: ListTile(
|
||||
leading: const Icon(Icons.location_on, color: Colors.green),
|
||||
title: const Text('Ubicación'),
|
||||
subtitle: Text(state.contenedor.gps!.toString()),
|
||||
),
|
||||
),
|
||||
],
|
||||
|
||||
const SizedBox(height: 24),
|
||||
|
||||
// Send button
|
||||
BlocBuilder<AppCubit, AppState>(
|
||||
builder: (context, appState) {
|
||||
final destino = appState.destinoActivo;
|
||||
return FilledButton.icon(
|
||||
onPressed: destino != null && state.status != CapturaStatus.sending
|
||||
? () {
|
||||
final etiquetas = context
|
||||
.read<EtiquetasCubit>()
|
||||
.state
|
||||
.seleccionadas;
|
||||
context.read<CapturaCubit>().setEtiquetas(etiquetas);
|
||||
context.read<CapturaCubit>().enviar(destino);
|
||||
}
|
||||
: null,
|
||||
icon: state.status == CapturaStatus.sending
|
||||
? const SizedBox(
|
||||
width: 20,
|
||||
height: 20,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
)
|
||||
: const Icon(Icons.send),
|
||||
label: Text(state.status == CapturaStatus.sending
|
||||
? 'Enviando...'
|
||||
: 'Enviar'),
|
||||
);
|
||||
},
|
||||
),
|
||||
|
||||
if (state.status == CapturaStatus.error && state.errorMessage != null)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 16),
|
||||
child: Text(
|
||||
state.errorMessage!,
|
||||
style: TextStyle(color: Theme.of(context).colorScheme.error),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
IconData _getFileIcon(dynamic tipo) {
|
||||
switch (tipo.toString()) {
|
||||
case 'FileType.image':
|
||||
return Icons.image;
|
||||
case 'FileType.audio':
|
||||
return Icons.audiotrack;
|
||||
case 'FileType.video':
|
||||
return Icons.videocam;
|
||||
default:
|
||||
return Icons.insert_drive_file;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class _CaptureButton extends StatelessWidget {
|
||||
final IconData icon;
|
||||
final String label;
|
||||
final VoidCallback onPressed;
|
||||
final bool selected;
|
||||
|
||||
const _CaptureButton({
|
||||
required this.icon,
|
||||
required this.label,
|
||||
required this.onPressed,
|
||||
this.selected = false,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return FilledButton.tonal(
|
||||
onPressed: onPressed,
|
||||
style: FilledButton.styleFrom(
|
||||
backgroundColor: selected ? Theme.of(context).colorScheme.primaryContainer : null,
|
||||
),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(icon, size: 18),
|
||||
const SizedBox(width: 4),
|
||||
Text(label),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
303
lib/presentation/pages/config_page.dart
Normal file
303
lib/presentation/pages/config_page.dart
Normal file
@@ -0,0 +1,303 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import '../bloc/app/app_cubit.dart';
|
||||
import '../bloc/app/app_state.dart';
|
||||
import '../bloc/etiquetas/etiquetas_cubit.dart';
|
||||
import '../bloc/etiquetas/etiquetas_state.dart';
|
||||
import '../../domain/entities/destino.dart';
|
||||
import '../../domain/entities/biblioteca.dart';
|
||||
import '../../data/repositories/config_repository.dart';
|
||||
|
||||
class ConfigPage extends StatelessWidget {
|
||||
const ConfigPage({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: ListView(
|
||||
padding: const EdgeInsets.all(16),
|
||||
children: [
|
||||
// Destinos section
|
||||
Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
const Icon(Icons.cloud_upload),
|
||||
const SizedBox(width: 8),
|
||||
const Text(
|
||||
'Destinos',
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 18,
|
||||
),
|
||||
),
|
||||
const Spacer(),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.add),
|
||||
onPressed: () => _showDestinoDialog(context),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
BlocBuilder<AppCubit, AppState>(
|
||||
builder: (context, state) {
|
||||
if (state.destinos.isEmpty) {
|
||||
return const Padding(
|
||||
padding: EdgeInsets.all(16),
|
||||
child: Text('No hay destinos configurados'),
|
||||
);
|
||||
}
|
||||
return Column(
|
||||
children: state.destinos.map((destino) {
|
||||
final isActive = state.destinoActivo?.id == destino.id;
|
||||
return ListTile(
|
||||
leading: Icon(
|
||||
isActive ? Icons.radio_button_checked : Icons.radio_button_off,
|
||||
color: isActive ? Theme.of(context).colorScheme.primary : null,
|
||||
),
|
||||
title: Text(destino.nombre),
|
||||
subtitle: Text(
|
||||
destino.url,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
trailing: IconButton(
|
||||
icon: const Icon(Icons.delete_outline),
|
||||
onPressed: () => context.read<AppCubit>().removeDestino(destino.id!),
|
||||
),
|
||||
onTap: () => context.read<AppCubit>().setDestinoActivo(destino),
|
||||
);
|
||||
}).toList(),
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// Bibliotecas section
|
||||
Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
const Icon(Icons.library_books),
|
||||
const SizedBox(width: 8),
|
||||
const Text(
|
||||
'Bibliotecas',
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 18,
|
||||
),
|
||||
),
|
||||
const Spacer(),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.add),
|
||||
onPressed: () => _showBibliotecaDialog(context),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
BlocBuilder<EtiquetasCubit, EtiquetasState>(
|
||||
builder: (context, state) {
|
||||
if (state.bibliotecas.isEmpty) {
|
||||
return const Padding(
|
||||
padding: EdgeInsets.all(16),
|
||||
child: Text('No hay bibliotecas configuradas'),
|
||||
);
|
||||
}
|
||||
return Column(
|
||||
children: state.bibliotecas.map((bib) {
|
||||
final etiquetas = state.etiquetasPorBiblioteca[bib.id] ?? [];
|
||||
return ListTile(
|
||||
leading: const Icon(Icons.folder),
|
||||
title: Text(bib.nombre),
|
||||
subtitle: Text(
|
||||
'${bib.url}${bib.endpoint}\n${etiquetas.length} etiquetas en cache',
|
||||
),
|
||||
isThreeLine: true,
|
||||
trailing: IconButton(
|
||||
icon: const Icon(Icons.sync),
|
||||
onPressed: () =>
|
||||
context.read<EtiquetasCubit>().syncBiblioteca(bib.id!),
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// App info
|
||||
Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Row(
|
||||
children: [
|
||||
Icon(Icons.info_outline),
|
||||
SizedBox(width: 8),
|
||||
Text(
|
||||
'Información',
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 18,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
const ListTile(
|
||||
title: Text('Packet'),
|
||||
subtitle: Text('v1.0.0'),
|
||||
leading: Icon(Icons.apps),
|
||||
),
|
||||
const ListTile(
|
||||
title: Text('Biblioteca HST'),
|
||||
subtitle: Text('tzrtech.org'),
|
||||
leading: Icon(Icons.link),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _showDestinoDialog(BuildContext context) {
|
||||
final nombreController = TextEditingController();
|
||||
final urlController = TextEditingController();
|
||||
final hashController = TextEditingController();
|
||||
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (ctx) => AlertDialog(
|
||||
title: const Text('Nuevo Destino'),
|
||||
content: SingleChildScrollView(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
TextField(
|
||||
controller: nombreController,
|
||||
decoration: const InputDecoration(labelText: 'Nombre'),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
TextField(
|
||||
controller: urlController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'URL',
|
||||
hintText: 'https://api.example.com',
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
TextField(
|
||||
controller: hashController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Llave (hash 64 chars)',
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(ctx),
|
||||
child: const Text('Cancelar'),
|
||||
),
|
||||
FilledButton(
|
||||
onPressed: () {
|
||||
if (nombreController.text.isNotEmpty &&
|
||||
urlController.text.isNotEmpty &&
|
||||
hashController.text.length == 64) {
|
||||
context.read<AppCubit>().addDestino(Destino(
|
||||
nombre: nombreController.text,
|
||||
url: urlController.text,
|
||||
hash: hashController.text,
|
||||
));
|
||||
Navigator.pop(ctx);
|
||||
}
|
||||
},
|
||||
child: const Text('Guardar'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _showBibliotecaDialog(BuildContext context) {
|
||||
final nombreController = TextEditingController();
|
||||
final urlController = TextEditingController();
|
||||
final endpointController = TextEditingController(text: '/api/tags');
|
||||
final configRepo = ConfigRepository();
|
||||
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (ctx) => AlertDialog(
|
||||
title: const Text('Nueva Biblioteca'),
|
||||
content: SingleChildScrollView(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
TextField(
|
||||
controller: nombreController,
|
||||
decoration: const InputDecoration(labelText: 'Nombre'),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
TextField(
|
||||
controller: urlController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'URL',
|
||||
hintText: 'https://example.com',
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
TextField(
|
||||
controller: endpointController,
|
||||
decoration: const InputDecoration(labelText: 'Endpoint'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(ctx),
|
||||
child: const Text('Cancelar'),
|
||||
),
|
||||
FilledButton(
|
||||
onPressed: () async {
|
||||
if (nombreController.text.isNotEmpty && urlController.text.isNotEmpty) {
|
||||
await configRepo.insertBiblioteca(Biblioteca(
|
||||
nombre: nombreController.text,
|
||||
url: urlController.text,
|
||||
endpoint: endpointController.text,
|
||||
));
|
||||
if (ctx.mounted) {
|
||||
Navigator.pop(ctx);
|
||||
context.read<EtiquetasCubit>().init();
|
||||
}
|
||||
}
|
||||
},
|
||||
child: const Text('Guardar'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
235
lib/presentation/pages/etiquetas_page.dart
Normal file
235
lib/presentation/pages/etiquetas_page.dart
Normal file
@@ -0,0 +1,235 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:cached_network_image/cached_network_image.dart';
|
||||
import '../bloc/etiquetas/etiquetas_cubit.dart';
|
||||
import '../bloc/etiquetas/etiquetas_state.dart';
|
||||
import '../../core/utils/hash_utils.dart';
|
||||
|
||||
class EtiquetasPage extends StatefulWidget {
|
||||
const EtiquetasPage({super.key});
|
||||
|
||||
@override
|
||||
State<EtiquetasPage> createState() => _EtiquetasPageState();
|
||||
}
|
||||
|
||||
class _EtiquetasPageState extends State<EtiquetasPage> {
|
||||
final _hashController = TextEditingController();
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_hashController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<EtiquetasCubit, EtiquetasState>(
|
||||
builder: (context, state) {
|
||||
return Scaffold(
|
||||
body: RefreshIndicator(
|
||||
onRefresh: () async {
|
||||
for (final bib in state.bibliotecas) {
|
||||
if (bib.id != null) {
|
||||
await context.read<EtiquetasCubit>().syncBiblioteca(bib.id!);
|
||||
}
|
||||
}
|
||||
},
|
||||
child: ListView(
|
||||
padding: const EdgeInsets.all(16),
|
||||
children: [
|
||||
// External hash input
|
||||
Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'Añadir hash externo',
|
||||
style: TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
TextField(
|
||||
controller: _hashController,
|
||||
decoration: InputDecoration(
|
||||
hintText: 'Pegar URL o hash...',
|
||||
suffixIcon: IconButton(
|
||||
icon: const Icon(Icons.add),
|
||||
onPressed: () {
|
||||
context
|
||||
.read<EtiquetasCubit>()
|
||||
.addHashExterno(_hashController.text);
|
||||
_hashController.clear();
|
||||
},
|
||||
),
|
||||
),
|
||||
onSubmitted: (v) {
|
||||
context.read<EtiquetasCubit>().addHashExterno(v);
|
||||
_hashController.clear();
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// Selected tags
|
||||
if (state.seleccionadas.isNotEmpty) ...[
|
||||
Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Text(
|
||||
'Seleccionadas (${state.seleccionadas.length})',
|
||||
style: const TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
const Spacer(),
|
||||
TextButton(
|
||||
onPressed: () =>
|
||||
context.read<EtiquetasCubit>().clearSeleccion(),
|
||||
child: const Text('Limpiar'),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Wrap(
|
||||
spacing: 8,
|
||||
runSpacing: 8,
|
||||
children: state.seleccionadas.map((hash) {
|
||||
final resolved = state.resolvedHashes[hash];
|
||||
return Chip(
|
||||
avatar: resolved?.imagenUrl != null
|
||||
? CircleAvatar(
|
||||
backgroundImage: CachedNetworkImageProvider(
|
||||
resolved!.imagenUrl!,
|
||||
),
|
||||
)
|
||||
: const CircleAvatar(child: Icon(Icons.tag, size: 16)),
|
||||
label: Text(resolved?.displayName ??
|
||||
HashUtils.truncateHash(hash)),
|
||||
onDeleted: () =>
|
||||
context.read<EtiquetasCubit>().removeEtiqueta(hash),
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
],
|
||||
|
||||
// Bibliotecas
|
||||
...state.bibliotecas.map((biblioteca) {
|
||||
final etiquetas = state.etiquetasPorBiblioteca[biblioteca.id] ?? [];
|
||||
return Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Text(
|
||||
biblioteca.nombre,
|
||||
style: const TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
const Spacer(),
|
||||
if (etiquetas.isEmpty)
|
||||
TextButton.icon(
|
||||
onPressed: () => context
|
||||
.read<EtiquetasCubit>()
|
||||
.syncBiblioteca(biblioteca.id!),
|
||||
icon: const Icon(Icons.download, size: 18),
|
||||
label: const Text('Cargar'),
|
||||
),
|
||||
],
|
||||
),
|
||||
if (etiquetas.isNotEmpty) ...[
|
||||
const SizedBox(height: 12),
|
||||
GridView.builder(
|
||||
shrinkWrap: true,
|
||||
physics: const NeverScrollableScrollPhysics(),
|
||||
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
|
||||
crossAxisCount: 4,
|
||||
crossAxisSpacing: 8,
|
||||
mainAxisSpacing: 8,
|
||||
),
|
||||
itemCount: etiquetas.length,
|
||||
itemBuilder: (context, index) {
|
||||
final etiqueta = etiquetas[index];
|
||||
final isSelected = state.isSelected(etiqueta.hMaestro);
|
||||
return InkWell(
|
||||
onTap: () => context
|
||||
.read<EtiquetasCubit>()
|
||||
.toggleEtiqueta(etiqueta.hMaestro),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
child: Container(
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border.all(
|
||||
color: isSelected
|
||||
? Theme.of(context).colorScheme.primary
|
||||
: Colors.grey.shade300,
|
||||
width: isSelected ? 2 : 1,
|
||||
),
|
||||
color: isSelected
|
||||
? Theme.of(context)
|
||||
.colorScheme
|
||||
.primaryContainer
|
||||
.withOpacity(0.3)
|
||||
: null,
|
||||
),
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
if (etiqueta.imagenUrl != null)
|
||||
Expanded(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(4),
|
||||
child: CachedNetworkImage(
|
||||
imageUrl: etiqueta.imagenUrl!,
|
||||
fit: BoxFit.contain,
|
||||
placeholder: (_, __) =>
|
||||
const Icon(Icons.image),
|
||||
errorWidget: (_, __, ___) =>
|
||||
const Icon(Icons.tag),
|
||||
),
|
||||
),
|
||||
)
|
||||
else
|
||||
const Icon(Icons.tag),
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(4),
|
||||
child: Text(
|
||||
etiqueta.ref ?? etiqueta.displayName,
|
||||
style: const TextStyle(fontSize: 10),
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
141
lib/presentation/pages/packs_page.dart
Normal file
141
lib/presentation/pages/packs_page.dart
Normal file
@@ -0,0 +1,141 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import '../bloc/packs/packs_cubit.dart';
|
||||
import '../bloc/etiquetas/etiquetas_cubit.dart';
|
||||
import '../bloc/etiquetas/etiquetas_state.dart';
|
||||
|
||||
class PacksPage extends StatelessWidget {
|
||||
const PacksPage({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<PacksCubit, PacksState>(
|
||||
builder: (context, state) {
|
||||
if (state.isLoading) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
|
||||
return Scaffold(
|
||||
body: state.packs.isEmpty
|
||||
? Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(Icons.inventory_2_outlined,
|
||||
size: 64, color: Colors.grey.shade400),
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
'No hay packs',
|
||||
style: TextStyle(color: Colors.grey.shade600),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
const Text('Crea uno seleccionando etiquetas'),
|
||||
],
|
||||
),
|
||||
)
|
||||
: ListView.builder(
|
||||
padding: const EdgeInsets.all(16),
|
||||
itemCount: state.packs.length,
|
||||
itemBuilder: (context, index) {
|
||||
final pack = state.packs[index];
|
||||
return Card(
|
||||
child: ListTile(
|
||||
leading: Text(pack.icono, style: const TextStyle(fontSize: 24)),
|
||||
title: Text(pack.nombre),
|
||||
subtitle: Text('${pack.tagCount} etiquetas'),
|
||||
trailing: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.play_arrow),
|
||||
onPressed: () {
|
||||
context.read<EtiquetasCubit>().setSeleccionadas(pack.tags);
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Pack "${pack.nombre}" aplicado'),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.delete_outline),
|
||||
onPressed: () => _confirmDelete(context, pack),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
floatingActionButton: BlocBuilder<EtiquetasCubit, EtiquetasState>(
|
||||
builder: (context, etState) {
|
||||
if (etState.seleccionadas.isEmpty) {
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
return FloatingActionButton.extended(
|
||||
onPressed: () => _showCreateDialog(context, etState.seleccionadas),
|
||||
icon: const Icon(Icons.add),
|
||||
label: Text('Crear (${etState.seleccionadas.length})'),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
void _showCreateDialog(BuildContext context, List<String> tags) {
|
||||
final controller = TextEditingController();
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (ctx) => AlertDialog(
|
||||
title: const Text('Nuevo Pack'),
|
||||
content: TextField(
|
||||
controller: controller,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Nombre del pack',
|
||||
),
|
||||
autofocus: true,
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(ctx),
|
||||
child: const Text('Cancelar'),
|
||||
),
|
||||
FilledButton(
|
||||
onPressed: () {
|
||||
if (controller.text.isNotEmpty) {
|
||||
context.read<PacksCubit>().addPack(controller.text, tags);
|
||||
Navigator.pop(ctx);
|
||||
}
|
||||
},
|
||||
child: const Text('Crear'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _confirmDelete(BuildContext context, dynamic pack) {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (ctx) => AlertDialog(
|
||||
title: const Text('Eliminar pack'),
|
||||
content: Text('¿Eliminar "${pack.nombre}"?'),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(ctx),
|
||||
child: const Text('Cancelar'),
|
||||
),
|
||||
FilledButton(
|
||||
onPressed: () {
|
||||
context.read<PacksCubit>().deletePack(pack.id);
|
||||
Navigator.pop(ctx);
|
||||
},
|
||||
child: const Text('Eliminar'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
223
lib/presentation/pages/pendientes_page.dart
Normal file
223
lib/presentation/pages/pendientes_page.dart
Normal file
@@ -0,0 +1,223 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import '../bloc/pendientes/pendientes_cubit.dart';
|
||||
import '../bloc/app/app_cubit.dart';
|
||||
import '../bloc/app/app_state.dart';
|
||||
import '../../core/utils/hash_utils.dart';
|
||||
import '../../core/utils/retry_utils.dart';
|
||||
|
||||
class PendientesPage extends StatelessWidget {
|
||||
const PendientesPage({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<PendientesCubit, PendientesState>(
|
||||
builder: (context, state) {
|
||||
if (state.isLoading) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
|
||||
return Scaffold(
|
||||
body: Column(
|
||||
children: [
|
||||
if (state.queueFull)
|
||||
Container(
|
||||
width: double.infinity,
|
||||
color: Theme.of(context).colorScheme.errorContainer,
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(Icons.warning,
|
||||
color: Theme.of(context).colorScheme.error),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: Text(
|
||||
'Cola llena (20/20). Libera espacio para continuar.',
|
||||
style: TextStyle(
|
||||
color: Theme.of(context).colorScheme.error,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: state.pendientes.isEmpty
|
||||
? Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(Icons.check_circle_outline,
|
||||
size: 64, color: Colors.green.shade400),
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
'Sin pendientes',
|
||||
style: TextStyle(color: Colors.grey.shade600),
|
||||
),
|
||||
],
|
||||
),
|
||||
)
|
||||
: RefreshIndicator(
|
||||
onRefresh: () => context.read<PendientesCubit>().load(),
|
||||
child: ListView.builder(
|
||||
padding: const EdgeInsets.all(16),
|
||||
itemCount: state.pendientes.length,
|
||||
itemBuilder: (context, index) {
|
||||
final pendiente = state.pendientes[index];
|
||||
final dateFormat = DateFormat('HH:mm dd/MM');
|
||||
return Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Icon(
|
||||
pendiente.puedeReintentar
|
||||
? Icons.schedule
|
||||
: Icons.error,
|
||||
color: pendiente.puedeReintentar
|
||||
? Colors.orange
|
||||
: Colors.red,
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment:
|
||||
CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
pendiente.titulo ?? 'Sin título',
|
||||
style: const TextStyle(
|
||||
fontWeight: FontWeight.bold),
|
||||
),
|
||||
Text(
|
||||
HashUtils.truncateHash(
|
||||
pendiente.hash),
|
||||
style: TextStyle(
|
||||
fontFamily: 'monospace',
|
||||
fontSize: 12,
|
||||
color: Colors.grey.shade600,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Row(
|
||||
children: [
|
||||
_InfoChip(
|
||||
icon: Icons.repeat,
|
||||
label: '${pendiente.intentos}/20',
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
if (pendiente.ultimoIntento != null)
|
||||
_InfoChip(
|
||||
icon: Icons.access_time,
|
||||
label: dateFormat
|
||||
.format(pendiente.ultimoIntento!),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
if (pendiente.proximoIntento != null)
|
||||
_InfoChip(
|
||||
icon: Icons.timer,
|
||||
label: RetryUtils.formatTimeRemaining(
|
||||
pendiente.proximoIntento!),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
children: [
|
||||
TextButton.icon(
|
||||
onPressed: () => context
|
||||
.read<PendientesCubit>()
|
||||
.eliminar(pendiente.hash),
|
||||
icon: const Icon(Icons.download),
|
||||
label: const Text('Recuperar'),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
BlocBuilder<AppCubit, AppState>(
|
||||
builder: (context, appState) {
|
||||
return FilledButton.icon(
|
||||
onPressed: pendiente.puedeReintentar &&
|
||||
appState.destinoActivo !=
|
||||
null
|
||||
? () => context
|
||||
.read<PendientesCubit>()
|
||||
.reintentar(
|
||||
pendiente,
|
||||
appState.destinoActivo!,
|
||||
)
|
||||
: null,
|
||||
icon: const Icon(Icons.replay),
|
||||
label: const Text('Reintentar'),
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
floatingActionButton: state.pendientes.isNotEmpty
|
||||
? BlocBuilder<AppCubit, AppState>(
|
||||
builder: (context, appState) {
|
||||
return FloatingActionButton.extended(
|
||||
onPressed: appState.destinoActivo != null
|
||||
? () => context
|
||||
.read<PendientesCubit>()
|
||||
.reintentarTodos(appState.destinoActivo!)
|
||||
: null,
|
||||
icon: const Icon(Icons.replay),
|
||||
label: const Text('Reintentar todos'),
|
||||
);
|
||||
},
|
||||
)
|
||||
: null,
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _InfoChip extends StatelessWidget {
|
||||
final IconData icon;
|
||||
final String label;
|
||||
|
||||
const _InfoChip({required this.icon, required this.label});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.grey.shade200,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(icon, size: 14, color: Colors.grey.shade600),
|
||||
const SizedBox(width: 4),
|
||||
Text(
|
||||
label,
|
||||
style: TextStyle(fontSize: 12, color: Colors.grey.shade600),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
106
lib/presentation/widgets/audio_recorder.dart
Normal file
106
lib/presentation/widgets/audio_recorder.dart
Normal file
@@ -0,0 +1,106 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
import 'dart:typed_data';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:record/record.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
|
||||
class AudioRecorderButton extends StatefulWidget {
|
||||
final void Function(Uint8List bytes, String nombre) onRecorded;
|
||||
|
||||
const AudioRecorderButton({super.key, required this.onRecorded});
|
||||
|
||||
@override
|
||||
State<AudioRecorderButton> createState() => _AudioRecorderButtonState();
|
||||
}
|
||||
|
||||
class _AudioRecorderButtonState extends State<AudioRecorderButton> {
|
||||
final _recorder = AudioRecorder();
|
||||
bool _isRecording = false;
|
||||
int _seconds = 0;
|
||||
Timer? _timer;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_timer?.cancel();
|
||||
_recorder.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> _toggleRecording() async {
|
||||
if (_isRecording) {
|
||||
await _stopRecording();
|
||||
} else {
|
||||
await _startRecording();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _startRecording() async {
|
||||
final hasPermission = await _recorder.hasPermission();
|
||||
if (!hasPermission) return;
|
||||
|
||||
final dir = await getTemporaryDirectory();
|
||||
final path = '${dir.path}/audio_${DateTime.now().millisecondsSinceEpoch}.m4a';
|
||||
|
||||
await _recorder.start(
|
||||
const RecordConfig(encoder: AudioEncoder.aacLc),
|
||||
path: path,
|
||||
);
|
||||
|
||||
setState(() {
|
||||
_isRecording = true;
|
||||
_seconds = 0;
|
||||
});
|
||||
|
||||
_timer = Timer.periodic(const Duration(seconds: 1), (t) {
|
||||
setState(() => _seconds++);
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _stopRecording() async {
|
||||
_timer?.cancel();
|
||||
final path = await _recorder.stop();
|
||||
|
||||
setState(() {
|
||||
_isRecording = false;
|
||||
_seconds = 0;
|
||||
});
|
||||
|
||||
if (path != null) {
|
||||
final file = File(path);
|
||||
final bytes = await file.readAsBytes();
|
||||
final nombre = 'audio_${DateTime.now().millisecondsSinceEpoch}.m4a';
|
||||
widget.onRecorded(bytes, nombre);
|
||||
await file.delete();
|
||||
}
|
||||
}
|
||||
|
||||
String _formatDuration(int seconds) {
|
||||
final mins = seconds ~/ 60;
|
||||
final secs = seconds % 60;
|
||||
return '${mins.toString().padLeft(2, '0')}:${secs.toString().padLeft(2, '0')}';
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return FilledButton.tonal(
|
||||
onPressed: _toggleRecording,
|
||||
style: FilledButton.styleFrom(
|
||||
backgroundColor:
|
||||
_isRecording ? Theme.of(context).colorScheme.errorContainer : null,
|
||||
),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(
|
||||
_isRecording ? Icons.stop : Icons.mic,
|
||||
size: 18,
|
||||
color: _isRecording ? Theme.of(context).colorScheme.error : null,
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
Text(_isRecording ? _formatDuration(_seconds) : 'Audio'),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user