43 lines
1.2 KiB
Dart
43 lines
1.2 KiB
Dart
|
|
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();
|
||
|
|
}
|
||
|
|
}
|