98 lines
3.2 KiB
Dart
98 lines
3.2 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:device_info_plus/device_info_plus.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
|
|
final fileRepositoryProvider = Provider.autoDispose((ref) {
|
|
final repository = FileRepository();
|
|
ref.onDispose(repository.release);
|
|
return repository;
|
|
});
|
|
|
|
final fileStorageProvider = FutureProvider.autoDispose<String?>((ref) {
|
|
return ref.watch(fileRepositoryProvider).requestStorage();
|
|
});
|
|
|
|
final directoryEntriesProvider = FutureProvider.autoDispose
|
|
.family<List<FileSystemEntity>, String>((ref, path) {
|
|
return ref.watch(fileRepositoryProvider).list(path);
|
|
});
|
|
|
|
/// 本机共享存储目录。系统保护目录的访问限制由 Android 执行。
|
|
class FileRepository {
|
|
static const _channel = MethodChannel('com.ttstd.familycare/app');
|
|
|
|
Future<String?> requestStorage() async {
|
|
if (Platform.isIOS) {
|
|
return _channel.invokeMethod<String>('chooseFileDirectory');
|
|
}
|
|
if (!Platform.isAndroid) return null;
|
|
final sdk = (await DeviceInfoPlugin().androidInfo).version.sdkInt;
|
|
final permission = sdk >= 30
|
|
? Permission.manageExternalStorage
|
|
: Permission.storage;
|
|
if (await permission.isPermanentlyDenied) {
|
|
await openAppSettings();
|
|
return null;
|
|
}
|
|
if (!await permission.isGranted &&
|
|
!(await permission.request()).isGranted) {
|
|
return null;
|
|
}
|
|
return _channel.invokeMethod<String>('externalStorageRoot');
|
|
}
|
|
|
|
Future<List<FileSystemEntity>> list(String path) async {
|
|
final List<FileSystemEntity> entries;
|
|
if (Platform.isIOS) {
|
|
final data = await _channel.invokeListMethod<dynamic>(
|
|
'listFileDirectory',
|
|
path,
|
|
);
|
|
entries = (data ?? []).map<FileSystemEntity>((item) {
|
|
final entry = Map<String, dynamic>.from(item as Map);
|
|
final path = entry['path'] as String;
|
|
return entry['directory'] == true ? Directory(path) : File(path);
|
|
}).toList();
|
|
} else {
|
|
entries = await Directory(path).list(followLinks: false).toList();
|
|
}
|
|
// 不跟随符号链接,避免目录导航跳出共享存储。
|
|
entries.removeWhere((entry) => entry is Link);
|
|
entries.sort((left, right) {
|
|
if (left is Directory && right is! Directory) return -1;
|
|
if (left is! Directory && right is Directory) return 1;
|
|
return left.uri.pathSegments
|
|
.where((s) => s.isNotEmpty)
|
|
.last
|
|
.toLowerCase()
|
|
.compareTo(
|
|
right.uri.pathSegments
|
|
.where((s) => s.isNotEmpty)
|
|
.last
|
|
.toLowerCase(),
|
|
);
|
|
});
|
|
return entries;
|
|
}
|
|
|
|
Future<bool> open(String path) async {
|
|
return await _channel.invokeMethod<bool>('openLocalFile', path) ?? false;
|
|
}
|
|
|
|
/// Present the platform sharesheet; cancellation after presentation is not an error.
|
|
Future<bool> share(String path) async {
|
|
return await _channel.invokeMethod<bool>('shareLocalFile', path) ?? false;
|
|
}
|
|
|
|
void release() {
|
|
if (Platform.isIOS) {
|
|
_channel
|
|
.invokeMethod<void>('releaseFileDirectory')
|
|
.catchError((Object _) {});
|
|
}
|
|
}
|
|
}
|