2021-02-19 16:32:38 +00:00
|
|
|
import 'package:comunic/helpers/account_helper.dart';
|
2020-04-18 11:48:21 +00:00
|
|
|
import 'package:comunic/helpers/events_helper.dart';
|
2021-02-19 16:32:38 +00:00
|
|
|
import 'package:comunic/helpers/server_config_helper.dart';
|
2021-02-20 07:58:03 +00:00
|
|
|
import 'package:comunic/helpers/version_helper.dart';
|
2020-04-17 13:25:26 +00:00
|
|
|
import 'package:comunic/helpers/websocket_helper.dart';
|
2021-02-20 08:24:51 +00:00
|
|
|
import 'package:comunic/ui/dialogs/deprecation_dialog.dart';
|
2020-04-20 07:02:54 +00:00
|
|
|
import 'package:comunic/ui/routes/login_route.dart';
|
2020-05-09 12:07:14 +00:00
|
|
|
import 'package:comunic/ui/routes/main_route/main_route.dart';
|
2020-05-05 11:21:37 +00:00
|
|
|
import 'package:comunic/ui/routes/main_route/smartphone_route.dart';
|
2020-05-05 11:31:03 +00:00
|
|
|
import 'package:comunic/ui/routes/main_route/tablet_route.dart';
|
2020-04-18 11:48:21 +00:00
|
|
|
import 'package:comunic/ui/widgets/safe_state.dart';
|
2021-03-14 16:44:29 +00:00
|
|
|
import 'package:comunic/utils/flutter_utils.dart';
|
2020-04-17 13:25:26 +00:00
|
|
|
import 'package:comunic/utils/intl_utils.dart';
|
2020-05-05 11:31:03 +00:00
|
|
|
import 'package:comunic/utils/ui_utils.dart';
|
2020-04-17 13:25:26 +00:00
|
|
|
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
|
|
|
|
2020-04-20 07:02:54 +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
|
2021-02-18 18:11:50 +00:00
|
|
|
super.listen<WSClosedEvent>((e) {
|
|
|
|
_popToMainRoute();
|
|
|
|
_tryConnect();
|
|
|
|
});
|
2020-04-17 13:25:26 +00:00
|
|
|
}
|
|
|
|
|
2020-04-20 06:52:01 +00:00
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
super.dispose();
|
|
|
|
WebSocketHelper.close();
|
|
|
|
}
|
|
|
|
|
2020-04-20 07:02:54 +00:00
|
|
|
/// 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 {
|
2021-02-19 16:32:38 +00:00
|
|
|
await ServerConfigurationHelper.ensureLoaded();
|
|
|
|
|
2021-03-14 16:44:29 +00:00
|
|
|
if (!isWeb &&
|
|
|
|
ServerConfigurationHelper.config.minSupportedMobileVersion >
|
|
|
|
VersionHelper.version) await showDeprecationDialog(context);
|
2021-02-20 08:24:51 +00:00
|
|
|
|
2021-02-19 16:32:38 +00:00
|
|
|
if (!AccountHelper.isUserIDLoaded) {
|
|
|
|
_popToMainRoute();
|
|
|
|
_openLoginPage();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-18 18:36:51 +00:00
|
|
|
print("Attempting WebSocket connection...");
|
|
|
|
|
2020-04-17 13:25:26 +00:00
|
|
|
setState(() {
|
|
|
|
_error = false;
|
|
|
|
});
|
|
|
|
|
|
|
|
await WebSocketHelper.connect();
|
|
|
|
|
|
|
|
setState(() {});
|
|
|
|
} catch (e, stack) {
|
|
|
|
print("Could not connect to server! $e");
|
|
|
|
print(stack);
|
|
|
|
|
2021-02-18 18:11:50 +00:00
|
|
|
_popToMainRoute();
|
2020-05-01 07:17:08 +00:00
|
|
|
|
2020-04-17 13:25:26 +00:00
|
|
|
setState(() {
|
|
|
|
_error = true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2021-02-18 18:36:51 +00:00
|
|
|
return (!_error && WebSocketHelper.isConnected())
|
2020-05-09 12:07:14 +00:00
|
|
|
? (isTablet(context)
|
|
|
|
? TabletRoute(key: mainControllerKey)
|
|
|
|
: SmartphoneMainRoute(key: mainControllerKey))
|
2020-04-17 14:04:47 +00:00
|
|
|
: _buildNonReadyWidget();
|
2020-04-17 13:25:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Build loading widget
|
|
|
|
Widget _buildNonReadyWidget() {
|
|
|
|
return Scaffold(
|
|
|
|
backgroundColor: Colors.indigo.shade900,
|
|
|
|
body: Center(
|
2020-05-03 19:13:20 +00:00
|
|
|
child: Material(
|
|
|
|
color: Colors.transparent,
|
|
|
|
textStyle: TextStyle(color: Colors.white),
|
|
|
|
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,
|
|
|
|
),
|
2021-03-14 16:44:29 +00:00
|
|
|
!isWeb
|
|
|
|
? Text(tr("Version %version% - Build %build%", args: {
|
|
|
|
"version": VersionHelper.info.version.toString(),
|
|
|
|
"build": VersionHelper.info.buildNumber.toString()
|
|
|
|
}))
|
|
|
|
: Container(),
|
2021-02-20 07:58:03 +00:00
|
|
|
Spacer(flex: 1),
|
2020-05-03 19:13:20 +00:00
|
|
|
],
|
|
|
|
),
|
2020-04-17 13:25:26 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget _buildConnectingWidget() {
|
|
|
|
return CircularProgressIndicator();
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget _buildErrorWidget() {
|
|
|
|
return Column(
|
|
|
|
children: <Widget>[
|
2020-05-03 19:13:20 +00:00
|
|
|
Icon(Icons.error, color: Colors.white),
|
2020-04-17 13:25:26 +00:00
|
|
|
SizedBox(height: 30),
|
|
|
|
Text(tr("Could not connect to server!")),
|
|
|
|
SizedBox(
|
|
|
|
height: 30,
|
|
|
|
),
|
2021-03-13 14:38:43 +00:00
|
|
|
ElevatedButton(
|
2020-04-17 13:25:26 +00:00
|
|
|
onPressed: () => _tryConnect(),
|
|
|
|
child: Text(tr("Try again")),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
2021-02-18 18:11:50 +00:00
|
|
|
|
|
|
|
void _popToMainRoute() {
|
|
|
|
// Pop until we reach main route
|
|
|
|
Navigator.of(context).popUntil((settings) =>
|
|
|
|
ModalRoute.of(context).isCurrent || !ModalRoute.of(context).isActive);
|
|
|
|
}
|
2020-04-17 13:25:26 +00:00
|
|
|
}
|