import 'package:flutter/material.dart'; import '../../core/data/models/drama.dart'; import '../../core/data/remote/api_client.dart'; import '../../core/data/repositories/watch_history_repository.dart'; import '../player/player_view.dart'; class DramaDetailLogic extends ChangeNotifier { DramaDetailLogic({required this.drama, required this.history}); /// 先用列表卡片渲染,取到详情后换成带完整剧集的版本。 Drama drama; final WatchHistoryRepository history; bool loading = false; bool failed = false; /// 接口给出的失败原因;为空时界面回退到本地文案。 String? failureMessage; int? continueIndex; bool _disposed = false; void notify() { if (!_disposed) notifyListeners(); } /// 列表卡片只带一集可播预览,进详情时补一次完整剧集列表。 Future load(Future Function() fetch) async { if (drama.detailLoaded) { await loadContinue(); return; } loading = true; failed = false; failureMessage = null; notify(); try { drama = await fetch(); await loadContinue(); } on ApiException catch (exception) { failed = true; failureMessage = exception.message; } catch (_) { failed = true; } finally { loading = false; notify(); } } Future loadContinue() async { try { final id = await history.lastEpisodeId(demoProfileId, drama.id); if (id == null || _disposed) return; final index = drama.episodes.indexWhere((episode) => episode.id == id); if (index >= 0 && drama.episodes[index].isFree) { continueIndex = index; notify(); } } catch (_) { // Keep the first episode available if preferences cannot be read. } } Future openPlayer(BuildContext context, int index) async { await Navigator.of(context).push( MaterialPageRoute( builder: (_) => PlayerPage(drama: drama, initialIndex: index), ), ); await loadContinue(); } @override void dispose() { _disposed = true; super.dispose(); } }