simplified code across the app and consolidated duplicated helpers
This commit is contained in:
@@ -28,18 +28,16 @@ class FilesBloc
|
||||
@override
|
||||
Map<String, dynamic>? toStorage(FilesState state) => null;
|
||||
|
||||
List<String> get _currentPath => innerState?.currentPath ?? initialPath;
|
||||
|
||||
@override
|
||||
Future<void> gatherData() async {
|
||||
final path = innerState?.currentPath ?? initialPath;
|
||||
await _query(path);
|
||||
}
|
||||
Future<void> gatherData() => _query(_currentPath);
|
||||
|
||||
Future<void> refresh() async {
|
||||
add(RefetchStarted<FilesState>());
|
||||
final path = innerState?.currentPath ?? initialPath;
|
||||
// Explicit user action — bypass the cache TTL so the root listing also
|
||||
// refetches even though it is otherwise cached for a day.
|
||||
await _query(path, renew: true);
|
||||
await _query(_currentPath, renew: true);
|
||||
}
|
||||
|
||||
/// Pull-to-refresh and error-screen retry route through here. Unlike the
|
||||
@@ -57,13 +55,15 @@ class FilesBloc
|
||||
}
|
||||
|
||||
Future<void> createFolder(String name) async {
|
||||
final path = innerState?.currentPath ?? initialPath;
|
||||
await repo.data.createFolder('${path.join('/')}/$name');
|
||||
await repo.data.createFolder('${_currentPath.join('/')}/$name');
|
||||
await refresh();
|
||||
}
|
||||
|
||||
Future<void> _query(List<String> path, {bool renew = false}) async {
|
||||
final pathString = path.isEmpty ? '/' : path.join('/');
|
||||
// The listing contains the folder itself; only its children are shown.
|
||||
void dropSelf(ListFilesResponse listing) => listing.files.removeWhere(
|
||||
(file) => file.name.isEmpty || file.name == path.lastOrNull,
|
||||
);
|
||||
|
||||
// Drop late results when [setPath] has navigated elsewhere or when the
|
||||
// bloc has been disposed (e.g. share-flow picker closed mid-fetch). Both
|
||||
@@ -80,16 +80,14 @@ class FilesBloc
|
||||
ListFilesResponse? listing;
|
||||
try {
|
||||
listing = await repo.data.listFiles(
|
||||
pathString,
|
||||
path.isEmpty ? '/' : path.join('/'),
|
||||
renew: renew,
|
||||
onCacheData: (cached) {
|
||||
if (isStale()) return;
|
||||
// Cached payload arrives before the network call settles. Surface it
|
||||
// immediately via Emit so the listing is visible while isLoading
|
||||
// stays true and the top loading bar keeps spinning.
|
||||
cached.files.removeWhere(
|
||||
(file) => file.name.isEmpty || file.name == path.lastOrNull,
|
||||
);
|
||||
dropSelf(cached);
|
||||
add(Emit((s) => s.copyWith(listing: cached)));
|
||||
},
|
||||
onRetry: (next, max) {
|
||||
@@ -105,9 +103,7 @@ class FilesBloc
|
||||
if (isStale()) return;
|
||||
|
||||
if (listing != null) {
|
||||
listing.files.removeWhere(
|
||||
(file) => file.name.isEmpty || file.name == path.lastOrNull,
|
||||
);
|
||||
dropSelf(listing);
|
||||
add(DataGathered((s) => s.copyWith(listing: listing)));
|
||||
}
|
||||
if (capturedError != null) addLoadingError(capturedError!);
|
||||
|
||||
@@ -3,10 +3,5 @@ import '../bloc/files_state.dart';
|
||||
import '../data_provider/files_data_provider.dart';
|
||||
|
||||
class FilesRepository extends Repository<FilesState> {
|
||||
final FilesDataProvider _provider;
|
||||
|
||||
FilesRepository([FilesDataProvider? provider])
|
||||
: _provider = provider ?? FilesDataProvider();
|
||||
|
||||
FilesDataProvider get data => _provider;
|
||||
final FilesDataProvider data = FilesDataProvider();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user