1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-26 06:49:22 +00:00

Add initialization layer

This commit is contained in:
Pierre HUBERT 2020-04-17 15:25:26 +02:00
parent 6239c10579
commit 7549a9ff22
6 changed files with 134 additions and 4 deletions

View File

@ -0,0 +1,15 @@
/// User web socket helper
///
/// @author Pierre Hubert
class WebSocketHelper {
/// Check out whether we are currently connected to websocket or not
static bool isConnected() {
return true;
}
/// Connect to WebSocket
static connect() async {
if (isConnected()) return;
}
}

View File

@ -1,8 +1,8 @@
import 'package:comunic/helpers/account_helper.dart'; import 'package:comunic/helpers/account_helper.dart';
import 'package:comunic/helpers/database/database_helper.dart'; import 'package:comunic/helpers/database/database_helper.dart';
import 'package:comunic/helpers/preferences_helper.dart'; import 'package:comunic/helpers/preferences_helper.dart';
import 'package:comunic/ui/routes/home_route.dart';
import 'package:comunic/ui/routes/login_route.dart'; import 'package:comunic/ui/routes/login_route.dart';
import 'package:comunic/ui/widgets/init_widget.dart';
import 'package:comunic/utils/intl_utils.dart'; import 'package:comunic/utils/intl_utils.dart';
import 'package:comunic/utils/ui_utils.dart'; import 'package:comunic/utils/ui_utils.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
@ -68,7 +68,7 @@ class _ComunicApplicationHomeState extends State<ComunicApplicationHome> {
if (_signedIn == null) return buildLoadingPage(); if (_signedIn == null) return buildLoadingPage();
if (_signedIn) if (_signedIn)
return HomeRoute(); return InitializeWidget();
else else
return LoginRoute(); return LoginRoute();
} }

View File

@ -1,7 +1,7 @@
import 'package:comunic/helpers/account_helper.dart'; import 'package:comunic/helpers/account_helper.dart';
import 'package:comunic/models/authentication_details.dart'; import 'package:comunic/models/authentication_details.dart';
import 'package:comunic/ui/routes/create_account_route.dart'; import 'package:comunic/ui/routes/create_account_route.dart';
import 'package:comunic/ui/routes/home_route.dart'; import 'package:comunic/ui/widgets/init_widget.dart';
import 'package:comunic/utils/input_utils.dart'; import 'package:comunic/utils/input_utils.dart';
import 'package:comunic/utils/intl_utils.dart'; import 'package:comunic/utils/intl_utils.dart';
import 'package:comunic/utils/ui_utils.dart'; import 'package:comunic/utils/ui_utils.dart';
@ -52,7 +52,7 @@ class _LoginRouteState extends State<LoginRoute> {
if (loginResult == AuthResult.SUCCESS) if (loginResult == AuthResult.SUCCESS)
Navigator.pushReplacement( Navigator.pushReplacement(
context, MaterialPageRoute(builder: (b) => HomeRoute())); context, MaterialPageRoute(builder: (b) => InitializeWidget()));
else else
setState(() { setState(() {
_authResult = loginResult; _authResult = loginResult;

View File

@ -0,0 +1,105 @@
import 'package:comunic/helpers/websocket_helper.dart';
import 'package:comunic/ui/routes/home_route.dart';
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();
}
class _InitializeWidgetState extends State<InitializeWidget> {
bool _error = false;
@override
void initState() {
_tryConnect();
super.initState();
}
/// 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) {
return WebSocketHelper.isConnected() ? HomeRoute() : _buildNonReadyWidget();
}
/// 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")),
),
],
);
}
}

View File

@ -441,6 +441,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.0.8" version: "2.0.8"
web_socket_channel:
dependency: "direct main"
description:
name: web_socket_channel
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
xml: xml:
dependency: transitive dependency: transitive
description: description:

View File

@ -69,6 +69,9 @@ dependencies:
# Check Internet connection # Check Internet connection
connectivity: ^0.4.8+2 connectivity: ^0.4.8+2
# Establish WebSocket connections
web_socket_channel: ^1.1.0
dev_dependencies: dev_dependencies:
flutter_test: flutter_test:
sdk: flutter sdk: flutter