From a67b9ccad1d09d86c424daebd326c4020c96ec26 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Thu, 25 Aug 2022 18:31:30 +0200 Subject: [PATCH] refactor: App widget --- lib/main.dart | 133 ++--------------------------- lib/pages/chat_list/chat_list.dart | 2 +- lib/widgets/fluffy_chat_app.dart | 107 +++++++++++++++++++++++ lib/widgets/matrix.dart | 12 +++ 4 files changed, 125 insertions(+), 129 deletions(-) create mode 100644 lib/widgets/fluffy_chat_app.dart diff --git a/lib/main.dart b/lib/main.dart index db82b626..3d3b3f7f 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -2,28 +2,15 @@ import 'dart:async'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; -import 'package:flutter/services.dart'; -import 'package:adaptive_theme/adaptive_theme.dart'; -import 'package:dynamic_color/dynamic_color.dart'; import 'package:flutter_app_lock/flutter_app_lock.dart'; -import 'package:flutter_gen/gen_l10n/l10n.dart'; -import 'package:future_loading_dialog/future_loading_dialog.dart'; -import 'package:matrix/matrix.dart'; import 'package:universal_html/html.dart' as html; -import 'package:vrouter/vrouter.dart'; -import 'package:fluffychat/config/routes.dart'; import 'package:fluffychat/utils/client_manager.dart'; import 'package:fluffychat/utils/platform_infos.dart'; -import 'config/app_config.dart'; -import 'config/themes.dart'; import 'utils/background_push.dart'; -import 'utils/custom_scroll_behaviour.dart'; -import 'utils/localized_exception_extension.dart'; -import 'utils/space_navigator.dart'; +import 'widgets/fluffy_chat_app.dart'; import 'widgets/lock_screen.dart'; -import 'widgets/matrix.dart'; void main() async { // Our background push shared isolate accesses flutter-internal things very early in the startup proccess @@ -58,119 +45,9 @@ void main() async { lockScreen: const LockScreen(), enabled: false, ) - : FluffyChatApp(clients: clients, queryParameters: queryParameters), + : FluffyChatApp( + clients: clients, + queryParameters: queryParameters, + ), ); } - -class FluffyChatApp extends StatefulWidget { - final Widget? testWidget; - final List clients; - final Map? queryParameters; - - const FluffyChatApp({ - Key? key, - this.testWidget, - required this.clients, - this.queryParameters, - }) : super(key: key); - - /// 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; - - @override - FluffyChatAppState createState() => FluffyChatAppState(); -} - -class FluffyChatAppState extends State { - GlobalKey? _router; - bool? columnMode; - String? _initialUrl; - - @override - void initState() { - super.initState(); - _initialUrl = - widget.clients.any((client) => client.isLogged()) ? '/rooms' : '/home'; - } - - @override - Widget build(BuildContext context) { - return DynamicColorBuilder( - builder: (lightColorScheme, darkColorScheme) => AdaptiveTheme( - light: FluffyThemes.buildTheme( - Brightness.light, - lightColorScheme, - ), - dark: FluffyThemes.buildTheme( - Brightness.dark, - lightColorScheme, - ), - initial: AdaptiveThemeMode.system, - builder: (theme, darkTheme) => LayoutBuilder( - builder: (context, constraints) { - const maxColumns = 3; - var newColumns = - (constraints.maxWidth / FluffyThemes.columnWidth).floor(); - if (newColumns > maxColumns) newColumns = maxColumns; - columnMode ??= newColumns > 1; - _router ??= GlobalKey(); - if (columnMode != newColumns > 1) { - Logs().v('Set Column Mode = $columnMode'); - WidgetsBinding.instance.addPostFrameCallback((_) { - setState(() { - _initialUrl = _router?.currentState?.url; - columnMode = newColumns > 1; - _router = GlobalKey(); - }); - }); - } - return VRouter( - key: _router, - title: AppConfig.applicationName, - theme: theme, - scrollBehavior: CustomScrollBehavior(), - logs: kReleaseMode ? VLogs.none : VLogs.info, - darkTheme: darkTheme, - localizationsDelegates: const [ - ...L10n.localizationsDelegates, - ], - navigatorObservers: [ - SpaceNavigator.routeObserver, - ], - supportedLocales: L10n.supportedLocales, - initialUrl: _initialUrl ?? '/', - routes: AppRoutes(columnMode ?? false).routes, - builder: (context, child) { - LoadingDialog.defaultTitle = - L10n.of(context)!.loadingPleaseWait; - LoadingDialog.defaultBackLabel = L10n.of(context)!.close; - LoadingDialog.defaultOnError = - (e) => (e as Object?)!.toLocalizedString(context); - WidgetsBinding.instance.addPostFrameCallback((_) { - SystemChrome.setSystemUIOverlayStyle( - SystemUiOverlayStyle( - statusBarColor: Colors.transparent, - systemNavigationBarColor: - Theme.of(context).appBarTheme.backgroundColor, - systemNavigationBarIconBrightness: - Theme.of(context).brightness == Brightness.light - ? Brightness.dark - : Brightness.light, - ), - ); - }); - return Matrix( - context: context, - router: _router, - clients: widget.clients, - child: child, - ); - }, - ); - }, - ), - )); - } -} diff --git a/lib/pages/chat_list/chat_list.dart b/lib/pages/chat_list/chat_list.dart index b4787e97..27db1d7d 100644 --- a/lib/pages/chat_list/chat_list.dart +++ b/lib/pages/chat_list/chat_list.dart @@ -20,9 +20,9 @@ import 'package:fluffychat/utils/localized_exception_extension.dart'; import 'package:fluffychat/utils/platform_infos.dart'; import 'package:fluffychat/utils/space_navigator.dart'; import '../../../utils/account_bundles.dart'; -import '../../main.dart'; import '../../utils/matrix_sdk_extensions.dart/matrix_file_extension.dart'; import '../../utils/url_launcher.dart'; +import '../../widgets/fluffy_chat_app.dart'; import '../../widgets/matrix.dart'; import '../bootstrap/bootstrap_dialog.dart'; import '../settings_account/settings_account.dart'; diff --git a/lib/widgets/fluffy_chat_app.dart b/lib/widgets/fluffy_chat_app.dart new file mode 100644 index 00000000..e1c6c7be --- /dev/null +++ b/lib/widgets/fluffy_chat_app.dart @@ -0,0 +1,107 @@ +import 'package:flutter/foundation.dart'; +import 'package:flutter/material.dart'; + +import 'package:adaptive_theme/adaptive_theme.dart'; +import 'package:dynamic_color/dynamic_color.dart'; +import 'package:flutter_gen/gen_l10n/l10n.dart'; +import 'package:matrix/matrix.dart'; +import 'package:vrouter/vrouter.dart'; + +import 'package:fluffychat/config/routes.dart'; +import 'package:fluffychat/config/themes.dart'; +import '../config/app_config.dart'; +import '../utils/custom_scroll_behaviour.dart'; +import '../utils/space_navigator.dart'; +import 'matrix.dart'; + +class FluffyChatApp extends StatefulWidget { + final Widget? testWidget; + final List clients; + final Map? queryParameters; + + const FluffyChatApp({ + Key? key, + this.testWidget, + required this.clients, + this.queryParameters, + }) : super(key: key); + + /// 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; + + @override + FluffyChatAppState createState() => FluffyChatAppState(); +} + +class FluffyChatAppState extends State { + GlobalKey? _router; + bool? columnMode; + String? _initialUrl; + + @override + void initState() { + super.initState(); + _initialUrl = + widget.clients.any((client) => client.isLogged()) ? '/rooms' : '/home'; + } + + @override + Widget build(BuildContext context) { + return DynamicColorBuilder( + builder: (lightColorScheme, darkColorScheme) => AdaptiveTheme( + light: FluffyThemes.buildTheme( + Brightness.light, + lightColorScheme, + ), + dark: FluffyThemes.buildTheme( + Brightness.dark, + lightColorScheme, + ), + initial: AdaptiveThemeMode.system, + builder: (theme, darkTheme) => LayoutBuilder( + builder: (context, constraints) { + const maxColumns = 3; + var newColumns = + (constraints.maxWidth / FluffyThemes.columnWidth).floor(); + if (newColumns > maxColumns) newColumns = maxColumns; + columnMode ??= newColumns > 1; + _router ??= GlobalKey(); + if (columnMode != newColumns > 1) { + Logs().v('Set Column Mode = $columnMode'); + WidgetsBinding.instance.addPostFrameCallback((_) { + setState(() { + _initialUrl = _router?.currentState?.url; + columnMode = newColumns > 1; + _router = GlobalKey(); + }); + }); + } + return VRouter( + key: _router, + title: AppConfig.applicationName, + theme: theme, + scrollBehavior: CustomScrollBehavior(), + logs: kReleaseMode ? VLogs.none : VLogs.info, + darkTheme: darkTheme, + localizationsDelegates: L10n.localizationsDelegates, + navigatorObservers: [ + SpaceNavigator.routeObserver, + ], + supportedLocales: L10n.supportedLocales, + initialUrl: _initialUrl ?? '/', + routes: AppRoutes(columnMode ?? false).routes, + builder: (context, child) => Matrix( + context: context, + router: _router, + clients: widget.clients, + child: child, + ), + ); + }, + ), + ), + ); + } +} diff --git a/lib/widgets/matrix.dart b/lib/widgets/matrix.dart index 2a740bee..583a79bf 100644 --- a/lib/widgets/matrix.dart +++ b/lib/widgets/matrix.dart @@ -11,6 +11,7 @@ import 'package:desktop_notifications/desktop_notifications.dart'; import 'package:flutter_app_lock/flutter_app_lock.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart'; import 'package:flutter_secure_storage/flutter_secure_storage.dart'; +import 'package:future_loading_dialog/future_loading_dialog.dart'; import 'package:http/http.dart' as http; import 'package:image_picker/image_picker.dart'; import 'package:matrix/encryption.dart'; @@ -23,6 +24,7 @@ import 'package:vrouter/vrouter.dart'; import 'package:fluffychat/config/themes.dart'; import 'package:fluffychat/utils/client_manager.dart'; +import 'package:fluffychat/utils/localized_exception_extension.dart'; import 'package:fluffychat/utils/platform_infos.dart'; import 'package:fluffychat/utils/uia_request_manager.dart'; import 'package:fluffychat/utils/voip_plugin.dart'; @@ -256,6 +258,16 @@ class MatrixState extends State with WidgetsBindingObserver { } else { initSettings(); } + initLoadingDialog(); + } + + void initLoadingDialog() { + WidgetsBinding.instance.addPostFrameCallback((_) { + LoadingDialog.defaultTitle = L10n.of(context)!.loadingPleaseWait; + LoadingDialog.defaultBackLabel = L10n.of(context)!.close; + LoadingDialog.defaultOnError = + (e) => (e as Object?)!.toLocalizedString(context); + }); } Future initConfig() async {