From 14e6ba3be3812fafce736b06d703f3711f61b200 Mon Sep 17 00:00:00 2001 From: Jaime <52668514+jsgalarraga@users.noreply.github.com> Date: Wed, 29 May 2024 18:09:36 +0200 Subject: [PATCH] fix: find random word (#539) * refactor: WordSelected event * fix: randomize words in section to avoid using always the first unsolved word * Revert "fix: randomize words in section to avoid using always the first unsolved word" This reverts commit 1e1ce96b7c463f5c7d528b35d04568ceb62b9918. * Revert "refactor: WordSelected event" This reverts commit a37250aa086d9cb58cb24e6367f2c48cc4e466b9. * fix: randomize words in section to avoid selecting the same word --- lib/crossword/view/crossword_page.dart | 22 ++++--------------- .../bloc/random_word_selection_bloc.dart | 13 ++++++++++- .../bloc/random_word_selection_state.dart | 14 +++++++----- test/crossword/view/crossword_page_test.dart | 6 +++-- .../bloc/random_word_selection_bloc_test.dart | 3 ++- .../random_word_selection_state_test.dart | 21 ++++++++++-------- 6 files changed, 43 insertions(+), 36 deletions(-) diff --git a/lib/crossword/view/crossword_page.dart b/lib/crossword/view/crossword_page.dart index de5a0aa71..e715f7c81 100644 --- a/lib/crossword/view/crossword_page.dart +++ b/lib/crossword/view/crossword_page.dart @@ -108,34 +108,20 @@ class _CrosswordViewState extends State case RandomWordSelectionStatus.failure: break; case RandomWordSelectionStatus.initialSuccess: - final position = ( - state.uncompletedSection!.position.x, - state.uncompletedSection!.position.y - ); - final initialWord = SelectedWord( - section: position, - word: state.uncompletedSection!.words.firstWhere( - (element) => element.solvedTimestamp == null, - ), + section: state.sectionPosition!, + word: state.randomWord!, ); context .read() .add(CrosswordSectionsLoaded(initialWord)); case RandomWordSelectionStatus.success: - final position = ( - state.uncompletedSection!.position.x, - state.uncompletedSection!.position.y - ); - context.read().add( WordSelected( selectedWord: SelectedWord( - section: position, - word: state.uncompletedSection!.words.firstWhere( - (element) => element.solvedTimestamp == null, - ), + section: state.sectionPosition!, + word: state.randomWord!, ), ), ); diff --git a/lib/random_word_selection/bloc/random_word_selection_bloc.dart b/lib/random_word_selection/bloc/random_word_selection_bloc.dart index 21d7b4d4a..dfe88a0bf 100644 --- a/lib/random_word_selection/bloc/random_word_selection_bloc.dart +++ b/lib/random_word_selection/bloc/random_word_selection_bloc.dart @@ -35,12 +35,23 @@ class RandomWordSelectionBloc await _crosswordRepository.getRandomUncompletedSection(bottomRight); if (randomSection != null) { + final sectionPosition = ( + randomSection.position.x, + randomSection.position.y, + ); + + final randomizedWords = [...randomSection.words]..shuffle(); + final randomWord = randomizedWords.firstWhere( + (element) => element.solvedTimestamp == null, + ); + emit( state.copyWith( status: event.isInitial ? RandomWordSelectionStatus.initialSuccess : RandomWordSelectionStatus.success, - uncompletedSection: randomSection, + randomWord: randomWord, + sectionPosition: sectionPosition, ), ); } else { diff --git a/lib/random_word_selection/bloc/random_word_selection_state.dart b/lib/random_word_selection/bloc/random_word_selection_state.dart index 5999e531c..01eb20d85 100644 --- a/lib/random_word_selection/bloc/random_word_selection_state.dart +++ b/lib/random_word_selection/bloc/random_word_selection_state.dart @@ -12,22 +12,26 @@ enum RandomWordSelectionStatus { class RandomWordSelectionState extends Equatable { const RandomWordSelectionState({ this.status = RandomWordSelectionStatus.initial, - this.uncompletedSection, + this.randomWord, + this.sectionPosition, }); final RandomWordSelectionStatus status; - final BoardSection? uncompletedSection; + final Word? randomWord; + final (int, int)? sectionPosition; RandomWordSelectionState copyWith({ RandomWordSelectionStatus? status, - BoardSection? uncompletedSection, + Word? randomWord, + (int, int)? sectionPosition, }) { return RandomWordSelectionState( status: status ?? this.status, - uncompletedSection: uncompletedSection ?? this.uncompletedSection, + randomWord: randomWord ?? this.randomWord, + sectionPosition: sectionPosition ?? this.sectionPosition, ); } @override - List get props => [status, uncompletedSection]; + List get props => [status, randomWord, sectionPosition]; } diff --git a/test/crossword/view/crossword_page_test.dart b/test/crossword/view/crossword_page_test.dart index eb850facb..c5e0022c2 100644 --- a/test/crossword/view/crossword_page_test.dart +++ b/test/crossword/view/crossword_page_test.dart @@ -131,7 +131,8 @@ void main() { Stream.fromIterable([ RandomWordSelectionState( status: RandomWordSelectionStatus.success, - uncompletedSection: section, + randomWord: word, + sectionPosition: (1, 1), ), ]), initialState: RandomWordSelectionState(), @@ -183,7 +184,8 @@ void main() { [ RandomWordSelectionState( status: RandomWordSelectionStatus.initialSuccess, - uncompletedSection: section, + randomWord: word, + sectionPosition: (1, 1), ), ], ), diff --git a/test/random_word_selection/bloc/random_word_selection_bloc_test.dart b/test/random_word_selection/bloc/random_word_selection_bloc_test.dart index e77a443d1..f929b0182 100644 --- a/test/random_word_selection/bloc/random_word_selection_bloc_test.dart +++ b/test/random_word_selection/bloc/random_word_selection_bloc_test.dart @@ -61,7 +61,8 @@ void main() { RandomWordSelectionState(status: RandomWordSelectionStatus.loading), RandomWordSelectionState( status: RandomWordSelectionStatus.success, - uncompletedSection: section, + randomWord: word, + sectionPosition: (1, 1), ), ], ); diff --git a/test/random_word_selection/bloc/random_word_selection_state_test.dart b/test/random_word_selection/bloc/random_word_selection_state_test.dart index 8aea3c1cd..7435a9453 100644 --- a/test/random_word_selection/bloc/random_word_selection_state_test.dart +++ b/test/random_word_selection/bloc/random_word_selection_state_test.dart @@ -6,19 +6,20 @@ import 'package:io_crossword/random_word_selection/bloc/random_word_selection_bl void main() { group('$RandomWordSelectionState', () { - final section = BoardSection( - id: '', - position: Point(1, 1), - size: 10, - words: const [], - borderWords: const [], + final word = Word( + id: 'id', + answer: 'answer', + axis: WordAxis.horizontal, + clue: 'clue', + position: Point(0, 0), ); test('initializes correctly', () { final state = RandomWordSelectionState(); expect(state.status, RandomWordSelectionStatus.initial); - expect(state.uncompletedSection, isNull); + expect(state.randomWord, isNull); + expect(state.sectionPosition, isNull); }); group('copyWith', () { @@ -33,12 +34,14 @@ void main() { final state = RandomWordSelectionState(); final newState = RandomWordSelectionState( status: RandomWordSelectionStatus.success, - uncompletedSection: section, + randomWord: word, + sectionPosition: (1, 1), ); final copy = state.copyWith( status: newState.status, - uncompletedSection: newState.uncompletedSection, + randomWord: newState.randomWord, + sectionPosition: newState.sectionPosition, ); expect(copy, equals(newState));