Skip to content

Commit bf5be93

Browse files
cleanup sorting logic
1 parent bd000c9 commit bf5be93

File tree

1 file changed

+1
-59
lines changed

1 file changed

+1
-59
lines changed

lib/features/sorting/bubble/view_model/bubble_sort_notifier.dart

Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,8 @@
11
import 'package:algorithm_visualizer/features/sorting/base/view_model/sorting_notifier.dart';
2-
import 'package:collection/collection.dart';
3-
import 'package:flutter/material.dart';
42

53
class BubbleSortNotifier extends SortingNotifier {
64
@override
7-
Future<void> buildSort() async {
8-
final list = List<SortableItem>.from(state.list);
9-
final values = list.map((e) => e.value).toList();
10-
11-
final steps = bubbleSortSteps(values);
12-
13-
for (final step in steps) {
14-
if (operation != SortingEnum.played) return;
15-
16-
switch (step.action) {
17-
case SortingStatus.compared:
18-
list[step.index1] = list[step.index1].copyWith(sortedStatus: SortingStatus.compared);
19-
list[step.index2] = list[step.index2].copyWith(sortedStatus: SortingStatus.compared);
20-
state = state.copyWith(list: list);
21-
22-
await Future.delayed(speedDuration);
23-
24-
break;
25-
26-
case SortingStatus.swapped:
27-
list[step.index1] = list[step.index1].copyWith(sortedStatus: SortingStatus.swapped);
28-
list[step.index2] = list[step.index2].copyWith(sortedStatus: SortingStatus.swapped);
29-
state = state.copyWith(list: list);
30-
31-
await Future.delayed(speedDuration);
32-
33-
list.swap(step.index1, step.index2);
34-
35-
final positions = Map<int, Offset>.from(state.positions);
36-
final tempPosition = positions[list[step.index1].id]!;
37-
positions[list[step.index1].id] = positions[list[step.index2].id]!;
38-
positions[list[step.index2].id] = tempPosition;
39-
40-
state = state.copyWith(list: list, positions: positions);
41-
break;
42-
43-
case SortingStatus.unSorted:
44-
list[step.index1] = list[step.index1].copyWith(sortedStatus: SortingStatus.unSorted);
45-
list[step.index2] = list[step.index2].copyWith(sortedStatus: SortingStatus.unSorted);
46-
state = state.copyWith(list: list);
47-
break;
48-
49-
// i don't want to make it green while sorting and mark all of them at once as green at the end
50-
case SortingStatus.sorted:
51-
case SortingStatus.none:
52-
list[step.index1] = list[step.index1].copyWith(sortedStatus: SortingStatus.none);
53-
state = state.copyWith(list: list);
54-
break;
55-
}
56-
57-
await Future.delayed(speedDuration);
58-
}
59-
60-
await greenSortedItemsAsDone();
61-
}
62-
63-
List<SortingStep> bubbleSortSteps(List<int> values) {
5+
List<SortingStep> buildSorting(List<int> values) {
646
final steps = <SortingStep>[];
657
final arr = List<int>.from(values);
668

0 commit comments

Comments
 (0)