2020-01-03 17:23:40 +01:00
|
|
|
import 'dart:async';
|
2020-12-18 11:43:13 +01:00
|
|
|
import 'dart:convert';
|
2021-10-26 18:50:34 +02:00
|
|
|
import 'dart:io';
|
2020-02-16 15:57:50 +01:00
|
|
|
|
2020-01-01 19:10:13 +01:00
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
2021-10-26 18:50:34 +02:00
|
|
|
|
2021-11-13 17:57:55 +01:00
|
|
|
import 'package:adaptive_theme/adaptive_theme.dart';
|
2022-01-29 12:35:03 +01:00
|
|
|
import 'package:collection/collection.dart';
|
2021-10-26 18:50:34 +02:00
|
|
|
import 'package:desktop_notifications/desktop_notifications.dart';
|
2021-01-18 17:31:27 +01:00
|
|
|
import 'package:flutter_app_lock/flutter_app_lock.dart';
|
2020-10-03 13:11:07 +02:00
|
|
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
2021-01-18 17:31:27 +01:00
|
|
|
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
2021-10-26 18:50:34 +02:00
|
|
|
import 'package:http/http.dart' as http;
|
2022-04-15 11:42:59 +02:00
|
|
|
import 'package:image_picker/image_picker.dart';
|
2021-10-26 18:50:34 +02:00
|
|
|
import 'package:matrix/encryption.dart';
|
|
|
|
import 'package:matrix/matrix.dart';
|
2021-01-15 19:59:30 +01:00
|
|
|
import 'package:provider/provider.dart';
|
2022-01-13 13:44:48 +01:00
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
2021-04-21 14:19:54 +02:00
|
|
|
import 'package:universal_html/html.dart' as html;
|
2021-02-01 11:47:33 +01:00
|
|
|
import 'package:url_launcher/url_launcher.dart';
|
2021-10-26 18:50:34 +02:00
|
|
|
import 'package:vrouter/vrouter.dart';
|
|
|
|
|
2021-11-13 17:57:55 +01:00
|
|
|
import 'package:fluffychat/config/themes.dart';
|
2021-10-26 18:50:34 +02:00
|
|
|
import 'package:fluffychat/utils/client_manager.dart';
|
|
|
|
import 'package:fluffychat/utils/platform_infos.dart';
|
|
|
|
import 'package:fluffychat/utils/sentry_controller.dart';
|
|
|
|
import 'package:fluffychat/utils/uia_request_manager.dart';
|
2022-02-15 09:25:13 +01:00
|
|
|
import 'package:fluffychat/utils/voip_plugin.dart';
|
2021-05-22 08:53:52 +02:00
|
|
|
import '../config/app_config.dart';
|
|
|
|
import '../config/setting_keys.dart';
|
2021-11-09 21:32:16 +01:00
|
|
|
import '../pages/key_verification/key_verification_dialog.dart';
|
2021-09-19 13:48:23 +02:00
|
|
|
import '../utils/account_bundles.dart';
|
2021-05-22 08:53:52 +02:00
|
|
|
import '../utils/background_push.dart';
|
2021-10-26 18:50:34 +02:00
|
|
|
import '../utils/famedlysdk_store.dart';
|
|
|
|
import '../utils/platform_infos.dart';
|
2022-01-22 11:16:40 +01:00
|
|
|
import 'local_notifications_extension.dart';
|
2020-02-16 15:57:50 +01:00
|
|
|
|
2022-01-13 13:44:48 +01:00
|
|
|
// import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
|
|
|
|
2020-01-01 19:10:13 +01:00
|
|
|
class Matrix extends StatefulWidget {
|
2022-01-29 12:35:03 +01:00
|
|
|
final Widget? child;
|
2020-01-01 19:10:13 +01:00
|
|
|
|
2022-01-29 12:35:03 +01:00
|
|
|
final GlobalKey<VRouterState>? router;
|
2021-01-16 12:46:38 +01:00
|
|
|
|
|
|
|
final BuildContext context;
|
|
|
|
|
2021-09-19 13:48:23 +02:00
|
|
|
final List<Client> clients;
|
2021-04-12 17:31:53 +02:00
|
|
|
|
2022-01-29 12:35:03 +01:00
|
|
|
final Map<String, String>? queryParameters;
|
2021-07-08 18:42:46 +02:00
|
|
|
|
2021-10-14 18:09:30 +02:00
|
|
|
const Matrix({
|
2021-01-16 12:46:38 +01:00
|
|
|
this.child,
|
2022-01-29 12:35:03 +01:00
|
|
|
required this.router,
|
|
|
|
required this.context,
|
|
|
|
required this.clients,
|
2021-07-08 18:42:46 +02:00
|
|
|
this.queryParameters,
|
2022-01-29 12:35:03 +01:00
|
|
|
Key? key,
|
2021-01-16 12:46:38 +01:00
|
|
|
}) : super(key: key);
|
2020-01-01 19:10:13 +01:00
|
|
|
|
|
|
|
@override
|
|
|
|
MatrixState createState() => MatrixState();
|
|
|
|
|
|
|
|
/// Returns the (nearest) Client instance of your application.
|
2021-01-15 19:59:30 +01:00
|
|
|
static MatrixState of(BuildContext context) =>
|
|
|
|
Provider.of<MatrixState>(context, listen: false);
|
2020-01-01 19:10:13 +01:00
|
|
|
}
|
|
|
|
|
2021-02-07 17:18:38 +01:00
|
|
|
class MatrixState extends State<Matrix> with WidgetsBindingObserver {
|
2021-11-24 18:39:40 +01:00
|
|
|
int _activeClient = -1;
|
2022-01-29 12:35:03 +01:00
|
|
|
String? activeBundle;
|
2020-12-11 14:14:33 +01:00
|
|
|
Store store = Store();
|
2022-01-29 12:35:03 +01:00
|
|
|
late BuildContext navigatorContext;
|
2020-01-01 19:10:13 +01:00
|
|
|
|
2022-04-15 11:42:59 +02:00
|
|
|
HomeserverSummary? loginHomeserverSummary;
|
|
|
|
XFile? loginAvatar;
|
|
|
|
String? loginUsername;
|
|
|
|
bool? loginRegistrationSupported;
|
|
|
|
|
2022-01-29 12:35:03 +01:00
|
|
|
BackgroundPush? _backgroundPush;
|
2021-02-07 17:18:38 +01:00
|
|
|
|
2021-11-24 18:39:40 +01:00
|
|
|
Client get client {
|
|
|
|
if (widget.clients.isEmpty) {
|
|
|
|
widget.clients.add(getLoginClient());
|
|
|
|
}
|
|
|
|
if (_activeClient < 0 || _activeClient >= widget.clients.length) {
|
2022-01-29 12:35:03 +01:00
|
|
|
return currentBundle!.first!;
|
2021-11-24 18:39:40 +01:00
|
|
|
}
|
|
|
|
return widget.clients[_activeClient];
|
|
|
|
}
|
2021-09-19 13:48:23 +02:00
|
|
|
|
2022-02-19 09:27:03 +01:00
|
|
|
bool get webrtcIsSupported =>
|
|
|
|
kIsWeb ||
|
|
|
|
PlatformInfos.isMobile ||
|
|
|
|
PlatformInfos.isWindows ||
|
|
|
|
PlatformInfos.isMacOS;
|
2022-02-17 19:46:02 +01:00
|
|
|
|
2022-02-19 09:27:03 +01:00
|
|
|
VoipPlugin? voipPlugin;
|
2022-02-15 09:25:13 +01:00
|
|
|
|
2021-09-19 13:48:23 +02:00
|
|
|
bool get isMultiAccount => widget.clients.length > 1;
|
|
|
|
|
|
|
|
int getClientIndexByMatrixId(String matrixId) =>
|
|
|
|
widget.clients.indexWhere((client) => client.userID == matrixId);
|
|
|
|
|
2022-01-29 12:35:03 +01:00
|
|
|
late String currentClientSecret;
|
|
|
|
RequestTokenResponse? currentThreepidCreds;
|
2021-10-30 14:06:10 +02:00
|
|
|
|
2022-01-29 12:35:03 +01:00
|
|
|
void setActiveClient(Client? cl) {
|
2021-09-19 13:48:23 +02:00
|
|
|
final i = widget.clients.indexWhere((c) => c == cl);
|
2022-01-29 12:35:03 +01:00
|
|
|
if (i != -1) {
|
2021-11-24 18:39:40 +01:00
|
|
|
_activeClient = i;
|
2022-02-19 09:27:03 +01:00
|
|
|
// TODO: Multi-client VoiP support
|
|
|
|
createVoipPlugin();
|
2021-09-19 13:48:23 +02:00
|
|
|
} else {
|
2022-01-29 12:35:03 +01:00
|
|
|
Logs().w('Tried to set an unknown client ${cl!.userID} as active');
|
2021-09-19 13:48:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-29 12:35:03 +01:00
|
|
|
List<Client?>? get currentBundle {
|
2021-09-19 13:48:23 +02:00
|
|
|
if (!hasComplexBundles) {
|
|
|
|
return List.from(widget.clients);
|
|
|
|
}
|
|
|
|
final bundles = accountBundles;
|
|
|
|
if (bundles.containsKey(activeBundle)) {
|
|
|
|
return bundles[activeBundle];
|
|
|
|
}
|
|
|
|
return bundles.values.first;
|
|
|
|
}
|
|
|
|
|
2022-01-29 12:35:03 +01:00
|
|
|
Map<String?, List<Client?>> get accountBundles {
|
|
|
|
final resBundles = <String?, List<_AccountBundleWithClient>>{};
|
2021-09-19 13:48:23 +02:00
|
|
|
for (var i = 0; i < widget.clients.length; i++) {
|
|
|
|
final bundles = widget.clients[i].accountBundles;
|
|
|
|
for (final bundle in bundles) {
|
|
|
|
if (bundle.name == null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
resBundles[bundle.name] ??= [];
|
2022-01-29 12:35:03 +01:00
|
|
|
resBundles[bundle.name]!.add(_AccountBundleWithClient(
|
2021-09-19 13:48:23 +02:00
|
|
|
client: widget.clients[i],
|
|
|
|
bundle: bundle,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (final b in resBundles.values) {
|
2022-01-29 12:35:03 +01:00
|
|
|
b.sort((a, b) => a.bundle!.priority == null
|
2021-09-19 13:48:23 +02:00
|
|
|
? 1
|
2022-01-29 12:35:03 +01:00
|
|
|
: b.bundle!.priority == null
|
2021-09-19 13:48:23 +02:00
|
|
|
? -1
|
2022-01-29 12:35:03 +01:00
|
|
|
: a.bundle!.priority!.compareTo(b.bundle!.priority!));
|
2021-09-19 13:48:23 +02:00
|
|
|
}
|
|
|
|
return resBundles
|
|
|
|
.map((k, v) => MapEntry(k, v.map((vv) => vv.client).toList()));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool get hasComplexBundles => accountBundles.values.any((v) => v.length > 1);
|
|
|
|
|
2022-01-29 12:35:03 +01:00
|
|
|
Client? _loginClientCandidate;
|
2021-09-19 13:48:23 +02:00
|
|
|
|
|
|
|
Client getLoginClient() {
|
2021-10-27 11:14:27 +02:00
|
|
|
if (widget.clients.isNotEmpty && !client.isLogged()) {
|
|
|
|
return client;
|
|
|
|
}
|
2022-01-29 12:35:03 +01:00
|
|
|
final candidate = _loginClientCandidate ??= ClientManager.createClient(
|
2021-10-27 11:14:27 +02:00
|
|
|
'${AppConfig.applicationName}-${DateTime.now().millisecondsSinceEpoch}')
|
2021-09-19 13:48:23 +02:00
|
|
|
..onLoginStateChanged
|
|
|
|
.stream
|
|
|
|
.where((l) => l == LoginState.loggedIn)
|
|
|
|
.first
|
|
|
|
.then((_) {
|
2021-10-27 11:14:27 +02:00
|
|
|
if (!widget.clients.contains(_loginClientCandidate)) {
|
2022-01-29 12:35:03 +01:00
|
|
|
widget.clients.add(_loginClientCandidate!);
|
2021-10-27 11:14:27 +02:00
|
|
|
}
|
2022-01-29 12:35:03 +01:00
|
|
|
ClientManager.addClientNameToStore(_loginClientCandidate!.clientName);
|
|
|
|
_registerSubs(_loginClientCandidate!.clientName);
|
2021-09-19 14:44:09 +02:00
|
|
|
_loginClientCandidate = null;
|
2022-01-29 12:35:03 +01:00
|
|
|
widget.router!.currentState!.to('/rooms');
|
2021-09-19 13:48:23 +02:00
|
|
|
});
|
2022-01-29 12:35:03 +01:00
|
|
|
return candidate;
|
2021-09-19 13:48:23 +02:00
|
|
|
}
|
|
|
|
|
2022-01-29 12:35:03 +01:00
|
|
|
Client? getClientByName(String name) =>
|
|
|
|
widget.clients.firstWhereOrNull((c) => c.clientName == name);
|
2021-09-19 13:48:23 +02:00
|
|
|
|
2022-01-29 12:35:03 +01:00
|
|
|
Map<String, dynamic>? get shareContent => _shareContent;
|
2022-02-19 09:27:03 +01:00
|
|
|
|
2022-01-29 12:35:03 +01:00
|
|
|
set shareContent(Map<String, dynamic>? content) {
|
2020-04-09 09:51:52 +02:00
|
|
|
_shareContent = content;
|
|
|
|
onShareContentChanged.add(_shareContent);
|
|
|
|
}
|
|
|
|
|
2022-01-29 12:35:03 +01:00
|
|
|
Map<String, dynamic>? _shareContent;
|
2020-04-09 09:51:52 +02:00
|
|
|
|
2022-01-29 12:35:03 +01:00
|
|
|
final StreamController<Map<String, dynamic>?> onShareContentChanged =
|
2020-04-09 09:51:52 +02:00
|
|
|
StreamController.broadcast();
|
2020-01-08 14:19:15 +01:00
|
|
|
|
2022-01-29 12:35:03 +01:00
|
|
|
File? wallpaper;
|
2020-01-01 19:10:13 +01:00
|
|
|
|
2021-03-12 09:30:10 +01:00
|
|
|
void _initWithStore() async {
|
2020-10-04 11:52:06 +02:00
|
|
|
try {
|
2021-03-12 09:30:10 +01:00
|
|
|
if (client.isLogged()) {
|
2021-09-19 13:48:23 +02:00
|
|
|
// TODO: Figure out how this works in multi account
|
2021-03-12 09:30:10 +01:00
|
|
|
final statusMsg = await store.getItem(SettingKeys.ownStatusMessage);
|
|
|
|
if (statusMsg?.isNotEmpty ?? false) {
|
|
|
|
Logs().v('Send cached status message: "$statusMsg"');
|
2021-05-20 13:59:55 +02:00
|
|
|
await client.setPresence(
|
2022-01-29 12:35:03 +01:00
|
|
|
client.userID!,
|
2021-03-12 09:30:10 +01:00
|
|
|
PresenceType.online,
|
|
|
|
statusMsg: statusMsg,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2020-10-04 11:52:06 +02:00
|
|
|
} catch (e, s) {
|
2022-07-09 10:18:53 +02:00
|
|
|
client.onLoginStateChanged.addError(e, s);
|
2020-10-28 10:56:24 +01:00
|
|
|
SentryController.captureException(e, s);
|
2020-10-04 12:32:29 +02:00
|
|
|
rethrow;
|
2020-01-08 14:19:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-19 13:48:23 +02:00
|
|
|
final onRoomKeyRequestSub = <String, StreamSubscription>{};
|
|
|
|
final onKeyVerificationRequestSub = <String, StreamSubscription>{};
|
|
|
|
final onNotification = <String, StreamSubscription>{};
|
|
|
|
final onLoginStateChanged = <String, StreamSubscription<LoginState>>{};
|
|
|
|
final onUiaRequest = <String, StreamSubscription<UiaRequest>>{};
|
2022-01-29 12:35:03 +01:00
|
|
|
StreamSubscription<html.Event>? onFocusSub;
|
|
|
|
StreamSubscription<html.Event>? onBlurSub;
|
2022-05-14 09:51:21 +02:00
|
|
|
final onOwnPresence = <String, StreamSubscription<CachedPresence>>{};
|
2020-04-08 17:43:07 +02:00
|
|
|
|
2022-01-29 12:35:03 +01:00
|
|
|
String? _cachedPassword;
|
|
|
|
Timer? _cachedPasswordClearTimer;
|
2022-02-19 09:27:03 +01:00
|
|
|
|
2022-01-29 12:35:03 +01:00
|
|
|
String? get cachedPassword => _cachedPassword;
|
2021-10-16 10:33:58 +02:00
|
|
|
|
2022-01-29 12:35:03 +01:00
|
|
|
set cachedPassword(String? p) {
|
2021-10-27 11:14:27 +02:00
|
|
|
Logs().d('Password cached');
|
2021-10-16 10:33:58 +02:00
|
|
|
_cachedPasswordClearTimer?.cancel();
|
|
|
|
_cachedPassword = p;
|
|
|
|
_cachedPasswordClearTimer = Timer(const Duration(minutes: 10), () {
|
|
|
|
_cachedPassword = null;
|
2021-10-27 11:14:27 +02:00
|
|
|
Logs().d('Cached Password cleared');
|
2021-10-16 10:33:58 +02:00
|
|
|
});
|
2020-12-10 15:06:02 +01:00
|
|
|
}
|
|
|
|
|
2020-08-22 15:20:07 +02:00
|
|
|
bool webHasFocus = true;
|
|
|
|
|
2022-01-29 12:35:03 +01:00
|
|
|
String? get activeRoomId =>
|
2021-06-06 10:00:52 +02:00
|
|
|
VRouter.of(navigatorContext).pathParameters['roomid'];
|
|
|
|
|
2021-05-01 15:42:23 +02:00
|
|
|
final linuxNotifications =
|
|
|
|
PlatformInfos.isLinux ? NotificationsClient() : null;
|
2022-01-22 11:16:40 +01:00
|
|
|
final Map<String, int> linuxNotificationIds = {};
|
2020-10-17 09:29:27 +02:00
|
|
|
|
2020-01-01 19:10:13 +01:00
|
|
|
@override
|
|
|
|
void initState() {
|
2020-11-08 20:42:35 +01:00
|
|
|
super.initState();
|
2022-05-12 11:25:34 +02:00
|
|
|
WidgetsBinding.instance.addObserver(this);
|
2020-11-08 20:42:35 +01:00
|
|
|
initMatrix();
|
2021-01-19 16:58:30 +01:00
|
|
|
if (PlatformInfos.isWeb) {
|
|
|
|
initConfig().then((_) => initSettings());
|
|
|
|
} else {
|
|
|
|
initSettings();
|
|
|
|
}
|
2020-12-18 11:43:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> initConfig() async {
|
|
|
|
try {
|
2021-04-14 10:37:15 +02:00
|
|
|
final configJsonString =
|
2021-04-21 14:19:54 +02:00
|
|
|
utf8.decode((await http.get(Uri.parse('config.json'))).bodyBytes);
|
2020-12-18 11:43:13 +01:00
|
|
|
final configJson = json.decode(configJsonString);
|
|
|
|
AppConfig.loadFromJson(configJson);
|
2021-11-23 11:37:25 +01:00
|
|
|
} on FormatException catch (_) {
|
|
|
|
Logs().v('[ConfigLoader] config.json not found');
|
|
|
|
} catch (e) {
|
2021-06-10 10:20:00 +02:00
|
|
|
Logs().v('[ConfigLoader] config.json not found', e);
|
2020-12-18 11:43:13 +01:00
|
|
|
}
|
2020-11-08 20:42:35 +01:00
|
|
|
}
|
|
|
|
|
2021-11-23 11:37:25 +01:00
|
|
|
void _reportSyncError(SyncStatusUpdate update) =>
|
|
|
|
SentryController.captureException(
|
2022-01-29 12:35:03 +01:00
|
|
|
update.error!.exception,
|
|
|
|
update.error!.stackTrace,
|
2021-11-23 11:37:25 +01:00
|
|
|
);
|
|
|
|
|
2021-09-19 13:48:23 +02:00
|
|
|
void _registerSubs(String name) {
|
|
|
|
final c = getClientByName(name);
|
|
|
|
if (c == null) {
|
|
|
|
Logs().w(
|
|
|
|
'Attempted to register subscriptions for non-existing client $name');
|
|
|
|
return;
|
2021-01-18 17:31:27 +01:00
|
|
|
}
|
2021-11-23 11:37:25 +01:00
|
|
|
c.onSyncStatus.stream
|
|
|
|
.where((s) => s.status == SyncStatus.error)
|
|
|
|
.listen(_reportSyncError);
|
2021-11-24 18:39:40 +01:00
|
|
|
onRoomKeyRequestSub[name] ??=
|
|
|
|
c.onRoomKeyRequest.stream.listen((RoomKeyRequest request) async {
|
2022-01-29 12:35:03 +01:00
|
|
|
if (widget.clients.any(((cl) =>
|
2021-11-24 18:39:40 +01:00
|
|
|
cl.userID == request.requestingDevice.userId &&
|
2022-01-29 12:35:03 +01:00
|
|
|
cl.identityKey == request.requestingDevice.curve25519Key))) {
|
2021-11-24 18:39:40 +01:00
|
|
|
Logs().i(
|
|
|
|
'[Key Request] Request is from one of our own clients, forwarding the key...');
|
|
|
|
await request.forwardKey();
|
|
|
|
}
|
|
|
|
});
|
2021-09-19 13:48:23 +02:00
|
|
|
onKeyVerificationRequestSub[name] ??= c.onKeyVerificationRequest.stream
|
2020-11-21 09:22:35 +01:00
|
|
|
.listen((KeyVerification request) async {
|
|
|
|
var hidPopup = false;
|
|
|
|
request.onUpdate = () {
|
|
|
|
if (!hidPopup &&
|
|
|
|
{KeyVerificationState.done, KeyVerificationState.error}
|
|
|
|
.contains(request.state)) {
|
2021-05-23 13:28:55 +02:00
|
|
|
Navigator.of(navigatorContext).pop('dialog');
|
2020-06-25 16:29:06 +02:00
|
|
|
}
|
2020-11-21 09:22:35 +01:00
|
|
|
hidPopup = true;
|
|
|
|
};
|
2021-10-10 12:11:39 +02:00
|
|
|
request.onUpdate = null;
|
|
|
|
hidPopup = true;
|
|
|
|
await KeyVerificationDialog(request: request).show(navigatorContext);
|
2020-11-21 09:22:35 +01:00
|
|
|
});
|
2021-09-19 13:48:23 +02:00
|
|
|
onLoginStateChanged[name] ??= c.onLoginStateChanged.stream.listen((state) {
|
|
|
|
final loggedInWithMultipleClients = widget.clients.length > 1;
|
2022-04-16 14:30:54 +02:00
|
|
|
if (loggedInWithMultipleClients && state != LoginState.loggedIn) {
|
2021-09-19 13:48:23 +02:00
|
|
|
_cancelSubs(c.clientName);
|
|
|
|
widget.clients.remove(c);
|
2021-11-24 18:39:40 +01:00
|
|
|
ClientManager.removeClientNameFromStore(c.clientName);
|
2021-09-19 14:25:18 +02:00
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
SnackBar(
|
2022-01-29 12:35:03 +01:00
|
|
|
content: Text(L10n.of(context)!.oneClientLoggedOut),
|
2021-09-19 14:25:18 +02:00
|
|
|
),
|
2021-09-19 13:48:23 +02:00
|
|
|
);
|
2021-09-19 14:25:18 +02:00
|
|
|
|
2021-09-19 13:48:23 +02:00
|
|
|
if (state != LoginState.loggedIn) {
|
2022-01-29 12:35:03 +01:00
|
|
|
widget.router!.currentState!.to(
|
2021-09-19 13:48:23 +02:00
|
|
|
'/rooms',
|
2022-01-29 12:35:03 +01:00
|
|
|
queryParameters: widget.router!.currentState!.queryParameters,
|
2021-06-18 16:15:11 +02:00
|
|
|
);
|
2021-06-10 10:20:00 +02:00
|
|
|
}
|
2021-09-19 13:48:23 +02:00
|
|
|
} else {
|
2022-01-29 12:35:03 +01:00
|
|
|
widget.router!.currentState!.to(
|
2021-09-19 13:48:23 +02:00
|
|
|
state == LoginState.loggedIn ? '/rooms' : '/home',
|
2022-01-29 12:35:03 +01:00
|
|
|
queryParameters: widget.router!.currentState!.queryParameters,
|
2021-09-19 13:48:23 +02:00
|
|
|
);
|
2021-01-16 12:46:38 +01:00
|
|
|
}
|
|
|
|
});
|
2021-02-27 14:25:55 +01:00
|
|
|
// Cache and resend status message
|
2022-05-14 09:51:21 +02:00
|
|
|
onOwnPresence[name] ??= c.onPresenceChanged.stream.listen((presence) {
|
2021-09-19 13:48:23 +02:00
|
|
|
if (c.isLogged() &&
|
2022-05-14 09:51:21 +02:00
|
|
|
c.userID == presence.userid &&
|
|
|
|
presence.statusMsg != null) {
|
|
|
|
Logs().v('Update status message: "${presence.statusMsg}"');
|
|
|
|
store.setItem(SettingKeys.ownStatusMessage, presence.statusMsg);
|
2021-02-27 14:25:55 +01:00
|
|
|
}
|
|
|
|
});
|
2021-10-26 20:01:53 +02:00
|
|
|
onUiaRequest[name] ??= c.onUiaRequest.stream.listen(uiaRequestHandler);
|
2021-02-07 17:18:38 +01:00
|
|
|
if (PlatformInfos.isWeb || PlatformInfos.isLinux) {
|
2021-09-19 13:48:23 +02:00
|
|
|
c.onSync.stream.first.then((s) {
|
2020-11-08 20:42:35 +01:00
|
|
|
html.Notification.requestPermission();
|
2021-09-19 13:48:23 +02:00
|
|
|
onNotification[name] ??= c.onEvent.stream
|
2020-11-08 20:42:35 +01:00
|
|
|
.where((e) =>
|
|
|
|
e.type == EventUpdateType.timeline &&
|
|
|
|
[EventTypes.Message, EventTypes.Sticker, EventTypes.Encrypted]
|
2021-03-09 19:39:25 +01:00
|
|
|
.contains(e.content['type']) &&
|
2021-09-19 13:48:23 +02:00
|
|
|
e.content['sender'] != c.userID)
|
2022-01-22 11:16:40 +01:00
|
|
|
.listen(showLocalNotification);
|
2020-11-08 20:42:35 +01:00
|
|
|
});
|
|
|
|
}
|
2021-09-19 13:48:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void _cancelSubs(String name) {
|
|
|
|
onRoomKeyRequestSub[name]?.cancel();
|
|
|
|
onRoomKeyRequestSub.remove(name);
|
|
|
|
onKeyVerificationRequestSub[name]?.cancel();
|
|
|
|
onKeyVerificationRequestSub.remove(name);
|
|
|
|
onLoginStateChanged[name]?.cancel();
|
|
|
|
onLoginStateChanged.remove(name);
|
|
|
|
onOwnPresence[name]?.cancel();
|
|
|
|
onOwnPresence.remove(name);
|
|
|
|
onNotification[name]?.cancel();
|
|
|
|
onNotification.remove(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
void initMatrix() {
|
|
|
|
// Display the app lock
|
|
|
|
if (PlatformInfos.isMobile) {
|
2022-05-12 11:25:34 +02:00
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
2022-01-13 13:44:48 +01:00
|
|
|
([TargetPlatform.linux].contains(Theme.of(context).platform)
|
|
|
|
? SharedPreferences.getInstance()
|
|
|
|
.then((prefs) => prefs.getString(SettingKeys.appLockKey))
|
|
|
|
: const FlutterSecureStorage()
|
|
|
|
.read(key: SettingKeys.appLockKey))
|
2021-10-14 18:09:30 +02:00
|
|
|
.then((lock) {
|
2021-09-19 13:48:23 +02:00
|
|
|
if (lock?.isNotEmpty ?? false) {
|
2022-01-29 12:35:03 +01:00
|
|
|
AppLock.of(widget.context)!.enable();
|
|
|
|
AppLock.of(widget.context)!.showLockScreen();
|
2021-09-19 13:48:23 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_initWithStore();
|
|
|
|
|
|
|
|
for (final c in widget.clients) {
|
|
|
|
_registerSubs(c.clientName);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kIsWeb) {
|
|
|
|
onFocusSub = html.window.onFocus.listen((_) => webHasFocus = true);
|
|
|
|
onBlurSub = html.window.onBlur.listen((_) => webHasFocus = false);
|
|
|
|
}
|
2021-02-07 17:18:38 +01:00
|
|
|
|
|
|
|
if (PlatformInfos.isMobile) {
|
2021-05-28 20:32:52 +02:00
|
|
|
_backgroundPush = BackgroundPush(
|
|
|
|
client,
|
|
|
|
context,
|
|
|
|
widget.router,
|
2022-01-29 12:35:03 +01:00
|
|
|
onFcmError: (errorMsg, {Uri? link}) => Timer(
|
2021-10-14 18:09:30 +02:00
|
|
|
const Duration(seconds: 1),
|
2021-09-24 11:42:56 +02:00
|
|
|
() {
|
|
|
|
final banner = SnackBar(
|
|
|
|
content: Text(errorMsg),
|
2021-10-14 18:09:30 +02:00
|
|
|
duration: const Duration(seconds: 30),
|
2021-09-24 11:42:56 +02:00
|
|
|
action: link == null
|
|
|
|
? null
|
|
|
|
: SnackBarAction(
|
2022-01-29 12:35:03 +01:00
|
|
|
label: L10n.of(context)!.link,
|
2021-09-24 11:42:56 +02:00
|
|
|
onPressed: () => launch(link.toString()),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
ScaffoldMessenger.of(navigatorContext).showSnackBar(banner);
|
|
|
|
},
|
|
|
|
),
|
2021-05-28 20:32:52 +02:00
|
|
|
);
|
2021-02-07 17:18:38 +01:00
|
|
|
}
|
2022-02-19 09:27:03 +01:00
|
|
|
|
|
|
|
createVoipPlugin();
|
|
|
|
}
|
|
|
|
|
2022-02-19 11:58:21 +01:00
|
|
|
void createVoipPlugin() async {
|
|
|
|
if (await store.getItemBool(SettingKeys.experimentalVoip) == false) {
|
|
|
|
voipPlugin = null;
|
|
|
|
return;
|
|
|
|
}
|
2022-02-19 09:27:03 +01:00
|
|
|
voipPlugin =
|
|
|
|
webrtcIsSupported ? VoipPlugin(client: client, context: context) : null;
|
2021-02-07 17:18:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool _firstStartup = true;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void didChangeAppLifecycleState(AppLifecycleState state) {
|
|
|
|
Logs().v('AppLifecycleState = $state');
|
|
|
|
final foreground = state != AppLifecycleState.detached &&
|
|
|
|
state != AppLifecycleState.paused;
|
|
|
|
client.backgroundSync = foreground;
|
|
|
|
client.syncPresence = foreground ? null : PresenceType.unavailable;
|
|
|
|
client.requestHistoryOnLimitedTimeline = !foreground;
|
|
|
|
if (_firstStartup) {
|
|
|
|
_firstStartup = false;
|
|
|
|
_backgroundPush?.setupPush();
|
|
|
|
}
|
2020-11-08 20:42:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void initSettings() {
|
2022-01-29 12:35:03 +01:00
|
|
|
store.getItem(SettingKeys.wallpaper).then((final path) async {
|
|
|
|
if (path == null) return;
|
|
|
|
final file = File(path);
|
|
|
|
if (await file.exists()) {
|
|
|
|
wallpaper = file;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
store.getItem(SettingKeys.fontSizeFactor).then((value) =>
|
|
|
|
AppConfig.fontSizeFactor =
|
|
|
|
double.tryParse(value ?? '') ?? AppConfig.fontSizeFactor);
|
|
|
|
store.getItem(SettingKeys.bubbleSizeFactor).then((value) =>
|
|
|
|
AppConfig.bubbleSizeFactor =
|
|
|
|
double.tryParse(value ?? '') ?? AppConfig.bubbleSizeFactor);
|
|
|
|
store
|
|
|
|
.getItemBool(SettingKeys.renderHtml, AppConfig.renderHtml)
|
|
|
|
.then((value) => AppConfig.renderHtml = value);
|
|
|
|
store
|
|
|
|
.getItemBool(
|
|
|
|
SettingKeys.hideRedactedEvents, AppConfig.hideRedactedEvents)
|
|
|
|
.then((value) => AppConfig.hideRedactedEvents = value);
|
|
|
|
store
|
|
|
|
.getItemBool(SettingKeys.hideUnknownEvents, AppConfig.hideUnknownEvents)
|
|
|
|
.then((value) => AppConfig.hideUnknownEvents = value);
|
2022-04-09 08:10:53 +02:00
|
|
|
store
|
|
|
|
.getItemBool(SettingKeys.showDirectChatsInSpaces,
|
|
|
|
AppConfig.showDirectChatsInSpaces)
|
|
|
|
.then((value) => AppConfig.showDirectChatsInSpaces = value);
|
2022-04-03 19:00:35 +02:00
|
|
|
store
|
|
|
|
.getItemBool(SettingKeys.separateChatTypes, AppConfig.separateChatTypes)
|
|
|
|
.then((value) => AppConfig.separateChatTypes = value);
|
2022-01-29 12:35:03 +01:00
|
|
|
store
|
|
|
|
.getItemBool(SettingKeys.autoplayImages, AppConfig.autoplayImages)
|
|
|
|
.then((value) => AppConfig.autoplayImages = value);
|
|
|
|
store
|
|
|
|
.getItemBool(SettingKeys.sendOnEnter, AppConfig.sendOnEnter)
|
|
|
|
.then((value) => AppConfig.sendOnEnter = value);
|
2022-02-19 11:58:21 +01:00
|
|
|
store
|
|
|
|
.getItemBool(SettingKeys.experimentalVoip, AppConfig.experimentalVoip)
|
|
|
|
.then((value) => AppConfig.experimentalVoip = value);
|
2022-01-29 12:35:03 +01:00
|
|
|
store.getItem(SettingKeys.chatColor).then((value) {
|
|
|
|
if (value != null && int.tryParse(value) != null) {
|
2022-05-18 08:54:50 +02:00
|
|
|
AppConfig.colorSchemeSeed = Color(int.parse(value));
|
2022-01-29 12:35:03 +01:00
|
|
|
AdaptiveTheme.of(context).setTheme(
|
2022-05-18 12:13:59 +02:00
|
|
|
light: FluffyThemes.light(),
|
|
|
|
dark: FluffyThemes.dark(),
|
2022-01-29 12:35:03 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
2020-01-01 19:10:13 +01:00
|
|
|
}
|
|
|
|
|
2020-01-03 17:23:40 +01:00
|
|
|
@override
|
|
|
|
void dispose() {
|
2022-05-12 11:25:34 +02:00
|
|
|
WidgetsBinding.instance.removeObserver(this);
|
2021-02-07 17:18:38 +01:00
|
|
|
|
2021-09-19 13:48:23 +02:00
|
|
|
onRoomKeyRequestSub.values.map((s) => s.cancel());
|
|
|
|
onKeyVerificationRequestSub.values.map((s) => s.cancel());
|
|
|
|
onLoginStateChanged.values.map((s) => s.cancel());
|
|
|
|
onOwnPresence.values.map((s) => s.cancel());
|
|
|
|
onNotification.values.map((s) => s.cancel());
|
2022-06-17 22:17:41 +02:00
|
|
|
client.httpClient.close();
|
2020-08-22 15:20:07 +02:00
|
|
|
onFocusSub?.cancel();
|
|
|
|
onBlurSub?.cancel();
|
2021-02-07 17:18:38 +01:00
|
|
|
_backgroundPush?.onLogin?.cancel();
|
|
|
|
|
2021-06-18 10:29:48 +02:00
|
|
|
linuxNotifications?.close();
|
2021-05-01 11:52:47 +02:00
|
|
|
|
2020-01-03 17:23:40 +01:00
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
2020-01-01 19:10:13 +01:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2021-01-15 19:59:30 +01:00
|
|
|
return Provider(
|
|
|
|
create: (_) => this,
|
2020-12-19 16:37:32 +01:00
|
|
|
child: widget.child,
|
2020-01-01 19:10:13 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2021-05-13 12:18:50 +02:00
|
|
|
|
|
|
|
class FixedThreepidCreds extends ThreepidCreds {
|
|
|
|
FixedThreepidCreds({
|
2022-01-29 12:35:03 +01:00
|
|
|
required String sid,
|
|
|
|
required String clientSecret,
|
|
|
|
String? idServer,
|
|
|
|
String? idAccessToken,
|
2021-05-13 12:18:50 +02:00
|
|
|
}) : super(
|
|
|
|
sid: sid,
|
|
|
|
clientSecret: clientSecret,
|
|
|
|
idServer: idServer,
|
|
|
|
idAccessToken: idAccessToken,
|
|
|
|
);
|
|
|
|
|
|
|
|
@override
|
|
|
|
Map<String, dynamic> toJson() {
|
|
|
|
final data = <String, dynamic>{};
|
|
|
|
data['sid'] = sid;
|
|
|
|
data['client_secret'] = clientSecret;
|
|
|
|
if (idServer != null) data['id_server'] = idServer;
|
|
|
|
if (idAccessToken != null) data['id_access_token'] = idAccessToken;
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
}
|
2021-09-19 13:48:23 +02:00
|
|
|
|
|
|
|
class _AccountBundleWithClient {
|
2022-01-29 12:35:03 +01:00
|
|
|
final Client? client;
|
|
|
|
final AccountBundle? bundle;
|
2022-02-19 09:27:03 +01:00
|
|
|
|
2021-09-19 13:48:23 +02:00
|
|
|
_AccountBundleWithClient({this.client, this.bundle});
|
|
|
|
}
|