import 'package:flutter/foundation.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:paperless_mobile/features/labels/model/label.model.dart'; import 'package:paperless_mobile/features/labels/repository/label_repository.dart'; abstract class LabelCubit extends Cubit> { final LabelRepository labelRepository; LabelCubit(this.labelRepository) : super({}); @protected void loadFrom(Iterable items) => emit(Map.fromIterable(items, key: (e) => (e as T).id!)); Future add(T item) async { assert(item.id == null); final addedItem = await save(item); final newState = {...state}; newState.putIfAbsent(addedItem.id!, () => addedItem); emit(newState); return addedItem; } Future replace(T item) async { assert(item.id != null); final updatedItem = await update(item); final newState = {...state}; newState[item.id!] = updatedItem; emit(newState); return updatedItem; } Future remove(T item) async { assert(item.id != null); if (state.containsKey(item.id)) { final deletedId = await delete(item); final newState = {...state}; newState.remove(deletedId); emit(newState); } } void reset() => emit({}); Future initialize(); @protected Future save(T item); @protected Future update(T item); @protected Future delete(T item); }