Feat/saf (#81)

* [Android] Use SAF to change local path

* Use IOOverrides to replace openDirectoryPlatform and openFilePlatform

* fix io
This commit is contained in:
nyne
2024-11-29 21:33:28 +08:00
parent 06094fc5fc
commit 72507d907a
14 changed files with 147 additions and 134 deletions

View File

@@ -71,7 +71,7 @@ class LocalComic with HistoryMixin implements Comic {
downloadedChapters = List.from(jsonDecode(row[8] as String)),
createdAt = DateTime.fromMillisecondsSinceEpoch(row[9] as int);
File get coverFile => openFilePlatform(FilePath.join(
File get coverFile => File(FilePath.join(
baseDir,
cover,
));
@@ -151,6 +151,8 @@ class LocalManager with ChangeNotifier {
/// path to the directory where all the comics are stored
late String path;
Directory get directory => Directory(path);
// return error message if failed
Future<String?> setNewPath(String newPath) async {
var newDir = Directory(newPath);
@@ -162,7 +164,7 @@ class LocalManager with ChangeNotifier {
}
try {
await copyDirectoryIsolate(
Directory(path),
directory,
newDir,
);
await File(FilePath.join(App.dataPath, 'local_path')).writeAsString(newPath);
@@ -170,7 +172,7 @@ class LocalManager with ChangeNotifier {
Log.error("IO", e, s);
return e.toString();
}
await Directory(path).deleteIgnoreError(recursive:true);
await directory.deleteContents(recursive: true);
path = newPath;
return null;
}
@@ -217,15 +219,15 @@ class LocalManager with ChangeNotifier {
''');
if (File(FilePath.join(App.dataPath, 'local_path')).existsSync()) {
path = File(FilePath.join(App.dataPath, 'local_path')).readAsStringSync();
if (!Directory(path).existsSync()) {
if (!directory.existsSync()) {
path = await findDefaultPath();
}
} else {
path = await findDefaultPath();
}
try {
if (!Directory(path).existsSync()) {
await Directory(path).create();
if (!directory.existsSync()) {
await directory.create();
}
}
catch(e, s) {
@@ -354,12 +356,12 @@ class LocalManager with ChangeNotifier {
throw "Invalid ep";
}
var comic = find(id, type) ?? (throw "Comic Not Found");
var directory = openDirectoryPlatform(comic.baseDir);
var directory = Directory(comic.baseDir);
if (comic.chapters != null) {
var cid = ep is int
? comic.chapters!.keys.elementAt(ep - 1)
: (ep as String);
directory = openDirectoryPlatform(FilePath.join(directory.path, cid));
directory = Directory(FilePath.join(directory.path, cid));
}
var files = <File>[];
await for (var entity in directory.list()) {
@@ -406,10 +408,10 @@ class LocalManager with ChangeNotifier {
String id, ComicType type, String name) async {
var comic = find(id, type);
if (comic != null) {
return openDirectoryPlatform(FilePath.join(path, comic.directory));
return Directory(FilePath.join(path, comic.directory));
}
var dir = findValidDirectoryName(path, name);
return openDirectoryPlatform(FilePath.join(path, dir)).create().then((value) => value);
return Directory(FilePath.join(path, dir)).create().then((value) => value);
}
void completeTask(DownloadTask task) {
@@ -468,7 +470,7 @@ class LocalManager with ChangeNotifier {
void deleteComic(LocalComic c, [bool removeFileOnDisk = true]) {
if(removeFileOnDisk) {
var dir = openDirectoryPlatform(FilePath.join(path, c.directory));
var dir = Directory(FilePath.join(path, c.directory));
dir.deleteIgnoreError(recursive: true);
}
//Deleting a local comic means that it's nolonger available, thus both favorite and history should be deleted.