Skip to content

Commit

Permalink
Presets can be re-ordered
Browse files Browse the repository at this point in the history
  • Loading branch information
Levi-Lesches committed Oct 9, 2024
1 parent d4b442f commit de4d85e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
22 changes: 22 additions & 0 deletions lib/src/models/data/views.dart
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,26 @@ class ViewsModel extends Model {
}
notifyListeners();
}

/// Swaps two presets in the user's settings.
void swapPresets(int oldIndex, int newIndex) {
final presets = models.settings.dashboard.presets;
// ignore: parameter_assignments
if (oldIndex < newIndex) newIndex -= 1;
final element = presets.removeAt(oldIndex);
presets.insert(newIndex, element);
// This notifyListeners call is needed to update the UI smoothly.
//
// A ResizableListView simply *simulates* re-ordering its children. After
// the child is dropped in its new position, it is sent back to its original
// position, and it is the backend's job to actually update the underlying data.
//
// Calling [SettingsModel.update] here does the job, but its [notifyListeners] call
// is (correctly) placed *after* the settings file is updated on disk. This introduces
// a delay in the re-ordering, so items will animate back and forth.
//
// This call will update the UI based on the in-memory list before the disk is updated.
notifyListeners();
models.settings.update();
}
}
12 changes: 11 additions & 1 deletion lib/src/widgets/navigation/views_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ class ViewsSidebarModel with ChangeNotifier {
/// Listens for changes in the network and settings.
ViewsSidebarModel() {
models.sockets.addListener(notifyListeners);
models.views.addListener(notifyListeners);
models.settings.addListener(notifyListeners);
}

@override
void dispose() {
models.sockets.removeListener(notifyListeners);
models.views.removeListener(notifyListeners);
models.settings.removeListener(notifyListeners);
super.dispose();
}
Expand Down Expand Up @@ -58,16 +60,24 @@ class ViewsList extends ReactiveWidget<ViewsSidebarModel> {
ExpansionTile(
title: const Text("Presets"),
children: [
ReorderableListView(
shrinkWrap: true,
onReorder: models.views.swapPresets,
children: [
for (final preset in models.settings.dashboard.presets) ListTile(
key: ValueKey(preset.name),
title: Text(preset.name),
onTap: () => models.views.loadPreset(preset),
trailing: IconButton(
leading: IconButton(
onPressed: () => _deletePreset(context, preset),
icon: const Icon(Icons.remove_circle),
splashColor: Colors.blueGrey,
color: Colors.red,
),
),
],
),

ListTile(
title: const Text("Save current layout"),
onTap: () => _savePreset(context),
Expand Down

0 comments on commit de4d85e

Please sign in to comment.