|
| 1 | +import 'package:algorithm_visualizer/core/helpers/screen_size.dart'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | +import 'package:flutter_riverpod/flutter_riverpod.dart'; |
| 4 | +import 'package:flutter_screenutil/flutter_screenutil.dart'; |
| 5 | + |
| 6 | +part 'sorting_state.dart'; |
| 7 | + |
| 8 | +class SortableItem { |
| 9 | + final int id; |
| 10 | + final int value; |
| 11 | + |
| 12 | + SortableItem(this.id, this.value); |
| 13 | +} |
| 14 | + |
| 15 | +class SortingNotifier extends StateNotifier<SortingNotifierState> { |
| 16 | + SortingNotifier() |
| 17 | + : super(SortingNotifierState( |
| 18 | + list: List.generate(_listSize, (index) => SortableItem(index, index + 1))..shuffle(), |
| 19 | + )) { |
| 20 | + _initializePositions(); |
| 21 | + } |
| 22 | + |
| 23 | + static const int _listSize = 50; |
| 24 | + static double maxListItemHeight = 250.h; |
| 25 | + static double itemsPadding = 1.w; |
| 26 | + static const Duration swipeDuration = Duration(milliseconds: 5); |
| 27 | + |
| 28 | + static double calculateItemWidth(BuildContext context) { |
| 29 | + final screenWidth = MediaQuery.of(context).size.width; |
| 30 | + final availableWidth = screenWidth - (itemsPadding * (_listSize - 1)); |
| 31 | + |
| 32 | + // Ensure a positive width |
| 33 | + return availableWidth / _listSize > 0 ? availableWidth / _listSize : 1.0; |
| 34 | + } |
| 35 | + |
| 36 | + static double calculateItemHeight(int itemIndex) { |
| 37 | + final value = (maxListItemHeight / _listSize) * (itemIndex + 1); |
| 38 | + return value.h; |
| 39 | + } |
| 40 | + |
| 41 | + static Color getColor(int itemIndex) { |
| 42 | + double step = (itemIndex * 2) / 100; |
| 43 | + return Colors.indigo.withOpacity(step); |
| 44 | + } |
| 45 | + |
| 46 | + void _initializePositions() { |
| 47 | + final positions = <int, Offset>{}; |
| 48 | + final itemWidth = calculateItemWidth(ScreenSize.context!); |
| 49 | + |
| 50 | + for (int i = 0; i < state.list.length; i++) { |
| 51 | + positions[state.list[i].id] = Offset(i * (itemWidth + itemsPadding), 0); |
| 52 | + } |
| 53 | + state = state.copyWith(positions: positions); |
| 54 | + } |
| 55 | + |
| 56 | + Future<void> bubbleSort() async { |
| 57 | + 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++) { |
| 60 | + 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; |
| 65 | + |
| 66 | + // Update positions for swap |
| 67 | + final positions = Map<int, Offset>.from(state.positions); |
| 68 | + final tempPosition = positions[list[j].id]!; |
| 69 | + positions[list[j].id] = positions[list[j + 1].id]!; |
| 70 | + positions[list[j + 1].id] = tempPosition; |
| 71 | + |
| 72 | + // Update state |
| 73 | + state = state.copyWith(list: list, positions: positions); |
| 74 | + |
| 75 | + // Delay for the animation to complete |
| 76 | + await Future.delayed(swipeDuration); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments