From 8d5cf7592d6376cb6064b96a02131a7c2505f59f Mon Sep 17 00:00:00 2001 From: Marco Date: Sat, 19 Nov 2022 16:36:29 +0100 Subject: [PATCH] Add Connection cubit that updates the part that must be rebuilt after a reconnect. --- lib/chessapp/chess_app.dart | 99 ++++++++++++---------- lib/connection_cubit/connection_cubit.dart | 29 +++++++ pubspec.lock | 8 +- 3 files changed, 87 insertions(+), 49 deletions(-) create mode 100644 lib/connection_cubit/connection_cubit.dart diff --git a/lib/chessapp/chess_app.dart b/lib/chessapp/chess_app.dart index 435afcc..77ab77f 100644 --- a/lib/chessapp/chess_app.dart +++ b/lib/chessapp/chess_app.dart @@ -4,61 +4,69 @@ import 'package:mchess/chess_bloc/chess_bloc.dart'; import 'package:mchess/connection/ws_connection.dart'; import 'package:mchess/chessapp/chess_board.dart'; +import 'package:mchess/connection_cubit/connection_cubit.dart'; class ChessApp extends StatelessWidget { const ChessApp({super.key}); @override Widget build(BuildContext context) { - return MaterialApp( - title: 'mChess', - home: Scaffold( - body: Container( - decoration: const BoxDecoration( - gradient: LinearGradient( - begin: Alignment.topCenter, - end: Alignment.bottomCenter, - colors: [ - Color.fromARGB(255, 20, 20, 20), - Color.fromARGB(255, 30, 30, 30), - Color.fromARGB(255, 40, 40, 40), - ], - ), - ), - child: Center( - child: FittedBox( - fit: BoxFit.contain, - child: Column( - children: [ - Container( - margin: const EdgeInsets.all(20), - child: BlocProvider( - create: (_) => ChessBloc.getInstance(), - child: BlocBuilder( - builder: (context, state) { - return ChessBoard( - bState: state, - ); - }, - ), - ), - ), - StreamBuilder( - stream: ServerConnection.getInstance().channel.stream, - builder: (context, snapshot) { - return Text( - style: const TextStyle(color: Colors.white), - snapshot.data.toString()); - }, - ) + return BlocProvider( + create: (_) => ConnectionCubit.getInstance(), + child: MaterialApp( + title: 'mChess', + home: Scaffold( + body: Container( + decoration: const BoxDecoration( + gradient: LinearGradient( + begin: Alignment.topCenter, + end: Alignment.bottomCenter, + colors: [ + Color.fromARGB(255, 20, 20, 20), + Color.fromARGB(255, 30, 30, 30), + Color.fromARGB(255, 40, 40, 40), ], ), ), + child: Center( + child: FittedBox( + fit: BoxFit.contain, + child: Column( + children: [ + Container( + margin: const EdgeInsets.all(20), + child: BlocProvider( + create: (_) => ChessBloc.getInstance(), + child: BlocBuilder( + builder: (context, state) { + return ChessBoard( + bState: state, + ); + }, + ), + ), + ), + BlocBuilder( + builder: (context, state) { + return StreamBuilder( + stream: ServerConnection.getInstance().channel.stream, + builder: (context, snapshot) { + return Text( + style: const TextStyle(color: Colors.white), + snapshot.data.toString()); + }, + ); + }, + ) + ], + ), + ), + ), + ), + floatingActionButton: const FloatingActionButton( + onPressed: reconnect, + child: Icon(Icons.connect_without_contact), ), - ), - floatingActionButton: const FloatingActionButton( - onPressed: reconnect, - child: Icon(Icons.connect_without_contact), ), ), ); @@ -67,4 +75,5 @@ class ChessApp extends StatelessWidget { void reconnect() { ServerConnection.getInstance().reconnect(); + ConnectionCubit.getInstance().reconnect(); } diff --git a/lib/connection_cubit/connection_cubit.dart b/lib/connection_cubit/connection_cubit.dart new file mode 100644 index 0000000..74d699a --- /dev/null +++ b/lib/connection_cubit/connection_cubit.dart @@ -0,0 +1,29 @@ +import 'package:flutter_bloc/flutter_bloc.dart'; + +class ConnectionCubit extends Cubit { + static final ConnectionCubit _instance = ConnectionCubit._internal(); + + ConnectionCubit._internal() : super(ConnectionCubitState.init()); + + factory ConnectionCubit.getInstance() { + return ConnectionCubit(); + } + + factory ConnectionCubit() { + return _instance; + } + + void reconnect() { + emit(ConnectionCubitState(true)); + } +} + +class ConnectionCubitState { + final bool reconnecting; + + ConnectionCubitState(this.reconnecting); + + factory ConnectionCubitState.init() { + return ConnectionCubitState(false); + } +} diff --git a/pubspec.lock b/pubspec.lock index 3d0b75e..3daec7f 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -236,10 +236,10 @@ packages: dependency: transitive description: name: string_scanner - sha256: "862015c5db1f3f3c4ea3b94dc2490363a84262994b88902315ed74be1155612f" + sha256: "556692adab6cfa87322a115640c11f13cb77b3f076ddcc5d6ae3c20242bedcde" url: "https://pub.dev" source: hosted - version: "1.1.1" + version: "1.2.0" term_glyph: dependency: transitive description: @@ -252,10 +252,10 @@ packages: dependency: transitive description: name: test_api - sha256: c9aba3b3dbfe8878845dfab5fa096eb8de7b62231baeeb1cea8e3ee81ca8c6d8 + sha256: ad540f65f92caa91bf21dfc8ffb8c589d6e4dc0c2267818b4cc2792857706206 url: "https://pub.dev" source: hosted - version: "0.4.15" + version: "0.4.16" typed_data: dependency: transitive description: