后端接口从 6 个扩到 13 个,全部走真数据:
- 新增 api:playback_api(/app/episodes/{id}/play)、watch_api(/app/watch/*)、
library_api(/app/favorites*);票据从 CatalogApi 搬到 PlaybackApi
- 新增 repository:PlaybackRepository(含票据 TTL 缓存,默认 10 分钟,时钟可注入)、
FavoriteRepository、WatchHistoryRepository 换成服务端模型(每剧一条续看记录,删掉 demoProfileId)
- PlayerLogic:起播前取票(拿不到给三态 UI,失败可重试);accessType 驱动锁定文案,
不在前端猜;签名地址原样交给播放器(只补 host,不动 path/query);
进度上报节流 5s → 10s,切集/退出/切后台各补一次,播完带 finished
- 续看定位:服务端记录里的那一集才 seek(其它集从头播),续看位置进详情/播放页时读一次
- FavoriteController:收藏以服务端为准,乐观更新 + 失败回滚;退出登录清空
- UI 三态:详情页(加载/失败可重试/空剧集 + 续看进度条 + 收藏按钮)、
播放页(票据加载中/失败可重试/后端判定不可播)、我的页(续看失败可重试、收藏三态、最近观看)
- l10n 新增 8 个键(en/zh/id 三份):网络/进度/收藏失败、锁定提示、续看文案等
测试:flutter test 188 全过(存量 27 个测试文件未跑红);新增票据过期重取、进度节流、
续看定位、收藏回滚、签名 query 原样保留、真实后端端到端联调(live_watch_flow)
331 lines
11 KiB
Dart
331 lines
11 KiB
Dart
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/formatting/error_text.dart';
|
|
import '../../core/formatting/localized_content.dart';
|
|
import '../../core/widgets/remote_image.dart';
|
|
import '../../core/widgets/auth_gate.dart';
|
|
import '../../l10n/app_localizations.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';
|
|
|
|
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 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: [_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: const Color(0xFF2F7BFF),
|
|
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: const Color(
|
|
0xFF2F7BFF,
|
|
).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 _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;
|
|
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(Color(0xFF2F7BFF)),
|
|
),
|
|
),
|
|
if (label != null) ...[
|
|
const SizedBox(height: 4),
|
|
Text(
|
|
label!,
|
|
style: const TextStyle(color: Colors.white54, fontSize: 11),
|
|
),
|
|
],
|
|
],
|
|
);
|
|
}
|
|
}
|