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: replace GameWidget for Crossword2View #377

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class CrosswordRepository {

final DbClient _dbClient;

static const _sectionsCollection = 'boardChunks';
static const _answersCollection = 'answers';
static const _sectionsCollection = 'boardChunks2';
static const _answersCollection = 'answers2';
static const _boardInfoCollection = 'boardInfo';

/// Fetches all sections from the board.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ void main() {
group('CrosswordRepository', () {
late DbClient dbClient;

const sectionsCollection = 'boardChunks';
const answersCollection = 'answers';
const sectionsCollection = 'boardChunks2';
const answersCollection = 'answers2';

setUpAll(() {
registerFallbackValue(_MockDbEntityRecord());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class HintRepository {
final DbClient _dbClient;
final GenerativeModelWrapper _generativeModel;

static const _answersCollection = 'answers';
static const _answersCollection = 'answers2';
static const _hintsCollection = 'hints';
static const _boardInfoCollection = 'boardInfo';

Expand Down
5 changes: 0 additions & 5 deletions lib/crossword/game/crossword_game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,6 @@ class CrosswordGame extends FlameGame
_updateVisibleSections();
}

@override
Color backgroundColor() {
return const Color(0xFF212123);
}

bool get isMobile {
return defaultTargetPlatform == TargetPlatform.android ||
defaultTargetPlatform == TargetPlatform.iOS;
Expand Down
63 changes: 4 additions & 59 deletions lib/crossword/view/crossword_page.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import 'package:api_client/api_client.dart';
import 'package:flame/game.dart' hide Route;
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:io_crossword/bottom_bar/view/bottom_bar.dart';
import 'package:io_crossword/crossword/crossword.dart';
import 'package:io_crossword/crossword2/crossword2.dart';
import 'package:io_crossword/drawer/drawer.dart';
import 'package:io_crossword/l10n/l10n.dart';
import 'package:io_crossword/music/music.dart';
Expand Down Expand Up @@ -81,76 +81,21 @@ class CrosswordView extends StatelessWidget {
}
}

class LoadedBoardView extends StatefulWidget {
@visibleForTesting
class LoadedBoardView extends StatelessWidget {
@visibleForTesting
const LoadedBoardView({super.key});

@visibleForTesting
static const zoomInKey = Key('game_zoomIn');

@visibleForTesting
static const zoomOutKey = Key('game_zoomOut');

@override
State<LoadedBoardView> createState() => LoadedBoardViewState();
}

@visibleForTesting
class LoadedBoardViewState extends State<LoadedBoardView> {
late final CrosswordGame game;

@override
void initState() {
super.initState();
game = CrosswordGame(
crosswordBloc: context.read(),
wordSelectionBloc: context.read(),
playerBloc: context.read(),
);
}

@override
Widget build(BuildContext context) {
final layout = IoLayout.of(context);

return Stack(
children: [
GameWidget(game: game),
const Crossword2View(),
const WordSelectionPage(),
if (layout == IoLayoutData.large) const BottomBar(),
_ZoomControls(game: game),
],
);
}
}

class _ZoomControls extends StatelessWidget {
const _ZoomControls({
required this.game,
});

final CrosswordGame game;

@override
Widget build(BuildContext context) {
return Positioned(
left: 16,
bottom: 16,
child: Column(
children: [
ElevatedButton(
key: LoadedBoardView.zoomInKey,
onPressed: game.zoomIn,
child: const Icon(Icons.zoom_in),
),
const SizedBox(height: 16),
ElevatedButton(
key: LoadedBoardView.zoomOutKey,
onPressed: game.zoomOut,
child: const Icon(Icons.zoom_out),
),
],
),
);
}
}
4 changes: 2 additions & 2 deletions packages/board_generator/lib/src/crossword_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class CrosswordRepository {
}

Future<void> _addAnswers(List<Answer> answers) async {
final answersCollection = firestore.collection('answers');
final answersCollection = firestore.collection('answers2');
for (final answer in answers) {
await answersCollection.doc(answer.id).set(answer.toJson());
}
Expand All @@ -38,7 +38,7 @@ class CrosswordRepository {
/// Adds a list of sections to the database.
Future<void> _addSections(List<BoardSection> sections) async {
for (final section in sections) {
await firestore.collection('boardChunks').add(section.toJson());
await firestore.collection('boardChunks2').add(section.toJson());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class CrosswordRepository {
required this.db,
Random? rng,
}) : _rng = rng ?? Random() {
sectionCollection = db.collection('boardChunks');
sectionCollection = db.collection('boardChunks2');
}

/// The [FirebaseFirestore] instance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void main() {
],
borderWords: const [],
);
const sectionsCollection = 'boardChunks';
const sectionsCollection = 'boardChunks2';

late FirebaseFirestore firebaseFirestore;
late CrosswordRepository crosswordRepository;
Expand Down
41 changes: 11 additions & 30 deletions test/crossword/view/crossword_page_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
import 'package:bloc_test/bloc_test.dart';
import 'package:flame/cache.dart';
import 'package:flame/flame.dart';
import 'package:flame/game.dart';
import 'package:flutter/material.dart' hide Axis;
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:game_domain/game_domain.dart';
import 'package:io_crossword/crossword/crossword.dart';
import 'package:io_crossword/crossword2/crossword2.dart';
import 'package:io_crossword/drawer/drawer.dart';
import 'package:io_crossword/music/widget/mute_button.dart';
import 'package:io_crossword/player/player.dart';
Expand All @@ -28,11 +27,6 @@ class _MockWordSelectionBloc
extends MockBloc<WordSelectionEvent, WordSelectionState>
implements WordSelectionBloc {}

class _FakeBoardSection extends Fake implements BoardSection {
@override
List<Word> get words => [];
}

void main() {
group('$CrosswordPage', () {
testWidgets('renders $CrosswordView', (tester) async {
Expand All @@ -49,6 +43,12 @@ void main() {
setUp(() {
Flame.images = Images(prefix: '');
crosswordBloc = _MockCrosswordBloc();
when(() => crosswordBloc.state).thenReturn(
CrosswordState(
status: CrosswordStatus.success,
sectionSize: 40,
),
);
});

testWidgets('renders $IoAppBar', (tester) async {
Expand Down Expand Up @@ -108,38 +108,19 @@ void main() {
expect(find.byType(ErrorView), findsOneWidget);
});

testWidgets('renders game with ${CrosswordStatus.success}', (tester) async {
when(() => crosswordBloc.state).thenReturn(
CrosswordState(
status: CrosswordStatus.success,
sectionSize: 40,
sections: {
(0, 0): _FakeBoardSection(),
},
),
);

testWidgets('renders $Crossword2View with ${CrosswordStatus.success}',
(tester) async {
await tester.pumpSubject(
CrosswordView(),
crosswordBloc: crosswordBloc,
CrosswordView(),
);

expect(find.byType(GameWidget<CrosswordGame>), findsOneWidget);
expect(find.byType(Crossword2View), findsOneWidget);
});

testWidgets(
'renders $WordSelectionPage when loaded',
(tester) async {
when(() => crosswordBloc.state).thenReturn(
CrosswordState(
status: CrosswordStatus.success,
sectionSize: 40,
sections: {
(0, 0): _FakeBoardSection(),
},
),
);

await tester.pumpSubject(
CrosswordView(),
crosswordBloc: crosswordBloc,
Expand Down
Loading