Files
ReelShort/lib/pages/mine/mine_logic.dart

84 lines
2.1 KiB
Dart

// Author: fruit.com.cn
import 'package:flutter/material.dart';
import '../../core/data/models/watch_progress.dart';
import '../../core/data/repositories/watch_history_repository.dart';
import 'mine_types.dart';
/// 「我的」页状态:最近观看与根导航动作。
class MineLogic extends ChangeNotifier {
MineLogic({required this.history}) {
refreshRecent();
}
final WatchHistoryRepository history;
List<WatchProgress> recent = const [];
int? watchRecordTotal;
bool recentLoading = false;
String? recentError;
bool _disposed = false;
int _requestSeq = 0;
void notify() {
if (!_disposed) notifyListeners();
}
/// 最近观看:取分页首屏,剧名封面仍由目录卡片按 id 映射。
Future<void> refreshRecent() async {
final request = ++_requestSeq;
recentLoading = true;
recentError = null;
notify();
try {
final page = await history.recent(pageIndex: 1, pageSize: 10);
if (_disposed || request != _requestSeq) return;
recent = page.list;
watchRecordTotal = page.total;
} catch (_) {
if (!_disposed && request == _requestSeq) {
recentError = 'progressError';
}
} finally {
if (!_disposed && request == _requestSeq) {
recentLoading = false;
notify();
}
}
}
/// 切换账号/设备身份时先清空,避免旧身份数据回写或短暂混显。
void resetRecent() {
_requestSeq += 1;
recent = const [];
watchRecordTotal = null;
recentLoading = false;
recentError = null;
notify();
}
/// 页面重新可见时刷新最近观看,避免停留在旧进度。
void syncPage({required bool active, required bool wasActive}) {
if (active && !wasActive) refreshRecent();
}
void act(
BuildContext context,
MyPageAction action, {
required bool embedded,
ValueChanged<MyPageAction>? onAction,
}) {
if (embedded) {
onAction?.call(action);
} else {
Navigator.pop(context, action);
}
}
@override
void dispose() {
_disposed = true;
super.dispose();
}
}