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/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();
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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;
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										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"
 | 
					      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:
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user