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(),
), ),
@@ -64,6 +74,12 @@ class _DownloadedPageState extends State<DownloadedPage> {
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: GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () {
context.to(() => _DownloadedIllustViewPage(
DownloadManager().getImagePaths(illusts[index].illustId)));
},
child: Row( child: Row(
children: [ children: [
Container( Container(
@@ -71,10 +87,11 @@ class _DownloadedPageState extends State<DownloadedPage> {
height: double.infinity, height: double.infinity,
decoration: BoxDecoration( decoration: BoxDecoration(
borderRadius: BorderRadius.circular(4), borderRadius: BorderRadius.circular(4),
color: ColorScheme.of(context).secondaryContainer color: ColorScheme.of(context).secondaryContainer),
),
clipBehavior: Clip.antiAlias, clipBehavior: Clip.antiAlias,
child: image == null ? null : AnimatedImage( child: image == null
? null
: AnimatedImage(
image: FileImage(image), image: FileImage(image),
fit: BoxFit.cover, fit: BoxFit.cover,
width: 96, width: 96,
@@ -115,22 +132,6 @@ class _DownloadedPageState extends State<DownloadedPage> {
Row( Row(
children: [ children: [
const Spacer(), 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( Button(
child: Text("Info".tl).fixWidth(42), child: Text("Info".tl).fixWidth(42),
onPressed: () { onPressed: () {
@@ -145,25 +146,32 @@ class _DownloadedPageState extends State<DownloadedPage> {
child: Text("Delete".tl).fixWidth(42), child: Text("Delete".tl).fixWidth(42),
onPressed: () { onPressed: () {
flyoutControllers[index].showFlyout( flyoutControllers[index].showFlyout(
navigatorKey: App.rootNavigatorKey.currentState, navigatorKey:
App.rootNavigatorKey.currentState,
builder: (context) { builder: (context) {
return FlyoutContent( return FlyoutContent(
child: Column( child: Column(
mainAxisSize: MainAxisSize.min, mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment:
CrossAxisAlignment.start,
children: [ children: [
Text( Text(
'Are you sure you want to delete?'.tl, 'Are you sure you want to delete?'
style: const TextStyle(fontWeight: FontWeight.bold), .tl,
style: const TextStyle(
fontWeight:
FontWeight.bold),
), ),
const SizedBox(height: 12.0), const SizedBox(height: 12.0),
Button( Button(
onPressed: () { onPressed: () {
Flyout.of(context).close(); Flyout.of(context).close();
DownloadManager().delete(illusts[index]); DownloadManager()
.delete(illusts[index]);
setState(() { setState(() {
illusts.removeAt(index); illusts.removeAt(index);
flyoutControllers.removeAt(index); flyoutControllers
.removeAt(index);
}); });
}, },
child: Text('Yes'.tl), child: Text('Yes'.tl),
@@ -182,9 +190,9 @@ class _DownloadedPageState extends State<DownloadedPage> {
), ),
], ],
), ),
),
); );
} }).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
@@ -241,15 +251,20 @@ class _DownloadedIllustViewPageState extends State<_DownloadedIllustViewPage> wi
} }
void showMenu() { void showMenu() {
menuController.showFlyout(builder: (context) => MenuFlyout( menuController.showFlyout(
builder: (context) => MenuFlyout(
items: [ items: [
MenuFlyoutItem(text: Text("Save to".tl), onPressed: () async{ MenuFlyoutItem(
text: Text("Save to".tl),
onPressed: () async {
var file = await getFile(); var file = await getFile();
if (file != null) { if (file != null) {
saveFile(file); saveFile(file);
} }
}), }),
MenuFlyoutItem(text: Text("Share".tl), onPressed: () async{ MenuFlyoutItem(
text: Text("Share".tl),
onPressed: () async {
var file = await getFile(); var file = await getFile();
if (file != null) { if (file != null) {
var ext = file.path.split('.').last; var ext = file.path.split('.').last;
@@ -261,7 +276,11 @@ class _DownloadedIllustViewPageState extends State<_DownloadedIllustViewPage> wi
'webp' => 'image/webp', 'webp' => 'image/webp',
_ => 'application/octet-stream' _ => 'application/octet-stream'
}; };
Share.shareXFiles([XFile(file.path, mimeType: mediaType, name: file.path.split('/').last)]); Share.shareXFiles([
XFile(file.path,
mimeType: mediaType,
name: file.path.split('/').last)
]);
} }
}), }),
], ],
@@ -277,10 +296,11 @@ class _DownloadedIllustViewPageState extends State<_DownloadedIllustViewPage> wi
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),
@@ -399,4 +427,3 @@ class _DownloadedIllustViewPageState extends State<_DownloadedIllustViewPage> wi
); );
} }
} }