Skip to content

Commit

Permalink
reordering implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
C41f0N committed Mar 18, 2024
1 parent 11b0aef commit d65c676
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
24 changes: 24 additions & 0 deletions lib/data_ops/task_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand Down
1 change: 1 addition & 0 deletions lib/dialogues/transfer_task_dialogue.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class _TransferTaskDialogueState extends State<TransferTaskDialogue> {
database.currentTaskListName,
taskLists[index].name,
);
Navigator.pop(context);
},
child: Container(
padding: const EdgeInsets.symmetric(
Expand Down
20 changes: 20 additions & 0 deletions lib/models/task_list_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
8 changes: 7 additions & 1 deletion lib/pages/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,13 @@ class _HomePageState extends State<HomePage> {
),
)
: 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],
Expand Down

0 comments on commit d65c676

Please sign in to comment.