67 lines
1.8 KiB
Dart
67 lines
1.8 KiB
Dart
|
|
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();
|
||
|
|
}
|
||
|
|
}
|