From ab1479c0eb83f4172b7d99e0f223a222d0cd56e1 Mon Sep 17 00:00:00 2001 From: Kingkor Roy Tirtho Date: Fri, 6 Oct 2023 11:15:59 +0600 Subject: [PATCH] feat(infinite_query): add isLoadingNextPage & remove isLoadingPage --- .../fl_query/lib/src/core/infinite_query.dart | 63 +++++++++++-------- .../src/widgets/tabs/infinite_query_tab.dart | 6 +- 2 files changed, 40 insertions(+), 29 deletions(-) diff --git a/packages/fl_query/lib/src/core/infinite_query.dart b/packages/fl_query/lib/src/core/infinite_query.dart index 715638b..aa89767 100644 --- a/packages/fl_query/lib/src/core/infinite_query.dart +++ b/packages/fl_query/lib/src/core/infinite_query.dart @@ -30,17 +30,21 @@ class InfiniteQueryPage with Invalidation { final DateTime updatedAt; final Duration staleDuration; + final bool _loading; + const InfiniteQueryPage({ required this.page, this.data, this.error, required this.updatedAt, required this.staleDuration, - }); + bool loading = false, + }) : _loading = loading; InfiniteQueryPage copyWith({ DataType? data, ErrorType? error, + bool? loading, }) { return InfiniteQueryPage( page: page, @@ -48,6 +52,7 @@ class InfiniteQueryPage with Invalidation { staleDuration: staleDuration, data: data ?? this.data, error: error ?? this.error, + loading: loading ?? _loading, ); } @@ -70,7 +75,8 @@ class InfiniteQueryState { required this.pages, }); - PageType get lastPage => pages.last.page; + PageType get lastPage => + pages.whereNot((e) => e.data == null && e.error == null).last.page; InfiniteQueryState copyWith( {Set>? pages}) { @@ -243,8 +249,16 @@ class InfiniteQuery return _nextPage(lastPage, lastPageData); } - bool get isLoadingPage => !hasPageData && !hasPageError && _mutex.isLocked; - bool get isRefreshingPage => (hasPageData || hasPageError) && _mutex.isLocked; + bool get isLoadingNextPage { + final nextPage = state.pages.firstWhereOrNull((p) => p.page == getNextPage); + return nextPage?._loading == true; + } + + bool get isRefreshingPage { + final currentPage = state.pages.firstWhereOrNull((p) => p.page == lastPage); + return currentPage?._loading == true; + } + bool get isInactive => !hasListeners; bool get hasPages => pages.isNotEmpty; @@ -261,21 +275,27 @@ class InfiniteQuery return; } return _mutex.protect(() async { - state = state.copyWith(); + final storedPage = state.pages.firstWhere( + (e) => e.page == page, + orElse: () => InfiniteQueryPage( + page: page, + updatedAt: DateTime.now(), + staleDuration: refreshConfig.staleDuration, + loading: true, + ), + ); + state = state.copyWith( + pages: { + ...state.pages..remove(storedPage), + storedPage, + }, + ); _operation = cancellableRetryOperation( () => _queryFn(page), config: retryConfig, onSuccessful: (data) async { - final dataPage = state.pages - .firstWhere( - (e) => e.page == page, - orElse: () => InfiniteQueryPage( - page: page, - updatedAt: DateTime.now(), - staleDuration: refreshConfig.staleDuration, - ), - ) - .copyWith(data: data, error: null); + final dataPage = + storedPage.copyWith(data: data, error: null, loading: false); state = state.copyWith( pages: {...state.pages..remove(dataPage), dataPage}, ); @@ -297,16 +317,7 @@ class InfiniteQuery } }, onFailed: (error) { - final errorPage = state.pages - .firstWhere( - (e) => e.page == page, - orElse: () => InfiniteQueryPage( - page: page, - updatedAt: DateTime.now(), - staleDuration: refreshConfig.staleDuration, - ), - ) - .copyWith(error: error); + final errorPage = storedPage.copyWith(error: error, loading: false); state = state.copyWith( pages: { ...state.pages..remove(errorPage), @@ -407,7 +418,7 @@ class InfiniteQuery staleDuration: refreshConfig.staleDuration, ), ) - .copyWith(data: data); + .copyWith(data: data, loading: false); state = state.copyWith( pages: { diff --git a/packages/fl_query_devtools/lib/src/widgets/tabs/infinite_query_tab.dart b/packages/fl_query_devtools/lib/src/widgets/tabs/infinite_query_tab.dart index ed58198..3e668e8 100644 --- a/packages/fl_query_devtools/lib/src/widgets/tabs/infinite_query_tab.dart +++ b/packages/fl_query_devtools/lib/src/widgets/tabs/infinite_query_tab.dart @@ -37,7 +37,7 @@ class _InfiniteQueryTabState extends State { return QueryTile( title: query.key, - isLoading: query.isLoadingPage, + isLoading: query.isLoadingNextPage, hasError: query.hasErrors, onTap: () { setState(() { @@ -63,7 +63,7 @@ class _InfiniteQueryTabState extends State { _selectedQueryKey ?? '', builder: (context, query) { if (query == null) { - return SizedBox.shrink(); + return const SizedBox.shrink(); } return ExplorerView( @@ -78,7 +78,7 @@ class _InfiniteQueryTabState extends State { "updatedAt": page.updatedAt.toString(), }; }).toList(), - 'isLoadingPage': query.isLoadingPage, + 'isLoadingNextPage': query.isLoadingNextPage, 'isRefreshingPage': query.isRefreshingPage, 'isInactive': query.isInactive, 'refreshConfig': query.refreshConfig.toJson(),