diff --git a/lib/views/home/settings/bookmarks/bookmark_state.dart b/lib/views/home/settings/bookmarks/bookmark_state.dart index 1807179..063dc4b 100644 --- a/lib/views/home/settings/bookmarks/bookmark_state.dart +++ b/lib/views/home/settings/bookmarks/bookmark_state.dart @@ -9,20 +9,27 @@ class BookmarksState extends CoreProvier { final List bookmarks = []; String search = ''; String lastSearch = ''; + int page = 1; + int lastPage = 1; bool get searching => search != ''; - Future getBookmarks() async { + Future getBookmarks({required int page}) async { if (search != '') { lastSearch = search; } + if (page == 1) { + bookmarks.clear(); + } + this.page = page; appState = AppState.busy; - final service = RequestService(RequestHelper.bookmarks()); + final service = + RequestService(RequestHelper.bookmarks(page: page, search: search)); await service.httpGet(); if (service.isSuccess) { + lastPage = service.result['lastPage']; final marks = service.result['contents']; - bookmarks.clear(); for (var i = 0; i < marks.length; i++) { bookmarks.add(OverviewData.fromJson(marks[i])); } diff --git a/lib/views/home/settings/bookmarks/bookmarks.dart b/lib/views/home/settings/bookmarks/bookmarks.dart index 1cb736a..31746b8 100644 --- a/lib/views/home/settings/bookmarks/bookmarks.dart +++ b/lib/views/home/settings/bookmarks/bookmarks.dart @@ -33,7 +33,7 @@ class _BookmarksState extends State { @override void initState() { Future.delayed(Duration.zero, () { - context.read().getBookmarks(); + context.read().getBookmarks(page: 1); }); super.initState(); } @@ -74,14 +74,14 @@ class _BookmarksState extends State { ), const DidvanDivider(), MenuItem( - onTap: () => _onCategorySelected('videos'), + onTap: () => _onCategorySelected('video'), title: 'ویدئو‌ها', icon: DidvanIcons.video_regular, iconSize: 24, ), const DidvanDivider(), MenuItem( - onTap: () => _onCategorySelected('podcasts'), + onTap: () => _onCategorySelected('podcast'), title: 'پادکست‌ها', icon: DidvanIcons.podcast_regular, iconSize: 24, @@ -103,26 +103,33 @@ class _BookmarksState extends State { SliverStateHandler( state: state, centerEmptyState: state.searching, - builder: (context, state, index) => MultitypeOverview( - item: state.bookmarks[index], - onMarkChanged: state.onMarkChanged, - hasUnmarkConfirmation: true, - ), + builder: (context, state, index) { + index++; + if (index % 15 == 0 && state.lastPage != state.page) { + state.getBookmarks(page: state.page + 1); + } + index--; + return MultitypeOverview( + item: state.bookmarks[index], + onMarkChanged: state.onMarkChanged, + hasUnmarkConfirmation: true, + ); + }, placeholder: MultitypeOverview.placeholder, itemPadding: const EdgeInsets.only(bottom: 8), emptyState: state.searching ? EmptyResult(onNewSearch: _focuseNode.requestFocus) : const EmptyList(), enableEmptyState: state.bookmarks.isEmpty, - childCount: state.bookmarks.length, - onRetry: state.getBookmarks, + childCount: + state.bookmarks.length + (state.page != state.lastPage ? 1 : 0), + onRetry: () => state.getBookmarks(page: state.page), ), ], ); } void _onCategorySelected(String type) { - if (type != 'radar' && type != 'news') return; FocusScope.of(context).unfocus(); Navigator.of(context).pushNamed(Routes.filteredBookmarks, arguments: type); } @@ -135,7 +142,7 @@ class _BookmarksState extends State { _timer?.cancel(); _timer = Timer(const Duration(seconds: 1), () { state.search = value; - state.getBookmarks(); + state.getBookmarks(page: 1); }); } } diff --git a/lib/views/home/settings/bookmarks/filtered_bookmark/filtered_bookmark.dart b/lib/views/home/settings/bookmarks/filtered_bookmark/filtered_bookmark.dart index f9a5454..106d718 100644 --- a/lib/views/home/settings/bookmarks/filtered_bookmark/filtered_bookmark.dart +++ b/lib/views/home/settings/bookmarks/filtered_bookmark/filtered_bookmark.dart @@ -20,7 +20,7 @@ class _FilteredBookmarksState extends State { void initState() { Future.delayed( Duration.zero, - context.read().getBookmarks, + () => context.read().getBookmarks(page: 1), ); super.initState(); } @@ -54,6 +54,11 @@ class _FilteredBookmarksState extends State { placeholder: RadarOverview.placeholder, emptyState: const EmptyList(), builder: (context, state, index) { + index++; + if (index % 15 == 0 && state.lastPage != state.page) { + state.getBookmarks(page: state.page + 1); + } + index--; if (state.type == 'radar') { return RadarOverview( radar: state.bookmarks[index], @@ -69,7 +74,7 @@ class _FilteredBookmarksState extends State { ); }, childCount: state.bookmarks.length, - onRetry: () => state.getBookmarks(), + onRetry: () => state.getBookmarks(page: state.page), ), ), ], diff --git a/lib/views/home/settings/bookmarks/filtered_bookmark/filtered_bookmarks_state.dart b/lib/views/home/settings/bookmarks/filtered_bookmark/filtered_bookmarks_state.dart index 697ee3a..7aa0463 100644 --- a/lib/views/home/settings/bookmarks/filtered_bookmark/filtered_bookmarks_state.dart +++ b/lib/views/home/settings/bookmarks/filtered_bookmark/filtered_bookmarks_state.dart @@ -10,24 +10,42 @@ class FilteredBookmarksState extends CoreProvier { String lastSearch = ''; final List bookmarks = []; final String type; + int page = 1; + int lastPage = 1; FilteredBookmarksState(this.type); bool get searching => search != ''; - Future getBookmarks() async { + Future getBookmarks({required int page}) async { if (search != '') { lastSearch = search; } + if (page == 1) { + bookmarks.clear(); + } + this.page = page; appState = AppState.busy; + String typeString = ''; + if (type == 'video' || type == 'podcast') { + typeString = 'studios'; + } else if (type == 'news') { + typeString = type; + } else { + typeString = type + 's'; + } final service = RequestService( - RequestHelper.bookmarks(type: type == 'news' ? type : type + 's'), + RequestHelper.bookmarks( + type: typeString, + page: page, + studioType: type == 'podcast' || type == 'video' ? type : null, + ), ); await service.httpGet(); if (service.isSuccess) { - final marks = service.result[type != 'news' ? type + 's' : type]; - bookmarks.clear(); + lastPage = service.result['lastPage']; + final marks = service.result[typeString]; for (var i = 0; i < marks.length; i++) { bookmarks.add(OverviewData.fromJson(marks[i])); }