refactor ui resource list

This commit is contained in:
Simon Ding
2024-08-03 23:05:17 +08:00
parent 6f80da779b
commit 56d5cdb2bf
3 changed files with 119 additions and 140 deletions

View File

@@ -3,9 +3,8 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:ui/providers/activity.dart';
import 'package:ui/providers/series_details.dart';
import 'package:ui/widgets/detail_card.dart';
import 'package:ui/widgets/utils.dart';
import 'package:ui/widgets/resource_list.dart';
import 'package:ui/widgets/progress_indicator.dart';
import 'package:ui/widgets/widgets.dart';
class MovieDetailsPage extends ConsumerStatefulWidget {
static const route = "/movie/:id";
@@ -125,59 +124,7 @@ class _NestedTabBarState extends ConsumerState<NestedTabBar>
error: (error, trace) => Text("$error"),
loading: () => const MyProgressIndicator());
} else {
return Consumer(
builder: (context, ref, child) {
var torrents = ref.watch(mediaTorrentsDataProvider(
(mediaId: widget.id, seasonNumber: 0, episodeNumber: 0)));
return torrents.when(
data: (v) {
if (v.isEmpty) {
return const Center(
child: Text("无可用资源"),
);
}
return DataTable(
columns: const [
DataColumn(label: Text("名称")),
DataColumn(label: Text("大小")),
DataColumn(label: Text("seeders")),
DataColumn(label: Text("peers")),
DataColumn(label: Text("操作"))
],
rows: List.generate(v.length, (i) {
final torrent = v[i];
return DataRow(cells: [
DataCell(Text("${torrent.name}")),
DataCell(
Text("${torrent.size?.readableFileSize()}")),
DataCell(Text("${torrent.seeders}")),
DataCell(Text("${torrent.peers}")),
DataCell(IconButton(
icon: const Icon(Icons.download),
onPressed: () {
final f = ref
.read(mediaTorrentsDataProvider((
mediaId: widget.id,
seasonNumber: 0,
episodeNumber: 0
)).notifier)
.download(torrent)
.then((v) => showSnakeBar(
"开始下载:${torrent.name}"));
// .onError((error, trace) =>
// Utils.showSnakeBar("操作失败: $error"));
showLoadingWithFuture(f);
},
))
]);
}),
);
},
error: (error, trace) => Text("$error"),
loading: () => const MyProgressIndicator());
},
);
return ResourceList(mediaId: widget.id);
}
})
],

View File

@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:ui/providers/series_details.dart';
import 'package:ui/widgets/detail_card.dart';
import 'package:ui/widgets/resource_list.dart';
import 'package:ui/widgets/utils.dart';
import 'package:ui/widgets/progress_indicator.dart';
import 'package:ui/widgets/widgets.dart';
@@ -106,10 +107,13 @@ class _TvDetailsPageState extends ConsumerState<TvDetailsPage> {
const SizedBox(
width: 10,
),
Tooltip(message: "查看可用资源",child: IconButton(
onPressed: () => showAvailableTorrents(widget.seriesId,
ep.seasonNumber ?? 0, ep.episodeNumber ?? 0),
icon: const Icon(Icons.manage_search)),)
Tooltip(
message: "查看可用资源",
child: IconButton(
onPressed: () => showAvailableTorrents(widget.seriesId,
ep.seasonNumber ?? 0, ep.episodeNumber ?? 0),
icon: const Icon(Icons.manage_search)),
)
],
))
]);
@@ -154,10 +158,13 @@ class _TvDetailsPageState extends ConsumerState<TvDetailsPage> {
const SizedBox(
width: 10,
),
Tooltip(message: "查看可用资源",child: IconButton(
onPressed: () =>
showAvailableTorrents(widget.seriesId, k, 0),
icon: const Icon(Icons.manage_search)),)
Tooltip(
message: "查看可用资源",
child: IconButton(
onPressed: () =>
showAvailableTorrents(widget.seriesId, k, 0),
icon: const Icon(Icons.manage_search)),
)
],
))
], rows: m[k]!),
@@ -185,84 +192,21 @@ class _TvDetailsPageState extends ConsumerState<TvDetailsPage> {
context: context,
barrierDismissible: true,
builder: (BuildContext context) {
return Consumer(builder: (context, ref, _) {
final torrents = ref.watch(mediaTorrentsDataProvider(
(mediaId: id, seasonNumber: season, episodeNumber: episode)));
return AlertDialog(
//title: Text("资源"),
content: SelectionArea(
child: SizedBox(
width: MediaQuery.of(context).size.width * 0.7,
height: MediaQuery.of(context).size.height * 0.6,
child: torrents.when(
data: (v) {
bool hasPrivate = false;
for (final item in v) {
if (item.isPrivate == true) {
hasPrivate = true;
}
}
final columns = [
const DataColumn(label: Text("名称")),
const DataColumn(label: Text("大小")),
const DataColumn(label: Text("S/P")),
const DataColumn(label: Text("来源")),
];
if (hasPrivate) {
columns.add(const DataColumn(label: Text("消耗")));
}
columns.add(const DataColumn(label: Text("下载")));
return SingleChildScrollView(
child: DataTable(
dataTextStyle: const TextStyle(fontSize: 12),
columns: columns,
rows: List.generate(v.length, (i) {
final torrent = v[i];
final rows = [
DataCell(Text("${torrent.name}")),
DataCell(Text(
"${torrent.size?.readableFileSize()}")),
DataCell(Text(
"${torrent.seeders}/${torrent.peers}")),
DataCell(Text(torrent.source ?? "-")),
];
if (hasPrivate) {
rows.add(DataCell(Text(torrent.isPrivate == true
? "${torrent.downloadFactor}dl/${torrent.uploadFactor}up"
: "-")));
}
rows.add(DataCell(IconButton(
icon: const Icon(Icons.download),
onPressed: () async {
var f = ref
.read(mediaTorrentsDataProvider((
mediaId: id,
seasonNumber: season,
episodeNumber: episode
)).notifier)
.download(torrent)
.then((v) =>
showSnakeBar("开始下载:${torrent.name}"));
showLoadingWithFuture(f);
},
)));
return DataRow(cells: rows);
})));
},
error: (err, trace) {
return "$err".contains("no resource found")
? const Center(
child: Text("没有资源"),
)
: Text("$err");
},
loading: () => const MyProgressIndicator()),
return AlertDialog(
//title: Text("资源"),
content: SelectionArea(
child: SizedBox(
width: MediaQuery.of(context).size.width * 0.7,
height: MediaQuery.of(context).size.height * 0.6,
child: SingleChildScrollView(
child: ResourceList(
mediaId: id,
seasonNum: season,
episodeNum: episode,
),
),
));
});
),
));
},
);
}

View File

@@ -0,0 +1,88 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:ui/providers/series_details.dart';
import 'package:ui/widgets/progress_indicator.dart';
import 'package:ui/widgets/utils.dart';
import 'package:ui/widgets/widgets.dart';
class ResourceList extends ConsumerWidget {
final String mediaId;
final int seasonNum;
final int episodeNum;
const ResourceList(
{super.key,
required this.mediaId,
this.seasonNum = 0,
this.episodeNum = 0});
@override
Widget build(BuildContext context, WidgetRef ref) {
final torrents = ref.watch(mediaTorrentsDataProvider((
mediaId: mediaId,
seasonNumber: seasonNum,
episodeNumber: episodeNum
)));
return torrents.when(
data: (v) {
bool hasPrivate = false;
for (final item in v) {
if (item.isPrivate == true) {
hasPrivate = true;
}
}
final columns = [
const DataColumn(label: Text("名称")),
const DataColumn(label: Text("大小")),
const DataColumn(label: Text("S/P")),
const DataColumn(label: Text("来源")),
];
if (hasPrivate) {
columns.add(const DataColumn(label: Text("消耗")));
}
columns.add(const DataColumn(label: Text("下载")));
return DataTable(
dataTextStyle: const TextStyle(fontSize: 12),
columns: columns,
rows: List.generate(v.length, (i) {
final torrent = v[i];
final rows = [
DataCell(Text("${torrent.name}")),
DataCell(Text("${torrent.size?.readableFileSize()}")),
DataCell(Text("${torrent.seeders}/${torrent.peers}")),
DataCell(Text(torrent.source ?? "-")),
];
if (hasPrivate) {
rows.add(DataCell(Text(torrent.isPrivate == true
? "${torrent.downloadFactor}dl/${torrent.uploadFactor}up"
: "-")));
}
rows.add(DataCell(IconButton(
icon: const Icon(Icons.download),
onPressed: () async {
var f = ref
.read(mediaTorrentsDataProvider((
mediaId: mediaId,
seasonNumber: seasonNum,
episodeNumber: episodeNum
)).notifier)
.download(torrent)
.then((v) => showSnakeBar("开始下载:${torrent.name}"));
showLoadingWithFuture(f);
},
)));
return DataRow(cells: rows);
}));
},
error: (err, trace) {
return "$err".contains("no resource found")
? const Center(
child: Text("没有资源"),
)
: Text("$err");
},
loading: () => const MyProgressIndicator());
}
}