feat(flutter): 接入分类记录收藏与下载授权
This commit is contained in:
1 parent
00280ca0da
commit
cc4d7c5a9d
52 files changed
+4440
-171
No files matched your search
@@ -0,0 +1,9 @@
|
||||
// Author: fruit.com.cn
|
||||
|
||||
import '../../core/data/services/download_controller.dart';
|
||||
|
||||
/// 下载管理页依赖装配。
|
||||
class DownloadsBinding {
|
||||
/// 从服务端刷新策略与授权列表。
|
||||
Future<void> load(DownloadController downloads) => downloads.load();
|
||||
}
|
||||
@@ -0,0 +1,409 @@
|
||||
// Author: fruit.com.cn
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../../core/data/models/download_license.dart';
|
||||
import '../../core/data/models/download_policy.dart';
|
||||
import '../../core/data/services/auth_controller.dart';
|
||||
import '../../core/data/services/download_controller.dart';
|
||||
import '../../core/formatting/error_text.dart';
|
||||
import '../../core/theme/app_colors.dart';
|
||||
import '../../core/widgets/auth_gate.dart';
|
||||
import '../../l10n/app_localizations.dart';
|
||||
import 'downloads_binding.dart';
|
||||
|
||||
/// 下载授权与状态管理页;本批不落盘,只管理服务端授权。
|
||||
class DownloadsPage extends StatefulWidget {
|
||||
const DownloadsPage({super.key});
|
||||
|
||||
@override
|
||||
State<DownloadsPage> createState() => _DownloadsPageState();
|
||||
}
|
||||
|
||||
class _DownloadsPageState extends State<DownloadsPage> {
|
||||
bool _requested = false;
|
||||
|
||||
@override
|
||||
void didChangeDependencies() {
|
||||
super.didChangeDependencies();
|
||||
final signedIn = context.watch<AuthController>().isSignedIn;
|
||||
if (!signedIn) {
|
||||
_requested = false;
|
||||
return;
|
||||
}
|
||||
if (_requested) return;
|
||||
_requested = true;
|
||||
final downloads = context.read<DownloadController>();
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
if (mounted) unawaited(DownloadsBinding().load(downloads));
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final l10n = AppLocalizations.of(context)!;
|
||||
final signedIn = context.watch<AuthController>().isSignedIn;
|
||||
return Scaffold(
|
||||
key: const Key('downloads-page'),
|
||||
backgroundColor: const Color(0xFF0B0C10),
|
||||
appBar: AppBar(
|
||||
backgroundColor: const Color(0xFF0B0C10),
|
||||
foregroundColor: Colors.white,
|
||||
title: Text(l10n.downloads),
|
||||
),
|
||||
body: signedIn
|
||||
? _DownloadsBody(downloads: context.watch<DownloadController>())
|
||||
: const AuthGatePanel(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _DownloadsBody extends StatelessWidget {
|
||||
const _DownloadsBody({required this.downloads});
|
||||
|
||||
final DownloadController downloads;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final l10n = AppLocalizations.of(context)!;
|
||||
if (downloads.loading && downloads.policy == null) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
if (downloads.error != null && downloads.policy == null) {
|
||||
return Center(
|
||||
key: const Key('downloads-error'),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 32),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Text(
|
||||
errorText(l10n, downloads.error, l10n.downloadsError),
|
||||
textAlign: TextAlign.center,
|
||||
style: const TextStyle(color: Colors.white70),
|
||||
),
|
||||
const SizedBox(height: 14),
|
||||
FilledButton(
|
||||
key: const Key('downloads-retry'),
|
||||
onPressed: () => unawaited(downloads.load()),
|
||||
child: Text(l10n.retry),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
final policy = downloads.policy;
|
||||
if (policy == null) {
|
||||
return Center(
|
||||
key: const Key('downloads-error'),
|
||||
child: FilledButton(
|
||||
key: const Key('downloads-retry'),
|
||||
onPressed: () => unawaited(downloads.load()),
|
||||
child: Text(l10n.retry),
|
||||
),
|
||||
);
|
||||
}
|
||||
return RefreshIndicator(
|
||||
onRefresh: downloads.load,
|
||||
child: ListView(
|
||||
padding: const EdgeInsets.fromLTRB(16, 10, 16, 28),
|
||||
children: [
|
||||
_PolicyPanel(downloads: downloads, policy: policy),
|
||||
const SizedBox(height: 14),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Text(
|
||||
l10n.downloadedItems,
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.w700,
|
||||
),
|
||||
),
|
||||
),
|
||||
if (downloads.licenses.isNotEmpty)
|
||||
TextButton.icon(
|
||||
key: const Key('download-clear'),
|
||||
onPressed: downloads.clearing
|
||||
? null
|
||||
: () => _confirmClear(context),
|
||||
icon: const Icon(Icons.delete_sweep_outlined),
|
||||
label: Text(l10n.clearAll),
|
||||
),
|
||||
],
|
||||
),
|
||||
if (downloads.licenses.isEmpty)
|
||||
Padding(
|
||||
key: const Key('downloads-empty'),
|
||||
padding: const EdgeInsets.symmetric(vertical: 34),
|
||||
child: Text(
|
||||
l10n.noDownloads,
|
||||
textAlign: TextAlign.center,
|
||||
style: const TextStyle(color: Colors.white70),
|
||||
),
|
||||
)
|
||||
else
|
||||
for (final license in downloads.licenses) ...[
|
||||
_LicenseTile(
|
||||
license: license,
|
||||
onRemove: () => _remove(context, license.episodeId),
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
],
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _remove(BuildContext context, String episodeId) async {
|
||||
final ok = await downloads.remove(episodeId);
|
||||
if (ok || !context.mounted) return;
|
||||
_showMessage(context, AppLocalizations.of(context)!.downloadDeleteFailed);
|
||||
}
|
||||
|
||||
Future<void> _confirmClear(BuildContext context) async {
|
||||
final l10n = AppLocalizations.of(context)!;
|
||||
final confirmed = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (dialogContext) => AlertDialog(
|
||||
title: Text(l10n.clearDownloadsTitle),
|
||||
content: Text(l10n.clearDownloadsMessage),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(dialogContext).pop(false),
|
||||
child: Text(l10n.cancel),
|
||||
),
|
||||
FilledButton(
|
||||
onPressed: () => Navigator.of(dialogContext).pop(true),
|
||||
child: Text(l10n.confirmClear),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
if (confirmed != true) return;
|
||||
final ok = await downloads.clear();
|
||||
if (!ok && context.mounted) {
|
||||
_showMessage(context, l10n.downloadClearFailed);
|
||||
}
|
||||
}
|
||||
|
||||
void _showMessage(BuildContext context, String message) {
|
||||
ScaffoldMessenger.of(
|
||||
context,
|
||||
).showSnackBar(SnackBar(content: Text(message)));
|
||||
}
|
||||
}
|
||||
|
||||
class _PolicyPanel extends StatelessWidget {
|
||||
const _PolicyPanel({required this.downloads, required this.policy});
|
||||
|
||||
final DownloadController downloads;
|
||||
final DownloadPolicy policy;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final l10n = AppLocalizations.of(context)!;
|
||||
return Container(
|
||||
key: const Key('download-policy'),
|
||||
padding: const EdgeInsets.fromLTRB(14, 14, 14, 10),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.card,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(color: const Color(0xFF2A2C33)),
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
l10n.downloadQuota(policy.remaining, policy.maxPerDevice),
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 17,
|
||||
fontWeight: FontWeight.w800,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 5),
|
||||
Text(
|
||||
l10n.downloadLicenseDays(policy.licenseDays),
|
||||
style: const TextStyle(color: AppColors.muted, fontSize: 12),
|
||||
),
|
||||
const SizedBox(height: 6),
|
||||
Text(
|
||||
policy.expirePolicyText,
|
||||
style: const TextStyle(color: Color(0xFFB7B3B8), fontSize: 12),
|
||||
),
|
||||
const Divider(height: 22, color: Color(0xFF2A2C33)),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Text(
|
||||
l10n.downloadWifiOnly,
|
||||
style: const TextStyle(color: Colors.white, fontSize: 13),
|
||||
),
|
||||
),
|
||||
Switch(
|
||||
key: const Key('download-wifi-switch'),
|
||||
value: policy.wifiOnly,
|
||||
onChanged: (value) => _update(context, wifiOnly: value),
|
||||
),
|
||||
],
|
||||
),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Text(
|
||||
l10n.downloadQuality,
|
||||
style: const TextStyle(color: Colors.white, fontSize: 13),
|
||||
),
|
||||
),
|
||||
DropdownButton<String>(
|
||||
key: const Key('download-quality'),
|
||||
value:
|
||||
_qualityChoices(
|
||||
policy.maxQuality,
|
||||
).contains(policy.maxQuality)
|
||||
? policy.maxQuality
|
||||
: 'AUTO',
|
||||
underline: const SizedBox.shrink(),
|
||||
items: [
|
||||
for (final quality in _qualityChoices(policy.maxQuality))
|
||||
DropdownMenuItem(value: quality, child: Text(quality)),
|
||||
],
|
||||
onChanged: (value) {
|
||||
if (value == null || value == policy.maxQuality) return;
|
||||
_update(context, maxQuality: value);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _update(
|
||||
BuildContext context, {
|
||||
bool? wifiOnly,
|
||||
String? maxQuality,
|
||||
}) async {
|
||||
final ok = await downloads.updateSettings(
|
||||
wifiOnly: wifiOnly,
|
||||
maxQuality: maxQuality,
|
||||
);
|
||||
if (ok || !context.mounted) return;
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(AppLocalizations.of(context)!.downloadSettingsFailed),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _LicenseTile extends StatelessWidget {
|
||||
const _LicenseTile({required this.license, required this.onRemove});
|
||||
|
||||
final DownloadLicense license;
|
||||
final VoidCallback onRemove;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final l10n = AppLocalizations.of(context)!;
|
||||
return Container(
|
||||
key: ValueKey('download-license-${license.episodeId}'),
|
||||
padding: const EdgeInsets.all(12),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.card,
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Text(
|
||||
license.dramaTitle,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 15,
|
||||
fontWeight: FontWeight.w700,
|
||||
),
|
||||
),
|
||||
),
|
||||
Text(l10n.episodeLabel(license.episodeNo)),
|
||||
IconButton(
|
||||
key: ValueKey('download-remove-${license.episodeId}'),
|
||||
tooltip: l10n.remove,
|
||||
onPressed: onRemove,
|
||||
icon: const Icon(Icons.close_rounded),
|
||||
),
|
||||
],
|
||||
),
|
||||
Wrap(
|
||||
spacing: 12,
|
||||
runSpacing: 5,
|
||||
children: [
|
||||
Text(
|
||||
l10n.downloadStatus(license.status),
|
||||
style: const TextStyle(color: AppColors.blueSoft, fontSize: 12),
|
||||
),
|
||||
Text(
|
||||
l10n.downloadTaskStatus(license.taskStatus),
|
||||
style: const TextStyle(color: AppColors.muted, fontSize: 12),
|
||||
),
|
||||
Text(
|
||||
l10n.downloadProgress(license.progressPercent),
|
||||
style: const TextStyle(color: Colors.white70, fontSize: 12),
|
||||
),
|
||||
if (license.quality.isNotEmpty)
|
||||
Text(
|
||||
license.quality,
|
||||
style: const TextStyle(color: Colors.white70, fontSize: 12),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
ClipRRect(
|
||||
borderRadius: BorderRadius.circular(4),
|
||||
child: LinearProgressIndicator(
|
||||
minHeight: 4,
|
||||
value: (license.progressPercent / 100).clamp(0.0, 1.0),
|
||||
),
|
||||
),
|
||||
if (license.expireAt != null) ...[
|
||||
const SizedBox(height: 7),
|
||||
Text(
|
||||
l10n.downloadExpiresAt(_formatTime(license.expireAt!)),
|
||||
style: const TextStyle(color: Color(0xFF8D8990), fontSize: 11),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
List<String> _qualityChoices(String current) {
|
||||
final choices = <String>['AUTO'];
|
||||
if (current.isNotEmpty && current != 'AUTO') choices.add(current);
|
||||
for (final value in const ['720P', '1080P']) {
|
||||
if (!choices.contains(value)) choices.add(value);
|
||||
}
|
||||
return choices;
|
||||
}
|
||||
|
||||
String _formatTime(DateTime value) {
|
||||
final local = value.toLocal();
|
||||
final month = local.month.toString().padLeft(2, '0');
|
||||
final day = local.day.toString().padLeft(2, '0');
|
||||
final hour = local.hour.toString().padLeft(2, '0');
|
||||
final minute = local.minute.toString().padLeft(2, '0');
|
||||
return '${local.year}-$month-$day $hour:$minute';
|
||||
}
|
||||
Reference in new issue
Block a user