feat(app): choose, manage and filter by tags

The card editor offers its tags: a picker that searches by the start of
a name, creates a tag (name and colour) and chooses it, and saves the
choice with the card. A sheet lists every tag with its count, to add,
rename (merging into a tag of that name), recolour and delete them, with
an undo. The card list shows each card's tags and narrows to one tag.

The editor closed twice once tags changed after saving; it now closes
on becoming done only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Tizian 「ティツィアーン」 <raisondetredev@pm.me>
This commit is contained in:
Tizian 「ティツィアーン」 2026-10-01 16:57:57 +02:00
commit 9dc9b6a5ed
Signed by: raisondetredev
GPG key ID: 07F6FCF405488B64

View file

@ -6,8 +6,10 @@ import 'package:halkyon_learn_flutter/src/feature/card/data/card_payload.dart';
import 'package:halkyon_learn_flutter/src/feature/card/data/card_repository.dart';
import 'package:halkyon_learn_flutter/src/feature/card/model/card.dart';
import 'package:halkyon_learn_flutter/src/feature/card/model/card_content.dart';
import 'package:halkyon_learn_flutter/src/feature/tag/data/tag_repository.dart';
import '../../../support/fake_card_gateway.dart';
import '../../../support/fake_tag_gateway.dart';
RichContent text(String text) => RichContent({
'document': {'type': 'page', 'note': text},
@ -19,17 +21,22 @@ CardContent card(String front, String back) =>
void main() {
late FakeCardGateway gateway;
late CardRepository cards;
late TagRepository tags;
setUp(() {
gateway = FakeCardGateway();
cards = CardRepository(gateway);
tags = TagRepository(FakeTagGateway(), cardChanges: cards.changes);
});
tearDown(() async {
await tags.dispose();
await cards.dispose();
});
tearDown(() => cards.dispose());
group('CardListBloc', () {
test('lists the cards and follows every change', () async {
await cards.create('japanese', card('猫', 'cat'));
final bloc = CardListBloc(cards, 'japanese')
final bloc = CardListBloc(cards, tags, 'japanese')
..add(const CardListEventStarted());
addTearDown(bloc.close);
@ -59,7 +66,7 @@ void main() {
test('moves a card', () async {
await cards.create('japanese', card('a', ''));
await cards.create('japanese', card('b', ''));
final bloc = CardListBloc(cards, 'japanese')
final bloc = CardListBloc(cards, tags, 'japanese')
..add(const CardListEventStarted());
addTearDown(bloc.close);
final loaded = await bloc.stream.firstWhere(
@ -81,10 +88,74 @@ void main() {
});
});
group('CardListBloc with tags', () {
late CardListBloc bloc;
late List<String> ids;
setUp(() async {
for (final front in ['猫', '犬', '食べる']) {
await cards.create('japanese', card(front, ''));
}
ids = [for (final card in await cards.cardsOf('japanese')) card.id];
final noun = await tags.create('noun');
final verb = await tags.create('verb');
await tags.create('unused');
await tags.setTagsOf(ids[0], {noun});
await tags.setTagsOf(ids[1], {noun});
await tags.setTagsOf(ids[2], {verb});
bloc = CardListBloc(cards, tags, 'japanese')
..add(const CardListEventStarted());
});
tearDown(() => bloc.close());
/// The next state that has the tags read.
Future<CardListStateLoaded> tagged() async => await bloc.stream.firstWhere(
(state) => state is CardListStateLoaded && state.tagging.all.isNotEmpty,
) as CardListStateLoaded;
test("shows each card's tags, and offers to filter by those the cards "
'have', () async {
final state = await tagged();
expect(state.tagging.of(ids[2]).map((tag) => tag.name), ['verb']);
expect(state.tagsInUse.map((tag) => tag.name), ['noun', 'verb']);
expect(state.shown, hasLength(3));
});
test('a tag narrows the list to its cards, and clearing it shows them '
'all again', () async {
final noun = (await tagged()).tagging.named('noun')!;
bloc.add(CardListEventTagFilterChanged(noun.id));
final narrowed = await bloc.stream.first as CardListStateLoaded;
bloc.add(const CardListEventTagFilterChanged(null));
final all = await bloc.stream.first as CardListStateLoaded;
expect(narrowed.filter, noun);
expect(narrowed.shown.map((card) => card.id), [ids[0], ids[1]]);
expect(all.shown, hasLength(3));
});
test('a deleted tag no longer narrows the list', () async {
final noun = (await tagged()).tagging.named('noun')!;
bloc.add(CardListEventTagFilterChanged(noun.id));
await bloc.stream.first;
await tags.delete(noun.id);
final state = await bloc.stream.firstWhere(
(state) =>
state is CardListStateLoaded && state.tagging.named('noun') == null,
) as CardListStateLoaded;
expect(state.filter, isNull);
expect(state.shown, hasLength(3));
});
});
group('CardEditBloc', () {
test('a new card can be saved once it has content, and then is '
'done', () async {
final bloc = CardEditBloc(cards, 'japanese', null);
final bloc = CardEditBloc(cards, tags, 'japanese', null);
addTearDown(bloc.close);
expect(bloc.state.canSave, isFalse);
@ -109,6 +180,7 @@ void main() {
await cards.create('japanese', card('猫', 'cat'));
final bloc = CardEditBloc(
cards,
tags,
'japanese',
(await cards.cardsOf('japanese')).single,
);
@ -138,7 +210,7 @@ void main() {
payload: encodeCardPayload(card('猫', 'the cat')),
);
conflicted = (await cards.cardsOf('japanese')).single;
bloc = CardEditBloc(cards, 'japanese', conflicted);
bloc = CardEditBloc(cards, tags, 'japanese', conflicted);
});
tearDown(() => bloc.close());
@ -171,10 +243,71 @@ void main() {
});
});
test("shows a card's tags; changing only them can be saved, without a "
'new revision of its sides', () async {
final cardId = await cards.create('japanese', card('猫', 'cat'));
final noun = await tags.create('noun');
final animal = await tags.create('animal');
await tags.setTagsOf(cardId, {noun});
final bloc = CardEditBloc(
cards,
tags,
'japanese',
(await cards.cardsOf('japanese')).single,
)..add(const CardEditEventStarted());
addTearDown(bloc.close);
final loaded = await bloc.stream.firstWhere((s) => s.tagIds != null);
expect(loaded.tagIds, {noun});
expect(loaded.canSave, isFalse);
bloc.add(CardEditEventTagsChanged({noun, animal}));
await expectLater(
bloc.stream,
emits(isA<CardEditState>().having((s) => s.canSave, 'can save', true)),
);
bloc.add(const CardEditEventSaveRequested());
await bloc.stream.firstWhere((s) => s.isDone);
final tagging = await tags.read();
expect(tagging.of(cardId).map((tag) => tag.name), ['animal', 'noun']);
expect(await gateway.revisionsOf({cardId}), hasLength(1));
});
test('a new card gets the tags chosen once saved', () async {
final noun = await tags.create('noun');
final bloc = CardEditBloc(cards, tags, 'japanese', null)
..add(const CardEditEventStarted());
addTearDown(bloc.close);
await bloc.stream.firstWhere((s) => s.tagIds != null);
bloc
..add(CardEditEventTagsChanged({noun}))
..add(CardEditEventSideChanged(CardSide.front, text('猫')))
..add(const CardEditEventSaveRequested());
await bloc.stream.firstWhere((s) => s.isDone);
final cardId = (await cards.cardsOf('japanese')).single.id;
expect((await tags.read()).of(cardId).map((tag) => tag.id), [noun]);
});
test('a new card with only tags has nothing to save', () async {
final noun = await tags.create('noun');
final bloc = CardEditBloc(cards, tags, 'japanese', null)
..add(const CardEditEventStarted());
addTearDown(bloc.close);
await bloc.stream.firstWhere((s) => s.tagIds != null);
bloc.add(CardEditEventTagsChanged({noun}));
final state = await bloc.stream.first;
expect(state.canSave, isFalse);
});
test('deleting the card is done too', () async {
await cards.create('japanese', card('猫', 'cat'));
final bloc = CardEditBloc(
cards,
tags,
'japanese',
(await cards.cardsOf('japanese')).single,
)..add(const CardEditEventDeleteRequested());