add Matrix.getBundles() and Matrix.hasComplexBundles

This commit is contained in:
Sorunome 2021-09-18 15:30:22 +02:00
parent b68910dae7
commit 1278b91eed
No known key found for this signature in database
GPG Key ID: B19471D07FC9BE9C
3 changed files with 102 additions and 1 deletions

View File

@ -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,

View File

@ -0,0 +1,64 @@
import 'package:matrix/matrix.dart';
class AccountBundles {
String prefix;
List<AccountBundle> bundles;
AccountBundles({this.prefix, this.bundles});
AccountBundles.fromJson(Map<String, dynamic> json)
: prefix = json.tryGet<String>('prefix'),
bundles = json['bundles'] is List
? json['bundles']
.map((b) {
try {
return AccountBundle.fromJson(b);
} catch (_) {
return null;
}
})
.whereType<AccountBundle>()
.toList()
: null;
Map<String, dynamic> 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<String, dynamic> json)
: name = json.tryGet<String>('name'),
priority = json.tryGet<int>('priority');
Map<String, dynamic> toJson() => <String, dynamic>{
if (name != null) 'name': name,
if (priority != null) 'priority': priority,
};
}
const _accountBundlesType = 'im.fluffychat.account_bundles';
extension AccountBundlesExtension on Client {
List<AccountBundle> get accountBundles {
List<AccountBundle> 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;
}
}

View File

@ -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<Matrix> with WidgetsBindingObserver {
int activeClient = 0;
String activeBundle;
Store store = Store();
BuildContext navigatorContext;
@ -75,6 +77,34 @@ class MatrixState extends State<Matrix> with WidgetsBindingObserver {
return activeClient;
}
Map<String, List<int>> getBundles() {
final resBundles = <String, List<_AccountBundleWithClientIndex>>{};
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});
}