feat(library): 支持设备追剧与观看历史

This commit is contained in:
Zhou Weidong committed 2026-10-27 09:54:51 +08:00
1 parent 4c3acb047c
commit 0d785bf1f8
12 files changed
+232 -76

No files matched your search

+5 -12
View File
@@ -141,17 +141,11 @@ class _LumaDramaAppState extends State<LumaDramaApp> {
late final WatchHistoryRepository _watchHistory =
widget.watchHistoryRepository ??
HttpWatchHistoryRepository(
WatchApi(_apiClient),
isSignedIn: () => _auth.isSignedIn,
);
HttpWatchHistoryRepository(WatchApi(_apiClient));
late final FavoriteRepository _favorites =
widget.favoriteRepository ??
HttpFavoriteRepository(
LibraryApi(_apiClient),
isSignedIn: () => _auth.isSignedIn,
);
HttpFavoriteRepository(LibraryApi(_apiClient));
late final PlaybackRepository _playback =
widget.playbackRepository ??
@@ -219,7 +213,7 @@ class _LumaDramaAppState extends State<LumaDramaApp> {
unawaited(_localeController.load());
// 登录/登出时同步收藏状态:收藏属于账号,不能跨账号串联
_auth.addListener(_onAuthChanged);
if (_auth.isSignedIn) unawaited(_favoriteController.load());
unawaited(_favoriteController.load());
if (_auth.isSignedIn) unawaited(_walletController.load());
unawaited(_downloadController.restore());
}
@@ -235,6 +229,7 @@ class _LumaDramaAppState extends State<LumaDramaApp> {
unawaited(_walletController.load());
} else {
_favoriteController.clear();
unawaited(_favoriteController.load());
_downloadController.reset();
_walletController.reset();
}
@@ -293,9 +288,7 @@ class _LumaDramaAppState extends State<LumaDramaApp> {
),
ChangeNotifierProvider(create: (_) => _createCategories()),
if (playlists != null)
ChangeNotifierProvider(
create: (_) => PlaylistController(playlists),
),
ChangeNotifierProvider(create: (_) => PlaylistController(playlists)),
ChangeNotifierProvider.value(value: _favoriteController),
ChangeNotifierProvider.value(value: _downloadController),
ChangeNotifierProvider.value(value: _walletController),
+1 -1
View File
@@ -4,7 +4,7 @@ import '../models/page_result.dart';
import '../models/watch_progress.dart';
import 'api_client.dart';
/// 观看进度接口:`/app/watch/*`(契约 §1.5)。全部需要登录态。
/// 观看进度接口:`/app/watch/*`(契约 §1.5)。支持账号或设备身份。
class WatchApi {
const WatchApi(this._client);
@@ -19,21 +19,15 @@ abstract interface class FavoriteRepository {
}
class HttpFavoriteRepository implements FavoriteRepository {
const HttpFavoriteRepository(this._api, {this.isSignedIn});
const HttpFavoriteRepository(this._api);
final LibraryApi _api;
/// 未登录时不请求:收藏属于账号,匿名浏览时列表按空处理(点收藏按钮会先引导登录)。
final bool Function()? isSignedIn;
bool get _signedIn => isSignedIn?.call() ?? true;
@override
Future<bool> toggle(String dramaId) => _api.toggleFavorite(dramaId);
@override
Future<PageResult<Drama>> page({int pageIndex = 1, int pageSize = 20}) async {
if (!_signedIn) return const PageResult();
return _api.favorites(pageIndex: pageIndex, pageSize: pageSize);
}
@@ -35,18 +35,12 @@ abstract interface class WatchHistoryRepository {
}
class HttpWatchHistoryRepository implements WatchHistoryRepository {
const HttpWatchHistoryRepository(this._api, {this.isSignedIn});
const HttpWatchHistoryRepository(this._api);
final WatchApi _api;
/// 未登录时这些接口必然 401:直接短路成"没有记录",让浏览/试看保持匿名可用。
final bool Function()? isSignedIn;
bool get _signedIn => isSignedIn?.call() ?? true;
@override
Future<WatchProgress?> progressFor(String dramaId) async {
if (!_signedIn) return null;
return _api.progressOfDrama(dramaId);
}
@@ -57,7 +51,6 @@ class HttpWatchHistoryRepository implements WatchHistoryRepository {
Duration? duration,
bool finished = false,
}) async {
if (!_signedIn) return null;
return _api.saveProgress(
episodeId: episodeId,
position: position,
@@ -71,19 +64,16 @@ class HttpWatchHistoryRepository implements WatchHistoryRepository {
int pageIndex = 1,
int pageSize = 20,
}) async {
if (!_signedIn) return const PageResult();
return _api.recent(pageIndex: pageIndex, pageSize: pageSize);
}
@override
Future<void> remove(String dramaId) async {
if (!_signedIn) return;
await _api.removeProgress(dramaId);
}
@override
Future<void> clear() async {
if (!_signedIn) return;
await _api.clearProgress();
}
}
@@ -93,6 +93,7 @@ class FavoriteController extends ChangeNotifier {
loadingMore = false;
error = null;
loadMoreError = null;
lastErrorCode = null;
notify();
try {
final page = await _repository.page(pageIndex: 1, pageSize: pageSize);
@@ -106,9 +107,10 @@ class FavoriteController extends ChangeNotifier {
total = page.total;
_pageIndex = 1;
hasMore = page.list.isNotEmpty && cards.length < total;
} catch (_) {
} catch (failure) {
if (!_disposed && request == _requestSeq) {
error = 'favoritesError';
if (failure is ApiException) lastErrorCode = failure.code;
}
} finally {
if (!_disposed && request == _requestSeq) {
@@ -124,6 +126,7 @@ class FavoriteController extends ChangeNotifier {
final request = ++_requestSeq;
loadingMore = true;
loadMoreError = null;
lastErrorCode = null;
notify();
try {
final page = await _repository.page(
@@ -141,9 +144,10 @@ class FavoriteController extends ChangeNotifier {
total = page.total;
_pageIndex += 1;
hasMore = page.list.isNotEmpty && cards.length < total;
} catch (_) {
} catch (failure) {
if (!_disposed && request == _requestSeq) {
loadMoreError = 'favoritesError';
if (failure is ApiException) lastErrorCode = failure.code;
}
} finally {
if (!_disposed && request == _requestSeq) {
@@ -38,6 +38,7 @@ class WatchRecordsController extends ChangeNotifier {
String? error;
String? loadMoreError;
String? actionError;
int? lastErrorCode;
int _pageIndex = 0;
int _requestSeq = 0;
bool _disposed = false;
@@ -50,6 +51,7 @@ class WatchRecordsController extends ChangeNotifier {
error = null;
loadMoreError = null;
actionError = null;
lastErrorCode = null;
_notify();
try {
final page = await _history.recent(pageIndex: 1, pageSize: pageSize);
@@ -71,6 +73,7 @@ class WatchRecordsController extends ChangeNotifier {
);
if (!_disposed && request == _requestSeq) {
error = failure is ApiException ? failure.message : 'progressError';
if (failure is ApiException) lastErrorCode = failure.code;
}
} finally {
if (!_disposed && request == _requestSeq) {
@@ -86,6 +89,7 @@ class WatchRecordsController extends ChangeNotifier {
final request = ++_requestSeq;
loadingMore = true;
loadMoreError = null;
lastErrorCode = null;
_notify();
try {
final page = await _history.recent(
@@ -108,6 +112,7 @@ class WatchRecordsController extends ChangeNotifier {
loadMoreError = failure is ApiException
? failure.message
: 'progressError';
if (failure is ApiException) lastErrorCode = failure.code;
}
} finally {
if (!_disposed && request == _requestSeq) {
@@ -148,17 +153,19 @@ class WatchRecordsController extends ChangeNotifier {
total = total > 0 ? total - 1 : 0;
hasMore = records.length < total;
actionError = null;
lastErrorCode = null;
_notify();
try {
await _history.remove(dramaId);
return true;
} catch (_) {
} catch (failure) {
final next = [...records];
next.insert(index > next.length ? next.length : index, removed);
records = next;
total += 1;
hasMore = records.length < total;
actionError = 'recordDeleteFailed';
if (failure is ApiException) lastErrorCode = failure.code;
return false;
} finally {
_notify();
@@ -179,16 +186,18 @@ class WatchRecordsController extends ChangeNotifier {
loadingMore = false;
clearing = true;
actionError = null;
lastErrorCode = null;
_notify();
try {
await _history.clear();
return true;
} catch (_) {
} catch (failure) {
records = previous;
total = previousTotal;
_pageIndex = previousPageIndex;
hasMore = records.length < total;
actionError = 'recordClearFailed';
if (failure is ApiException) lastErrorCode = failure.code;
return false;
} finally {
clearing = false;
+6 -10
View File
@@ -6,7 +6,6 @@ import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../../core/data/models/drama.dart';
import '../../core/data/services/auth_controller.dart';
import '../../core/data/services/favorite_controller.dart';
import '../../core/formatting/count_format.dart';
import '../../core/formatting/error_text.dart';
@@ -77,11 +76,6 @@ class _FavoritesContentState extends State<FavoritesContent> {
@override
void didChangeDependencies() {
super.didChangeDependencies();
final signedIn = context.watch<AuthController>().isSignedIn;
if (!signedIn) {
_requested = false;
return;
}
if (_requested) return;
_requested = true;
final favorites = context.read<FavoriteController>();
@@ -93,7 +87,6 @@ class _FavoritesContentState extends State<FavoritesContent> {
@override
Widget build(BuildContext context) {
final l10n = AppLocalizations.of(context)!;
final signedIn = context.watch<AuthController>().isSignedIn;
return SafeArea(
bottom: false,
child: Column(
@@ -104,9 +97,9 @@ class _FavoritesContentState extends State<FavoritesContent> {
showBack: widget.showBack,
),
Expanded(
child: signedIn
? _FavoritesBody(favorites: context.watch<FavoriteController>())
: const AuthGatePanel(),
child: _FavoritesBody(
favorites: context.watch<FavoriteController>(),
),
),
],
),
@@ -122,6 +115,9 @@ class _FavoritesBody extends StatelessWidget {
@override
Widget build(BuildContext context) {
final l10n = AppLocalizations.of(context)!;
if (favorites.lastErrorCode == 200007) {
return const AuthGatePanel();
}
if (favorites.loading && favorites.cards.isEmpty) {
return const Center(
child: CircularProgressIndicator(key: Key('favorites-loading')),
@@ -35,7 +35,6 @@ class WatchRecordsPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
final signedIn = context.watch<AuthController>().isSignedIn;
final l10n = AppLocalizations.of(context)!;
return Theme(
data: AppTheme.dark(),
@@ -49,11 +48,7 @@ class WatchRecordsPage extends StatelessWidget {
title: l10n.watchHistory,
backKey: const Key('watch-records-back'),
),
Expanded(
child: signedIn
? const _WatchRecordsBody()
: const AuthGatePanel(),
),
Expanded(child: const _WatchRecordsBody()),
],
),
),
@@ -71,10 +66,15 @@ class _WatchRecordsBody extends StatefulWidget {
class _WatchRecordsBodyState extends State<_WatchRecordsBody> {
late final WatchRecordsController controller;
late final AuthController _auth;
late bool _signedIn;
@override
void initState() {
super.initState();
_auth = context.read<AuthController>();
_signedIn = _auth.isSignedIn;
_auth.addListener(_handleAuthChanged);
controller = WatchRecordsBinding().create(
history: context.read<WatchHistoryRepository>(),
catalog: context.read<CatalogRepository>(),
@@ -82,8 +82,16 @@ class _WatchRecordsBodyState extends State<_WatchRecordsBody> {
unawaited(controller.load());
}
void _handleAuthChanged() {
final signedIn = _auth.isSignedIn;
if (_signedIn == signedIn || !mounted) return;
_signedIn = signedIn;
unawaited(controller.load());
}
@override
void dispose() {
_auth.removeListener(_handleAuthChanged);
controller.dispose();
super.dispose();
}
@@ -98,6 +106,9 @@ class _WatchRecordsBodyState extends State<_WatchRecordsBody> {
Widget _content(BuildContext context) {
final l10n = AppLocalizations.of(context)!;
if (controller.lastErrorCode == 200007) {
return const AuthGatePanel();
}
if (controller.loading && controller.records.isEmpty) {
return const Center(
child: CircularProgressIndicator(key: Key('watch-records-loading')),