fix & improve DownloadedPage

This commit is contained in:
wgh136
2024-05-21 14:59:09 +08:00
parent 5ae73bd7c8
commit 1a224114fc
3 changed files with 248 additions and 190 deletions

View File

@@ -92,6 +92,17 @@ class SliverGridDelegateWithFixedHeight extends SliverGridDelegate {
int calcCrossItemsCount(double width) { int calcCrossItemsCount(double width) {
int count = 20; int count = 20;
var itemWidth = width / 20; var itemWidth = width / 20;
if(minCrossAxisExtent == 0) {
count = 1;
itemWidth = width;
while(itemWidth > maxCrossAxisExtent) {
count++;
itemWidth = width / count;
}
return count;
}
while ( while (
!(itemWidth > minCrossAxisExtent && itemWidth < maxCrossAxisExtent)) { !(itemWidth > minCrossAxisExtent && itemWidth < maxCrossAxisExtent)) {
count--; count--;

View File

@@ -368,6 +368,26 @@ class DownloadManager {
} }
} }
Future<void> checkAndClearInvalidItems() async{
var illusts = listAll();
var shouldDelete = <DownloadedIllust>[];
for(var item in illusts) {
var paths = getImagePaths(item.illustId);
var validPaths = <String>[];
for(var path in paths) {
if(await File(path).exists()) {
validPaths.add(path);
}
}
if(validPaths.isEmpty) {
shouldDelete.add(item);
}
}
for(var item in shouldDelete) {
delete(item);
}
}
void resume() { void resume() {
_paused = false; _paused = false;
} }

View File

@@ -7,8 +7,6 @@ import 'package:photo_view/photo_view_gallery.dart';
import 'package:pixes/components/animated_image.dart'; import 'package:pixes/components/animated_image.dart';
import 'package:pixes/components/grid.dart'; import 'package:pixes/components/grid.dart';
import 'package:pixes/components/md.dart'; import 'package:pixes/components/md.dart';
import 'package:pixes/components/message.dart';
import 'package:pixes/components/page_route.dart';
import 'package:pixes/components/title_bar.dart'; import 'package:pixes/components/title_bar.dart';
import 'package:pixes/foundation/app.dart'; import 'package:pixes/foundation/app.dart';
import 'package:pixes/network/download.dart'; import 'package:pixes/network/download.dart';
@@ -33,7 +31,8 @@ class _DownloadedPageState extends State<DownloadedPage> {
void loadData() { void loadData() {
illusts = DownloadManager().listAll(); illusts = DownloadManager().listAll();
flyoutControllers = List.generate(illusts.length, (index) => FlyoutController()); flyoutControllers =
List.generate(illusts.length, (index) => FlyoutController());
} }
@override @override
@@ -46,7 +45,18 @@ class _DownloadedPageState extends State<DownloadedPage> {
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Column( return Column(
children: [ children: [
TitleBar(title: "Downloaded".tl), TitleBar(
title: "Downloaded".tl,
action: Button(
child: Text("Delete Invalid Items".tl),
onPressed: () async {
await DownloadManager().checkAndClearInvalidItems();
setState(() {
loadData();
});
},
),
),
Expanded( Expanded(
child: buildBody(), child: buildBody(),
), ),
@@ -56,135 +66,133 @@ class _DownloadedPageState extends State<DownloadedPage> {
Widget buildBody() { Widget buildBody() {
return GridViewWithFixedItemHeight( return GridViewWithFixedItemHeight(
itemCount: illusts.length, itemCount: illusts.length,
itemHeight: 152, itemHeight: 152,
maxCrossAxisExtent: 742, maxCrossAxisExtent: 742,
builder: (context, index) { builder: (context, index) {
var image = DownloadManager().getImage(illusts[index].illustId, 0); var image = DownloadManager().getImage(illusts[index].illustId, 0);
return Card( return Card(
margin: const EdgeInsets.symmetric(vertical: 4, horizontal: 8), margin: const EdgeInsets.symmetric(vertical: 4, horizontal: 8),
padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 16), padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 16),
child: Row( child: GestureDetector(
children: [ behavior: HitTestBehavior.opaque,
Container( onTap: () {
width: 96, context.to(() => _DownloadedIllustViewPage(
height: double.infinity, DownloadManager().getImagePaths(illusts[index].illustId)));
decoration: BoxDecoration( },
borderRadius: BorderRadius.circular(4), child: Row(
color: ColorScheme.of(context).secondaryContainer children: [
), Container(
clipBehavior: Clip.antiAlias, width: 96,
child: image == null ? null : AnimatedImage( height: double.infinity,
image: FileImage(image), decoration: BoxDecoration(
fit: BoxFit.cover, borderRadius: BorderRadius.circular(4),
width: 96, color: ColorScheme.of(context).secondaryContainer),
height: double.infinity, clipBehavior: Clip.antiAlias,
filterQuality: FilterQuality.medium, child: image == null
), ? null
), : AnimatedImage(
const SizedBox(width: 16), image: FileImage(image),
Expanded( fit: BoxFit.cover,
child: Column( width: 96,
crossAxisAlignment: CrossAxisAlignment.start, height: double.infinity,
children: [ filterQuality: FilterQuality.medium,
Text(
illusts[index].title,
style: const TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
),
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
const SizedBox(height: 4),
Text(
illusts[index].author,
style: const TextStyle(
fontSize: 12,
),
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
Text(
"${illusts[index].imageCount}P",
style: const TextStyle(
fontSize: 12,
),
),
const Spacer(),
Row(
children: [
const Spacer(),
Button(
child: Text("View".tl).fixWidth(42),
onPressed: () {
var images = DownloadManager().getImagePaths(
illusts[index].illustId);
if(images.isEmpty) {
showToast(context, message: "No images found".tl);
return;
}
App.rootNavigatorKey.currentState?.push(
AppPageRoute(builder: (context) {
return _DownloadedIllustViewPage(images);
}));
},
),
const SizedBox(width: 6),
Button(
child: Text("Info".tl).fixWidth(42),
onPressed: () {
context.to(() => IllustPageWithId(
illusts[index].illustId.toString()));
},
),
const SizedBox(width: 6),
FlyoutTarget(
controller: flyoutControllers[index],
child: Button(
child: Text("Delete".tl).fixWidth(42),
onPressed: () {
flyoutControllers[index].showFlyout(
navigatorKey: App.rootNavigatorKey.currentState,
builder: (context) {
return FlyoutContent(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Are you sure you want to delete?'.tl,
style: const TextStyle(fontWeight: FontWeight.bold),
),
const SizedBox(height: 12.0),
Button(
onPressed: () {
Flyout.of(context).close();
DownloadManager().delete(illusts[index]);
setState(() {
illusts.removeAt(index);
flyoutControllers.removeAt(index);
});
},
child: Text('Yes'.tl),
),
],
),
);
});
},
), ),
),
const SizedBox(width: 16),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
illusts[index].title,
style: const TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
),
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
const SizedBox(height: 4),
Text(
illusts[index].author,
style: const TextStyle(
fontSize: 12,
),
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
Text(
"${illusts[index].imageCount}P",
style: const TextStyle(
fontSize: 12,
),
),
const Spacer(),
Row(
children: [
const Spacer(),
Button(
child: Text("Info".tl).fixWidth(42),
onPressed: () {
context.to(() => IllustPageWithId(
illusts[index].illustId.toString()));
},
),
const SizedBox(width: 6),
FlyoutTarget(
controller: flyoutControllers[index],
child: Button(
child: Text("Delete".tl).fixWidth(42),
onPressed: () {
flyoutControllers[index].showFlyout(
navigatorKey:
App.rootNavigatorKey.currentState,
builder: (context) {
return FlyoutContent(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment:
CrossAxisAlignment.start,
children: [
Text(
'Are you sure you want to delete?'
.tl,
style: const TextStyle(
fontWeight:
FontWeight.bold),
),
const SizedBox(height: 12.0),
Button(
onPressed: () {
Flyout.of(context).close();
DownloadManager()
.delete(illusts[index]);
setState(() {
illusts.removeAt(index);
flyoutControllers
.removeAt(index);
});
},
child: Text('Yes'.tl),
),
],
),
);
});
},
),
),
],
), ),
], ],
), ),
], ),
), ],
), ),
], ),
), );
); }).paddingHorizontal(8);
}
).paddingHorizontal(8);
} }
} }
@@ -194,10 +202,12 @@ class _DownloadedIllustViewPage extends StatefulWidget {
final List<String> imagePaths; final List<String> imagePaths;
@override @override
State<_DownloadedIllustViewPage> createState() => _DownloadedIllustViewPageState(); State<_DownloadedIllustViewPage> createState() =>
_DownloadedIllustViewPageState();
} }
class _DownloadedIllustViewPageState extends State<_DownloadedIllustViewPage> with WindowListener{ class _DownloadedIllustViewPageState extends State<_DownloadedIllustViewPage>
with WindowListener {
int windowButtonKey = 0; int windowButtonKey = 0;
@override @override
@@ -234,38 +244,47 @@ class _DownloadedIllustViewPageState extends State<_DownloadedIllustViewPage> wi
Future<File?> getFile() async { Future<File?> getFile() async {
var file = File(widget.imagePaths[currentPage]); var file = File(widget.imagePaths[currentPage]);
if(file.existsSync()) { if (file.existsSync()) {
return file; return file;
} }
return null; return null;
} }
void showMenu() { void showMenu() {
menuController.showFlyout(builder: (context) => MenuFlyout( menuController.showFlyout(
items: [ builder: (context) => MenuFlyout(
MenuFlyoutItem(text: Text("Save to".tl), onPressed: () async{ items: [
var file = await getFile(); MenuFlyoutItem(
if(file != null){ text: Text("Save to".tl),
saveFile(file); onPressed: () async {
} var file = await getFile();
}), if (file != null) {
MenuFlyoutItem(text: Text("Share".tl), onPressed: () async{ saveFile(file);
var file = await getFile(); }
if(file != null){ }),
var ext = file.path.split('.').last; MenuFlyoutItem(
var mediaType = switch(ext){ text: Text("Share".tl),
'jpg' => 'image/jpeg', onPressed: () async {
'jpeg' => 'image/jpeg', var file = await getFile();
'png' => 'image/png', if (file != null) {
'gif' => 'image/gif', var ext = file.path.split('.').last;
'webp' => 'image/webp', var mediaType = switch (ext) {
_ => 'application/octet-stream' 'jpg' => 'image/jpeg',
}; 'jpeg' => 'image/jpeg',
Share.shareXFiles([XFile(file.path, mimeType: mediaType, name: file.path.split('/').last)]); 'png' => 'image/png',
} 'gif' => 'image/gif',
}), 'webp' => 'image/webp',
], _ => 'application/octet-stream'
)); };
Share.shareXFiles([
XFile(file.path,
mimeType: mediaType,
name: file.path.split('/').last)
]);
}
}),
],
));
} }
@override @override
@@ -275,12 +294,13 @@ class _DownloadedIllustViewPageState extends State<_DownloadedIllustViewPage> wi
color: FluentTheme.of(context).micaBackgroundColor, color: FluentTheme.of(context).micaBackgroundColor,
child: Listener( child: Listener(
onPointerSignal: (event) { onPointerSignal: (event) {
if(event is PointerScrollEvent && if (event is PointerScrollEvent &&
!HardwareKeyboard.instance.isControlPressed) { !HardwareKeyboard.instance.isControlPressed) {
if(event.scrollDelta.dy > 0 if (event.scrollDelta.dy > 0 &&
&& controller.page!.toInt() < widget.imagePaths.length - 1) { controller.page!.toInt() < widget.imagePaths.length - 1) {
controller.jumpToPage(controller.page!.toInt() + 1); controller.jumpToPage(controller.page!.toInt() + 1);
} else if(event.scrollDelta.dy < 0 && controller.page!.toInt() > 0){ } else if (event.scrollDelta.dy < 0 &&
controller.page!.toInt() > 0) {
controller.jumpToPage(controller.page!.toInt() - 1); controller.jumpToPage(controller.page!.toInt() - 1);
} }
} }
@@ -290,11 +310,11 @@ class _DownloadedIllustViewPageState extends State<_DownloadedIllustViewPage> wi
var height = constrains.maxHeight; var height = constrains.maxHeight;
return Stack( return Stack(
children: [ children: [
Positioned.fill(child: PhotoViewGallery.builder( Positioned.fill(
child: PhotoViewGallery.builder(
pageController: controller, pageController: controller,
backgroundDecoration: const BoxDecoration( backgroundDecoration:
color: Colors.transparent const BoxDecoration(color: Colors.transparent),
),
itemCount: widget.imagePaths.length, itemCount: widget.imagePaths.length,
builder: (context, index) { builder: (context, index) {
return PhotoViewGalleryPageOptions( return PhotoViewGalleryPageOptions(
@@ -315,17 +335,22 @@ class _DownloadedIllustViewPageState extends State<_DownloadedIllustViewPage> wi
height: 36, height: 36,
child: Row( child: Row(
children: [ children: [
const SizedBox(width: 6,), const SizedBox(
width: 6,
),
IconButton( IconButton(
icon: const Icon(FluentIcons.back).paddingAll(2), icon: const Icon(FluentIcons.back).paddingAll(2),
onPressed: () => context.pop() onPressed: () => context.pop()),
),
const Expanded( const Expanded(
child: DragToMoveArea(child: SizedBox.expand(),), child: DragToMoveArea(
child: SizedBox.expand(),
),
), ),
buildActions(), buildActions(),
if(App.isDesktop) if (App.isDesktop)
WindowButtons(key: ValueKey(windowButtonKey),), WindowButtons(
key: ValueKey(windowButtonKey),
),
], ],
), ),
), ),
@@ -334,7 +359,10 @@ class _DownloadedIllustViewPageState extends State<_DownloadedIllustViewPage> wi
left: 0, left: 0,
top: height / 2 - 9, top: height / 2 - 9,
child: IconButton( child: IconButton(
icon: const Icon(FluentIcons.chevron_left, size: 18,), icon: const Icon(
FluentIcons.chevron_left,
size: 18,
),
onPressed: () { onPressed: () {
controller.previousPage( controller.previousPage(
duration: const Duration(milliseconds: 300), duration: const Duration(milliseconds: 300),
@@ -377,26 +405,25 @@ class _DownloadedIllustViewPageState extends State<_DownloadedIllustViewPage> wi
controller: menuController, controller: menuController,
child: width > 600 child: width > 600
? Button( ? Button(
onPressed: showMenu, onPressed: showMenu,
child: const Row( child: const Row(
children: [ children: [
Icon( Icon(
MdIcons.menu, MdIcons.menu,
size: 18, size: 18,
), ),
SizedBox( SizedBox(
width: 8, width: 8,
), ),
Text('Actions'), Text('Actions'),
], ],
)) ))
: IconButton( : IconButton(
icon: const Icon( icon: const Icon(
MdIcons.more_horiz, MdIcons.more_horiz,
size: 20, size: 20,
), ),
onPressed: showMenu), onPressed: showMenu),
); );
} }
} }