parent
3a8d977f11
commit
641341ed90
1 changed files with 667 additions and 144 deletions
|
|
@ -1,23 +1,38 @@
|
|||
import "package:fast_immutable_collections/fast_immutable_collections.dart";
|
||||
import "package:flutter_riverpod/flutter_riverpod.dart";
|
||||
import "package:nexus/controllers/active_account.dart";
|
||||
import "package:nexus/controllers/shared_prefs.dart";
|
||||
import "package:nexus/models/account.dart";
|
||||
|
||||
class KeyController(final String key) extends AsyncNotifier<String?> {
|
||||
static const String spaceKey = "space";
|
||||
static const String roomKey = "room";
|
||||
static const String pushKeyKey = "pushKey";
|
||||
static const String accountsKey = "accounts";
|
||||
static const String activeAccountKey = "activeAccount";
|
||||
|
||||
static const ISet<String> _perAccountKeys = ISetConst({spaceKey, roomKey});
|
||||
|
||||
String get _prefsKey {
|
||||
final account = ref.read(ActiveAccountController.provider);
|
||||
|
||||
return _perAccountKeys.contains(key) && account != Account.legacyId
|
||||
? "$account/$key"
|
||||
: key;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<String?> build() =>
|
||||
ref.watch(SharedPrefsController.provider).getString(key);
|
||||
ref.watch(SharedPrefsController.provider).getString(_prefsKey);
|
||||
|
||||
Future<void> set(String? value) async {
|
||||
final prefs = ref.read(SharedPrefsController.provider);
|
||||
state = .data(value);
|
||||
|
||||
if (value == null) {
|
||||
await prefs.remove(key);
|
||||
await prefs.remove(_prefsKey);
|
||||
} else {
|
||||
await prefs.setString(key, value);
|
||||
await prefs.setString(_prefsKey, value);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue