mirror of
https://github.com/FoggedLens/deflock-app.git
synced 2026-07-07 21:27:57 +02:00
fix tests for profile order, add correct migration
This commit is contained in:
@@ -114,6 +114,34 @@ class OneTimeMigrations {
|
||||
}
|
||||
}
|
||||
|
||||
/// Initialize profile ordering for existing users (v2.7.3)
|
||||
static Future<void> migrate_2_7_3(AppState appState) async {
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
const orderKey = 'profile_order';
|
||||
|
||||
// Check if user already has custom profile ordering
|
||||
if (prefs.containsKey(orderKey)) {
|
||||
debugPrint('[Migration] 2.7.3: Profile order already exists, skipping');
|
||||
return;
|
||||
}
|
||||
|
||||
// Initialize with current profile order (preserves existing UI order)
|
||||
final currentProfiles = appState.profiles;
|
||||
final initialOrder = currentProfiles.map((p) => p.id).toList();
|
||||
|
||||
if (initialOrder.isNotEmpty) {
|
||||
await prefs.setStringList(orderKey, initialOrder);
|
||||
debugPrint('[Migration] 2.7.3: Initialized profile order with ${initialOrder.length} profiles');
|
||||
}
|
||||
|
||||
debugPrint('[Migration] 2.7.3 completed: initialized profile ordering');
|
||||
} catch (e) {
|
||||
debugPrint('[Migration] 2.7.3 ERROR: Failed to initialize profile ordering: $e');
|
||||
// Don't rethrow - this is non-critical, profiles will just use default order
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the migration function for a specific version
|
||||
static Future<void> Function(AppState)? getMigrationForVersion(String version) {
|
||||
switch (version) {
|
||||
@@ -127,6 +155,8 @@ class OneTimeMigrations {
|
||||
return migrate_1_8_0;
|
||||
case '2.1.0':
|
||||
return migrate_2_1_0;
|
||||
case '2.7.3':
|
||||
return migrate_2_7_3;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -225,6 +225,10 @@ class ChangelogService {
|
||||
versionsNeedingMigration.add('1.6.3');
|
||||
}
|
||||
|
||||
if (needsMigration(lastSeenVersion, currentVersion, '2.7.3')) {
|
||||
versionsNeedingMigration.add('2.7.3');
|
||||
}
|
||||
|
||||
// Future versions can be added here
|
||||
// if (needsMigration(lastSeenVersion, currentVersion, '2.0.0')) {
|
||||
// versionsNeedingMigration.add('2.0.0');
|
||||
|
||||
@@ -12,6 +12,12 @@ class ProfileState extends ChangeNotifier {
|
||||
final Set<NodeProfile> _enabled = {};
|
||||
List<String> _customOrder = []; // List of profile IDs in user's preferred order
|
||||
|
||||
// Test-only getters for accessing private state
|
||||
@visibleForTesting
|
||||
List<NodeProfile> get internalProfiles => _profiles;
|
||||
@visibleForTesting
|
||||
Set<NodeProfile> get internalEnabled => _enabled;
|
||||
|
||||
// Callback for when a profile is deleted (used to clear stale sessions)
|
||||
void Function(NodeProfile)? _onProfileDeleted;
|
||||
|
||||
@@ -75,7 +81,7 @@ class ProfileState extends ChangeNotifier {
|
||||
_enabled.add(p);
|
||||
_saveEnabledProfiles();
|
||||
}
|
||||
ProfileService().save(_profiles);
|
||||
_saveProfilesToStorage();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
@@ -89,7 +95,7 @@ class ProfileState extends ChangeNotifier {
|
||||
_enabled.add(builtIn);
|
||||
}
|
||||
_saveEnabledProfiles();
|
||||
ProfileService().save(_profiles);
|
||||
_saveProfilesToStorage();
|
||||
|
||||
// Notify about profile deletion so other parts can clean up
|
||||
_onProfileDeleted?.call(p);
|
||||
@@ -100,6 +106,8 @@ class ProfileState extends ChangeNotifier {
|
||||
// Reorder profiles (for drag-and-drop in settings)
|
||||
void reorderProfiles(int oldIndex, int newIndex) {
|
||||
final orderedProfiles = _getOrderedProfiles();
|
||||
|
||||
// Standard Flutter reordering logic
|
||||
if (oldIndex < newIndex) {
|
||||
newIndex -= 1;
|
||||
}
|
||||
@@ -138,16 +146,36 @@ class ProfileState extends ChangeNotifier {
|
||||
|
||||
// Save enabled profile IDs to disk
|
||||
Future<void> _saveEnabledProfiles() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setStringList(
|
||||
_enabledPrefsKey,
|
||||
_enabled.map((p) => p.id).toList(),
|
||||
);
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setStringList(
|
||||
_enabledPrefsKey,
|
||||
_enabled.map((p) => p.id).toList(),
|
||||
);
|
||||
} catch (e) {
|
||||
// Fail gracefully in tests or if SharedPreferences isn't available
|
||||
debugPrint('[ProfileState] Failed to save enabled profiles: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// Save profiles to storage
|
||||
Future<void> _saveProfilesToStorage() async {
|
||||
try {
|
||||
await ProfileService().save(_profiles);
|
||||
} catch (e) {
|
||||
// Fail gracefully in tests or if storage isn't available
|
||||
debugPrint('[ProfileState] Failed to save profiles: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// Save custom order to disk
|
||||
Future<void> _saveCustomOrder() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setStringList(_profileOrderPrefsKey, _customOrder);
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setStringList(_profileOrderPrefsKey, _customOrder);
|
||||
} catch (e) {
|
||||
// Fail gracefully in tests or if SharedPreferences isn't available
|
||||
debugPrint('[ProfileState] Failed to save custom order: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user