From a39790cdcaf84b99c95eba87bf758fd7ea3647c3 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Wed, 23 Jun 2021 15:35:23 +0200 Subject: [PATCH] refactor: Remove FluffyClient and make it no longer static --- lib/main.dart | 35 ++++++++++++---- lib/utils/background_push.dart | 9 ++--- .../fluffy_client.dart | 40 ------------------- .../event_content/message_reactions.dart | 3 +- lib/widgets/matrix.dart | 12 ++---- test/homeserver_picker_test.dart | 9 ++++- test/utils/test_client.dart | 24 +++++++++-- test/widget_test.dart | 6 ++- 8 files changed, 71 insertions(+), 67 deletions(-) delete mode 100644 lib/utils/matrix_sdk_extensions.dart/fluffy_client.dart diff --git a/lib/main.dart b/lib/main.dart index a5f01ca0..b894d46f 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -2,6 +2,8 @@ import 'dart:async'; import 'package:adaptive_theme/adaptive_theme.dart'; +import 'package:fluffychat/utils/database/flutter_famedly_sdk_hive_database.dart'; +import 'package:matrix/encryption/utils/key_verification.dart'; import 'package:matrix/matrix.dart'; import 'package:fluffychat/config/routes.dart'; import 'package:fluffychat/utils/platform_infos.dart'; @@ -10,6 +12,7 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; +import 'utils/famedlysdk_store.dart'; import 'utils/localized_exception_extension.dart'; import 'package:flutter_app_lock/flutter_app_lock.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart'; @@ -22,7 +25,6 @@ import 'widgets/lock_screen.dart'; import 'widgets/matrix.dart'; import 'config/themes.dart'; import 'config/app_config.dart'; -import 'utils/matrix_sdk_extensions.dart/fluffy_client.dart'; import 'utils/platform_infos.dart'; import 'utils/background_push.dart'; @@ -34,27 +36,46 @@ void main() async { FlutterError.onError = (FlutterErrorDetails details) => Zone.current.handleUncaughtError(details.exception, details.stack); + final client = Client( + PlatformInfos.clientName, + enableE2eeRecovery: true, + verificationMethods: { + KeyVerificationMethod.numbers, + if (PlatformInfos.isMobile || PlatformInfos.isLinux) + KeyVerificationMethod.emoji, + }, + importantStateEvents: { + 'im.ponies.room_emotes', // we want emotes to work properly + }, + databaseBuilder: FlutterFamedlySdkHiveDatabase.hiveDatabaseBuilder, + legacyDatabaseBuilder: getDatabase, + supportedLoginTypes: { + AuthenticationTypes.password, + if (PlatformInfos.isMobile || PlatformInfos.isWeb) AuthenticationTypes.sso + }, + ); + if (PlatformInfos.isMobile) { - BackgroundPush.clientOnly(FluffyClient()); + BackgroundPush.clientOnly(client); } runZonedGuarded( () => runApp(PlatformInfos.isMobile ? AppLock( - builder: (args) => FluffyChatApp(), + builder: (args) => FluffyChatApp(client: client), lockScreen: LockScreen(), enabled: false, ) - : FluffyChatApp()), + : FluffyChatApp(client: client)), SentryController.captureException, ); } class FluffyChatApp extends StatefulWidget { final Widget testWidget; - final Client testClient; + final Client client; - const FluffyChatApp({Key key, this.testWidget, this.testClient}) + const FluffyChatApp({Key key, this.testWidget, @required this.client}) : super(key: key); /// getInitialLink may rereturn the value multiple times if this view is @@ -127,7 +148,7 @@ class _FluffyChatAppState extends State { key: _matrix, context: context, router: _router, - testClient: widget.testClient, + client: widget.client, child: WaitForInitPage(child), ); }, diff --git a/lib/utils/background_push.dart b/lib/utils/background_push.dart index 3f2c12c3..2a366ea4 100644 --- a/lib/utils/background_push.dart +++ b/lib/utils/background_push.dart @@ -37,7 +37,6 @@ import 'platform_infos.dart'; import '../config/app_config.dart'; import '../config/setting_keys.dart'; import 'famedlysdk_store.dart'; -import 'matrix_sdk_extensions.dart/fluffy_client.dart'; import 'matrix_sdk_extensions.dart/matrix_locals.dart'; class NoTokenException implements Exception { @@ -48,7 +47,7 @@ class BackgroundPush { static BackgroundPush _instance; final FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin(); - FluffyClient client; + Client client; BuildContext context; GlobalKey router; String _fcmToken; @@ -89,13 +88,13 @@ class BackgroundPush { } } - factory BackgroundPush.clientOnly(FluffyClient client) { + factory BackgroundPush.clientOnly(Client client) { _instance ??= BackgroundPush._(client); return _instance; } - factory BackgroundPush(FluffyClient _client, BuildContext _context, - GlobalKey router, + factory BackgroundPush( + Client _client, BuildContext _context, GlobalKey router, {final void Function(String errorMsg) onFcmError}) { final instance = BackgroundPush.clientOnly(_client); instance.context = _context; diff --git a/lib/utils/matrix_sdk_extensions.dart/fluffy_client.dart b/lib/utils/matrix_sdk_extensions.dart/fluffy_client.dart deleted file mode 100644 index 2a2991c8..00000000 --- a/lib/utils/matrix_sdk_extensions.dart/fluffy_client.dart +++ /dev/null @@ -1,40 +0,0 @@ -import 'package:matrix/matrix.dart'; -import 'package:matrix/encryption.dart'; -import 'package:fluffychat/utils/database/flutter_famedly_sdk_hive_database.dart'; -import 'package:matrix_api_lite/fake_matrix_api.dart'; -import '../platform_infos.dart'; -import '../famedlysdk_store.dart'; - -class FluffyClient extends Client { - static FluffyClient _instance; - - factory FluffyClient({testMode = false}) { - Logs().level = Level.verbose; - _instance ??= FluffyClient._internal(testMode: testMode); - return _instance; - } - - FluffyClient._internal({testMode = false}) - : super( - testMode ? 'FluffyChat Widget Tests' : PlatformInfos.clientName, - httpClient: testMode ? FakeMatrixApi() : null, - enableE2eeRecovery: true, - verificationMethods: { - KeyVerificationMethod.numbers, - if (PlatformInfos.isMobile || PlatformInfos.isLinux) - KeyVerificationMethod.emoji, - }, - importantStateEvents: { - 'im.ponies.room_emotes', // we want emotes to work properly - }, - databaseBuilder: testMode - ? null - : FlutterFamedlySdkHiveDatabase.hiveDatabaseBuilder, - legacyDatabaseBuilder: testMode ? null : getDatabase, - supportedLoginTypes: { - AuthenticationTypes.password, - if (PlatformInfos.isMobile || PlatformInfos.isWeb) - AuthenticationTypes.sso - }, - ); -} diff --git a/lib/widgets/event_content/message_reactions.dart b/lib/widgets/event_content/message_reactions.dart index 8ce67639..28dfd59e 100644 --- a/lib/widgets/event_content/message_reactions.dart +++ b/lib/widgets/event_content/message_reactions.dart @@ -1,5 +1,4 @@ import 'package:matrix/matrix.dart'; -import 'package:fluffychat/utils/matrix_sdk_extensions.dart/fluffy_client.dart'; import 'package:fluffychat/utils/platform_infos.dart'; import 'package:fluffychat/widgets/avatar.dart'; import 'package:flutter/cupertino.dart'; @@ -168,7 +167,7 @@ class _ReactionEntry { } class _AdaptableReactorsDialog extends StatelessWidget { - final FluffyClient client; + final Client client; final _ReactionEntry reactionEntry; const _AdaptableReactorsDialog({ diff --git a/lib/widgets/matrix.dart b/lib/widgets/matrix.dart index 802083a7..41226279 100644 --- a/lib/widgets/matrix.dart +++ b/lib/widgets/matrix.dart @@ -23,7 +23,6 @@ import '../pages/key_verification_dialog.dart'; import '../utils/platform_infos.dart'; import '../config/app_config.dart'; import '../config/setting_keys.dart'; -import '../utils/matrix_sdk_extensions.dart/fluffy_client.dart'; import '../utils/background_push.dart'; import 'package:vrouter/vrouter.dart'; @@ -36,13 +35,13 @@ class Matrix extends StatefulWidget { final BuildContext context; - final Client testClient; + final Client client; Matrix({ this.child, @required this.router, @required this.context, - this.testClient, + @required this.client, Key key, }) : super(key: key); @@ -55,14 +54,12 @@ class Matrix extends StatefulWidget { } class MatrixState extends State with WidgetsBindingObserver { - FluffyClient client; + Client get client => widget.client; Store store = Store(); BuildContext navigatorContext; BackgroundPush _backgroundPush; - bool get testMode => widget.testClient != null; - Map get shareContent => _shareContent; set shareContent(Map content) { _shareContent = content; @@ -78,7 +75,7 @@ class MatrixState extends State with WidgetsBindingObserver { void _initWithStore() async { try { - if (!testMode) await client.init(); + await client.init(); if (client.isLogged()) { final statusMsg = await store.getItem(SettingKeys.ownStatusMessage); if (statusMsg?.isNotEmpty ?? false) { @@ -285,7 +282,6 @@ class MatrixState extends State with WidgetsBindingObserver { }); }); } - client = FluffyClient(); onKeyVerificationRequestSub ??= client.onKeyVerificationRequest.stream .listen((KeyVerification request) async { var hidPopup = false; diff --git a/test/homeserver_picker_test.dart b/test/homeserver_picker_test.dart index f3d9b2e6..d8d2a939 100644 --- a/test/homeserver_picker_test.dart +++ b/test/homeserver_picker_test.dart @@ -2,8 +2,15 @@ import 'package:fluffychat/pages/homeserver_picker.dart'; import 'package:fluffychat/main.dart'; import 'package:flutter_test/flutter_test.dart'; +import 'utils/test_client.dart'; + void main() { testWidgets('Test if the widget can be created', (WidgetTester tester) async { - await tester.pumpWidget(FluffyChatApp(testWidget: HomeserverPicker())); + await tester.pumpWidget( + FluffyChatApp( + client: await prepareTestClient(), + testWidget: HomeserverPicker(), + ), + ); }); } diff --git a/test/utils/test_client.dart b/test/utils/test_client.dart index 1e8ceb01..2eaf7939 100644 --- a/test/utils/test_client.dart +++ b/test/utils/test_client.dart @@ -1,12 +1,30 @@ +import 'package:fluffychat/utils/database/flutter_famedly_sdk_hive_database.dart'; +import 'package:matrix/encryption/utils/key_verification.dart'; import 'package:matrix/matrix.dart'; -import 'package:fluffychat/utils/matrix_sdk_extensions.dart/fluffy_client.dart'; +import 'package:matrix_api_lite/fake_matrix_api.dart'; -Future testClient({ +Future prepareTestClient({ bool loggedIn = false, String homeserver = 'https://fakeserver.notexisting', String id = 'FluffyChat Widget Test', }) async { - final client = FluffyClient(testMode: true); + final client = Client( + 'FluffyChat Widget Tests', + httpClient: FakeMatrixApi(), + enableE2eeRecovery: true, + verificationMethods: { + KeyVerificationMethod.numbers, + KeyVerificationMethod.emoji, + }, + importantStateEvents: { + 'im.ponies.room_emotes', // we want emotes to work properly + }, + databaseBuilder: FlutterFamedlySdkHiveDatabase.hiveDatabaseBuilder, + supportedLoginTypes: { + AuthenticationTypes.password, + AuthenticationTypes.sso + }, + ); if (homeserver != null) { await client.checkHomeserver(homeserver); } diff --git a/test/widget_test.dart b/test/widget_test.dart index ec8eddae..d6d14e4c 100644 --- a/test/widget_test.dart +++ b/test/widget_test.dart @@ -8,8 +8,12 @@ import 'package:fluffychat/main.dart'; import 'package:flutter_test/flutter_test.dart'; +import 'utils/test_client.dart'; + void main() { testWidgets('Test if the app starts', (WidgetTester tester) async { - await tester.pumpWidget(FluffyChatApp()); + await tester.pumpWidget(FluffyChatApp( + client: await prepareTestClient(), + )); }); }