diff --git a/lib/pages/views/homeserver_picker_view.dart b/lib/pages/views/homeserver_picker_view.dart index 173d9778..cc5f64db 100644 --- a/lib/pages/views/homeserver_picker_view.dart +++ b/lib/pages/views/homeserver_picker_view.dart @@ -100,7 +100,8 @@ class HomeserverPickerView extends StatelessWidget { imageUrl: Uri.parse( identityProvider.icon) .getDownloadLink( - Matrix.of(context).getLoginClient()) + Matrix.of(context) + .getLoginClient()) .toString(), width: 24, height: 24, diff --git a/lib/utils/account_bundles.dart b/lib/utils/account_bundles.dart new file mode 100644 index 00000000..93006acc --- /dev/null +++ b/lib/utils/account_bundles.dart @@ -0,0 +1,64 @@ +import 'package:matrix/matrix.dart'; + +class AccountBundles { + String prefix; + List bundles; + + AccountBundles({this.prefix, this.bundles}); + + AccountBundles.fromJson(Map json) + : prefix = json.tryGet('prefix'), + bundles = json['bundles'] is List + ? json['bundles'] + .map((b) { + try { + return AccountBundle.fromJson(b); + } catch (_) { + return null; + } + }) + .whereType() + .toList() + : null; + + Map toJson() => { + if (prefix != null) 'prefix': prefix, + if (bundles != null) 'bundles': bundles.map((v) => v.toJson()).toList(), + }; +} + +class AccountBundle { + String name; + int priority; + + AccountBundle({this.name, this.priority}); + + AccountBundle.fromJson(Map json) + : name = json.tryGet('name'), + priority = json.tryGet('priority'); + + Map toJson() => { + if (name != null) 'name': name, + if (priority != null) 'priority': priority, + }; +} + +const _accountBundlesType = 'im.fluffychat.account_bundles'; + +extension AccountBundlesExtension on Client { + List get accountBundles { + List ret; + if (accountData.containsKey(_accountBundlesType)) { + ret = AccountBundles.fromJson(accountData[_accountBundlesType].content) + .bundles; + } + ret ??= []; + if (ret.isEmpty) { + ret.add(AccountBundle( + name: userID, + priority: 0, + )); + } + return ret; + } +} diff --git a/lib/widgets/matrix.dart b/lib/widgets/matrix.dart index ffe257af..4ccdfe2b 100644 --- a/lib/widgets/matrix.dart +++ b/lib/widgets/matrix.dart @@ -24,6 +24,7 @@ import '../pages/key_verification_dialog.dart'; import '../utils/platform_infos.dart'; import '../config/app_config.dart'; import '../config/setting_keys.dart'; +import '../utils/account_bundles.dart'; import '../utils/background_push.dart'; import 'package:vrouter/vrouter.dart'; @@ -59,6 +60,7 @@ class Matrix extends StatefulWidget { class MatrixState extends State with WidgetsBindingObserver { int activeClient = 0; + String activeBundle; Store store = Store(); BuildContext navigatorContext; @@ -75,6 +77,34 @@ class MatrixState extends State with WidgetsBindingObserver { return activeClient; } + Map> getBundles() { + final resBundles = >{}; + 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] ??= []; + resBundles[bundle.name].add(_AccountBundleWithClientIndex( + index: i, + bundle: bundle, + )); + } + } + for (final b in resBundles.values) { + b.sort((a, b) => a.bundle.priority == null + ? 1 + : b.bundle.priority == null + ? -1 + : a.bundle.priority.compareTo(b.bundle.priority)); + } + return resBundles + .map((k, v) => MapEntry(k, v.map((vv) => vv.index).toList())); + } + + bool get hasComplexBundles => getBundles().values.any((v) => v.length > 1); + Client _loginClientCandidate; Client getLoginClient() { @@ -566,3 +596,9 @@ class FixedThreepidCreds extends ThreepidCreds { return data; } } + +class _AccountBundleWithClientIndex { + final int index; + final AccountBundle bundle; + _AccountBundleWithClientIndex({this.index, this.bundle}); +}