feat(library): 支持设备追剧与观看历史
This commit is contained in:
1 parent
4c3acb047c
commit
0d785bf1f8
12 files changed
+232
-76
No files matched your search
+5
-12
@@ -141,17 +141,11 @@ class _LumaDramaAppState extends State<LumaDramaApp> {
|
|||||||
|
|
||||||
late final WatchHistoryRepository _watchHistory =
|
late final WatchHistoryRepository _watchHistory =
|
||||||
widget.watchHistoryRepository ??
|
widget.watchHistoryRepository ??
|
||||||
HttpWatchHistoryRepository(
|
HttpWatchHistoryRepository(WatchApi(_apiClient));
|
||||||
WatchApi(_apiClient),
|
|
||||||
isSignedIn: () => _auth.isSignedIn,
|
|
||||||
);
|
|
||||||
|
|
||||||
late final FavoriteRepository _favorites =
|
late final FavoriteRepository _favorites =
|
||||||
widget.favoriteRepository ??
|
widget.favoriteRepository ??
|
||||||
HttpFavoriteRepository(
|
HttpFavoriteRepository(LibraryApi(_apiClient));
|
||||||
LibraryApi(_apiClient),
|
|
||||||
isSignedIn: () => _auth.isSignedIn,
|
|
||||||
);
|
|
||||||
|
|
||||||
late final PlaybackRepository _playback =
|
late final PlaybackRepository _playback =
|
||||||
widget.playbackRepository ??
|
widget.playbackRepository ??
|
||||||
@@ -219,7 +213,7 @@ class _LumaDramaAppState extends State<LumaDramaApp> {
|
|||||||
unawaited(_localeController.load());
|
unawaited(_localeController.load());
|
||||||
// 登录/登出时同步收藏状态:收藏属于账号,不能跨账号串联
|
// 登录/登出时同步收藏状态:收藏属于账号,不能跨账号串联
|
||||||
_auth.addListener(_onAuthChanged);
|
_auth.addListener(_onAuthChanged);
|
||||||
if (_auth.isSignedIn) unawaited(_favoriteController.load());
|
unawaited(_favoriteController.load());
|
||||||
if (_auth.isSignedIn) unawaited(_walletController.load());
|
if (_auth.isSignedIn) unawaited(_walletController.load());
|
||||||
unawaited(_downloadController.restore());
|
unawaited(_downloadController.restore());
|
||||||
}
|
}
|
||||||
@@ -235,6 +229,7 @@ class _LumaDramaAppState extends State<LumaDramaApp> {
|
|||||||
unawaited(_walletController.load());
|
unawaited(_walletController.load());
|
||||||
} else {
|
} else {
|
||||||
_favoriteController.clear();
|
_favoriteController.clear();
|
||||||
|
unawaited(_favoriteController.load());
|
||||||
_downloadController.reset();
|
_downloadController.reset();
|
||||||
_walletController.reset();
|
_walletController.reset();
|
||||||
}
|
}
|
||||||
@@ -293,9 +288,7 @@ class _LumaDramaAppState extends State<LumaDramaApp> {
|
|||||||
),
|
),
|
||||||
ChangeNotifierProvider(create: (_) => _createCategories()),
|
ChangeNotifierProvider(create: (_) => _createCategories()),
|
||||||
if (playlists != null)
|
if (playlists != null)
|
||||||
ChangeNotifierProvider(
|
ChangeNotifierProvider(create: (_) => PlaylistController(playlists)),
|
||||||
create: (_) => PlaylistController(playlists),
|
|
||||||
),
|
|
||||||
ChangeNotifierProvider.value(value: _favoriteController),
|
ChangeNotifierProvider.value(value: _favoriteController),
|
||||||
ChangeNotifierProvider.value(value: _downloadController),
|
ChangeNotifierProvider.value(value: _downloadController),
|
||||||
ChangeNotifierProvider.value(value: _walletController),
|
ChangeNotifierProvider.value(value: _walletController),
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import '../models/page_result.dart';
|
|||||||
import '../models/watch_progress.dart';
|
import '../models/watch_progress.dart';
|
||||||
import 'api_client.dart';
|
import 'api_client.dart';
|
||||||
|
|
||||||
/// 观看进度接口:`/app/watch/*`(契约 §1.5)。全部需要登录态。
|
/// 观看进度接口:`/app/watch/*`(契约 §1.5)。支持账号或设备身份。
|
||||||
class WatchApi {
|
class WatchApi {
|
||||||
const WatchApi(this._client);
|
const WatchApi(this._client);
|
||||||
|
|
||||||
|
|||||||
@@ -19,21 +19,15 @@ abstract interface class FavoriteRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class HttpFavoriteRepository implements FavoriteRepository {
|
class HttpFavoriteRepository implements FavoriteRepository {
|
||||||
const HttpFavoriteRepository(this._api, {this.isSignedIn});
|
const HttpFavoriteRepository(this._api);
|
||||||
|
|
||||||
final LibraryApi _api;
|
final LibraryApi _api;
|
||||||
|
|
||||||
/// 未登录时不请求:收藏属于账号,匿名浏览时列表按空处理(点收藏按钮会先引导登录)。
|
|
||||||
final bool Function()? isSignedIn;
|
|
||||||
|
|
||||||
bool get _signedIn => isSignedIn?.call() ?? true;
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<bool> toggle(String dramaId) => _api.toggleFavorite(dramaId);
|
Future<bool> toggle(String dramaId) => _api.toggleFavorite(dramaId);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<PageResult<Drama>> page({int pageIndex = 1, int pageSize = 20}) async {
|
Future<PageResult<Drama>> page({int pageIndex = 1, int pageSize = 20}) async {
|
||||||
if (!_signedIn) return const PageResult();
|
|
||||||
return _api.favorites(pageIndex: pageIndex, pageSize: pageSize);
|
return _api.favorites(pageIndex: pageIndex, pageSize: pageSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -35,18 +35,12 @@ abstract interface class WatchHistoryRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class HttpWatchHistoryRepository implements WatchHistoryRepository {
|
class HttpWatchHistoryRepository implements WatchHistoryRepository {
|
||||||
const HttpWatchHistoryRepository(this._api, {this.isSignedIn});
|
const HttpWatchHistoryRepository(this._api);
|
||||||
|
|
||||||
final WatchApi _api;
|
final WatchApi _api;
|
||||||
|
|
||||||
/// 未登录时这些接口必然 401:直接短路成"没有记录",让浏览/试看保持匿名可用。
|
|
||||||
final bool Function()? isSignedIn;
|
|
||||||
|
|
||||||
bool get _signedIn => isSignedIn?.call() ?? true;
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<WatchProgress?> progressFor(String dramaId) async {
|
Future<WatchProgress?> progressFor(String dramaId) async {
|
||||||
if (!_signedIn) return null;
|
|
||||||
return _api.progressOfDrama(dramaId);
|
return _api.progressOfDrama(dramaId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,7 +51,6 @@ class HttpWatchHistoryRepository implements WatchHistoryRepository {
|
|||||||
Duration? duration,
|
Duration? duration,
|
||||||
bool finished = false,
|
bool finished = false,
|
||||||
}) async {
|
}) async {
|
||||||
if (!_signedIn) return null;
|
|
||||||
return _api.saveProgress(
|
return _api.saveProgress(
|
||||||
episodeId: episodeId,
|
episodeId: episodeId,
|
||||||
position: position,
|
position: position,
|
||||||
@@ -71,19 +64,16 @@ class HttpWatchHistoryRepository implements WatchHistoryRepository {
|
|||||||
int pageIndex = 1,
|
int pageIndex = 1,
|
||||||
int pageSize = 20,
|
int pageSize = 20,
|
||||||
}) async {
|
}) async {
|
||||||
if (!_signedIn) return const PageResult();
|
|
||||||
return _api.recent(pageIndex: pageIndex, pageSize: pageSize);
|
return _api.recent(pageIndex: pageIndex, pageSize: pageSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> remove(String dramaId) async {
|
Future<void> remove(String dramaId) async {
|
||||||
if (!_signedIn) return;
|
|
||||||
await _api.removeProgress(dramaId);
|
await _api.removeProgress(dramaId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> clear() async {
|
Future<void> clear() async {
|
||||||
if (!_signedIn) return;
|
|
||||||
await _api.clearProgress();
|
await _api.clearProgress();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -93,6 +93,7 @@ class FavoriteController extends ChangeNotifier {
|
|||||||
loadingMore = false;
|
loadingMore = false;
|
||||||
error = null;
|
error = null;
|
||||||
loadMoreError = null;
|
loadMoreError = null;
|
||||||
|
lastErrorCode = null;
|
||||||
notify();
|
notify();
|
||||||
try {
|
try {
|
||||||
final page = await _repository.page(pageIndex: 1, pageSize: pageSize);
|
final page = await _repository.page(pageIndex: 1, pageSize: pageSize);
|
||||||
@@ -106,9 +107,10 @@ class FavoriteController extends ChangeNotifier {
|
|||||||
total = page.total;
|
total = page.total;
|
||||||
_pageIndex = 1;
|
_pageIndex = 1;
|
||||||
hasMore = page.list.isNotEmpty && cards.length < total;
|
hasMore = page.list.isNotEmpty && cards.length < total;
|
||||||
} catch (_) {
|
} catch (failure) {
|
||||||
if (!_disposed && request == _requestSeq) {
|
if (!_disposed && request == _requestSeq) {
|
||||||
error = 'favoritesError';
|
error = 'favoritesError';
|
||||||
|
if (failure is ApiException) lastErrorCode = failure.code;
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
if (!_disposed && request == _requestSeq) {
|
if (!_disposed && request == _requestSeq) {
|
||||||
@@ -124,6 +126,7 @@ class FavoriteController extends ChangeNotifier {
|
|||||||
final request = ++_requestSeq;
|
final request = ++_requestSeq;
|
||||||
loadingMore = true;
|
loadingMore = true;
|
||||||
loadMoreError = null;
|
loadMoreError = null;
|
||||||
|
lastErrorCode = null;
|
||||||
notify();
|
notify();
|
||||||
try {
|
try {
|
||||||
final page = await _repository.page(
|
final page = await _repository.page(
|
||||||
@@ -141,9 +144,10 @@ class FavoriteController extends ChangeNotifier {
|
|||||||
total = page.total;
|
total = page.total;
|
||||||
_pageIndex += 1;
|
_pageIndex += 1;
|
||||||
hasMore = page.list.isNotEmpty && cards.length < total;
|
hasMore = page.list.isNotEmpty && cards.length < total;
|
||||||
} catch (_) {
|
} catch (failure) {
|
||||||
if (!_disposed && request == _requestSeq) {
|
if (!_disposed && request == _requestSeq) {
|
||||||
loadMoreError = 'favoritesError';
|
loadMoreError = 'favoritesError';
|
||||||
|
if (failure is ApiException) lastErrorCode = failure.code;
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
if (!_disposed && request == _requestSeq) {
|
if (!_disposed && request == _requestSeq) {
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ class WatchRecordsController extends ChangeNotifier {
|
|||||||
String? error;
|
String? error;
|
||||||
String? loadMoreError;
|
String? loadMoreError;
|
||||||
String? actionError;
|
String? actionError;
|
||||||
|
int? lastErrorCode;
|
||||||
int _pageIndex = 0;
|
int _pageIndex = 0;
|
||||||
int _requestSeq = 0;
|
int _requestSeq = 0;
|
||||||
bool _disposed = false;
|
bool _disposed = false;
|
||||||
@@ -50,6 +51,7 @@ class WatchRecordsController extends ChangeNotifier {
|
|||||||
error = null;
|
error = null;
|
||||||
loadMoreError = null;
|
loadMoreError = null;
|
||||||
actionError = null;
|
actionError = null;
|
||||||
|
lastErrorCode = null;
|
||||||
_notify();
|
_notify();
|
||||||
try {
|
try {
|
||||||
final page = await _history.recent(pageIndex: 1, pageSize: pageSize);
|
final page = await _history.recent(pageIndex: 1, pageSize: pageSize);
|
||||||
@@ -71,6 +73,7 @@ class WatchRecordsController extends ChangeNotifier {
|
|||||||
);
|
);
|
||||||
if (!_disposed && request == _requestSeq) {
|
if (!_disposed && request == _requestSeq) {
|
||||||
error = failure is ApiException ? failure.message : 'progressError';
|
error = failure is ApiException ? failure.message : 'progressError';
|
||||||
|
if (failure is ApiException) lastErrorCode = failure.code;
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
if (!_disposed && request == _requestSeq) {
|
if (!_disposed && request == _requestSeq) {
|
||||||
@@ -86,6 +89,7 @@ class WatchRecordsController extends ChangeNotifier {
|
|||||||
final request = ++_requestSeq;
|
final request = ++_requestSeq;
|
||||||
loadingMore = true;
|
loadingMore = true;
|
||||||
loadMoreError = null;
|
loadMoreError = null;
|
||||||
|
lastErrorCode = null;
|
||||||
_notify();
|
_notify();
|
||||||
try {
|
try {
|
||||||
final page = await _history.recent(
|
final page = await _history.recent(
|
||||||
@@ -108,6 +112,7 @@ class WatchRecordsController extends ChangeNotifier {
|
|||||||
loadMoreError = failure is ApiException
|
loadMoreError = failure is ApiException
|
||||||
? failure.message
|
? failure.message
|
||||||
: 'progressError';
|
: 'progressError';
|
||||||
|
if (failure is ApiException) lastErrorCode = failure.code;
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
if (!_disposed && request == _requestSeq) {
|
if (!_disposed && request == _requestSeq) {
|
||||||
@@ -148,17 +153,19 @@ class WatchRecordsController extends ChangeNotifier {
|
|||||||
total = total > 0 ? total - 1 : 0;
|
total = total > 0 ? total - 1 : 0;
|
||||||
hasMore = records.length < total;
|
hasMore = records.length < total;
|
||||||
actionError = null;
|
actionError = null;
|
||||||
|
lastErrorCode = null;
|
||||||
_notify();
|
_notify();
|
||||||
try {
|
try {
|
||||||
await _history.remove(dramaId);
|
await _history.remove(dramaId);
|
||||||
return true;
|
return true;
|
||||||
} catch (_) {
|
} catch (failure) {
|
||||||
final next = [...records];
|
final next = [...records];
|
||||||
next.insert(index > next.length ? next.length : index, removed);
|
next.insert(index > next.length ? next.length : index, removed);
|
||||||
records = next;
|
records = next;
|
||||||
total += 1;
|
total += 1;
|
||||||
hasMore = records.length < total;
|
hasMore = records.length < total;
|
||||||
actionError = 'recordDeleteFailed';
|
actionError = 'recordDeleteFailed';
|
||||||
|
if (failure is ApiException) lastErrorCode = failure.code;
|
||||||
return false;
|
return false;
|
||||||
} finally {
|
} finally {
|
||||||
_notify();
|
_notify();
|
||||||
@@ -179,16 +186,18 @@ class WatchRecordsController extends ChangeNotifier {
|
|||||||
loadingMore = false;
|
loadingMore = false;
|
||||||
clearing = true;
|
clearing = true;
|
||||||
actionError = null;
|
actionError = null;
|
||||||
|
lastErrorCode = null;
|
||||||
_notify();
|
_notify();
|
||||||
try {
|
try {
|
||||||
await _history.clear();
|
await _history.clear();
|
||||||
return true;
|
return true;
|
||||||
} catch (_) {
|
} catch (failure) {
|
||||||
records = previous;
|
records = previous;
|
||||||
total = previousTotal;
|
total = previousTotal;
|
||||||
_pageIndex = previousPageIndex;
|
_pageIndex = previousPageIndex;
|
||||||
hasMore = records.length < total;
|
hasMore = records.length < total;
|
||||||
actionError = 'recordClearFailed';
|
actionError = 'recordClearFailed';
|
||||||
|
if (failure is ApiException) lastErrorCode = failure.code;
|
||||||
return false;
|
return false;
|
||||||
} finally {
|
} finally {
|
||||||
clearing = false;
|
clearing = false;
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import 'package:flutter/material.dart';
|
|||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
import '../../core/data/models/drama.dart';
|
import '../../core/data/models/drama.dart';
|
||||||
import '../../core/data/services/auth_controller.dart';
|
|
||||||
import '../../core/data/services/favorite_controller.dart';
|
import '../../core/data/services/favorite_controller.dart';
|
||||||
import '../../core/formatting/count_format.dart';
|
import '../../core/formatting/count_format.dart';
|
||||||
import '../../core/formatting/error_text.dart';
|
import '../../core/formatting/error_text.dart';
|
||||||
@@ -77,11 +76,6 @@ class _FavoritesContentState extends State<FavoritesContent> {
|
|||||||
@override
|
@override
|
||||||
void didChangeDependencies() {
|
void didChangeDependencies() {
|
||||||
super.didChangeDependencies();
|
super.didChangeDependencies();
|
||||||
final signedIn = context.watch<AuthController>().isSignedIn;
|
|
||||||
if (!signedIn) {
|
|
||||||
_requested = false;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (_requested) return;
|
if (_requested) return;
|
||||||
_requested = true;
|
_requested = true;
|
||||||
final favorites = context.read<FavoriteController>();
|
final favorites = context.read<FavoriteController>();
|
||||||
@@ -93,7 +87,6 @@ class _FavoritesContentState extends State<FavoritesContent> {
|
|||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final l10n = AppLocalizations.of(context)!;
|
final l10n = AppLocalizations.of(context)!;
|
||||||
final signedIn = context.watch<AuthController>().isSignedIn;
|
|
||||||
return SafeArea(
|
return SafeArea(
|
||||||
bottom: false,
|
bottom: false,
|
||||||
child: Column(
|
child: Column(
|
||||||
@@ -104,9 +97,9 @@ class _FavoritesContentState extends State<FavoritesContent> {
|
|||||||
showBack: widget.showBack,
|
showBack: widget.showBack,
|
||||||
),
|
),
|
||||||
Expanded(
|
Expanded(
|
||||||
child: signedIn
|
child: _FavoritesBody(
|
||||||
? _FavoritesBody(favorites: context.watch<FavoriteController>())
|
favorites: context.watch<FavoriteController>(),
|
||||||
: const AuthGatePanel(),
|
),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
@@ -122,6 +115,9 @@ class _FavoritesBody extends StatelessWidget {
|
|||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final l10n = AppLocalizations.of(context)!;
|
final l10n = AppLocalizations.of(context)!;
|
||||||
|
if (favorites.lastErrorCode == 200007) {
|
||||||
|
return const AuthGatePanel();
|
||||||
|
}
|
||||||
if (favorites.loading && favorites.cards.isEmpty) {
|
if (favorites.loading && favorites.cards.isEmpty) {
|
||||||
return const Center(
|
return const Center(
|
||||||
child: CircularProgressIndicator(key: Key('favorites-loading')),
|
child: CircularProgressIndicator(key: Key('favorites-loading')),
|
||||||
|
|||||||
@@ -35,7 +35,6 @@ class WatchRecordsPage extends StatelessWidget {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final signedIn = context.watch<AuthController>().isSignedIn;
|
|
||||||
final l10n = AppLocalizations.of(context)!;
|
final l10n = AppLocalizations.of(context)!;
|
||||||
return Theme(
|
return Theme(
|
||||||
data: AppTheme.dark(),
|
data: AppTheme.dark(),
|
||||||
@@ -49,11 +48,7 @@ class WatchRecordsPage extends StatelessWidget {
|
|||||||
title: l10n.watchHistory,
|
title: l10n.watchHistory,
|
||||||
backKey: const Key('watch-records-back'),
|
backKey: const Key('watch-records-back'),
|
||||||
),
|
),
|
||||||
Expanded(
|
Expanded(child: const _WatchRecordsBody()),
|
||||||
child: signedIn
|
|
||||||
? const _WatchRecordsBody()
|
|
||||||
: const AuthGatePanel(),
|
|
||||||
),
|
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
@@ -71,10 +66,15 @@ class _WatchRecordsBody extends StatefulWidget {
|
|||||||
|
|
||||||
class _WatchRecordsBodyState extends State<_WatchRecordsBody> {
|
class _WatchRecordsBodyState extends State<_WatchRecordsBody> {
|
||||||
late final WatchRecordsController controller;
|
late final WatchRecordsController controller;
|
||||||
|
late final AuthController _auth;
|
||||||
|
late bool _signedIn;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
|
_auth = context.read<AuthController>();
|
||||||
|
_signedIn = _auth.isSignedIn;
|
||||||
|
_auth.addListener(_handleAuthChanged);
|
||||||
controller = WatchRecordsBinding().create(
|
controller = WatchRecordsBinding().create(
|
||||||
history: context.read<WatchHistoryRepository>(),
|
history: context.read<WatchHistoryRepository>(),
|
||||||
catalog: context.read<CatalogRepository>(),
|
catalog: context.read<CatalogRepository>(),
|
||||||
@@ -82,8 +82,16 @@ class _WatchRecordsBodyState extends State<_WatchRecordsBody> {
|
|||||||
unawaited(controller.load());
|
unawaited(controller.load());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _handleAuthChanged() {
|
||||||
|
final signedIn = _auth.isSignedIn;
|
||||||
|
if (_signedIn == signedIn || !mounted) return;
|
||||||
|
_signedIn = signedIn;
|
||||||
|
unawaited(controller.load());
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
|
_auth.removeListener(_handleAuthChanged);
|
||||||
controller.dispose();
|
controller.dispose();
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
@@ -98,6 +106,9 @@ class _WatchRecordsBodyState extends State<_WatchRecordsBody> {
|
|||||||
|
|
||||||
Widget _content(BuildContext context) {
|
Widget _content(BuildContext context) {
|
||||||
final l10n = AppLocalizations.of(context)!;
|
final l10n = AppLocalizations.of(context)!;
|
||||||
|
if (controller.lastErrorCode == 200007) {
|
||||||
|
return const AuthGatePanel();
|
||||||
|
}
|
||||||
if (controller.loading && controller.records.isEmpty) {
|
if (controller.loading && controller.records.isEmpty) {
|
||||||
return const Center(
|
return const Center(
|
||||||
child: CircularProgressIndicator(key: Key('watch-records-loading')),
|
child: CircularProgressIndicator(key: Key('watch-records-loading')),
|
||||||
|
|||||||
@@ -187,22 +187,20 @@ void main() {
|
|||||||
expect(find.byKey(const Key('favorites-retry')), findsOneWidget);
|
expect(find.byKey(const Key('favorites-retry')), findsOneWidget);
|
||||||
});
|
});
|
||||||
|
|
||||||
testWidgets(
|
testWidgets('should_show_device_favorites_when_signed_out', (tester) async {
|
||||||
'should_show_auth_gate_without_loading_favorites_when_signed_out',
|
final auth = await _auth(signedIn: false);
|
||||||
(tester) async {
|
final repository = InMemoryFavoriteRepository(
|
||||||
final auth = await _auth(signedIn: false);
|
saved: {'signal'},
|
||||||
final repository = InMemoryFavoriteRepository(
|
cards: [_card('signal')],
|
||||||
saved: {'signal'},
|
);
|
||||||
cards: [_card('signal')],
|
final favorites = FavoriteController(repository);
|
||||||
);
|
|
||||||
final favorites = FavoriteController(repository);
|
|
||||||
|
|
||||||
await _pumpPage(tester, auth: auth, favorites: favorites);
|
await _pumpPage(tester, auth: auth, favorites: favorites);
|
||||||
|
|
||||||
expect(find.byKey(const Key('auth-gate-panel')), findsOneWidget);
|
expect(find.byKey(const Key('auth-gate-panel')), findsNothing);
|
||||||
expect(repository.pageCalls, 0);
|
expect(find.text('Drama signal'), findsOneWidget);
|
||||||
},
|
expect(repository.pageCalls, 1);
|
||||||
);
|
});
|
||||||
|
|
||||||
testWidgets('should_open_player_when_favorite_card_is_tapped', (
|
testWidgets('should_open_player_when_favorite_card_is_tapped', (
|
||||||
tester,
|
tester,
|
||||||
|
|||||||
@@ -4,8 +4,11 @@ import 'package:flutter/material.dart';
|
|||||||
import 'package:flutter_test/flutter_test.dart';
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
import 'package:fruitshorts/core/data/remote/api_client.dart';
|
import 'package:fruitshorts/core/data/remote/api_client.dart';
|
||||||
import 'package:fruitshorts/core/data/remote/auth_api.dart';
|
import 'package:fruitshorts/core/data/remote/auth_api.dart';
|
||||||
|
import 'package:fruitshorts/core/data/repositories/catalog_repository.dart';
|
||||||
|
import 'package:fruitshorts/core/data/repositories/watch_history_repository.dart';
|
||||||
import 'package:fruitshorts/core/data/services/auth_controller.dart';
|
import 'package:fruitshorts/core/data/services/auth_controller.dart';
|
||||||
import 'package:fruitshorts/core/data/services/auth_session_store.dart';
|
import 'package:fruitshorts/core/data/services/auth_session_store.dart';
|
||||||
|
import 'package:fruitshorts/core/platform/haptics.dart';
|
||||||
import 'package:fruitshorts/core/theme/app_colors.dart';
|
import 'package:fruitshorts/core/theme/app_colors.dart';
|
||||||
import 'package:fruitshorts/core/theme/app_theme.dart';
|
import 'package:fruitshorts/core/theme/app_theme.dart';
|
||||||
import 'package:fruitshorts/l10n/app_localizations.dart';
|
import 'package:fruitshorts/l10n/app_localizations.dart';
|
||||||
@@ -15,6 +18,8 @@ import 'package:shared_preferences_platform_interface/in_memory_shared_preferenc
|
|||||||
import 'package:shared_preferences_platform_interface/shared_preferences_async_platform_interface.dart';
|
import 'package:shared_preferences_platform_interface/shared_preferences_async_platform_interface.dart';
|
||||||
|
|
||||||
import '../support/fake_http_adapter.dart';
|
import '../support/fake_http_adapter.dart';
|
||||||
|
import '../support/fake_catalog.dart';
|
||||||
|
import '../support/fake_haptics.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
setUp(() {
|
setUp(() {
|
||||||
@@ -92,8 +97,15 @@ void main() {
|
|||||||
);
|
);
|
||||||
|
|
||||||
await tester.pumpWidget(
|
await tester.pumpWidget(
|
||||||
ChangeNotifierProvider<AuthController>.value(
|
MultiProvider(
|
||||||
value: auth,
|
providers: [
|
||||||
|
ChangeNotifierProvider<AuthController>.value(value: auth),
|
||||||
|
Provider<WatchHistoryRepository>.value(
|
||||||
|
value: InMemoryWatchHistoryRepository(),
|
||||||
|
),
|
||||||
|
Provider<CatalogRepository>.value(value: FakeCatalogRepository()),
|
||||||
|
Provider<Haptics>.value(value: FakeHaptics()),
|
||||||
|
],
|
||||||
child: MaterialApp(
|
child: MaterialApp(
|
||||||
theme: AppTheme.dark(),
|
theme: AppTheme.dark(),
|
||||||
darkTheme: AppTheme.dark(),
|
darkTheme: AppTheme.dark(),
|
||||||
@@ -111,7 +123,8 @@ void main() {
|
|||||||
find.byKey(const Key('watch-records-page')),
|
find.byKey(const Key('watch-records-page')),
|
||||||
);
|
);
|
||||||
expect(scaffold.backgroundColor, AppColors.ink);
|
expect(scaffold.backgroundColor, AppColors.ink);
|
||||||
expect(find.byKey(const Key('auth-gate-panel')), findsOneWidget);
|
expect(find.byKey(const Key('auth-gate-panel')), findsNothing);
|
||||||
|
expect(find.byKey(const Key('watch-records-empty')), findsOneWidget);
|
||||||
expect(find.byType(Scaffold), findsOneWidget);
|
expect(find.byType(Scaffold), findsOneWidget);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -5,6 +5,7 @@ import 'dart:convert';
|
|||||||
import 'package:flutter_test/flutter_test.dart';
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
import 'package:fruitshorts/core/data/remote/api_client.dart';
|
import 'package:fruitshorts/core/data/remote/api_client.dart';
|
||||||
import 'package:fruitshorts/core/data/remote/watch_api.dart';
|
import 'package:fruitshorts/core/data/remote/watch_api.dart';
|
||||||
|
import 'package:fruitshorts/core/data/repositories/watch_history_repository.dart';
|
||||||
|
|
||||||
import '../support/fake_http_adapter.dart';
|
import '../support/fake_http_adapter.dart';
|
||||||
|
|
||||||
@@ -94,4 +95,60 @@ void main() {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('should_allow_guest_watch_read_save_and_clear', () async {
|
||||||
|
final adapter = FakeHttpAdapter((options) async {
|
||||||
|
if (options.method == 'GET' && options.uri.path.endsWith('/progress')) {
|
||||||
|
return FakeHttpResponse(
|
||||||
|
jsonEncode({
|
||||||
|
'code': 0,
|
||||||
|
'message': 'ok',
|
||||||
|
'data': {
|
||||||
|
'dramaId': '12',
|
||||||
|
'episodeId': '88',
|
||||||
|
'positionMs': '8000',
|
||||||
|
'durationMs': '60000',
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (options.method == 'GET') {
|
||||||
|
return FakeHttpResponse(
|
||||||
|
jsonEncode({
|
||||||
|
'code': 0,
|
||||||
|
'message': 'ok',
|
||||||
|
'data': {'total': '0', 'list': <Object>[]},
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return FakeHttpResponse(
|
||||||
|
jsonEncode({'code': 0, 'message': 'ok', 'data': null}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
final repository = HttpWatchHistoryRepository(
|
||||||
|
WatchApi(ApiClient(baseUrl: 'http://127.0.0.1:8081', adapter: adapter)),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect((await repository.progressFor('12'))?.episodeId, '88');
|
||||||
|
expect((await repository.recent()).list, isEmpty);
|
||||||
|
await repository.save(
|
||||||
|
episodeId: '88',
|
||||||
|
position: const Duration(seconds: 8),
|
||||||
|
);
|
||||||
|
await repository.remove('12');
|
||||||
|
await repository.clear();
|
||||||
|
|
||||||
|
expect(
|
||||||
|
adapter.requests.map(
|
||||||
|
(request) => '${request.method} ${request.uri.path}',
|
||||||
|
),
|
||||||
|
[
|
||||||
|
'GET /app/watch/progress',
|
||||||
|
'GET /app/watch/recent',
|
||||||
|
'POST /app/watch/progress',
|
||||||
|
'DELETE /app/watch/progress/12',
|
||||||
|
'DELETE /app/watch/progress',
|
||||||
|
],
|
||||||
|
);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
@@ -174,9 +174,7 @@ void main() {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
testWidgets('should_show_auth_gate_without_loading_records_when_signed_out', (
|
testWidgets('should_show_device_history_when_signed_out', (tester) async {
|
||||||
tester,
|
|
||||||
) async {
|
|
||||||
final auth = await _auth(signedIn: false);
|
final auth = await _auth(signedIn: false);
|
||||||
final history = CountingWatchHistory([_progress('the-signal-demo')]);
|
final history = CountingWatchHistory([_progress('the-signal-demo')]);
|
||||||
|
|
||||||
@@ -187,8 +185,59 @@ void main() {
|
|||||||
catalog: FakeCatalogRepository(),
|
catalog: FakeCatalogRepository(),
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(find.byKey(const Key('auth-gate-panel')), findsOneWidget);
|
expect(find.byKey(const Key('auth-gate-panel')), findsNothing);
|
||||||
expect(history.recentCalls, 0);
|
expect(find.text('The Signal'), findsOneWidget);
|
||||||
|
expect(history.recentCalls, 1);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('should_reload_device_history_after_login', (tester) async {
|
||||||
|
final auth = _ToggleAuthController();
|
||||||
|
final history = CountingWatchHistory([_progress('the-signal-demo')]);
|
||||||
|
await _pumpPage(
|
||||||
|
tester,
|
||||||
|
auth: auth,
|
||||||
|
history: history,
|
||||||
|
catalog: FakeCatalogRepository(),
|
||||||
|
);
|
||||||
|
expect(history.recentCalls, 1);
|
||||||
|
|
||||||
|
auth.setSignedIn(true);
|
||||||
|
await tester.pump();
|
||||||
|
await tester.pump(const Duration(milliseconds: 50));
|
||||||
|
|
||||||
|
expect(history.recentCalls, 2);
|
||||||
|
expect(find.text('The Signal'), findsOneWidget);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('should_remove_and_clear_device_history_when_signed_out', (
|
||||||
|
tester,
|
||||||
|
) async {
|
||||||
|
final auth = await _auth(signedIn: false);
|
||||||
|
final history = CountingWatchHistory([
|
||||||
|
_progress('the-signal-demo'),
|
||||||
|
_progress('city-lights-demo'),
|
||||||
|
]);
|
||||||
|
await _pumpPage(
|
||||||
|
tester,
|
||||||
|
auth: auth,
|
||||||
|
history: history,
|
||||||
|
catalog: FakeCatalogRepository(),
|
||||||
|
);
|
||||||
|
|
||||||
|
await tester.tap(
|
||||||
|
find.byKey(const Key('watch-record-remove-the-signal-demo')),
|
||||||
|
);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(find.byKey(const Key('watch-record-the-signal-demo')), findsNothing);
|
||||||
|
expect(history.removeCalls, 1);
|
||||||
|
|
||||||
|
await tester.tap(find.byKey(const Key('watch-records-clear')));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
await tester.tap(find.widgetWithText(FilledButton, 'Clear'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(history.clearCalls, 1);
|
||||||
|
expect(find.byKey(const Key('watch-records-empty')), findsOneWidget);
|
||||||
});
|
});
|
||||||
|
|
||||||
testWidgets(
|
testWidgets(
|
||||||
@@ -313,9 +362,17 @@ Future<AuthController> _auth({required bool signedIn}) async {
|
|||||||
}
|
}
|
||||||
final client = ApiClient(
|
final client = ApiClient(
|
||||||
baseUrl: 'http://127.0.0.1:8081',
|
baseUrl: 'http://127.0.0.1:8081',
|
||||||
adapter: FakeHttpAdapter(
|
adapter: FakeHttpAdapter((options) async {
|
||||||
(_) async => FakeHttpResponse('{"code":0,"message":"ok","data":null}'),
|
if (options.uri.path == '/app/auth/login') {
|
||||||
),
|
return FakeHttpResponse(
|
||||||
|
'{"code":0,"message":"ok","data":{'
|
||||||
|
'"accessToken":"test-access","refreshToken":"test-refresh",'
|
||||||
|
'"expiresIn":7200,"user":{"uid":"1","nickname":"Viewer",'
|
||||||
|
'"avatarUrl":""}}}',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return FakeHttpResponse('{"code":0,"message":"ok","data":null}');
|
||||||
|
}),
|
||||||
);
|
);
|
||||||
final auth = AuthController(AuthApi(client), AuthSessionStore());
|
final auth = AuthController(AuthApi(client), AuthSessionStore());
|
||||||
await auth.load();
|
await auth.load();
|
||||||
@@ -353,6 +410,7 @@ class CountingWatchHistory extends InMemoryWatchHistoryRepository {
|
|||||||
|
|
||||||
final List<WatchProgress> visibleRecords;
|
final List<WatchProgress> visibleRecords;
|
||||||
int recentCalls = 0;
|
int recentCalls = 0;
|
||||||
|
int removeCalls = 0;
|
||||||
int clearCalls = 0;
|
int clearCalls = 0;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -371,6 +429,12 @@ class CountingWatchHistory extends InMemoryWatchHistoryRepository {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> remove(String dramaId) async {
|
||||||
|
removeCalls++;
|
||||||
|
await super.remove(dramaId);
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> clear() async {
|
Future<void> clear() async {
|
||||||
clearCalls++;
|
clearCalls++;
|
||||||
@@ -440,3 +504,30 @@ class _AlwaysFailingWatchCatalog implements CatalogRepository {
|
|||||||
@override
|
@override
|
||||||
Future<HomeFeed> fetchHomeFeed() async => const HomeFeed();
|
Future<HomeFeed> fetchHomeFeed() async => const HomeFeed();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class _ToggleAuthController extends AuthController {
|
||||||
|
_ToggleAuthController()
|
||||||
|
: super(
|
||||||
|
AuthApi(
|
||||||
|
ApiClient(
|
||||||
|
baseUrl: 'http://127.0.0.1:8081',
|
||||||
|
adapter: FakeHttpAdapter(
|
||||||
|
(_) async =>
|
||||||
|
FakeHttpResponse('{"code":0,"message":"ok","data":null}'),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
AuthSessionStore(),
|
||||||
|
);
|
||||||
|
|
||||||
|
bool _signedIn = false;
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool get isSignedIn => _signedIn;
|
||||||
|
|
||||||
|
void setSignedIn(bool value) {
|
||||||
|
if (_signedIn == value) return;
|
||||||
|
_signedIn = value;
|
||||||
|
notifyListeners();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user