为什么:客户端原型在 14d7bd3 之后又做了一轮(页面分层、发现页、分类滑动、播放器控件等),
一直留在工作区没入库。下一批账号链路要改其中的 app.dart / home_view / player_view / mine_view,
先归档这批既存改动,本批的实际 diff 才说得清边界。
只归档,不改行为:当前工作区 flutter analyze 0 问题、flutter test 148 全过。
165 lines
4.2 KiB
Dart
165 lines
4.2 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() {
|
|
position = history.resumePositionFor(demoProfileId, drama);
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|