163 lines
4.1 KiB
Dart
163 lines
4.1 KiB
Dart
// Author: fruit.com.cn
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:provider/provider.dart';
|
|
|
|
import '../../core/data/models/drama.dart';
|
|
import '../../core/data/services/catalog_controller.dart';
|
|
import '../../core/data/services/download_controller.dart';
|
|
import '../../core/data/services/wallet_controller.dart';
|
|
import '../discover/discover_types.dart';
|
|
import '../drama_detail/drama_detail_view.dart';
|
|
import '../favorites/favorites_page.dart';
|
|
import '../mine/mine_types.dart';
|
|
import '../player/player_view.dart';
|
|
import '../watch_records/watch_records_page.dart';
|
|
|
|
class HomeLogic extends ChangeNotifier {
|
|
HomeLogic(List<Drama> dramas) : currentDrama = dramas.first;
|
|
|
|
final scaffoldKey = GlobalKey<ScaffoldState>();
|
|
Drama currentDrama;
|
|
int tab = 0;
|
|
int searchRequest = 0;
|
|
int resumeRequest = 0;
|
|
int backRequest = 0;
|
|
bool drawerOpen = false;
|
|
bool detailOpen = false;
|
|
bool videoImmersive = false;
|
|
bool _disposed = false;
|
|
|
|
void notify() {
|
|
if (!_disposed) notifyListeners();
|
|
}
|
|
|
|
void select(int nextTab, {bool search = false, bool resume = false}) {
|
|
FocusManager.instance.primaryFocus?.unfocus();
|
|
tab = nextTab;
|
|
if (search) searchRequest++;
|
|
if (resume) resumeRequest++;
|
|
notify();
|
|
}
|
|
|
|
void setMenuOpen(bool value) {
|
|
if (drawerOpen == value) return;
|
|
drawerOpen = value;
|
|
notify();
|
|
}
|
|
|
|
Future<void> openCurrentSeries(BuildContext context) async {
|
|
detailOpen = true;
|
|
notify();
|
|
await WidgetsBinding.instance.endOfFrame;
|
|
if (!context.mounted) return;
|
|
try {
|
|
await Navigator.of(context).push<void>(
|
|
MaterialPageRoute(builder: (_) => DramaDetailPage(drama: currentDrama)),
|
|
);
|
|
} finally {
|
|
if (context.mounted) {
|
|
detailOpen = false;
|
|
notify();
|
|
}
|
|
}
|
|
}
|
|
|
|
void openDrama(BuildContext context, Drama drama) {
|
|
Navigator.of(context).push(
|
|
MaterialPageRoute<void>(builder: (_) => DramaDetailPage(drama: drama)),
|
|
);
|
|
}
|
|
|
|
Future<void> playDrama(
|
|
BuildContext context,
|
|
Drama drama, {
|
|
String? episodeId,
|
|
}) async {
|
|
// 列表卡片只有一集预览,进入播放前补齐完整剧集,否则选集列表是空的。
|
|
var playable = drama;
|
|
try {
|
|
playable = await context.read<CatalogController>().requireDetail(drama);
|
|
} catch (_) {
|
|
// 取详情失败就用卡片自带的那一集起播。
|
|
}
|
|
if (!context.mounted) return;
|
|
final index = episodeId == null
|
|
? 0
|
|
: playable.episodes.indexWhere((episode) => episode.id == episodeId);
|
|
await Navigator.of(context).push(
|
|
MaterialPageRoute<void>(
|
|
builder: (_) => PlayerPage(
|
|
drama: playable,
|
|
initialIndex: index < 0 ? 0 : index,
|
|
wallet: context.read<WalletController>(),
|
|
downloads: context.read<DownloadController>(),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
void openProfile() => select(3);
|
|
|
|
void discoverAction(DiscoverPageAction action) {
|
|
switch (action) {
|
|
case DiscoverPageAction.home:
|
|
select(0);
|
|
case DiscoverPageAction.create:
|
|
select(2);
|
|
case DiscoverPageAction.my:
|
|
select(3);
|
|
}
|
|
}
|
|
|
|
void openFavorites(BuildContext context) {
|
|
Navigator.of(
|
|
context,
|
|
).push(MaterialPageRoute<void>(builder: (_) => const FavoritesPage()));
|
|
}
|
|
|
|
void openHistory(BuildContext context) {
|
|
Navigator.of(
|
|
context,
|
|
).push(MaterialPageRoute<void>(builder: (_) => const WatchRecordsPage()));
|
|
}
|
|
|
|
void myAction(MyPageAction action) {
|
|
switch (action) {
|
|
case MyPageAction.home:
|
|
select(0);
|
|
case MyPageAction.discover:
|
|
select(1, search: true);
|
|
case MyPageAction.create:
|
|
select(2);
|
|
case MyPageAction.resume:
|
|
select(0, resume: true);
|
|
}
|
|
}
|
|
|
|
void onSystemBack() {
|
|
if (videoImmersive) {
|
|
backRequest++;
|
|
notify();
|
|
} else if (tab != 0) {
|
|
select(0);
|
|
}
|
|
}
|
|
|
|
void onDramaChanged(Drama drama) {
|
|
currentDrama = drama;
|
|
notify();
|
|
}
|
|
|
|
void onImmersiveChanged(bool value) {
|
|
videoImmersive = value;
|
|
notify();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_disposed = true;
|
|
super.dispose();
|
|
}
|
|
}
|