后端接口从 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)
169 lines
4.3 KiB
Dart
169 lines
4.3 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
import '../../core/data/models/drama.dart';
|
|
import '../../core/data/repositories/watch_history_repository.dart';
|
|
import '../../l10n/app_localizations.dart';
|
|
|
|
enum DiscoverPageAction { home, my, watchlist, detail, resume }
|
|
|
|
class DiscoverLogic extends ChangeNotifier {
|
|
DiscoverLogic({required this.history, required this.drama}) {
|
|
refreshPosition();
|
|
}
|
|
|
|
final WatchHistoryRepository history;
|
|
Drama drama;
|
|
late Future<Duration?> position;
|
|
final TextEditingController search = TextEditingController();
|
|
final FocusNode searchFocus = FocusNode();
|
|
final ScrollController scroll = ScrollController();
|
|
int category = 0;
|
|
bool searchOpen = false;
|
|
bool _disposed = false;
|
|
|
|
void notify() {
|
|
if (!_disposed) notifyListeners();
|
|
}
|
|
|
|
void refreshPosition() {
|
|
// 续看位置来自服务端(每剧一条记录);未登录或没有记录时给 null,UI 显示"从头看"
|
|
position = history
|
|
.resumePointFor(drama.id)
|
|
.then((point) => point?.position)
|
|
.catchError((Object _) => null);
|
|
}
|
|
|
|
void selectCategory(int index) {
|
|
category = index;
|
|
notify();
|
|
}
|
|
|
|
void onSearchChanged() => notify();
|
|
|
|
bool get showSearch => searchOpen || search.text.isNotEmpty;
|
|
|
|
void openSearch() {
|
|
searchOpen = true;
|
|
notify();
|
|
searchFocus.requestFocus();
|
|
}
|
|
|
|
void syncDrama({
|
|
required bool active,
|
|
required bool wasActive,
|
|
required Drama nextDrama,
|
|
required bool dramaChanged,
|
|
required int searchRequest,
|
|
required int previousSearchRequest,
|
|
required bool Function() isActive,
|
|
}) {
|
|
if (active && (!wasActive || dramaChanged)) {
|
|
drama = nextDrama;
|
|
refreshPosition();
|
|
notify();
|
|
}
|
|
if (active && searchRequest != previousSearchRequest) {
|
|
searchOpen = true;
|
|
notify();
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
if (isActive()) searchFocus.requestFocus();
|
|
});
|
|
}
|
|
}
|
|
|
|
void act(
|
|
BuildContext context,
|
|
DiscoverPageAction action, {
|
|
required bool embedded,
|
|
ValueChanged<DiscoverPageAction>? onAction,
|
|
}) {
|
|
if (embedded) {
|
|
onAction?.call(action);
|
|
} else {
|
|
Navigator.pop(context, action);
|
|
}
|
|
}
|
|
|
|
Drama? dramaFor(String? dramaId, List<Drama> dramas) {
|
|
for (final item in dramas) {
|
|
if (item.id == dramaId) return item;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
void openCard(
|
|
BuildContext context,
|
|
Drama? selected, {
|
|
required bool embedded,
|
|
ValueChanged<Drama>? onDramaSelected,
|
|
ValueChanged<DiscoverPageAction>? onAction,
|
|
}) {
|
|
if (selected == null) {
|
|
unavailable(context);
|
|
} else if (embedded && onDramaSelected != null) {
|
|
onDramaSelected(selected);
|
|
} else {
|
|
act(context, DiscoverPageAction.detail, embedded: embedded, onAction: onAction);
|
|
}
|
|
}
|
|
|
|
void unavailable(BuildContext context) {
|
|
final l10n = AppLocalizations.of(context)!;
|
|
ScaffoldMessenger.of(
|
|
context,
|
|
).showSnackBar(SnackBar(content: Text(l10n.artworkOnlyNotice)));
|
|
}
|
|
|
|
void catalogInfo(BuildContext context) {
|
|
final l10n = AppLocalizations.of(context)!;
|
|
ScaffoldMessenger.of(
|
|
context,
|
|
).showSnackBar(SnackBar(content: Text(l10n.demoCatalogNotice)));
|
|
}
|
|
|
|
void showFilters(BuildContext context) {
|
|
final l10n = AppLocalizations.of(context)!;
|
|
final labels = [
|
|
l10n.forYou,
|
|
l10n.hot,
|
|
l10n.newest,
|
|
l10n.romance,
|
|
l10n.actionGenre,
|
|
];
|
|
showModalBottomSheet<void>(
|
|
context: context,
|
|
backgroundColor: const Color(0xFF202126),
|
|
showDragHandle: true,
|
|
builder: (sheetContext) => SafeArea(
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
ListTile(title: Text(l10n.filter)),
|
|
for (var index = 0; index < labels.length; index++)
|
|
ListTile(
|
|
title: Text(labels[index]),
|
|
trailing: index == category
|
|
? const Icon(Icons.check_rounded, color: Color(0xFF2F7BFF))
|
|
: null,
|
|
onTap: () {
|
|
selectCategory(index);
|
|
Navigator.pop(sheetContext);
|
|
},
|
|
),
|
|
const SizedBox(height: 8),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_disposed = true;
|
|
search.dispose();
|
|
searchFocus.dispose();
|
|
scroll.dispose();
|
|
super.dispose();
|
|
}
|
|
}
|