fix deleting local comic, favorites

This commit is contained in:
nyne
2024-10-27 16:03:46 +08:00
parent 35fb5ec752
commit fa39bdf3eb
9 changed files with 160 additions and 36 deletions

View File

@@ -3,6 +3,7 @@ import 'package:venera/foundation/appdata.dart';
import 'dart:io';
import 'app.dart';
import 'comic_source/comic_source.dart';
import 'comic_type.dart';
String _getCurTime() {
@@ -12,11 +13,13 @@ String _getCurTime() {
.substring(0, 19);
}
class FavoriteItem {
class FavoriteItem implements Comic {
String name;
String author;
ComicType type;
@override
List<String> tags;
@override
String id;
String coverPath;
String time = _getCurTime();
@@ -57,6 +60,38 @@ class FavoriteItem {
}
return s;
}
@override
String get cover => coverPath;
@override
String get description => "$time | ${type.comicSource?.name ?? "Unknown"}";
@override
String? get favoriteId => null;
@override
String? get language => null;
@override
int? get maxPage => null;
@override
String get sourceKey => type.comicSource?.key ?? "Unknown:${type.value}";
@override
double? get stars => null;
@override
String? get subtitle => author;
@override
String get title => name;
@override
Map<String, dynamic> toJson() {
throw UnimplementedError();
}
}
class FavoriteItemWithFolderInfo {

View File

@@ -0,0 +1,53 @@
import 'dart:async' show Future, StreamController;
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:venera/foundation/app.dart';
import 'package:venera/foundation/comic_source/comic_source.dart';
import 'package:venera/network/images.dart';
import 'package:venera/utils/io.dart';
import 'base_image_provider.dart';
import 'local_favorite_image.dart' as image_provider;
class LocalFavoriteImageProvider
extends BaseImageProvider<image_provider.LocalFavoriteImageProvider> {
/// Image provider for normal image.
const LocalFavoriteImageProvider(this.url, this.id, this.intKey);
final String url;
final String id;
final int intKey;
@override
Future<Uint8List> load(StreamController<ImageChunkEvent> chunkEvents) async {
var sourceKey = ComicSource.fromIntKey(intKey)?.key;
var fileName = key.hashCode.toString();
var file = File(FilePath.join(App.dataPath, 'favorite_cover', fileName));
if (await file.exists()) {
return await file.readAsBytes();
} else {
await file.create(recursive: true);
}
await for (var progress in ImageDownloader.loadThumbnail(url, sourceKey)) {
chunkEvents.add(ImageChunkEvent(
cumulativeBytesLoaded: progress.currentBytes,
expectedTotalBytes: progress.totalBytes,
));
if(progress.imageBytes != null) {
var data = progress.imageBytes!;
await file.writeAsBytes(data);
return data;
}
}
throw "Error: Empty response body.";
}
@override
Future<LocalFavoriteImageProvider> obtainKey(ImageConfiguration configuration) {
return SynchronousFuture(this);
}
@override
String get key => id + intKey.toString();
}

View File

@@ -26,7 +26,7 @@ class LocalComic with HistoryMixin implements Comic {
@override
final List<String> tags;
/// name of the directory, which is in `LocalManager.path`
/// The name of the directory where the comic is stored
final String directory;
/// key: chapter id, value: chapter title
@@ -143,6 +143,7 @@ class LocalManager with ChangeNotifier {
late Database _db;
/// path to the directory where all the comics are stored
late String path;
// return error message if failed
@@ -413,4 +414,11 @@ class LocalManager with ChangeNotifier {
saveCurrentDownloadingTasks();
downloadingTasks.first.resume();
}
void deleteComic(LocalComic c) {
var dir = Directory(FilePath.join(path, c.directory));
dir.deleteSync(recursive: true);
remove(c.id, c.comicType);
notifyListeners();
}
}