Skip to content

Commit

Permalink
fix: find random word (#539)
Browse files Browse the repository at this point in the history
* 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 1e1ce96.

* Revert "refactor: WordSelected event"

This reverts commit a37250a.

* fix: randomize words in section to avoid selecting the same word
  • Loading branch information
jsgalarraga authored May 29, 2024
1 parent 6bb4d29 commit 14e6ba3
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 36 deletions.
22 changes: 4 additions & 18 deletions lib/crossword/view/crossword_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -108,34 +108,20 @@ class _CrosswordViewState extends State<CrosswordView>
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<CrosswordBloc>()
.add(CrosswordSectionsLoaded(initialWord));
case RandomWordSelectionStatus.success:
final position = (
state.uncompletedSection!.position.x,
state.uncompletedSection!.position.y
);

context.read<WordSelectionBloc>().add(
WordSelected(
selectedWord: SelectedWord(
section: position,
word: state.uncompletedSection!.words.firstWhere(
(element) => element.solvedTimestamp == null,
),
section: state.sectionPosition!,
word: state.randomWord!,
),
),
);
Expand Down
13 changes: 12 additions & 1 deletion lib/random_word_selection/bloc/random_word_selection_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 9 additions & 5 deletions lib/random_word_selection/bloc/random_word_selection_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object?> get props => [status, uncompletedSection];
List<Object?> get props => [status, randomWord, sectionPosition];
}
6 changes: 4 additions & 2 deletions test/crossword/view/crossword_page_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ void main() {
Stream.fromIterable([
RandomWordSelectionState(
status: RandomWordSelectionStatus.success,
uncompletedSection: section,
randomWord: word,
sectionPosition: (1, 1),
),
]),
initialState: RandomWordSelectionState(),
Expand Down Expand Up @@ -183,7 +184,8 @@ void main() {
[
RandomWordSelectionState(
status: RandomWordSelectionStatus.initialSuccess,
uncompletedSection: section,
randomWord: word,
sectionPosition: (1, 1),
),
],
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ void main() {
RandomWordSelectionState(status: RandomWordSelectionStatus.loading),
RandomWordSelectionState(
status: RandomWordSelectionStatus.success,
uncompletedSection: section,
randomWord: word,
sectionPosition: (1, 1),
),
],
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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', () {
Expand All @@ -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));
Expand Down

0 comments on commit 14e6ba3

Please sign in to comment.