43 lines
1.2 KiB
Dart
43 lines
1.2 KiB
Dart
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
const _legacyActiveCollectionPrefKey = 'active_collection_id';
|
|
|
|
String activeCollectionPrefKeyForUser(String userId) {
|
|
return 'active_collection_id_$userId';
|
|
}
|
|
|
|
Future<String?> readActiveCollectionId(
|
|
SharedPreferences prefs, {
|
|
required String userId,
|
|
}) async {
|
|
final scopedKey = activeCollectionPrefKeyForUser(userId);
|
|
final scopedValue = prefs.getString(scopedKey);
|
|
if (scopedValue != null && scopedValue.isNotEmpty) {
|
|
return scopedValue;
|
|
}
|
|
|
|
final legacyValue = prefs.getString(_legacyActiveCollectionPrefKey);
|
|
if (legacyValue == null || legacyValue.isEmpty) {
|
|
return null;
|
|
}
|
|
|
|
await prefs.setString(scopedKey, legacyValue);
|
|
await prefs.remove(_legacyActiveCollectionPrefKey);
|
|
return legacyValue;
|
|
}
|
|
|
|
Future<void> writeActiveCollectionId(
|
|
SharedPreferences prefs, {
|
|
required String userId,
|
|
required String collectionId,
|
|
}) {
|
|
return prefs.setString(activeCollectionPrefKeyForUser(userId), collectionId);
|
|
}
|
|
|
|
Future<void> clearActiveCollectionId(
|
|
SharedPreferences prefs, {
|
|
required String userId,
|
|
}) async {
|
|
await prefs.remove(activeCollectionPrefKeyForUser(userId));
|
|
await prefs.remove(_legacyActiveCollectionPrefKey);
|
|
}
|