Gallery/lib/data_provider/local_data_provider.dart

42 lines
1.0 KiB
Dart
Raw Normal View History

2022-09-25 22:12:57 +00:00
import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:gallery/data_provider/data_provider.dart';
class LocalDataProvider extends DataProvider {
final Future<Directory> initialPath;
2022-09-25 22:12:57 +00:00
LocalDataProvider(this.initialPath);
@override
Future<void> connect() async {}
2022-09-25 22:12:57 +00:00
@override
Future<Folder> listOfFiles({Uri? uri}) async {
final dir = uri != null ? Directory.fromUri(uri) : await initialPath;
2022-09-25 22:12:57 +00:00
final list = dir.listSync();
final List<Item> res = [];
for (var val in list) {
if (validSuffix.any((suff) => val.path.endsWith(suff))) {
res.add(Item(false, val.uri, val.uri.pathSegments.last));
} else if (await FileSystemEntity.isDirectory(val.path)) {
res.add(
Item(true, val.uri, val.uri.pathSegments.reversed.skip(1).first));
}
}
res.sort(
(a, b) => b.isFolder ? 1 : -1,
);
return Folder(res, dir.uri, dir.parent.uri);
}
@override
ImageProvider getImageProvider(Uri uri) {
return FileImage(File.fromUri(uri));
}
}