11import 'package:algorithm_visualizer/core/helpers/screen_size.dart' ;
2+ import 'package:collection/collection.dart' ;
23import 'package:flutter/material.dart' ;
34import 'package:flutter_riverpod/flutter_riverpod.dart' ;
45import 'package:flutter_screenutil/flutter_screenutil.dart' ;
6+ import 'package:async/async.dart' ;
57
68part 'sorting_state.dart' ;
79
10+ enum SortingEnum { played, stopped, none }
11+
812class SortableItem {
913 final int id;
1014 final int value;
@@ -13,17 +17,20 @@ class SortableItem {
1317}
1418
1519class SortingNotifier extends StateNotifier <SortingNotifierState > {
16- SortingNotifier ()
17- : super (SortingNotifierState (
18- list: List .generate (_listSize, (index) => SortableItem (index, index + 1 ))..shuffle (),
19- )) {
20+ SortingNotifier () : super (SortingNotifierState (list: generateList ())) {
2021 _initializePositions ();
2122 }
2223
2324 static const int _listSize = 50 ;
2425 static double maxListItemHeight = 250. h;
2526 static double itemsPadding = 1. w;
2627 static const Duration swipeDuration = Duration (milliseconds: 5 );
28+ SortingEnum _operation = SortingEnum .none;
29+ CancelableOperation <void >? _cancelableBubbleSort;
30+
31+ static List <SortableItem > generateList () {
32+ return List .generate (_listSize, (index) => SortableItem (index, index + 1 ))..shuffle ();
33+ }
2734
2835 static double calculateItemWidth (BuildContext context) {
2936 final screenWidth = MediaQuery .of (context).size.width;
@@ -40,7 +47,9 @@ class SortingNotifier extends StateNotifier<SortingNotifierState> {
4047
4148 static Color getColor (int itemIndex) {
4249 double step = (itemIndex * 2 ) / 100 ;
43- return Colors .indigo.withOpacity (step);
50+ final value = step + 0.1 > 1 ? 1.0 : step + 0.1 ;
51+
52+ return Colors .indigo.withOpacity (value);
4453 }
4554
4655 void _initializePositions () {
@@ -53,27 +62,62 @@ class SortingNotifier extends StateNotifier<SortingNotifierState> {
5362 state = state.copyWith (positions: positions);
5463 }
5564
65+ void stopSorting () {
66+ _cancelableBubbleSort? .cancel ();
67+ _operation = SortingEnum .stopped;
68+ }
69+
70+ void playSorting () {
71+ if (_operation == SortingEnum .played) return ;
72+ _operation = SortingEnum .played;
73+
74+ bubbleSort ();
75+ }
76+
77+ void generateAgain () {
78+ _operation = SortingEnum .stopped;
79+
80+ i = 0 ;
81+ j = 0 ;
82+
83+ state = state.copyWith (list: generateList ());
84+ _initializePositions ();
85+ }
86+
87+ int i = 0 ;
88+ int j = 0 ;
89+
5690 Future <void > bubbleSort () async {
91+ _cancelableBubbleSort = CancelableOperation .fromFuture (_bubbleSort ());
92+ try {
93+ await _cancelableBubbleSort? .value;
94+ } catch (e) {
95+ debugPrint ("something wrong with bubbleSort: $e " );
96+ }
97+ }
98+
99+ Future <void > _bubbleSort () async {
57100 final list = List <SortableItem >.from (state.list);
58- for (int i = 0 ; i < list.length - 1 ; i++ ) {
59- for (int j = 0 ; j < list.length - i - 1 ; j++ ) {
101+
102+ for (i = 0 ; i < list.length - 1 ; i++ ) {
103+ if (_operation != SortingEnum .played) return ;
104+
105+ for (j = 0 ; j < list.length - i - 1 ; j++ ) {
106+ if (_operation != SortingEnum .played) return ;
107+
60108 if (list[j].value > list[j + 1 ].value) {
61- // Swap items in the list
62- final temp = list[j];
63- list[j] = list[j + 1 ];
64- list[j + 1 ] = temp;
109+ if (_operation != SortingEnum .played) return ;
110+ list.swap (j, j + 1 );
65111
66- // Update positions for swap
67112 final positions = Map <int , Offset >.from (state.positions);
68113 final tempPosition = positions[list[j].id]! ;
69114 positions[list[j].id] = positions[list[j + 1 ].id]! ;
70115 positions[list[j + 1 ].id] = tempPosition;
71116
72- // Update state
73117 state = state.copyWith (list: list, positions: positions);
74118
75- // Delay for the animation to complete
76119 await Future .delayed (swipeDuration);
120+ if (_operation != SortingEnum .played) return ;
77121 }
78122 }
79123 }
0 commit comments