1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-26 23:09:21 +00:00
comunicmobile/lib/ui/widgets/init_widget.dart

129 lines
3.0 KiB
Dart
Raw Normal View History

2020-04-18 11:48:21 +00:00
import 'package:comunic/helpers/events_helper.dart';
2020-04-17 13:25:26 +00:00
import 'package:comunic/helpers/websocket_helper.dart';
import 'package:comunic/ui/routes/login_route.dart';
2020-04-17 13:26:37 +00:00
import 'package:comunic/ui/routes/main_route.dart';
2020-04-18 11:48:21 +00:00
import 'package:comunic/ui/widgets/safe_state.dart';
2020-04-17 13:25:26 +00:00
import 'package:comunic/utils/intl_utils.dart';
import 'package:flutter/material.dart';
/// Comunic account initialization widget
///
/// Application screens should only appears as children of this widget
///
/// This widget ensures that the application is correctly initialized before
/// starting it
///
/// @author Pierre Hubert
class InitializeWidget extends StatefulWidget {
@override
_InitializeWidgetState createState() => _InitializeWidgetState();
}
2020-04-18 11:48:21 +00:00
class _InitializeWidgetState extends SafeState<InitializeWidget> {
2020-04-17 13:25:26 +00:00
bool _error = false;
@override
void initState() {
_tryConnect();
super.initState();
2020-04-18 11:48:21 +00:00
// Check if login token are considered as invalid
super.listen<InvalidLoginTokensEvent>((ev) => _openLoginPage());
2020-04-18 11:48:21 +00:00
// Listen to WebSocket close event
super.listen<WSClosedEvent>((e) => _tryConnect());
2020-04-17 13:25:26 +00:00
}
@override
void dispose() {
super.dispose();
WebSocketHelper.close();
}
/// Open login page
_openLoginPage() {
Navigator.of(context)
.pushReplacement(MaterialPageRoute(builder: (c) => LoginRoute()));
}
2020-04-17 13:25:26 +00:00
/// Try to connect to server
void _tryConnect() async {
try {
setState(() {
_error = false;
});
await WebSocketHelper.connect();
setState(() {});
} catch (e, stack) {
print("Could not connect to server! $e");
print(stack);
setState(() {
_error = true;
});
}
}
@override
Widget build(BuildContext context) {
2020-04-17 14:04:47 +00:00
return !_error && WebSocketHelper.isConnected()
? MainRoute()
: _buildNonReadyWidget();
2020-04-17 13:25:26 +00:00
}
/// Build loading widget
Widget _buildNonReadyWidget() {
return Scaffold(
backgroundColor: Colors.indigo.shade900,
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Spacer(
flex: 4,
),
Text(
tr("Comunic"),
style: TextStyle(fontSize: 50),
),
Spacer(
flex: 2,
),
_error ? _buildErrorWidget() : _buildConnectingWidget(),
Spacer(
flex: 2,
),
],
),
),
);
}
Widget _buildConnectingWidget() {
return CircularProgressIndicator();
}
Widget _buildErrorWidget() {
return Column(
children: <Widget>[
Icon(Icons.error),
SizedBox(height: 30),
Text(tr("Could not connect to server!")),
SizedBox(
height: 30,
),
RaisedButton(
color: Colors.indigo,
onPressed: () => _tryConnect(),
child: Text(tr("Try again")),
),
],
);
}
}