bookmarks search now works

This commit is contained in:
MohammadTaha Basiri 2022-03-14 15:10:39 +03:30
parent d0a4c6ea0a
commit aba3d69dab
4 changed files with 58 additions and 21 deletions

View File

@ -9,20 +9,27 @@ class BookmarksState extends CoreProvier {
final List<OverviewData> bookmarks = []; final List<OverviewData> bookmarks = [];
String search = ''; String search = '';
String lastSearch = ''; String lastSearch = '';
int page = 1;
int lastPage = 1;
bool get searching => search != ''; bool get searching => search != '';
Future<void> getBookmarks() async { Future<void> getBookmarks({required int page}) async {
if (search != '') { if (search != '') {
lastSearch = search; lastSearch = search;
} }
if (page == 1) {
bookmarks.clear();
}
this.page = page;
appState = AppState.busy; appState = AppState.busy;
final service = RequestService(RequestHelper.bookmarks()); final service =
RequestService(RequestHelper.bookmarks(page: page, search: search));
await service.httpGet(); await service.httpGet();
if (service.isSuccess) { if (service.isSuccess) {
lastPage = service.result['lastPage'];
final marks = service.result['contents']; final marks = service.result['contents'];
bookmarks.clear();
for (var i = 0; i < marks.length; i++) { for (var i = 0; i < marks.length; i++) {
bookmarks.add(OverviewData.fromJson(marks[i])); bookmarks.add(OverviewData.fromJson(marks[i]));
} }

View File

@ -33,7 +33,7 @@ class _BookmarksState extends State<Bookmarks> {
@override @override
void initState() { void initState() {
Future.delayed(Duration.zero, () { Future.delayed(Duration.zero, () {
context.read<BookmarksState>().getBookmarks(); context.read<BookmarksState>().getBookmarks(page: 1);
}); });
super.initState(); super.initState();
} }
@ -74,14 +74,14 @@ class _BookmarksState extends State<Bookmarks> {
), ),
const DidvanDivider(), const DidvanDivider(),
MenuItem( MenuItem(
onTap: () => _onCategorySelected('videos'), onTap: () => _onCategorySelected('video'),
title: 'ویدئو‌ها', title: 'ویدئو‌ها',
icon: DidvanIcons.video_regular, icon: DidvanIcons.video_regular,
iconSize: 24, iconSize: 24,
), ),
const DidvanDivider(), const DidvanDivider(),
MenuItem( MenuItem(
onTap: () => _onCategorySelected('podcasts'), onTap: () => _onCategorySelected('podcast'),
title: 'پادکست‌ها', title: 'پادکست‌ها',
icon: DidvanIcons.podcast_regular, icon: DidvanIcons.podcast_regular,
iconSize: 24, iconSize: 24,
@ -103,26 +103,33 @@ class _BookmarksState extends State<Bookmarks> {
SliverStateHandler<BookmarksState>( SliverStateHandler<BookmarksState>(
state: state, state: state,
centerEmptyState: state.searching, centerEmptyState: state.searching,
builder: (context, state, index) => MultitypeOverview( builder: (context, state, index) {
item: state.bookmarks[index], index++;
onMarkChanged: state.onMarkChanged, if (index % 15 == 0 && state.lastPage != state.page) {
hasUnmarkConfirmation: true, state.getBookmarks(page: state.page + 1);
), }
index--;
return MultitypeOverview(
item: state.bookmarks[index],
onMarkChanged: state.onMarkChanged,
hasUnmarkConfirmation: true,
);
},
placeholder: MultitypeOverview.placeholder, placeholder: MultitypeOverview.placeholder,
itemPadding: const EdgeInsets.only(bottom: 8), itemPadding: const EdgeInsets.only(bottom: 8),
emptyState: state.searching emptyState: state.searching
? EmptyResult(onNewSearch: _focuseNode.requestFocus) ? EmptyResult(onNewSearch: _focuseNode.requestFocus)
: const EmptyList(), : const EmptyList(),
enableEmptyState: state.bookmarks.isEmpty, enableEmptyState: state.bookmarks.isEmpty,
childCount: state.bookmarks.length, childCount:
onRetry: state.getBookmarks, state.bookmarks.length + (state.page != state.lastPage ? 1 : 0),
onRetry: () => state.getBookmarks(page: state.page),
), ),
], ],
); );
} }
void _onCategorySelected(String type) { void _onCategorySelected(String type) {
if (type != 'radar' && type != 'news') return;
FocusScope.of(context).unfocus(); FocusScope.of(context).unfocus();
Navigator.of(context).pushNamed(Routes.filteredBookmarks, arguments: type); Navigator.of(context).pushNamed(Routes.filteredBookmarks, arguments: type);
} }
@ -135,7 +142,7 @@ class _BookmarksState extends State<Bookmarks> {
_timer?.cancel(); _timer?.cancel();
_timer = Timer(const Duration(seconds: 1), () { _timer = Timer(const Duration(seconds: 1), () {
state.search = value; state.search = value;
state.getBookmarks(); state.getBookmarks(page: 1);
}); });
} }
} }

View File

@ -20,7 +20,7 @@ class _FilteredBookmarksState extends State<FilteredBookmarks> {
void initState() { void initState() {
Future.delayed( Future.delayed(
Duration.zero, Duration.zero,
context.read<FilteredBookmarksState>().getBookmarks, () => context.read<FilteredBookmarksState>().getBookmarks(page: 1),
); );
super.initState(); super.initState();
} }
@ -54,6 +54,11 @@ class _FilteredBookmarksState extends State<FilteredBookmarks> {
placeholder: RadarOverview.placeholder, placeholder: RadarOverview.placeholder,
emptyState: const EmptyList(), emptyState: const EmptyList(),
builder: (context, state, index) { builder: (context, state, index) {
index++;
if (index % 15 == 0 && state.lastPage != state.page) {
state.getBookmarks(page: state.page + 1);
}
index--;
if (state.type == 'radar') { if (state.type == 'radar') {
return RadarOverview( return RadarOverview(
radar: state.bookmarks[index], radar: state.bookmarks[index],
@ -69,7 +74,7 @@ class _FilteredBookmarksState extends State<FilteredBookmarks> {
); );
}, },
childCount: state.bookmarks.length, childCount: state.bookmarks.length,
onRetry: () => state.getBookmarks(), onRetry: () => state.getBookmarks(page: state.page),
), ),
), ),
], ],

View File

@ -10,24 +10,42 @@ class FilteredBookmarksState extends CoreProvier {
String lastSearch = ''; String lastSearch = '';
final List<OverviewData> bookmarks = []; final List<OverviewData> bookmarks = [];
final String type; final String type;
int page = 1;
int lastPage = 1;
FilteredBookmarksState(this.type); FilteredBookmarksState(this.type);
bool get searching => search != ''; bool get searching => search != '';
Future<void> getBookmarks() async { Future<void> getBookmarks({required int page}) async {
if (search != '') { if (search != '') {
lastSearch = search; lastSearch = search;
} }
if (page == 1) {
bookmarks.clear();
}
this.page = page;
appState = AppState.busy; 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( 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(); await service.httpGet();
if (service.isSuccess) { if (service.isSuccess) {
final marks = service.result[type != 'news' ? type + 's' : type]; lastPage = service.result['lastPage'];
bookmarks.clear(); final marks = service.result[typeString];
for (var i = 0; i < marks.length; i++) { for (var i = 0; i < marks.length; i++) {
bookmarks.add(OverviewData.fromJson(marks[i])); bookmarks.add(OverviewData.fromJson(marks[i]));
} }