Files
ReelShort/lib/pages/drama_detail/drama_detail_logic.dart
T

50 lines
1.3 KiB
Dart

// Author: fruit.com.cn
import 'package:flutter/material.dart';
import '../../core/data/models/drama.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});
final Drama drama;
final WatchHistoryRepository history;
int? continueIndex;
bool _disposed = false;
void notify() {
if (!_disposed) notifyListeners();
}
Future<void> 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<void> openPlayer(BuildContext context, int index) async {
await Navigator.of(context).push(
MaterialPageRoute<void>(
builder: (_) => PlayerPage(drama: drama, initialIndex: index),
),
);
await loadContinue();
}
@override
void dispose() {
_disposed = true;
super.dispose();
}
}