Add Connection cubit that updates the part that must be rebuilt after a reconnect.

This commit is contained in:
Marco 2022-11-19 16:36:29 +01:00
parent 4768d22168
commit 8d5cf7592d
3 changed files with 87 additions and 49 deletions

View File

@ -4,13 +4,16 @@ 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(
return BlocProvider(
create: (_) => ConnectionCubit.getInstance(),
child: MaterialApp(
title: 'mChess',
home: Scaffold(
body: Container(
@ -43,13 +46,17 @@ class ChessApp extends StatelessWidget {
),
),
),
StreamBuilder(
BlocBuilder<ConnectionCubit, ConnectionCubitState>(
builder: (context, state) {
return StreamBuilder(
stream: ServerConnection.getInstance().channel.stream,
builder: (context, snapshot) {
return Text(
style: const TextStyle(color: Colors.white),
snapshot.data.toString());
},
);
},
)
],
),
@ -61,10 +68,12 @@ class ChessApp extends StatelessWidget {
child: Icon(Icons.connect_without_contact),
),
),
),
);
}
}
void reconnect() {
ServerConnection.getInstance().reconnect();
ConnectionCubit.getInstance().reconnect();
}

View File

@ -0,0 +1,29 @@
import 'package:flutter_bloc/flutter_bloc.dart';
class ConnectionCubit extends Cubit<ConnectionCubitState> {
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);
}
}

View File

@ -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: