Skip to content

Commit c4e70f5

Browse files
handle deleting instances
1 parent e92196b commit c4e70f5

File tree

5 files changed

+92
-69
lines changed

5 files changed

+92
-69
lines changed

lib/features/sorting/base/view/sorting_page.dart

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,24 @@ part '../widgets/control_buttons.dart';
1414
part '../widgets/size_draggable.dart';
1515
part '../widgets/speed_draggable.dart';
1616

17-
class SortingPage extends StatelessWidget {
17+
class SortingPage extends ConsumerWidget {
1818
const SortingPage({required this.instance, required this.title, super.key});
1919
final StateNotifierProvider<SortingNotifier, SortingNotifierState> instance;
2020
final String title;
2121

2222
@override
23-
Widget build(BuildContext context) {
24-
return Scaffold(
25-
appBar: appBar(),
26-
body: _BuildBody(instance: instance),
23+
Widget build(BuildContext context, WidgetRef ref) {
24+
return PopScope(
25+
onPopInvokedWithResult: (didPop, result) async {
26+
if (didPop) {
27+
await ref.read(instance.notifier).cancelSorting();
28+
ref.invalidate(instance); // deletes current instance and resets
29+
}
30+
},
31+
child: Scaffold(
32+
appBar: appBar(),
33+
body: _BuildBody(instance: instance),
34+
),
2735
);
2836
}
2937

lib/features/sorting/base/view_model/sorting_notifier.dart

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,18 +104,23 @@ abstract class SortingNotifier extends StateNotifier<SortingNotifierState> {
104104
generateAgain();
105105
}
106106

107-
void stopSorting() {
108-
_cancelableSort?.cancel();
107+
Future<void> cancelSorting() async {
108+
await _cancelableSort?.cancel();
109+
}
110+
111+
Future<void> stopSorting() async {
112+
await _cancelableSort?.cancel();
109113
if (_getOperation == SortingEnum.played) _setOperation = SortingEnum.stopped;
110114
}
111115

112-
Future<void> playSorting() async {
116+
Future<void> playSorting(BuildContext context) async {
113117
if (_getOperation == SortingEnum.played) return;
114118
_setOperation = SortingEnum.played;
115119

116120
await _startSelectedSorting();
117121

118-
_setOperation = SortingEnum.none;
122+
// to avoid error of mounted when popup while the sorting algorithm still running
123+
if (context.mounted) _setOperation = SortingEnum.none;
119124
}
120125

121126
Future<void> generateAgain() async {

lib/features/sorting/base/widgets/control_buttons.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class _SortingControlButtons extends ConsumerWidget {
77
@override
88
Widget build(BuildContext context, ref) {
99
return SortingControlButtons(
10-
playSorting: () => ref.read(instance.notifier).playSorting(),
10+
playSorting: () => ref.read(instance.notifier).playSorting(context),
1111
stopSorting: () => ref.read(instance.notifier).stopSorting(),
1212
generateAgain: () => ref.read(instance.notifier).generateAgain(),
1313
);

lib/features/sorting/comparison/view/comparison_sort_page.dart

Lines changed: 54 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -24,63 +24,65 @@ class ComparisonSortPage extends ConsumerStatefulWidget {
2424

2525
class _ComparisonSortPageState extends ConsumerState<ComparisonSortPage> {
2626
@override
27-
void deactivate() {
28-
ref.invalidate(_notifierProvider); // deletes current instance and resets
29-
30-
ComparisonSortNotifier.sortingAlgorithms.values.toList().forEach(
31-
(element) {
32-
try {
33-
ref.invalidate(element.provider);
34-
} catch (e) {
35-
//
27+
Widget build(BuildContext context) {
28+
return PopScope(
29+
onPopInvokedWithResult: (didPop, result) async {
30+
if (didPop) {
31+
await ref.read(_notifierProvider.notifier).cancelSorting(ref);
32+
33+
ComparisonSortNotifier.sortingAlgorithms.values.toList().forEach(
34+
(element) {
35+
try {
36+
ref.invalidate(element.provider);
37+
} catch (e) {
38+
//
39+
}
40+
},
41+
);
42+
ref.invalidate(_notifierProvider); // deletes current instance and resets
3643
}
3744
},
38-
);
39-
super.deactivate();
40-
}
41-
42-
@override
43-
Widget build(BuildContext context) {
44-
return Scaffold(
45-
appBar: appBar(),
46-
drawer: const _DrawerMenu(),
47-
body: SafeArea(
48-
child: Column(
49-
children: [
50-
const Flexible(child: _BuildComparisonLists()),
51-
const RSizedBox(height: 15),
52-
Padding(
53-
padding: REdgeInsets.symmetric(horizontal: 0),
54-
child: Align(
55-
alignment: AlignmentDirectional.bottomCenter,
56-
child: Column(
57-
mainAxisAlignment: MainAxisAlignment.end,
58-
spacing: 10,
59-
children: [
60-
SortingControlButtons(
61-
playSorting: () => ref.read(_notifierProvider.notifier).playSorting(ref),
62-
stopSorting: () => ref.read(_notifierProvider.notifier).stopSorting(ref),
63-
generateAgain: () => ref.read(_notifierProvider.notifier).generateAgain(ref),
64-
),
65-
SymmetricPadding(
66-
horizontal: 15,
67-
child: Row(
68-
mainAxisAlignment: MainAxisAlignment.spaceBetween,
69-
children: [
70-
SpeedDraggable(
71-
onChanged: (persent) {
72-
ref.read(_notifierProvider.notifier).changeSpeed(persent, ref);
73-
},
74-
),
75-
_SizeDraggable(ref: ref),
76-
],
45+
child: Scaffold(
46+
appBar: appBar(),
47+
drawer: const _DrawerMenu(),
48+
body: SafeArea(
49+
child: Column(
50+
children: [
51+
const Flexible(child: _BuildComparisonLists()),
52+
const RSizedBox(height: 15),
53+
Padding(
54+
padding: REdgeInsets.symmetric(horizontal: 0),
55+
child: Align(
56+
alignment: AlignmentDirectional.bottomCenter,
57+
child: Column(
58+
mainAxisAlignment: MainAxisAlignment.end,
59+
spacing: 10,
60+
children: [
61+
SortingControlButtons(
62+
playSorting: () => ref.read(_notifierProvider.notifier).playSorting(context, ref),
63+
stopSorting: () => ref.read(_notifierProvider.notifier).stopSorting(ref),
64+
generateAgain: () => ref.read(_notifierProvider.notifier).generateAgain(ref),
65+
),
66+
SymmetricPadding(
67+
horizontal: 15,
68+
child: Row(
69+
mainAxisAlignment: MainAxisAlignment.spaceBetween,
70+
children: [
71+
SpeedDraggable(
72+
onChanged: (persent) {
73+
ref.read(_notifierProvider.notifier).changeSpeed(persent, ref);
74+
},
75+
),
76+
_SizeDraggable(ref: ref),
77+
],
78+
),
7779
),
78-
),
79-
],
80+
],
81+
),
8082
),
8183
),
82-
),
83-
],
84+
],
85+
),
8486
),
8587
),
8688
);

lib/features/sorting/comparison/view_model/comparison_sort_notifier.dart

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import 'package:algorithm_visualizer/features/sorting/radix/view_model/radix_sor
1111
import 'package:algorithm_visualizer/features/sorting/selection/view_model/selection_sort_notifier.dart';
1212
import 'package:algorithm_visualizer/features/sorting/shell/view_model/shell_sort_notifier.dart';
1313
import 'package:collection/collection.dart';
14+
import 'package:flutter/cupertino.dart';
1415
import 'package:flutter_riverpod/flutter_riverpod.dart';
1516
part 'comparison_sort_state.dart';
1617

@@ -138,20 +139,27 @@ class ComparisonSortNotifier extends StateNotifier<ComparisonSortingNotifierStat
138139
_setOperation = SortingEnum.none;
139140
}
140141

141-
Future<void> playSorting(WidgetRef ref) async {
142+
Future<void> playSorting(BuildContext context, WidgetRef ref) async {
142143
if (_getOperation == SortingEnum.played) return;
143144

144-
final playSorting = state.selectedAlgorithms.map((e) => ref.read(e.provider.notifier).playSorting());
145+
final playSorting =
146+
state.selectedAlgorithms.map((e) => ref.read(e.provider.notifier).playSorting(context));
145147
_setOperation = SortingEnum.played;
146148

147149
await Future.wait(playSorting.toList());
148-
_setOperation = SortingEnum.none;
150+
if (context.mounted) _setOperation = SortingEnum.none;
149151
}
150152

151-
void stopSorting(WidgetRef ref) {
152-
for (var element in state.selectedAlgorithms) {
153-
ref.read(element.provider.notifier).stopSorting();
154-
}
153+
Future<void> stopSorting(WidgetRef ref) async {
154+
final stopSorting = state.selectedAlgorithms.map((e) => ref.read(e.provider.notifier).stopSorting());
155+
_setOperation = SortingEnum.played;
156+
157+
await Future.wait(stopSorting.toList());
158+
155159
if (_getOperation == SortingEnum.played) _setOperation = SortingEnum.stopped;
156160
}
161+
Future<void> cancelSorting(WidgetRef ref) async {
162+
final cancelSorting = state.selectedAlgorithms.map((e) => ref.read(e.provider.notifier).cancelSorting());
163+
await Future.wait(cancelSorting.toList());
164+
}
157165
}

0 commit comments

Comments
 (0)