Merge branch 'krille/refactor-fluffyclient' into 'main'

refactor: Remove FluffyClient and make it no longer static

See merge request famedly/fluffychat!427
This commit is contained in:
Krille Fear 2021-06-24 10:40:30 +00:00
commit 6108c1bb15
8 changed files with 71 additions and 67 deletions

View File

@ -2,6 +2,8 @@
import 'dart:async'; import 'dart:async';
import 'package:adaptive_theme/adaptive_theme.dart'; 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:matrix/matrix.dart';
import 'package:fluffychat/config/routes.dart'; import 'package:fluffychat/config/routes.dart';
import 'package:fluffychat/utils/platform_infos.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/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'utils/famedlysdk_store.dart';
import 'utils/localized_exception_extension.dart'; import 'utils/localized_exception_extension.dart';
import 'package:flutter_app_lock/flutter_app_lock.dart'; import 'package:flutter_app_lock/flutter_app_lock.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart';
@ -22,7 +25,6 @@ import 'widgets/lock_screen.dart';
import 'widgets/matrix.dart'; import 'widgets/matrix.dart';
import 'config/themes.dart'; import 'config/themes.dart';
import 'config/app_config.dart'; import 'config/app_config.dart';
import 'utils/matrix_sdk_extensions.dart/fluffy_client.dart';
import 'utils/platform_infos.dart'; import 'utils/platform_infos.dart';
import 'utils/background_push.dart'; import 'utils/background_push.dart';
@ -34,27 +36,46 @@ void main() async {
FlutterError.onError = (FlutterErrorDetails details) => FlutterError.onError = (FlutterErrorDetails details) =>
Zone.current.handleUncaughtError(details.exception, details.stack); 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: <String>{
'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) { if (PlatformInfos.isMobile) {
BackgroundPush.clientOnly(FluffyClient()); BackgroundPush.clientOnly(client);
} }
runZonedGuarded( runZonedGuarded(
() => runApp(PlatformInfos.isMobile () => runApp(PlatformInfos.isMobile
? AppLock( ? AppLock(
builder: (args) => FluffyChatApp(), builder: (args) => FluffyChatApp(client: client),
lockScreen: LockScreen(), lockScreen: LockScreen(),
enabled: false, enabled: false,
) )
: FluffyChatApp()), : FluffyChatApp(client: client)),
SentryController.captureException, SentryController.captureException,
); );
} }
class FluffyChatApp extends StatefulWidget { class FluffyChatApp extends StatefulWidget {
final Widget testWidget; 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); : super(key: key);
/// getInitialLink may rereturn the value multiple times if this view is /// getInitialLink may rereturn the value multiple times if this view is
@ -127,7 +148,7 @@ class _FluffyChatAppState extends State<FluffyChatApp> {
key: _matrix, key: _matrix,
context: context, context: context,
router: _router, router: _router,
testClient: widget.testClient, client: widget.client,
child: WaitForInitPage(child), child: WaitForInitPage(child),
); );
}, },

View File

@ -37,7 +37,6 @@ import 'platform_infos.dart';
import '../config/app_config.dart'; import '../config/app_config.dart';
import '../config/setting_keys.dart'; import '../config/setting_keys.dart';
import 'famedlysdk_store.dart'; import 'famedlysdk_store.dart';
import 'matrix_sdk_extensions.dart/fluffy_client.dart';
import 'matrix_sdk_extensions.dart/matrix_locals.dart'; import 'matrix_sdk_extensions.dart/matrix_locals.dart';
class NoTokenException implements Exception { class NoTokenException implements Exception {
@ -48,7 +47,7 @@ class BackgroundPush {
static BackgroundPush _instance; static BackgroundPush _instance;
final FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin = final FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin(); FlutterLocalNotificationsPlugin();
FluffyClient client; Client client;
BuildContext context; BuildContext context;
GlobalKey<VRouterState> router; GlobalKey<VRouterState> router;
String _fcmToken; String _fcmToken;
@ -89,13 +88,13 @@ class BackgroundPush {
} }
} }
factory BackgroundPush.clientOnly(FluffyClient client) { factory BackgroundPush.clientOnly(Client client) {
_instance ??= BackgroundPush._(client); _instance ??= BackgroundPush._(client);
return _instance; return _instance;
} }
factory BackgroundPush(FluffyClient _client, BuildContext _context, factory BackgroundPush(
GlobalKey<VRouterState> router, Client _client, BuildContext _context, GlobalKey<VRouterState> router,
{final void Function(String errorMsg) onFcmError}) { {final void Function(String errorMsg) onFcmError}) {
final instance = BackgroundPush.clientOnly(_client); final instance = BackgroundPush.clientOnly(_client);
instance.context = _context; instance.context = _context;

View File

@ -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: <String>{
'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
},
);
}

View File

@ -1,5 +1,4 @@
import 'package:matrix/matrix.dart'; 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/utils/platform_infos.dart';
import 'package:fluffychat/widgets/avatar.dart'; import 'package:fluffychat/widgets/avatar.dart';
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
@ -168,7 +167,7 @@ class _ReactionEntry {
} }
class _AdaptableReactorsDialog extends StatelessWidget { class _AdaptableReactorsDialog extends StatelessWidget {
final FluffyClient client; final Client client;
final _ReactionEntry reactionEntry; final _ReactionEntry reactionEntry;
const _AdaptableReactorsDialog({ const _AdaptableReactorsDialog({

View File

@ -23,7 +23,6 @@ import '../pages/key_verification_dialog.dart';
import '../utils/platform_infos.dart'; import '../utils/platform_infos.dart';
import '../config/app_config.dart'; import '../config/app_config.dart';
import '../config/setting_keys.dart'; import '../config/setting_keys.dart';
import '../utils/matrix_sdk_extensions.dart/fluffy_client.dart';
import '../utils/background_push.dart'; import '../utils/background_push.dart';
import 'package:vrouter/vrouter.dart'; import 'package:vrouter/vrouter.dart';
@ -36,13 +35,13 @@ class Matrix extends StatefulWidget {
final BuildContext context; final BuildContext context;
final Client testClient; final Client client;
Matrix({ Matrix({
this.child, this.child,
@required this.router, @required this.router,
@required this.context, @required this.context,
this.testClient, @required this.client,
Key key, Key key,
}) : super(key: key); }) : super(key: key);
@ -55,14 +54,12 @@ class Matrix extends StatefulWidget {
} }
class MatrixState extends State<Matrix> with WidgetsBindingObserver { class MatrixState extends State<Matrix> with WidgetsBindingObserver {
FluffyClient client; Client get client => widget.client;
Store store = Store(); Store store = Store();
BuildContext navigatorContext; BuildContext navigatorContext;
BackgroundPush _backgroundPush; BackgroundPush _backgroundPush;
bool get testMode => widget.testClient != null;
Map<String, dynamic> get shareContent => _shareContent; Map<String, dynamic> get shareContent => _shareContent;
set shareContent(Map<String, dynamic> content) { set shareContent(Map<String, dynamic> content) {
_shareContent = content; _shareContent = content;
@ -78,7 +75,7 @@ class MatrixState extends State<Matrix> with WidgetsBindingObserver {
void _initWithStore() async { void _initWithStore() async {
try { try {
if (!testMode) await client.init(); await client.init();
if (client.isLogged()) { if (client.isLogged()) {
final statusMsg = await store.getItem(SettingKeys.ownStatusMessage); final statusMsg = await store.getItem(SettingKeys.ownStatusMessage);
if (statusMsg?.isNotEmpty ?? false) { if (statusMsg?.isNotEmpty ?? false) {
@ -285,7 +282,6 @@ class MatrixState extends State<Matrix> with WidgetsBindingObserver {
}); });
}); });
} }
client = FluffyClient();
onKeyVerificationRequestSub ??= client.onKeyVerificationRequest.stream onKeyVerificationRequestSub ??= client.onKeyVerificationRequest.stream
.listen((KeyVerification request) async { .listen((KeyVerification request) async {
var hidPopup = false; var hidPopup = false;

View File

@ -2,8 +2,15 @@ import 'package:fluffychat/pages/homeserver_picker.dart';
import 'package:fluffychat/main.dart'; import 'package:fluffychat/main.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'utils/test_client.dart';
void main() { void main() {
testWidgets('Test if the widget can be created', (WidgetTester tester) async { 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(),
),
);
}); });
} }

View File

@ -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:matrix/matrix.dart';
import 'package:fluffychat/utils/matrix_sdk_extensions.dart/fluffy_client.dart'; import 'package:matrix_api_lite/fake_matrix_api.dart';
Future<FluffyClient> testClient({ Future<Client> prepareTestClient({
bool loggedIn = false, bool loggedIn = false,
String homeserver = 'https://fakeserver.notexisting', String homeserver = 'https://fakeserver.notexisting',
String id = 'FluffyChat Widget Test', String id = 'FluffyChat Widget Test',
}) async { }) async {
final client = FluffyClient(testMode: true); final client = Client(
'FluffyChat Widget Tests',
httpClient: FakeMatrixApi(),
enableE2eeRecovery: true,
verificationMethods: {
KeyVerificationMethod.numbers,
KeyVerificationMethod.emoji,
},
importantStateEvents: <String>{
'im.ponies.room_emotes', // we want emotes to work properly
},
databaseBuilder: FlutterFamedlySdkHiveDatabase.hiveDatabaseBuilder,
supportedLoginTypes: {
AuthenticationTypes.password,
AuthenticationTypes.sso
},
);
if (homeserver != null) { if (homeserver != null) {
await client.checkHomeserver(homeserver); await client.checkHomeserver(homeserver);
} }

View File

@ -8,8 +8,12 @@
import 'package:fluffychat/main.dart'; import 'package:fluffychat/main.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'utils/test_client.dart';
void main() { void main() {
testWidgets('Test if the app starts', (WidgetTester tester) async { testWidgets('Test if the app starts', (WidgetTester tester) async {
await tester.pumpWidget(FluffyChatApp()); await tester.pumpWidget(FluffyChatApp(
client: await prepareTestClient(),
));
}); });
} }