399 lines
14 KiB
Dart
399 lines
14 KiB
Dart
// Author: fruit.com.cn
|
|
|
|
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:provider/provider.dart';
|
|
|
|
import '../../core/data/models/drama.dart';
|
|
import '../../core/data/services/catalog_controller.dart';
|
|
import '../../core/data/services/favorite_controller.dart';
|
|
import '../../core/data/services/interaction_controller.dart';
|
|
import '../../core/formatting/error_text.dart';
|
|
import '../../core/formatting/localized_content.dart';
|
|
import '../../core/platform/haptics.dart';
|
|
import '../../core/theme/app_colors.dart';
|
|
import '../../core/theme/app_theme.dart';
|
|
import '../../core/widgets/remote_image.dart';
|
|
import '../../core/widgets/auth_gate.dart';
|
|
import '../../core/widgets/download_action.dart';
|
|
import '../../l10n/app_localizations.dart';
|
|
import '../interaction/widgets/report_dialog.dart';
|
|
import 'drama_detail_binding.dart';
|
|
import 'drama_detail_logic.dart';
|
|
import '../../core/data/repositories/watch_history_repository.dart';
|
|
import '../../core/data/repositories/playback_repository.dart';
|
|
|
|
const String _reportMenuValue = 'report';
|
|
|
|
class DramaDetailPage extends StatefulWidget {
|
|
const DramaDetailPage({super.key, required this.drama});
|
|
final Drama drama;
|
|
|
|
@override
|
|
State<DramaDetailPage> createState() => _DramaDetailPageState();
|
|
}
|
|
|
|
class _DramaDetailPageState extends State<DramaDetailPage> {
|
|
late final DramaDetailLogic logic;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
logic = DramaDetailBinding().create(
|
|
drama: widget.drama,
|
|
history: context.read<WatchHistoryRepository>(),
|
|
playback: context.read<PlaybackRepository>(),
|
|
favorites: context.read<FavoriteController>(),
|
|
);
|
|
_load();
|
|
}
|
|
|
|
void _load() {
|
|
unawaited(
|
|
logic.load(
|
|
() => context.read<CatalogController>().requireDetail(widget.drama),
|
|
),
|
|
);
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
logic.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Theme(
|
|
data: AppTheme.dark(),
|
|
child: ListenableBuilder(
|
|
listenable: logic,
|
|
builder: (context, _) => _page(context),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _page(BuildContext context) {
|
|
final l10n = AppLocalizations.of(context)!;
|
|
if (logic.loading) {
|
|
return const Scaffold(
|
|
backgroundColor: Color(0xFF111111),
|
|
body: Center(child: CircularProgressIndicator()),
|
|
);
|
|
}
|
|
if (logic.failed) {
|
|
return Scaffold(
|
|
backgroundColor: const Color(0xFF111111),
|
|
body: Center(
|
|
child: Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 32),
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Text(
|
|
errorText(l10n, logic.failureMessage, l10n.detailError),
|
|
textAlign: TextAlign.center,
|
|
style: const TextStyle(color: Colors.white70),
|
|
),
|
|
const SizedBox(height: 16),
|
|
FilledButton(onPressed: _load, child: Text(l10n.retry)),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
final drama = logic.drama;
|
|
if (drama.episodes.isEmpty) {
|
|
// 空态:详情拿到了但一集都没有(还没上传/审核中),不能白屏
|
|
return Scaffold(
|
|
backgroundColor: const Color(0xFF111111),
|
|
appBar: AppBar(
|
|
backgroundColor: const Color(0xFF111111),
|
|
foregroundColor: Colors.white,
|
|
title: Text(localizedDramaTitle(l10n, drama)),
|
|
),
|
|
body: Center(
|
|
child: Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 32),
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
const Icon(
|
|
Icons.movie_filter_outlined,
|
|
size: 48,
|
|
color: Colors.white38,
|
|
),
|
|
const SizedBox(height: 12),
|
|
Text(
|
|
l10n.noEpisodesYet,
|
|
textAlign: TextAlign.center,
|
|
style: const TextStyle(color: Colors.white70),
|
|
),
|
|
const SizedBox(height: 16),
|
|
FilledButton(onPressed: _load, child: Text(l10n.retry)),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
return Scaffold(
|
|
body: CustomScrollView(
|
|
slivers: [
|
|
SliverAppBar(
|
|
expandedHeight: 300,
|
|
pinned: true,
|
|
backgroundColor: const Color(0xFF111111),
|
|
foregroundColor: Colors.white,
|
|
actions: [
|
|
_reportAction(context, l10n),
|
|
_downloadAction(context, l10n),
|
|
_favoriteAction(context, l10n),
|
|
],
|
|
flexibleSpace: FlexibleSpaceBar(
|
|
background: RemoteImage(drama.posterUrl),
|
|
),
|
|
),
|
|
SliverPadding(
|
|
padding: const EdgeInsets.fromLTRB(24, 24, 24, 40),
|
|
sliver: SliverList(
|
|
delegate: SliverChildListDelegate([
|
|
Text(
|
|
l10n.technicalDemoCount(drama.episodes.length),
|
|
style: Theme.of(context).textTheme.labelSmall?.copyWith(
|
|
letterSpacing: 1.4,
|
|
color: AppColors.blueSoft,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
const SizedBox(height: 8),
|
|
Text(
|
|
localizedDramaTitle(l10n, drama),
|
|
style: Theme.of(context).textTheme.headlineLarge?.copyWith(
|
|
fontWeight: FontWeight.w800,
|
|
),
|
|
),
|
|
const SizedBox(height: 10),
|
|
Text(
|
|
localizedDramaSynopsis(l10n, drama),
|
|
style: Theme.of(context).textTheme.bodyLarge?.copyWith(
|
|
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
|
),
|
|
),
|
|
const SizedBox(height: 20),
|
|
Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
FilledButton.icon(
|
|
onPressed: () =>
|
|
logic.openPlayer(context, logic.continueIndex ?? 0),
|
|
icon: const Icon(Icons.play_arrow_rounded),
|
|
label: Text(
|
|
logic.continueIndex == null
|
|
? l10n.watchEpisodeOne
|
|
: l10n.continueEpisode(logic.continueIndex! + 1),
|
|
),
|
|
),
|
|
if (_continueFraction != null) ...[
|
|
const SizedBox(height: 8),
|
|
_ResumeBar(
|
|
fraction: _continueFraction!,
|
|
label: l10n.resumeProgress(
|
|
_clock(logic.progress!.position),
|
|
_clock(logic.progress!.duration),
|
|
),
|
|
),
|
|
],
|
|
if (logic.progressError != null) ...[
|
|
const SizedBox(height: 8),
|
|
Row(
|
|
children: [
|
|
Expanded(
|
|
child: Text(
|
|
errorText(
|
|
l10n,
|
|
logic.progressError,
|
|
l10n.progressError,
|
|
),
|
|
style: const TextStyle(
|
|
color: Colors.white60,
|
|
fontSize: 12,
|
|
),
|
|
),
|
|
),
|
|
TextButton(
|
|
onPressed: () => unawaited(logic.loadContinue()),
|
|
child: Text(l10n.retry),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
],
|
|
),
|
|
const SizedBox(height: 30),
|
|
Text(
|
|
l10n.episodes,
|
|
style: Theme.of(
|
|
context,
|
|
).textTheme.titleLarge?.copyWith(fontWeight: FontWeight.bold),
|
|
),
|
|
]),
|
|
),
|
|
),
|
|
SliverPadding(
|
|
padding: const EdgeInsets.fromLTRB(24, 0, 24, 40),
|
|
sliver: SliverList.builder(
|
|
itemCount: drama.episodes.length,
|
|
itemBuilder: (context, index) {
|
|
final episode = drama.episodes[index];
|
|
final fraction = logic.progressFractionFor(episode.id);
|
|
return Card(
|
|
margin: const EdgeInsets.only(bottom: 10),
|
|
child: ListTile(
|
|
minTileHeight: 76,
|
|
leading: CircleAvatar(
|
|
backgroundColor: AppColors.blue.withValues(alpha: 0.18),
|
|
child: Text('${episode.number}'),
|
|
),
|
|
title: Text(l10n.episodeLabel(episode.number)),
|
|
subtitle: fraction == null
|
|
? null
|
|
: Padding(
|
|
padding: const EdgeInsets.only(top: 6),
|
|
child: _ResumeBar(fraction: fraction),
|
|
),
|
|
trailing: const Icon(Icons.play_circle_outline_rounded),
|
|
onTap: () => logic.openPlayer(context, index),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
double? get _continueFraction {
|
|
final record = logic.progress;
|
|
if (record == null || !record.hasPosition) return null;
|
|
if (logic.continueIndex == null) return null;
|
|
return record.fraction;
|
|
}
|
|
|
|
/// 下载当前续看集或第一集;业务判定完全交给服务端。
|
|
Widget _downloadAction(BuildContext context, AppLocalizations l10n) {
|
|
final episodes = logic.drama.episodes;
|
|
final index = logic.continueIndex ?? 0;
|
|
final episode = episodes.isEmpty
|
|
? null
|
|
: episodes[index.clamp(0, episodes.length - 1)];
|
|
return IconButton(
|
|
key: const Key('detail-download'),
|
|
tooltip: l10n.downloads,
|
|
icon: const Icon(Icons.download_outlined),
|
|
onPressed: episode == null
|
|
? null
|
|
: () => requestEpisodeDownload(context, episode.id),
|
|
);
|
|
}
|
|
|
|
/// 详情页更多菜单:本批只提供举报短剧。
|
|
Widget _reportAction(BuildContext context, AppLocalizations l10n) {
|
|
return PopupMenuButton<String>(
|
|
key: const Key('detail-more'),
|
|
tooltip: l10n.more,
|
|
onSelected: (_) => unawaited(_reportDrama(l10n)),
|
|
itemBuilder: (_) => [
|
|
PopupMenuItem(value: _reportMenuValue, child: Text(l10n.reportDrama)),
|
|
],
|
|
);
|
|
}
|
|
|
|
Future<void> _reportDrama(AppLocalizations l10n) async {
|
|
if (!await ensureSignedIn(context) || !mounted) return;
|
|
final draft = await showReportDialog(context, title: l10n.reportDrama);
|
|
if (draft == null || !mounted) return;
|
|
unawaited(context.read<Haptics>().lightImpact());
|
|
final result = await context.read<InteractionController>().reportDrama(
|
|
logic.drama.id,
|
|
reason: draft.reason,
|
|
description: draft.description,
|
|
);
|
|
if (!mounted) return;
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text(
|
|
result.isSuccess
|
|
? l10n.reportSubmitted
|
|
: result.errorMessage ?? l10n.reportFailed,
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
/// 收藏按钮:状态取自服务端(乐观更新,失败回滚并提示)。
|
|
Widget _favoriteAction(BuildContext context, AppLocalizations l10n) {
|
|
final favorites = context.watch<FavoriteController>();
|
|
final saved = favorites.isSaved(logic.drama.id);
|
|
return IconButton(
|
|
tooltip: l10n.favorite,
|
|
icon: Icon(saved ? Icons.bookmark : Icons.bookmark_border),
|
|
onPressed: () async {
|
|
if (!await ensureSignedIn(context)) return;
|
|
if (!context.mounted) return;
|
|
unawaited(context.read<Haptics>().lightImpact());
|
|
final ok = await logic.toggleFavorite();
|
|
if (ok || !context.mounted) return;
|
|
// 成功靠图标变化反馈;失败必须说清楚(乐观更新已回滚)
|
|
ScaffoldMessenger.of(
|
|
context,
|
|
).showSnackBar(SnackBar(content: Text(l10n.favoriteToggleFailed)));
|
|
},
|
|
);
|
|
}
|
|
|
|
String _clock(Duration duration) {
|
|
final minutes = duration.inMinutes.remainder(60).toString().padLeft(2, '0');
|
|
final seconds = duration.inSeconds.remainder(60).toString().padLeft(2, '0');
|
|
return duration.inHours > 0
|
|
? '${duration.inHours}:$minutes:$seconds'
|
|
: '$minutes:$seconds';
|
|
}
|
|
}
|
|
|
|
/// 续看进度条(详情页按钮下 / 剧集行内)。fraction 由服务端记录算出。
|
|
class _ResumeBar extends StatelessWidget {
|
|
const _ResumeBar({required this.fraction, this.label});
|
|
|
|
final double fraction;
|
|
final String? label;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
ClipRRect(
|
|
borderRadius: BorderRadius.circular(2),
|
|
child: LinearProgressIndicator(
|
|
value: fraction,
|
|
minHeight: 3,
|
|
backgroundColor: Colors.white24,
|
|
valueColor: const AlwaysStoppedAnimation(AppColors.blueSoft),
|
|
),
|
|
),
|
|
if (label != null) ...[
|
|
const SizedBox(height: 4),
|
|
Text(
|
|
label!,
|
|
style: const TextStyle(color: Colors.white54, fontSize: 11),
|
|
),
|
|
],
|
|
],
|
|
);
|
|
}
|
|
}
|