465 lines
16 KiB
Dart
465 lines
16 KiB
Dart
// Author: fruit.com.cn
|
|
|
|
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:provider/provider.dart';
|
|
|
|
import '../../core/data/models/drama.dart';
|
|
import '../../core/data/services/favorite_controller.dart';
|
|
import '../../core/formatting/count_format.dart';
|
|
import '../../core/formatting/error_text.dart';
|
|
import '../../core/platform/haptics.dart';
|
|
import '../../core/theme/app_colors.dart';
|
|
import '../../core/theme/app_dimens.dart';
|
|
import '../../core/theme/app_theme.dart';
|
|
import '../../core/widgets/app_pull_to_refresh.dart';
|
|
import '../../core/widgets/auth_gate.dart';
|
|
import '../../core/widgets/empty_state.dart';
|
|
import '../../core/widgets/paged_list_footer.dart';
|
|
import '../../core/widgets/remote_image.dart';
|
|
import '../../core/widgets/subpage_header.dart';
|
|
import '../../l10n/app_localizations.dart';
|
|
import '../player/open_player.dart';
|
|
import 'favorites_binding.dart';
|
|
|
|
const double _pageHorizontal = 14;
|
|
const double _cardRadius = AppDimens.cardRadius;
|
|
const double _cardPadding = 10;
|
|
const double _coverWidth = 80;
|
|
const double _coverHeight = 106;
|
|
const double _coverRadius = 10;
|
|
const double _removeSize = 28;
|
|
const double _removeIconSize = 20;
|
|
const double _statsFontSize = 12;
|
|
const double _metaFontSize = 11;
|
|
const double _titleFontSize = 15;
|
|
const double _synopsisFontSize = 12;
|
|
const double _synopsisLineHeight = 1.15;
|
|
const double _synopsisGap = 4;
|
|
const int _maxTags = 2;
|
|
const double _loadMoreThreshold = 240;
|
|
|
|
final RegExp _tagSplitter = RegExp(r'[,/、·]');
|
|
|
|
/// 独立我的收藏页。
|
|
class FavoritesPage extends StatelessWidget {
|
|
const FavoritesPage({super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Theme(
|
|
data: AppTheme.dark(),
|
|
child: Scaffold(
|
|
key: const Key('favorites-page'),
|
|
backgroundColor: AppColors.ink,
|
|
body: const SafeArea(child: FavoritesContent()),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
/// 收藏列表主体;可独立使用,也可直接嵌入根导航。
|
|
class FavoritesContent extends StatefulWidget {
|
|
const FavoritesContent({super.key, this.showBack = true});
|
|
|
|
final bool showBack;
|
|
|
|
@override
|
|
State<FavoritesContent> createState() => _FavoritesContentState();
|
|
}
|
|
|
|
class _FavoritesContentState extends State<FavoritesContent> {
|
|
bool _requested = false;
|
|
|
|
@override
|
|
void didChangeDependencies() {
|
|
super.didChangeDependencies();
|
|
if (_requested) return;
|
|
_requested = true;
|
|
final favorites = context.read<FavoriteController>();
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
if (mounted) unawaited(FavoritesBinding().load(favorites));
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final l10n = AppLocalizations.of(context)!;
|
|
return SafeArea(
|
|
bottom: false,
|
|
child: Column(
|
|
children: [
|
|
AppSubpageHeader(
|
|
title: l10n.myFavorites,
|
|
backKey: const Key('favorites-back'),
|
|
showBack: widget.showBack,
|
|
),
|
|
Expanded(
|
|
child: _FavoritesBody(
|
|
favorites: context.watch<FavoriteController>(),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class _FavoritesBody extends StatelessWidget {
|
|
const _FavoritesBody({required this.favorites});
|
|
|
|
final FavoriteController favorites;
|
|
|
|
@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')),
|
|
);
|
|
}
|
|
if (favorites.error != null && favorites.cards.isEmpty) {
|
|
return _refreshableState(
|
|
favorites,
|
|
child: EmptyState(
|
|
key: const Key('favorites-error'),
|
|
icon: Icons.cloud_off_rounded,
|
|
title: errorText(l10n, favorites.error, l10n.favoritesError),
|
|
actionLabel: l10n.retry,
|
|
actionKey: const Key('favorites-retry'),
|
|
onAction: () => unawaited(favorites.load()),
|
|
stretchContent: true,
|
|
),
|
|
);
|
|
}
|
|
if (favorites.cards.isEmpty) {
|
|
return _refreshableState(
|
|
favorites,
|
|
child: EmptyState(
|
|
key: const Key('favorites-empty'),
|
|
icon: Icons.star_border_rounded,
|
|
title: l10n.noFavorites,
|
|
subtitle: l10n.emptyHint,
|
|
stretchContent: true,
|
|
),
|
|
);
|
|
}
|
|
return _refreshable(
|
|
favorites,
|
|
children: [
|
|
_summary(l10n),
|
|
for (final (index, drama) in favorites.cards.indexed) ...[
|
|
if (index > 0) const SizedBox(height: 10),
|
|
_FavoriteCard(
|
|
drama: drama,
|
|
isFavorite: favorites.isSaved(drama.id),
|
|
onOpen: () => _open(context, drama),
|
|
onRemove: () => _remove(context, drama.id),
|
|
),
|
|
],
|
|
PagedListFooter(
|
|
loading: favorites.loadingMore,
|
|
hasMore: favorites.hasMore,
|
|
hasItems: favorites.cards.isNotEmpty,
|
|
errorMessage: favorites.loadMoreError == null
|
|
? null
|
|
: errorText(l10n, favorites.loadMoreError, l10n.favoritesError),
|
|
onRetry: favorites.loadMore,
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _refreshable(
|
|
FavoriteController favorites, {
|
|
required List<Widget> children,
|
|
}) {
|
|
return NotificationListener<ScrollNotification>(
|
|
onNotification: _onScroll,
|
|
child: AppPullToRefresh(
|
|
onRefresh: favorites.load,
|
|
child: ListView(
|
|
physics: AppPullToRefresh.physics,
|
|
padding: const EdgeInsets.fromLTRB(
|
|
_pageHorizontal,
|
|
4,
|
|
_pageHorizontal,
|
|
28,
|
|
),
|
|
children: children,
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _refreshableState(
|
|
FavoriteController favorites, {
|
|
required Widget child,
|
|
}) {
|
|
return AppPullToRefresh(
|
|
onRefresh: favorites.load,
|
|
child: CustomScrollView(
|
|
key: const Key('favorites-empty-scroll'),
|
|
physics: AppPullToRefresh.physics,
|
|
slivers: [SliverFillRemaining(hasScrollBody: false, child: child)],
|
|
),
|
|
);
|
|
}
|
|
|
|
bool _onScroll(ScrollNotification notification) {
|
|
if (notification.depth != 0) return false;
|
|
final reachedBottom =
|
|
notification.metrics.extentAfter <= _loadMoreThreshold;
|
|
if (reachedBottom &&
|
|
(notification is ScrollUpdateNotification ||
|
|
notification is ScrollEndNotification ||
|
|
notification is OverscrollNotification)) {
|
|
unawaited(favorites.loadMore());
|
|
}
|
|
return false;
|
|
}
|
|
|
|
Widget _summary(AppLocalizations l10n) {
|
|
return Align(
|
|
alignment: Alignment.centerLeft,
|
|
child: Text(
|
|
l10n.favoritesCount(favorites.total),
|
|
key: const Key('favorites-count'),
|
|
style: const TextStyle(
|
|
color: AppColors.muted,
|
|
fontSize: _statsFontSize,
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Future<void> _open(BuildContext context, Drama drama) async {
|
|
await playDramaFromCatalog(context, drama);
|
|
}
|
|
|
|
Future<void> _remove(BuildContext context, String dramaId) async {
|
|
unawaited(context.read<Haptics>().lightImpact());
|
|
final ok = await favorites.remove(dramaId);
|
|
if (ok || !context.mounted) return;
|
|
final l10n = AppLocalizations.of(context)!;
|
|
ScaffoldMessenger.of(
|
|
context,
|
|
).showSnackBar(SnackBar(content: Text(l10n.favoriteToggleFailed)));
|
|
}
|
|
}
|
|
|
|
class _FavoriteCard extends StatelessWidget {
|
|
const _FavoriteCard({
|
|
required this.drama,
|
|
required this.isFavorite,
|
|
required this.onOpen,
|
|
required this.onRemove,
|
|
});
|
|
|
|
final Drama drama;
|
|
final bool isFavorite;
|
|
final VoidCallback onOpen;
|
|
final VoidCallback onRemove;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final l10n = AppLocalizations.of(context)!;
|
|
final languageCode = Localizations.localeOf(context).languageCode;
|
|
final tags = _tags(languageCode);
|
|
final synopsis = drama.localizedSynopsis(languageCode).trim();
|
|
return Container(
|
|
key: ValueKey('favorite-card-${drama.id}'),
|
|
decoration: BoxDecoration(
|
|
color: AppColors.card,
|
|
borderRadius: BorderRadius.circular(_cardRadius),
|
|
border: Border.all(color: AppColors.border),
|
|
),
|
|
child: Material(
|
|
color: AppColors.transparent,
|
|
borderRadius: BorderRadius.circular(_cardRadius),
|
|
clipBehavior: Clip.antiAlias,
|
|
child: InkWell(
|
|
onTap: onOpen,
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(_cardPadding),
|
|
child: Stack(
|
|
children: [
|
|
Row(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
ClipRRect(
|
|
borderRadius: BorderRadius.circular(_coverRadius),
|
|
child: RemoteImage(
|
|
drama.posterUrl,
|
|
key: ValueKey('favorite-cover-${drama.id}'),
|
|
width: _coverWidth,
|
|
height: _coverHeight,
|
|
),
|
|
),
|
|
const SizedBox(width: 12),
|
|
Expanded(
|
|
child: Padding(
|
|
padding: const EdgeInsets.only(
|
|
right: _removeSize,
|
|
top: 2,
|
|
),
|
|
child: SizedBox(
|
|
height: _coverHeight,
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
drama.localizedTitle(languageCode),
|
|
key: ValueKey('favorite-title-${drama.id}'),
|
|
maxLines: 2,
|
|
overflow: TextOverflow.ellipsis,
|
|
style: const TextStyle(
|
|
color: AppColors.onSurface,
|
|
fontSize: _titleFontSize,
|
|
fontWeight: FontWeight.w700,
|
|
),
|
|
),
|
|
const SizedBox(height: 6),
|
|
if (tags.isNotEmpty)
|
|
SizedBox(
|
|
height: 20,
|
|
child: SingleChildScrollView(
|
|
scrollDirection: Axis.horizontal,
|
|
physics:
|
|
const NeverScrollableScrollPhysics(),
|
|
child: Row(
|
|
children: [
|
|
for (final tag in tags)
|
|
Padding(
|
|
padding: const EdgeInsets.only(
|
|
right: 5,
|
|
),
|
|
child: Container(
|
|
padding:
|
|
const EdgeInsets.symmetric(
|
|
horizontal: 6,
|
|
vertical: 2,
|
|
),
|
|
decoration: BoxDecoration(
|
|
color: AppColors.raised,
|
|
borderRadius:
|
|
BorderRadius.circular(6),
|
|
),
|
|
child: Text(
|
|
tag,
|
|
maxLines: 1,
|
|
overflow: TextOverflow.ellipsis,
|
|
style: const TextStyle(
|
|
color: AppColors.muted,
|
|
fontSize: _metaFontSize,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
if (synopsis.isNotEmpty) ...[
|
|
const SizedBox(height: _synopsisGap),
|
|
Flexible(
|
|
child: Text(
|
|
synopsis,
|
|
key: ValueKey(
|
|
'favorite-synopsis-${drama.id}',
|
|
),
|
|
maxLines: 2,
|
|
overflow: TextOverflow.ellipsis,
|
|
style: const TextStyle(
|
|
color: AppColors.muted,
|
|
fontSize: _synopsisFontSize,
|
|
height: _synopsisLineHeight,
|
|
),
|
|
),
|
|
),
|
|
] else
|
|
const Spacer(),
|
|
Row(
|
|
children: [
|
|
const Icon(
|
|
Icons.local_fire_department_rounded,
|
|
color: AppColors.blue,
|
|
size: 13,
|
|
),
|
|
const SizedBox(width: 3),
|
|
Text(
|
|
formatInteractionCount(drama.viewCount),
|
|
style: const TextStyle(
|
|
color: AppColors.muted,
|
|
fontSize: _metaFontSize,
|
|
),
|
|
),
|
|
const Spacer(),
|
|
Text(
|
|
l10n.episodeCountLabel(drama.episodeCount),
|
|
style: const TextStyle(
|
|
color: AppColors.muted,
|
|
fontSize: _metaFontSize,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
Positioned(
|
|
top: 0,
|
|
right: 0,
|
|
child: SizedBox.square(
|
|
dimension: _removeSize,
|
|
child: Material(
|
|
color: AppColors.raised,
|
|
shape: const CircleBorder(),
|
|
clipBehavior: Clip.antiAlias,
|
|
child: IconButton(
|
|
key: ValueKey('favorite-remove-${drama.id}'),
|
|
tooltip: l10n.remove,
|
|
padding: EdgeInsets.zero,
|
|
onPressed: onRemove,
|
|
icon: Icon(
|
|
isFavorite
|
|
? Icons.star_rounded
|
|
: Icons.star_border_rounded,
|
|
color: isFavorite ? AppColors.blue : AppColors.muted,
|
|
size: _removeIconSize,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
List<String> _tags(String languageCode) {
|
|
final localized = drama.localizedTypeLabels(languageCode);
|
|
if (localized.isNotEmpty) return localized.take(_maxTags).toList();
|
|
final values = <String>[];
|
|
for (final source in [drama.genre, drama.category]) {
|
|
for (final part in source.split(_tagSplitter)) {
|
|
final value = part.trim();
|
|
if (value.isNotEmpty && !values.contains(value)) values.add(value);
|
|
if (values.length == _maxTags) return values;
|
|
}
|
|
}
|
|
return values;
|
|
}
|
|
}
|