后端接口从 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)
120 lines
3.4 KiB
Dart
120 lines
3.4 KiB
Dart
import 'package:dio/dio.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
import '../../core/data/models/drama.dart';
|
|
import '../../core/data/models/watch_progress.dart';
|
|
import '../../core/data/remote/api_client.dart';
|
|
import '../../core/data/repositories/watch_history_repository.dart';
|
|
import '../../core/data/repositories/playback_repository.dart';
|
|
import '../../core/data/services/favorite_controller.dart';
|
|
import '../player/player_view.dart';
|
|
|
|
class DramaDetailLogic extends ChangeNotifier {
|
|
DramaDetailLogic({
|
|
required this.drama,
|
|
required this.history,
|
|
required this.playback,
|
|
required this.favorites,
|
|
});
|
|
|
|
/// 先用列表卡片渲染,取到详情后换成带完整剧集的版本。
|
|
Drama drama;
|
|
final WatchHistoryRepository history;
|
|
final PlaybackRepository playback;
|
|
final FavoriteController favorites;
|
|
bool loading = false;
|
|
bool failed = false;
|
|
|
|
/// 接口给出的失败原因;为空时界面回退到本地文案。
|
|
String? failureMessage;
|
|
int? continueIndex;
|
|
WatchProgress? progress;
|
|
String? progressError;
|
|
bool _disposed = false;
|
|
|
|
void notify() {
|
|
if (!_disposed) notifyListeners();
|
|
}
|
|
|
|
/// 列表卡片只带一集可播预览,进详情时补一次完整剧集列表。
|
|
Future<void> load(Future<Drama> Function() fetch) async {
|
|
if (drama.detailLoaded) {
|
|
await loadContinue();
|
|
return;
|
|
}
|
|
loading = true;
|
|
failed = false;
|
|
failureMessage = null;
|
|
notify();
|
|
try {
|
|
drama = await fetch();
|
|
await loadContinue();
|
|
} on ApiException catch (exception) {
|
|
failed = true;
|
|
failureMessage = exception.message;
|
|
} on DioException {
|
|
// 断网/超时:交给 l10n 文案,不把 HTTP 细节丢给用户
|
|
failed = true;
|
|
failureMessage = 'networkError';
|
|
} catch (_) {
|
|
failed = true;
|
|
} finally {
|
|
loading = false;
|
|
notify();
|
|
}
|
|
}
|
|
|
|
Future<void> loadContinue() async {
|
|
try {
|
|
final record = await history.progressFor(drama.id);
|
|
if (_disposed) return;
|
|
progress = record;
|
|
progressError = null;
|
|
continueIndex = record == null
|
|
? null
|
|
: _indexOf(record.episodeId) ?? continueIndex;
|
|
notify();
|
|
} catch (exception) {
|
|
if (_disposed) return;
|
|
// 拿不到续看位置不影响看第一集,但要让用户知道(可重试)
|
|
progressError = exception is ApiException
|
|
? exception.message
|
|
: 'progressError';
|
|
notify();
|
|
}
|
|
}
|
|
|
|
int? _indexOf(String episodeId) {
|
|
final index = drama.episodes.indexWhere(
|
|
(episode) => episode.id == episodeId,
|
|
);
|
|
return index >= 0 ? index : null;
|
|
}
|
|
|
|
/// 该集的续看进度(0~1),用于剧集行上的进度条;没有记录返回 null。
|
|
double? progressFractionFor(String episodeId) {
|
|
final record = progress;
|
|
if (record == null || record.episodeId != episodeId) return null;
|
|
if (!record.hasPosition) return null;
|
|
return record.fraction;
|
|
}
|
|
|
|
Future<void> openPlayer(BuildContext context, int index) async {
|
|
await Navigator.of(context).push(
|
|
MaterialPageRoute<void>(
|
|
builder: (_) => PlayerPage(drama: drama, initialIndex: index),
|
|
),
|
|
);
|
|
await loadContinue();
|
|
}
|
|
|
|
/// 收藏切换:乐观更新失败时会回滚,由调用方给出提示。
|
|
Future<bool> toggleFavorite() => favorites.toggle(drama.id, card: drama);
|
|
|
|
@override
|
|
void dispose() {
|
|
_disposed = true;
|
|
super.dispose();
|
|
}
|
|
}
|