mnemo_cards/lib/features/packs/pack_holder.dart

41 lines
834 B
Dart
Raw Normal View History

2024-05-22 20:48:44 +00:00
import 'dart:async';
import 'package:rxdart/rxdart.dart';
class PackHolder {
2024-08-18 16:52:03 +00:00
final StreamController<PackState> _streamController =
2024-05-22 20:48:44 +00:00
StreamController.broadcast();
2024-08-18 16:52:03 +00:00
PackState _state;
2024-05-22 20:48:44 +00:00
2024-08-18 16:52:03 +00:00
PackHolder() : _state = PackState.empty;
2024-05-22 20:48:44 +00:00
2024-08-18 16:52:03 +00:00
void setPacksWithCards(
Iterable<String> packs,
) {
_state = PackState(packs.toSet());
2024-05-22 20:48:44 +00:00
_streamController.add(_state);
}
2024-08-18 16:52:03 +00:00
void removePackWithCards(String id) =>
setPacksWithCards(state.packsWithCards.toList()..remove(id));
2024-05-22 20:48:44 +00:00
void clear() {
2024-08-18 16:52:03 +00:00
_state = PackState.empty;
2024-05-22 20:48:44 +00:00
_streamController.add(_state);
}
2024-08-18 16:52:03 +00:00
PackState get state => _state;
2024-05-22 20:48:44 +00:00
2024-08-18 16:52:03 +00:00
Stream<PackState> get asStream => _streamController.stream.startWith(_state);
2024-05-22 20:48:44 +00:00
}
class PackState {
2024-08-18 16:52:03 +00:00
final Set<String> packsWithCards;
static const empty = PackState({});
2024-05-22 20:48:44 +00:00
2024-08-18 16:52:03 +00:00
const PackState(
this.packsWithCards,
);
2024-05-22 20:48:44 +00:00
}