Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: word selection #26

Merged
merged 9 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified assets/images/letters.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 33 additions & 13 deletions lib/crossword/bloc/crossword_bloc.dart
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
import 'dart:async';

import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:game_domain/game_domain.dart';

part 'crossword_event.dart';
part 'crossword_state.dart';

var _id = 0;
int _defaultIdGenerator() => _id++;

class CrosswordBloc extends Bloc<CrosswordEvent, CrosswordState> {
CrosswordBloc() : super(const CrosswordInitial()) {
CrosswordBloc({
int Function()? idGenerator,
}) : _idGenerator = idGenerator ?? _defaultIdGenerator,
super(const CrosswordInitial()) {
on<InitialBoardLoadRequested>(_onInitialBoardLoadRequested);
on<BoardSectionRequested>(_onBoardSectionRequested);
on<WordSelected>(_onWordSelected);
}

// TODO(any): Replace with real data
final int Function() _idGenerator;

Future<void> _onInitialBoardLoadRequested(
InitialBoardLoadRequested event,
Emitter<CrosswordState> emit,
) async {
final section = BoardSection(
id: '1',
id: '${_idGenerator()}',
position: const Point(2, 2),
size: 40,
words: [
Expand Down Expand Up @@ -52,7 +64,7 @@ class CrosswordBloc extends Bloc<CrosswordEvent, CrosswordState> {
final loadedState = state;
if (loadedState is CrosswordLoaded) {
final section = BoardSection(
id: '',
id: '${_idGenerator()}',
position: Point(event.position.$1, event.position.$2),
size: 40,
words: [
Expand All @@ -62,16 +74,7 @@ class CrosswordBloc extends Bloc<CrosswordEvent, CrosswordState> {
answer: 'flutter',
clue: 'flutter',
hints: const ['dart', 'mobile', 'cross-platform'],
visible: true,
solvedTimestamp: null,
),
Word(
axis: Axis.vertical,
position: const Point(4, 1),
answer: 'android',
clue: 'flutter',
hints: const ['dart', 'mobile', 'cross-platform'],
visible: true,
visible: false,
solvedTimestamp: null,
),
Word(
Expand Down Expand Up @@ -106,4 +109,21 @@ class CrosswordBloc extends Bloc<CrosswordEvent, CrosswordState> {
);
}
}

FutureOr<void> _onWordSelected(
WordSelected event,
Emitter<CrosswordState> emit,
) {
final currentState = state;
if (currentState is CrosswordLoaded) {
emit(
currentState.withSelectedWord(
WordSelection(
section: event.section,
wordId: event.wordId,
),
),
);
}
}
}
10 changes: 10 additions & 0 deletions lib/crossword/bloc/crossword_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,13 @@ class BoardSectionRequested extends CrosswordEvent {
@override
List<Object> get props => [position];
}

class WordSelected extends CrosswordEvent {
const WordSelected(this.section, this.wordId);

final (int, int) section;
final String wordId;

@override
List<Object> get props => [section, wordId];
}
33 changes: 32 additions & 1 deletion lib/crossword/bloc/crossword_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,33 @@ class CrosswordLoading extends CrosswordState {
List<Object> get props => [];
}

class WordSelection extends Equatable {
const WordSelection({
required this.section,
required this.wordId,
});

final (int, int) section;
final String wordId;

@override
List<Object> get props => [section, wordId];
}

class CrosswordLoaded extends CrosswordState {
const CrosswordLoaded({
required this.width,
required this.height,
required this.sectionSize,
required this.sections,
this.selectedWord,
});

final int width;
final int height;
final int sectionSize;
final Map<(int, int), BoardSection> sections;
final WordSelection? selectedWord;

CrosswordLoaded copyWith({
int? width,
Expand All @@ -43,8 +58,24 @@ class CrosswordLoaded extends CrosswordState {
);
}

CrosswordState withSelectedWord(WordSelection? selectedWord) {
return CrosswordLoaded(
width: width,
height: height,
sectionSize: sectionSize,
sections: sections,
selectedWord: selectedWord,
);
}

@override
List<Object> get props => [width, height, sectionSize, sections];
List<Object?> get props => [
width,
height,
sectionSize,
sections,
selectedWord,
];
}

class CrosswordError extends CrosswordState {
Expand Down
Loading
Loading