mirror of
				https://gitlab.com/comunic/comunicmobile
				synced 2025-11-03 19:54:12 +00:00 
			
		
		
		
	Add initialization layer
This commit is contained in:
		
							
								
								
									
										15
									
								
								lib/helpers/websocket_helper.dart
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								lib/helpers/websocket_helper.dart
									
									
									
									
									
										Normal 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;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,8 +1,8 @@
 | 
			
		||||
import 'package:comunic/helpers/account_helper.dart';
 | 
			
		||||
import 'package:comunic/helpers/database/database_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/widgets/init_widget.dart';
 | 
			
		||||
import 'package:comunic/utils/intl_utils.dart';
 | 
			
		||||
import 'package:comunic/utils/ui_utils.dart';
 | 
			
		||||
import 'package:flutter/material.dart';
 | 
			
		||||
@@ -68,7 +68,7 @@ class _ComunicApplicationHomeState extends State<ComunicApplicationHome> {
 | 
			
		||||
    if (_signedIn == null) return buildLoadingPage();
 | 
			
		||||
 | 
			
		||||
    if (_signedIn)
 | 
			
		||||
      return HomeRoute();
 | 
			
		||||
      return InitializeWidget();
 | 
			
		||||
    else
 | 
			
		||||
      return LoginRoute();
 | 
			
		||||
  }
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,7 @@
 | 
			
		||||
import 'package:comunic/helpers/account_helper.dart';
 | 
			
		||||
import 'package:comunic/models/authentication_details.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/intl_utils.dart';
 | 
			
		||||
import 'package:comunic/utils/ui_utils.dart';
 | 
			
		||||
@@ -52,7 +52,7 @@ class _LoginRouteState extends State<LoginRoute> {
 | 
			
		||||
 | 
			
		||||
    if (loginResult == AuthResult.SUCCESS)
 | 
			
		||||
      Navigator.pushReplacement(
 | 
			
		||||
          context, MaterialPageRoute(builder: (b) => HomeRoute()));
 | 
			
		||||
          context, MaterialPageRoute(builder: (b) => InitializeWidget()));
 | 
			
		||||
    else
 | 
			
		||||
      setState(() {
 | 
			
		||||
        _authResult = loginResult;
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										105
									
								
								lib/ui/widgets/init_widget.dart
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										105
									
								
								lib/ui/widgets/init_widget.dart
									
									
									
									
									
										Normal 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")),
 | 
			
		||||
        ),
 | 
			
		||||
      ],
 | 
			
		||||
    );
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@@ -441,6 +441,13 @@ packages:
 | 
			
		||||
      url: "https://pub.dartlang.org"
 | 
			
		||||
    source: hosted
 | 
			
		||||
    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:
 | 
			
		||||
    dependency: transitive
 | 
			
		||||
    description:
 | 
			
		||||
 
 | 
			
		||||
@@ -69,6 +69,9 @@ dependencies:
 | 
			
		||||
  # Check Internet connection
 | 
			
		||||
  connectivity: ^0.4.8+2
 | 
			
		||||
 | 
			
		||||
  # Establish WebSocket connections
 | 
			
		||||
  web_socket_channel: ^1.1.0
 | 
			
		||||
 | 
			
		||||
dev_dependencies:
 | 
			
		||||
  flutter_test:
 | 
			
		||||
    sdk: flutter
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user