Skip to content

Commit a73c899

Browse files
committed
Handle cancellation pipeline properly for dir fetch
Also remove progress display for dir fetch, it currently doesn't work
1 parent f2fe24b commit a73c899

File tree

4 files changed

+36
-133
lines changed

4 files changed

+36
-133
lines changed

lib/backend/fetch.dart

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,24 @@ import 'package:flutter/foundation.dart';
99
enum SortType { name, modified, type, size }
1010

1111
class CancelableFsFetch {
12-
final fs.File source;
13-
final ValueChanged<List<EntityInfo>?> onFetched;
14-
final VoidCallback? onCancel;
15-
final ValueChanged<double?>? onProgressChange;
16-
final bool ascending;
17-
final SortType sortType;
18-
final bool showHidden;
19-
2012
CancelableFsFetch({
2113
required this.source,
2214
required this.onFetched,
2315
this.onCancel,
24-
this.onProgressChange,
2516
this.ascending = false,
2617
this.sortType = SortType.name,
2718
this.showHidden = false,
2819
});
2920

21+
final fs.File source;
22+
final ValueChanged<List<EntityInfo>?> onFetched;
23+
final VoidCallback? onCancel;
24+
final bool ascending;
25+
final SortType sortType;
26+
final bool showHidden;
27+
3028
final fs.Cancellable cancellable = fs.Cancellable();
29+
final Completer<void> _cancellableCompleter = Completer();
3130

3231
bool get cancelled => cancellable.isCancelled;
3332

@@ -40,20 +39,23 @@ class CancelableFsFetch {
4039
);
4140
final enumerator = await enumeratorOp.result;
4241

43-
onProgressChange?.call(0.0);
44-
45-
// final directories = SortedList<EntityInfo>(
46-
// (a, b) => _sort(a, b, isDirectory: true)!,
47-
// );
48-
// final files = SortedList<EntityInfo>(
49-
// (a, b) => _sort(a, b, isDirectory: false)!,
50-
// );
5142
final directories = <EntityInfo>[];
5243
final files = <EntityInfo>[];
5344

5445
while (true) {
55-
final enumerateOp = enumerator.enumerate(cancellable: cancellable);
56-
final fileList = await enumerateOp.result;
46+
final fs.FileList? fileList;
47+
try {
48+
final enumerateOp = enumerator.enumerate(cancellable: cancellable);
49+
fileList = await enumerateOp.result;
50+
} on fs.NativeException catch (e) {
51+
// G_IO_CANCELLED
52+
if (e.code == 19) {
53+
_cancellableCompleter.complete();
54+
return;
55+
}
56+
57+
rethrow;
58+
}
5759

5860
if (fileList == null) break;
5961

@@ -75,6 +77,9 @@ class CancelableFsFetch {
7577
}
7678
fileList.destroy();
7779
}
80+
81+
_cancellableCompleter.complete();
82+
7883
directories.sort((a, b) => _sort(a, b, isDirectory: true)!);
7984
files.sort((a, b) => _sort(a, b, isDirectory: false)!);
8085

@@ -112,10 +117,11 @@ class CancelableFsFetch {
112117
return null;
113118
}
114119

115-
void cancel() async {
120+
Future<void> cancel() async {
116121
if (cancellable.isCancelled) return;
117122

118123
cancellable.cancel();
124+
return _cancellableCompleter.future;
119125
}
120126
}
121127

lib/backend/workspace.dart

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ class WorkspaceController with ChangeNotifier {
2626
// Loading related
2727
late String _currentDir;
2828
List<EntityInfo>? _currentInfo;
29-
double? _loadingProgress;
3029
OSError? _lastError;
3130

3231
// Sort, view and selection
@@ -55,19 +54,15 @@ class WorkspaceController with ChangeNotifier {
5554
}
5655

5756
Future<void> getInfoForDir(fs.File dir) async {
58-
/* await */
59-
_fetcher?.cancel();
57+
await _fetcher?.cancel();
6058
_lastError = null;
59+
_fetcher?.cancellable.destroy();
6160
_fetcher = CancelableFsFetch(
6261
source: dir,
6362
onFetched: (data) {
6463
_currentInfo = data;
6564
notifyListeners();
6665
},
67-
onProgressChange: (value) {
68-
_loadingProgress = value;
69-
notifyListeners();
70-
},
7166
showHidden: _showHidden,
7267
ascending: _ascending,
7368
sortType: _sortType,
@@ -77,7 +72,6 @@ class WorkspaceController with ChangeNotifier {
7772

7873
List<EntityInfo>? get currentInfo =>
7974
_currentInfo != null ? List.unmodifiable(_currentInfo!) : null;
80-
double? get loadingProgress => _loadingProgress;
8175

8276
void clearCurrentInfo() {
8377
_currentInfo = null;

lib/widgets/breadcrumbs_bar.dart

Lines changed: 8 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ class BreadcrumbsBar extends StatefulWidget {
1313
this.onPathSubmitted,
1414
this.leading,
1515
this.actions,
16-
this.loadingProgress,
1716
super.key,
1817
});
1918

@@ -22,7 +21,6 @@ class BreadcrumbsBar extends StatefulWidget {
2221
final ValueChanged<String>? onPathSubmitted;
2322
final List<Widget>? leading;
2423
final List<Widget>? actions;
25-
final double? loadingProgress;
2624

2725
@override
2826
State<BreadcrumbsBar> createState() => _BreadcrumbsBarState();
@@ -86,17 +84,14 @@ class _BreadcrumbsBarState extends State<BreadcrumbsBar> {
8684
: BorderSide.none,
8785
),
8886
clipBehavior: Clip.antiAlias,
89-
child: _LoadingIndicator(
90-
progress: widget.loadingProgress,
91-
child: GestureDetector(
92-
onTap: () {
93-
FocusScope.of(context).requestFocus(focusNode);
94-
},
95-
child: Container(
96-
height: double.infinity,
97-
alignment: AlignmentDirectional.centerStart,
98-
child: _guts,
99-
),
87+
child: GestureDetector(
88+
onTap: () {
89+
FocusScope.of(context).requestFocus(focusNode);
90+
},
91+
child: Container(
92+
height: double.infinity,
93+
alignment: AlignmentDirectional.centerStart,
94+
child: _guts,
10095
),
10196
),
10297
),
@@ -219,94 +214,3 @@ class _BreadcrumbChip extends StatelessWidget {
219214
);
220215
}
221216
}
222-
223-
class _LoadingIndicator extends StatefulWidget {
224-
const _LoadingIndicator({required this.progress, required this.child});
225-
final double? progress;
226-
final Widget child;
227-
228-
@override
229-
_LoadingIndicatorState createState() => _LoadingIndicatorState();
230-
}
231-
232-
class _LoadingIndicatorState extends State<_LoadingIndicator>
233-
with TickerProviderStateMixin {
234-
late AnimationController fadeController;
235-
late AnimationController progressController;
236-
237-
@override
238-
void initState() {
239-
super.initState();
240-
fadeController = AnimationController(
241-
vsync: this,
242-
duration: const Duration(milliseconds: 250),
243-
value: widget.progress != null ? 1 : 0,
244-
);
245-
progressController = AnimationController(
246-
vsync: this,
247-
duration: const Duration(milliseconds: 150),
248-
value: widget.progress,
249-
);
250-
}
251-
252-
@override
253-
void dispose() {
254-
fadeController.dispose();
255-
progressController.dispose();
256-
super.dispose();
257-
}
258-
259-
@override
260-
void didUpdateWidget(covariant _LoadingIndicator old) {
261-
super.didUpdateWidget(old);
262-
263-
_updateController(old);
264-
}
265-
266-
Future<void> _updateController(_LoadingIndicator old) async {
267-
if (widget.progress != old.progress) {
268-
if (widget.progress != null && old.progress == null) {
269-
fadeController.value = 1;
270-
await progressController.animateTo(widget.progress!);
271-
} else if (widget.progress == null && old.progress != null) {
272-
await fadeController.reverse();
273-
progressController.value = 0;
274-
} else if (widget.progress != null && old.progress != null) {
275-
if (widget.progress! > old.progress!) {
276-
await progressController.animateTo(widget.progress!);
277-
} else if (widget.progress! < old.progress!) {
278-
await progressController.animateBack(widget.progress!);
279-
}
280-
}
281-
}
282-
}
283-
284-
@override
285-
Widget build(BuildContext context) {
286-
return AnimatedBuilder(
287-
animation: Listenable.merge([progressController, fadeController]),
288-
builder: (context, child) {
289-
return Stack(
290-
children: [
291-
child!,
292-
Positioned.directional(
293-
textDirection: Directionality.of(context),
294-
top: 12,
295-
bottom: 12,
296-
end: 12,
297-
width: 16,
298-
child: FadeTransition(
299-
opacity: fadeController,
300-
child: CircularProgressIndicator(
301-
value: progressController.value,
302-
strokeWidth: 2,
303-
),
304-
),
305-
),
306-
],
307-
);
308-
},
309-
child: widget.child,
310-
);
311-
}
312-
}

lib/widgets/workspace.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,6 @@ class _WorkspaceTopbar extends StatelessWidget {
448448
},
449449
),
450450
],
451-
loadingProgress: controller.loadingProgress,
452451
);
453452
}
454453

0 commit comments

Comments
 (0)