diff --git a/lib/data_ops/task_database.dart b/lib/data_ops/task_database.dart index bca49ee..a414195 100644 --- a/lib/data_ops/task_database.dart +++ b/lib/data_ops/task_database.dart @@ -145,11 +145,35 @@ class TaskDatabase extends ChangeNotifier { completed: task.completed, ); + // Save data to local database + saveDataToDevice(); + // Notify Listeners notifyListeners(); } } + // toggle reorderTask in the specific taskList + void reorderTask({ + required String taskListName, + required int oldIndex, + required int newIndex, + }) { + taskLists + .where((taskList) => taskList.name == taskListName) + .first + .reorderTask( + oldIndex: oldIndex, + newIndex: newIndex, + ); + + // Save data to local database + saveDataToDevice(); + + // Notify listeners + notifyListeners(); + } + // // Task List Operations // diff --git a/lib/dialogues/transfer_task_dialogue.dart b/lib/dialogues/transfer_task_dialogue.dart index 3f1330e..3ff4fd9 100644 --- a/lib/dialogues/transfer_task_dialogue.dart +++ b/lib/dialogues/transfer_task_dialogue.dart @@ -59,6 +59,7 @@ class _TransferTaskDialogueState extends State { database.currentTaskListName, taskLists[index].name, ); + Navigator.pop(context); }, child: Container( padding: const EdgeInsets.symmetric( diff --git a/lib/models/task_list_model.dart b/lib/models/task_list_model.dart index 6fe5ef5..b8d768e 100644 --- a/lib/models/task_list_model.dart +++ b/lib/models/task_list_model.dart @@ -67,4 +67,24 @@ class TaskList { bool taskExists(String taskName) { return tasks.indexWhere((task) => task.name == taskName) != -1; } + + void reorderTask({ + required int oldIndex, + required int newIndex, + }) { + // Remove task from position + Task task = tasks.removeAt(oldIndex); + + if (oldIndex <= newIndex) newIndex--; + + // Put in new place + if (newIndex > tasks.length) { + addTask( + taskName: task.name, + completed: task.completed, + ); + } else { + tasks.insert(newIndex, task); + } + } } diff --git a/lib/pages/home.dart b/lib/pages/home.dart index d987cb5..5842b47 100644 --- a/lib/pages/home.dart +++ b/lib/pages/home.dart @@ -242,7 +242,13 @@ class _HomePageState extends State { ), ) : ReorderableListView.builder( - onReorder: ((oldIndex, newIndex) {}), + onReorder: ((oldIndex, newIndex) { + database.reorderTask( + taskListName: database.currentTaskListName, + oldIndex: oldIndex, + newIndex: newIndex, + ); + }), itemBuilder: (BuildContext context, int index) => TaskCard( key: ValueKey(taskList.tasks[index]), task: taskList.tasks[index],