feat: video download

Signed-off-by: bggRGjQaUbCoE <githubaccount56556@proton.me>
This commit is contained in:
bggRGjQaUbCoE
2025-11-06 12:12:32 +08:00
parent 976622df89
commit ffd4f9ee73
92 changed files with 4853 additions and 946 deletions

View File

@@ -0,0 +1,117 @@
import 'dart:async';
import 'package:PiliPlus/common/widgets/loading_widget/http_error.dart';
import 'package:PiliPlus/common/widgets/view_sliver_safe_area.dart';
import 'package:PiliPlus/models_new/download/bili_download_entry_info.dart';
import 'package:PiliPlus/pages/download/controller.dart';
import 'package:PiliPlus/pages/download/detail/widgets/item.dart';
import 'package:PiliPlus/services/download/download_service.dart';
import 'package:PiliPlus/utils/grid.dart';
import 'package:PiliPlus/utils/storage.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
class DownloadDetailPage extends StatefulWidget {
const DownloadDetailPage({
super.key,
required this.pageId,
required this.title,
required this.progress,
});
final String pageId;
final String title;
final ValueNotifier progress;
@override
State<DownloadDetailPage> createState() => _DownloadDetailPageState();
}
class _DownloadDetailPageState extends State<DownloadDetailPage>
with GridMixin {
StreamSubscription? _sub;
final _downloadItems = RxList<BiliDownloadEntryInfo>();
final _controller = Get.find<DownloadPageController>();
final _downloadService = Get.find<DownloadService>();
@override
void initState() {
super.initState();
_loadList();
_sub = _controller.flag.listen((_) {
_loadList();
});
}
Future _closeSub() async {
if (_sub != null) {
await _sub?.cancel();
_sub = null;
}
}
@override
void dispose() {
_closeSub();
super.dispose();
}
void _loadList() {
final list =
_controller.pages
.firstWhereOrNull((e) => e.pageId == widget.pageId)
?.entrys
?..sort((a, b) => a.sortKey.compareTo(b.sortKey));
if (list != null) {
_downloadItems.value = list;
} else {
_downloadItems.clear();
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: false,
appBar: AppBar(title: Text(widget.title)),
body: CustomScrollView(
slivers: [
ViewSliverSafeArea(
sliver: Obx(() {
if (_downloadItems.isNotEmpty) {
return SliverGrid.builder(
gridDelegate: gridDelegate,
itemBuilder: (context, index) {
final entry = _downloadItems[index];
return DetailItem(
entry: entry,
progress: widget.progress,
downloadService: _downloadService,
showTitle: false,
onDelete: () async {
if (_downloadItems.length == 1) {
await _closeSub();
await _downloadService.deletePage(
pageDirPath: entry.pageDirPath,
);
if (context.mounted) {
Get.back();
}
} else {
_downloadService.deleteDownload(entry: entry);
}
GStorage.watchProgress.delete(entry.cid.toString());
},
);
},
itemCount: _downloadItems.length,
);
}
return const HttpError();
}),
),
],
),
);
}
}

View File

@@ -0,0 +1,290 @@
import 'package:PiliPlus/common/constants.dart';
import 'package:PiliPlus/common/widgets/badge.dart';
import 'package:PiliPlus/common/widgets/dialog/dialog.dart';
import 'package:PiliPlus/common/widgets/image/network_img_layer.dart';
import 'package:PiliPlus/common/widgets/progress_bar/video_progress_indicator.dart';
import 'package:PiliPlus/models/common/badge_type.dart';
import 'package:PiliPlus/models/common/video/source_type.dart';
import 'package:PiliPlus/models/common/video/video_quality.dart';
import 'package:PiliPlus/models_new/download/bili_download_entry_info.dart';
import 'package:PiliPlus/services/download/download_service.dart';
import 'package:PiliPlus/utils/cache_manager.dart';
import 'package:PiliPlus/utils/duration_utils.dart';
import 'package:PiliPlus/utils/page_utils.dart';
import 'package:PiliPlus/utils/storage.dart';
import 'package:PiliPlus/utils/utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
class DetailItem extends StatelessWidget {
const DetailItem({
super.key,
required this.entry,
required this.progress,
required this.downloadService,
required this.onDelete,
required this.showTitle,
});
final BiliDownloadEntryInfo entry;
final ValueNotifier progress;
final DownloadService downloadService;
final VoidCallback onDelete;
final bool showTitle;
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final outline = theme.colorScheme.outline;
final cid = entry.source?.cid ?? entry.pageData?.cid;
void onLongPress() => showDialog(
context: context,
builder: (context) {
return AlertDialog(
clipBehavior: Clip.hardEdge,
contentPadding: const EdgeInsets.symmetric(vertical: 12),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
ListTile(
onTap: () {
Get.back();
showConfirmDialog(
context: context,
title: '确定删除该视频?',
onConfirm: onDelete,
);
},
dense: true,
title: const Text(
'删除',
style: TextStyle(fontSize: 14),
),
),
ListTile(
onTap: () async {
Get.back();
final res = await downloadService.downloadDanmaku(
entry: entry,
isUpdate: true,
);
if (res) {
SmartDialog.showToast('更新成功');
}
},
dense: true,
title: const Text(
'更新弹幕',
style: TextStyle(fontSize: 14),
),
),
],
),
);
},
);
return Material(
type: MaterialType.transparency,
child: InkWell(
onTap: () async {
if (entry.isCompleted) {
await PageUtils.toVideoPage(
aid: entry.avid,
cid: cid!,
cover: entry.cover,
title: entry.showTitle,
extraArguments: {
'sourceType': SourceType.file,
'entry': entry,
'dirPath': entry.entryDirPath,
},
);
if (context.mounted) {
Future.delayed(const Duration(milliseconds: 400), () {
if (context.mounted) {
// ignore: invalid_use_of_protected_member, invalid_use_of_visible_for_testing_member
progress.notifyListeners();
}
});
}
} else {
final curDownload = downloadService.curDownload.value;
if (curDownload != null &&
curDownload.cid == cid &&
curDownload.status!.index <= 3) {
downloadService.cancelDownload(
isDelete: false,
downloadNext: false,
);
} else {
if (entry.status == DownloadStatus.wait) {
downloadService.waitDownloadQueue.remove(entry);
}
downloadService.startDownload(entry);
}
}
},
onLongPress: onLongPress,
onSecondaryTap: Utils.isMobile ? null : onLongPress,
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: StyleString.safeSpace,
vertical: 5,
),
child: Row(
spacing: 10,
children: [
Stack(
clipBehavior: Clip.none,
children: [
AspectRatio(
aspectRatio: StyleString.aspectRatio,
child: LayoutBuilder(
builder: (context, constraints) => NetworkImgLayer(
src: entry.cover,
width: constraints.maxWidth,
height: constraints.maxHeight,
),
),
),
if (entry.videoQuality case final videoQuality?)
PBadge(
text: VideoQuality.fromCode(videoQuality).shortDesc,
right: 6.0,
top: 6.0,
type: PBadgeType.gray,
),
ValueListenableBuilder(
valueListenable: progress,
builder: (_, _, _) {
final progress = GStorage.watchProgress.get(
cid.toString(),
);
if (progress != null) {
return Positioned(
left: 0,
right: 0,
bottom: 0,
child: Stack(
clipBehavior: Clip.none,
children: [
videoProgressIndicator(
progress / entry.totalTimeMilli,
),
PBadge(
text: progress >= entry.totalTimeMilli - 400
? '已看完'
: '${DurationUtils.formatDuration(
progress ~/ 1000,
)}/'
'${DurationUtils.formatDuration(
entry.totalTimeMilli ~/ 1000,
)}',
right: 6,
bottom: 7,
type: PBadgeType.gray,
),
],
),
);
}
return PBadge(
text: DurationUtils.formatDuration(
entry.totalTimeMilli ~/ 1000,
),
right: 6.0,
bottom: 7.0,
type: PBadgeType.gray,
);
},
),
],
),
Expanded(
child: Stack(
clipBehavior: Clip.none,
children: [
Column(
spacing: 5,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
showTitle ? entry.title : entry.showTitle,
textAlign: TextAlign.start,
style: TextStyle(
fontSize: theme.textTheme.bodyMedium!.fontSize,
height: 1.42,
letterSpacing: 0.3,
),
maxLines: showTitle
? entry.ep != null
? 1
: 2
: 2,
overflow: TextOverflow.ellipsis,
),
if (showTitle) ...[
if (entry.pageData?.part case final part?)
if (part != entry.title)
Text(
part,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontSize: 12,
color: theme.colorScheme.onSurfaceVariant,
),
),
if (entry.ep?.showTitle case final showTitle?)
Text(
showTitle,
maxLines: 2,
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontSize: 12,
color: theme.colorScheme.onSurfaceVariant,
),
),
],
],
),
if (entry.isCompleted) ...[
Positioned(
left: 0,
bottom: 0,
child: Text(
'${CacheManager.formatSize(entry.totalBytes)}${entry.ownerName != null ? ' ${entry.ownerName}' : ''}',
style: TextStyle(
fontSize: 12,
height: 1.6,
color: outline,
),
),
),
Positioned(
right: 0,
bottom: 0,
child: entry.moreBtn(theme),
),
] else
Positioned(
left: 0,
right: 0,
bottom: 0,
child: entry.progressWidget(
theme: theme,
downloadService: downloadService,
isPage: false,
),
),
],
),
),
],
),
),
),
);
}
}