From f7701138a41036d976d0b70ea70a8cf6514c0119 Mon Sep 17 00:00:00 2001 From: Marco Date: Sun, 13 Nov 2022 01:03:09 +0100 Subject: [PATCH] Add chess bloc. --- lib/chess_bloc/chess_bloc.dart | 52 ++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 lib/chess_bloc/chess_bloc.dart diff --git a/lib/chess_bloc/chess_bloc.dart b/lib/chess_bloc/chess_bloc.dart new file mode 100644 index 0000000..9a1848d --- /dev/null +++ b/lib/chess_bloc/chess_bloc.dart @@ -0,0 +1,52 @@ +import 'dart:async'; + +import 'package:flutter_bloc/flutter_bloc.dart'; + +import 'package:mchess/chessapp/chess_utils.dart'; +import 'package:mchess/chessapp/chess_square.dart'; + +class ChessBloc extends Bloc { + static final ChessBloc _instance = ChessBloc._internal(); + + ChessBloc._internal() : super(ChessBoardState.init()) { + on(moveHandler); + } + + factory ChessBloc.getInstance() { + return ChessBloc(); + } + + factory ChessBloc() { + return _instance; + } + + FutureOr moveHandler( + PieceMoved event, + Emitter emit, + ) { + emit(ChessBoardState( + !state.flipped, + ChessColor.black, + state.position, + )); + } +} + +abstract class ChessEvent {} + +class PieceMoved extends ChessEvent {} + +class BoardFlippedEvent extends ChessEvent {} + +class ChessBoardState { + final bool flipped; + final ChessColor turnColor; + final Map position; + + ChessBoardState(this.flipped, this.turnColor, this.position); + + ChessBoardState.init() + : flipped = false, + turnColor = ChessColor.white, + position = {}; +}