fluffychat/lib/main.dart

170 lines
6.0 KiB
Dart
Raw Normal View History

2020-12-19 13:06:31 +01:00
// @dart=2.9
2020-09-08 10:55:32 +02:00
import 'dart:async';
2020-02-23 08:49:58 +01:00
2021-01-15 19:41:55 +01:00
import 'package:adaptive_theme/adaptive_theme.dart';
import 'package:matrix/encryption/utils/key_verification.dart';
import 'package:matrix/matrix.dart';
2021-01-16 12:46:38 +01:00
import 'package:fluffychat/config/routes.dart';
2021-01-18 08:38:19 +01:00
import 'package:fluffychat/utils/platform_infos.dart';
2020-10-28 10:56:24 +01:00
import 'package:fluffychat/utils/sentry_controller.dart';
2021-01-16 12:46:38 +01:00
import 'package:flutter/cupertino.dart';
2020-02-24 10:24:58 +01:00
import 'package:flutter/foundation.dart';
2020-01-01 19:10:13 +01:00
import 'package:flutter/material.dart';
2021-05-24 10:59:00 +02:00
import 'package:flutter/services.dart';
2021-05-23 13:11:55 +02:00
import 'utils/localized_exception_extension.dart';
2021-01-18 08:38:19 +01:00
import 'package:flutter_app_lock/flutter_app_lock.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
2021-05-23 13:11:55 +02:00
import 'package:future_loading_dialog/future_loading_dialog.dart';
2021-04-21 14:19:54 +02:00
import 'package:universal_html/html.dart' as html;
2021-05-23 13:11:55 +02:00
import 'package:vrouter/vrouter.dart';
2020-01-01 19:10:13 +01:00
2021-06-30 20:20:17 +02:00
import 'utils/matrix_sdk_extensions.dart/flutter_famedly_sdk_hive_database.dart';
2021-06-10 10:20:00 +02:00
import 'widgets/layouts/wait_for_login.dart';
2021-05-22 08:53:52 +02:00
import 'widgets/lock_screen.dart';
import 'widgets/matrix.dart';
2021-01-15 19:41:55 +01:00
import 'config/themes.dart';
2021-04-09 16:29:48 +02:00
import 'config/app_config.dart';
2021-02-07 17:18:38 +01:00
import 'utils/platform_infos.dart';
import 'utils/background_push.dart';
2020-09-08 10:55:32 +02:00
2020-12-11 14:14:33 +01:00
void main() async {
2021-02-07 17:18:38 +01:00
// Our background push shared isolate accesses flutter-internal things very early in the startup proccess
// To make sure that the parts of flutter needed are started up already, we need to ensure that the
// widget bindings are initialized already.
WidgetsFlutterBinding.ensureInitialized();
2020-10-28 18:13:04 +01:00
FlutterError.onError = (FlutterErrorDetails details) =>
Zone.current.handleUncaughtError(details.exception, details.stack);
2021-02-07 17:18:38 +01:00
final client = Client(
PlatformInfos.clientName,
enableE2eeRecovery: true,
verificationMethods: {
KeyVerificationMethod.numbers,
if (PlatformInfos.isMobile || PlatformInfos.isLinux)
KeyVerificationMethod.emoji,
},
importantStateEvents: <String>{
'im.ponies.room_emotes', // we want emotes to work properly
},
databaseBuilder: FlutterFamedlySdkHiveDatabase.hiveDatabaseBuilder,
supportedLoginTypes: {
AuthenticationTypes.password,
if (PlatformInfos.isMobile || PlatformInfos.isWeb) AuthenticationTypes.sso
},
);
2021-02-07 17:18:38 +01:00
if (PlatformInfos.isMobile) {
BackgroundPush.clientOnly(client);
2021-02-07 17:18:38 +01:00
}
2021-07-08 18:42:46 +02:00
final queryParameters = <String, String>{};
if (kIsWeb) {
queryParameters
.addAll(Uri.parse(html.window.location.href).queryParameters);
}
2020-09-08 10:55:32 +02:00
runZonedGuarded(
2021-01-18 08:38:19 +01:00
() => runApp(PlatformInfos.isMobile
? AppLock(
2021-07-08 18:42:46 +02:00
builder: (args) => FluffyChatApp(
client: client,
queryParameters: queryParameters,
),
2021-01-18 08:38:19 +01:00
lockScreen: LockScreen(),
2021-01-18 17:31:27 +01:00
enabled: false,
2021-01-18 08:38:19 +01:00
)
2021-07-08 18:42:46 +02:00
: FluffyChatApp(client: client, queryParameters: queryParameters)),
2020-10-28 10:56:24 +01:00
SentryController.captureException,
2020-09-08 10:55:32 +02:00
);
2020-01-02 22:31:39 +01:00
}
2020-01-01 19:10:13 +01:00
2021-05-23 13:11:55 +02:00
class FluffyChatApp extends StatefulWidget {
2021-04-12 17:31:53 +02:00
final Widget testWidget;
final Client client;
2021-07-08 18:42:46 +02:00
final Map<String, String> queryParameters;
2021-04-10 17:30:15 +02:00
2021-07-08 18:42:46 +02:00
const FluffyChatApp(
{Key key, this.testWidget, @required this.client, this.queryParameters})
2021-04-12 17:31:53 +02:00
: super(key: key);
2021-05-16 16:38:52 +02:00
/// getInitialLink may rereturn the value multiple times if this view is
/// opened multiple times for example if the user logs out after they logged
/// in with qr code or magic link.
static bool gotInitialLink = false;
2021-05-23 13:11:55 +02:00
@override
_FluffyChatAppState createState() => _FluffyChatAppState();
}
class _FluffyChatAppState extends State<FluffyChatApp> {
final GlobalKey<MatrixState> _matrix = GlobalKey<MatrixState>();
2021-05-23 13:11:55 +02:00
GlobalKey<VRouterState> _router;
2021-05-23 18:46:15 +02:00
bool columnMode;
2021-05-23 13:11:55 +02:00
String _initialUrl = '/';
2020-01-01 19:10:13 +01:00
@override
Widget build(BuildContext context) {
2021-01-16 12:46:38 +01:00
return AdaptiveTheme(
light: FluffyThemes.light,
dark: FluffyThemes.dark,
initial: AdaptiveThemeMode.system,
builder: (theme, darkTheme) => LayoutBuilder(
builder: (context, constraints) {
var newColumns =
2021-05-23 20:13:10 +02:00
(constraints.maxWidth / FluffyThemes.columnWidth).floor();
if (newColumns > 3) newColumns = 3;
2021-05-23 18:46:15 +02:00
columnMode ??= newColumns > 1;
_router ??= GlobalKey<VRouterState>();
2021-05-23 18:46:15 +02:00
if (columnMode != newColumns > 1) {
2021-06-22 12:31:18 +02:00
Logs().v('Set Column Mode = $columnMode');
2021-05-16 18:13:35 +02:00
WidgetsBinding.instance.addPostFrameCallback((_) {
setState(() {
_initialUrl = _router.currentState.url;
2021-05-23 18:46:15 +02:00
columnMode = newColumns > 1;
_router = GlobalKey<VRouterState>();
2021-05-23 13:11:55 +02:00
});
2021-05-16 18:13:35 +02:00
});
}
return VRouter(
key: _router,
title: '${AppConfig.applicationName}',
theme: theme,
darkTheme: darkTheme,
localizationsDelegates: L10n.localizationsDelegates,
supportedLocales: L10n.supportedLocales,
initialUrl: _initialUrl,
locale: kIsWeb
? Locale(html.window.navigator.language.split('-').first)
: null,
2021-06-22 12:31:18 +02:00
routes: AppRoutes(columnMode).routes,
builder: (context, child) {
LoadingDialog.defaultTitle = L10n.of(context).loadingPleaseWait;
LoadingDialog.defaultBackLabel = L10n.of(context).close;
LoadingDialog.defaultOnError =
(Object e) => e.toLocalizedString(context);
2021-05-24 10:24:49 +02:00
WidgetsBinding.instance.addPostFrameCallback((_) {
SystemChrome.setSystemUIOverlayStyle(
SystemUiOverlayStyle(
statusBarColor: Colors.transparent,
systemNavigationBarColor: Theme.of(context).backgroundColor,
systemNavigationBarIconBrightness:
Theme.of(context).brightness == Brightness.light
? Brightness.dark
: Brightness.light,
),
);
});
return Matrix(
key: _matrix,
context: context,
router: _router,
client: widget.client,
2021-06-10 10:20:00 +02:00
child: WaitForInitPage(child),
);
},
);
},
2020-01-01 19:10:13 +01:00
),
);
}
}