forked from Nexus/nexus
parent
2041b81141
commit
4b329b4c6f
1 changed files with 1115 additions and 119 deletions
|
|
@ -1,8 +1,10 @@
|
|||
import "dart:async";
|
||||
import "dart:convert";
|
||||
|
||||
import "package:fast_immutable_collections/fast_immutable_collections.dart";
|
||||
import "package:flutter/foundation.dart";
|
||||
import "package:flutter_riverpod/flutter_riverpod.dart";
|
||||
import "package:nexus/controllers/client.dart";
|
||||
import "package:nexus/models/account_data.dart";
|
||||
|
||||
class AccountDataController extends Notifier<AccountData> {
|
||||
|
|
@ -22,6 +24,44 @@ class AccountDataController extends Notifier<AccountData> {
|
|||
}
|
||||
});
|
||||
|
||||
Future<void> setEnabledImagePacks(ImagePackRooms packs) async {
|
||||
ISet<(String, String)> keysOf(ImagePackRooms rooms) => rooms.rooms.entries
|
||||
.expand((entry) => entry.value.keys.map((key) => (entry.key, key)))
|
||||
.toISet();
|
||||
final keys = keysOf(packs);
|
||||
bool arrived(AccountData data) {
|
||||
final current = keysOf(data.enabledImagePacks);
|
||||
return current.length == keys.length && current.containsAll(keys);
|
||||
}
|
||||
|
||||
final completer = Completer<void>();
|
||||
final removeListener = listenSelf((_, data) {
|
||||
if (arrived(data) && !completer.isCompleted) completer.complete();
|
||||
});
|
||||
|
||||
try {
|
||||
final client = ref.read(ClientController.provider.notifier);
|
||||
await Future.wait([
|
||||
if (state.imagePackRooms != null)
|
||||
client.setAccountData(
|
||||
.new(type: AccountData.imagePackRoomsKey, content: packs),
|
||||
),
|
||||
client.setAccountData(
|
||||
.new(type: AccountData.legacyImagePackRoomsKey, content: packs),
|
||||
),
|
||||
]);
|
||||
|
||||
if (!arrived(state)) {
|
||||
await completer.future.timeout(
|
||||
const .new(seconds: 10),
|
||||
onTimeout: () {},
|
||||
);
|
||||
}
|
||||
} finally {
|
||||
removeListener();
|
||||
}
|
||||
}
|
||||
|
||||
static final provider = NotifierProvider<AccountDataController, AccountData>(
|
||||
AccountDataController.new,
|
||||
);
|
||||
|
|
|
|||
Loading…
Reference in a new issue