operator profiles - import, reorderable

This commit is contained in:
stopflock
2026-03-23 12:58:50 -05:00
parent 6e2fa3a04c
commit bd71f88452
8 changed files with 279 additions and 44 deletions
+61 -1
View File
@@ -1,12 +1,16 @@
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../models/operator_profile.dart';
import '../services/operator_profile_service.dart';
class OperatorProfileState extends ChangeNotifier {
static const String _profileOrderPrefsKey = 'operator_profile_order';
final List<OperatorProfile> _profiles = [];
List<String> _customOrder = []; // List of profile IDs in user's preferred order
List<OperatorProfile> get profiles => List.unmodifiable(_profiles);
List<OperatorProfile> get profiles => List.unmodifiable(_getOrderedProfiles());
Future<void> init({bool addDefaults = false}) async {
_profiles.addAll(await OperatorProfileService().load());
@@ -16,6 +20,10 @@ class OperatorProfileState extends ChangeNotifier {
_profiles.addAll(OperatorProfile.getDefaults());
await OperatorProfileService().save(_profiles);
}
// Load custom order from prefs
final prefs = await SharedPreferences.getInstance();
_customOrder = prefs.getStringList(_profileOrderPrefsKey) ?? [];
}
void addOrUpdateProfile(OperatorProfile p) {
@@ -34,4 +42,56 @@ class OperatorProfileState extends ChangeNotifier {
OperatorProfileService().save(_profiles);
notifyListeners();
}
// 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;
}
final item = orderedProfiles.removeAt(oldIndex);
orderedProfiles.insert(newIndex, item);
// Update custom order with new sequence
_customOrder = orderedProfiles.map((p) => p.id).toList();
_saveCustomOrder();
notifyListeners();
}
// Get profiles in custom order, with unordered profiles at the end
List<OperatorProfile> _getOrderedProfiles() {
if (_customOrder.isEmpty) {
return List.from(_profiles);
}
final ordered = <OperatorProfile>[];
final profilesById = {for (final p in _profiles) p.id: p};
// Add profiles in custom order
for (final id in _customOrder) {
final profile = profilesById[id];
if (profile != null) {
ordered.add(profile);
profilesById.remove(id);
}
}
// Add any remaining profiles that weren't in the custom order
ordered.addAll(profilesById.values);
return ordered;
}
// Save custom order to disk
Future<void> _saveCustomOrder() async {
try {
final prefs = await SharedPreferences.getInstance();
await prefs.setStringList(_profileOrderPrefsKey, _customOrder);
} catch (e) {
// Fail gracefully in tests or if SharedPreferences isn't available
debugPrint('[OperatorProfileState] Failed to save custom order: $e');
}
}
}