修复0AI接入后列表无法触发更新的问题

This commit is contained in:
2026-09-28 09:01:53 +08:00
parent 9a144f159f
commit 187a7e0911
5 changed files with 295 additions and 1 deletions
+58
View File
@@ -74,8 +74,22 @@ class NoteListState {
class NoteListNotifier extends Notifier<NoteListState> {
static const _pageSize = 20;
/// 距上次成功加载超过这个时长,重新进入页面时再拉一次。
///
/// 存在的理由:在 Web 后台(或另一台设备上)改过笔记时,**App 收不到任何
/// 通知** —— 不像 AI 那样有 SSE 的 tool 事件可听。而这个 Notifier 是
/// keepAlive 的(为的是从详情页返回时保住搜索词和筛选),再次进入页面
/// 根本不会重新请求,于是永远显示几分钟前的老数据。
/// 只能在「用户重新看到这个页面」的时机主动对一次账。
///
/// 阈值的作用是避免频繁进出页面时反复打请求。
static const _staleAfter = Duration(seconds: 30);
Timer? _searchDebounce;
/// 上次成功拉到数据的时间;null = 还没加载过
DateTime? _lastLoadedAt;
@override
NoteListState build() {
ref.onDispose(() => _searchDebounce?.cancel());
@@ -108,6 +122,7 @@ class NoteListNotifier extends Notifier<NoteListState> {
tag: tag,
);
if (!ref.mounted) return;
_lastLoadedAt = DateTime.now();
state = state.copyWith(
items: result.items,
loading: false,
@@ -136,6 +151,49 @@ class NoteListNotifier extends Notifier<NoteListState> {
);
}
/// 重新进入页面(或在后台待了一阵再切回来)时调用。
///
/// 数据还新鲜就什么都不做 —— 频繁进出页面不该反复打接口;
/// 数据旧了才重新拉,用来兜住「在 Web 后台或别的设备上改过笔记」这种情况
/// —— 那种改动 App 完全收不到通知,只能靠这个时机补上。
///
/// 静默刷新:不置 loading。内容会原地换掉,不会闪一下加载圈,
/// 用户看到的就是「数据自己变新了」。
Future<void> refreshIfStale() async {
// 正在加载中就让它自己走完,别打断
if (state.loading || state.loadingMore) return;
final loadedAt = _lastLoadedAt;
if (loadedAt != null && DateTime.now().difference(loadedAt) < _staleAfter) {
return;
}
await _fetchFirstPage(
keyword: state.keyword,
tag: state.tag,
sort: state.sort,
location: state.location,
);
}
/// 列表页每次进入时调用。
///
/// 和 [refreshIfStale] 的区别:这里**总是重取**。
/// 因为「离开页面又回来」是一个明确的动作,用户的期待就是「看到最新的」;
/// 而 30 秒的节流是给 `resumed` 那种高频时机用的,不该用在这里 ——
/// 否则你在 Web 导完笔记立刻切回 App,可能因为「30 秒内刚对过账」而看不到。
///
/// 静默刷新:不置 loading,内容原地换掉,不闪加载圈。
Future<void> refreshOnEnter() async {
if (state.loading || state.loadingMore) return;
await _fetchFirstPage(
keyword: state.keyword,
tag: state.tag,
sort: state.sort,
location: state.location,
);
}
/// 上拉加载下一页
Future<void> loadMore() async {
if (state.loadingMore || !state.hasMore || state.loading) return;