Files
ReelShort/lib/pages/home/home_browse_logic.dart
T

71 lines
1.7 KiB
Dart

// Author: fruit.com.cn
import 'dart:async';
import 'package:flutter/foundation.dart';
import '../../core/data/models/watch_progress.dart';
import '../../core/data/repositories/watch_history_repository.dart';
/// 首页频道,只依赖目录中已有数据排序或过滤。
enum HomeChannel { hot, free, newReleases, ranking }
/// 浏览型首页的本地 UI 状态。
class HomeBrowseLogic extends ChangeNotifier {
HomeBrowseLogic({required this.history}) {
unawaited(refreshRecent());
}
static const int recentLimit = 20;
final WatchHistoryRepository history;
HomeChannel channel = HomeChannel.hot;
List<WatchProgress> recent = const [];
bool loadingRecent = false;
bool _disposed = false;
/// 第一条未完成且存在真实播放位置的记录。
WatchProgress? get primaryRecord {
for (final record in recent) {
if (!record.finished && record.hasPosition) return record;
}
return null;
}
/// 切换频道,不触发额外网络请求。
void selectChannel(HomeChannel value) {
if (channel == value) return;
channel = value;
_notify();
}
/// 重新加载续看数据;未登录时返回空列表。
Future<void> refreshRecent() async {
loadingRecent = true;
_notify();
try {
recent = await history.recent(limit: recentLimit);
} catch (_) {
recent = const [];
} finally {
loadingRecent = false;
_notify();
}
}
/// 首页重新可见时刷新续看数据。
void syncPage({required bool active, required bool wasActive}) {
if (active && !wasActive) unawaited(refreshRecent());
}
void _notify() {
if (!_disposed) notifyListeners();
}
@override
void dispose() {
_disposed = true;
super.dispose();
}
}