|
| 1 | +import 'package:collection/collection.dart'; |
| 2 | +import 'package:file_picker/file_picker.dart'; |
| 3 | +import 'package:file_selector/file_selector.dart'; |
| 4 | +import 'package:permission_handler/permission_handler.dart'; |
| 5 | + |
| 6 | +import 'platforms.dart'; |
| 7 | + |
| 8 | +class ExternalPathService { |
| 9 | + const ExternalPathService(); |
| 10 | + |
| 11 | + Future<String?> getPathOfDirectory() async { |
| 12 | + if (Platforms.isMobile && await _androidPermissionsGranted()) { |
| 13 | + return FilePicker.platform.getDirectoryPath(); |
| 14 | + } |
| 15 | + |
| 16 | + if (Platforms.isMacOS || Platforms.isLinux || Platforms.isWindows) { |
| 17 | + return getDirectoryPath(); |
| 18 | + } |
| 19 | + return null; |
| 20 | + } |
| 21 | + |
| 22 | + Future<String?> getPathOfFile() async { |
| 23 | + if (Platforms.isMobile && await _androidPermissionsGranted()) { |
| 24 | + return (await FilePicker.platform.pickFiles( |
| 25 | + allowMultiple: false, |
| 26 | + ))?.files.firstOrNull?.path; |
| 27 | + } |
| 28 | + |
| 29 | + if (Platforms.isMacOS || Platforms.isLinux || Platforms.isWindows) { |
| 30 | + return (await openFile())?.path; |
| 31 | + } |
| 32 | + return null; |
| 33 | + } |
| 34 | + |
| 35 | + Future<List<String>> getPathsOfFiles() async { |
| 36 | + if (Platforms.isMobile && await _androidPermissionsGranted()) { |
| 37 | + final filePickerResult = await FilePicker.platform.pickFiles( |
| 38 | + allowMultiple: true, |
| 39 | + ); |
| 40 | + |
| 41 | + if (filePickerResult == null) { |
| 42 | + return []; |
| 43 | + } |
| 44 | + |
| 45 | + return filePickerResult.files |
| 46 | + .map((e) => XFile(e.path!)) |
| 47 | + .map((e) => e.path) |
| 48 | + .toList(); |
| 49 | + } else if (Platforms.isMacOS || Platforms.isLinux || Platforms.isWindows) { |
| 50 | + return (await openFiles()).map((e) => e.path).toList(); |
| 51 | + } |
| 52 | + return []; |
| 53 | + } |
| 54 | + |
| 55 | + Future<bool> _androidPermissionsGranted() async => |
| 56 | + (await Permission.audio |
| 57 | + .onDeniedCallback(() {}) |
| 58 | + .onGrantedCallback(() {}) |
| 59 | + .onPermanentlyDeniedCallback(() {}) |
| 60 | + .onRestrictedCallback(() {}) |
| 61 | + .onLimitedCallback(() {}) |
| 62 | + .onProvisionalCallback(() {}) |
| 63 | + .request()) |
| 64 | + .isGranted; |
| 65 | +} |
0 commit comments