♻️ Continued to migrate list pagination
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import 'dart:async';
|
||||
import 'package:easy_localization/easy_localization.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_hooks/flutter_hooks.dart';
|
||||
@@ -6,52 +7,44 @@ import 'package:gap/gap.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:island/models/webfeed.dart';
|
||||
import 'package:island/pods/network.dart';
|
||||
import 'package:island/pods/paging.dart';
|
||||
import 'package:island/widgets/app_scaffold.dart';
|
||||
import 'package:island/widgets/paging/pagination_list.dart';
|
||||
import 'package:material_symbols_icons/symbols.dart';
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:riverpod_paging_utils/riverpod_paging_utils.dart';
|
||||
final marketplaceWebFeedsNotifierProvider = AsyncNotifierProvider(
|
||||
MarketplaceWebFeedsNotifier.new,
|
||||
);
|
||||
|
||||
part 'feed_marketplace.g.dart';
|
||||
|
||||
@riverpod
|
||||
class MarketplaceWebFeedsNotifier extends _$MarketplaceWebFeedsNotifier
|
||||
with CursorPagingNotifierMixin<SnWebFeed> {
|
||||
String? _query;
|
||||
class MarketplaceWebFeedsNotifier extends AsyncNotifier<List<SnWebFeed>>
|
||||
with
|
||||
AsyncPaginationController<SnWebFeed>,
|
||||
AsyncPaginationFilter<String?, SnWebFeed> {
|
||||
@override
|
||||
String? currentFilter;
|
||||
|
||||
@override
|
||||
Future<CursorPagingData<SnWebFeed>> build({required String? query}) {
|
||||
_query = query;
|
||||
return fetch(cursor: null);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<CursorPagingData<SnWebFeed>> fetch({required String? cursor}) async {
|
||||
Future<List<SnWebFeed>> fetch() async {
|
||||
final client = ref.read(apiClientProvider);
|
||||
final offset = cursor == null ? 0 : int.parse(cursor);
|
||||
|
||||
final response = await client.get(
|
||||
'/sphere/feeds/explore',
|
||||
queryParameters: {
|
||||
'offset': offset,
|
||||
'offset': fetchedCount.toString(),
|
||||
'take': 20,
|
||||
if (_query != null && _query!.isNotEmpty) 'query': _query,
|
||||
if (currentFilter != null && currentFilter!.isNotEmpty)
|
||||
'query': currentFilter,
|
||||
},
|
||||
);
|
||||
|
||||
final total = int.parse(response.headers.value('X-Total') ?? '0');
|
||||
final List<dynamic> data = response.data;
|
||||
final feeds = data.map((e) => SnWebFeed.fromJson(e)).toList();
|
||||
totalCount = int.parse(response.headers.value('X-Total') ?? '0');
|
||||
final feeds =
|
||||
response.data
|
||||
.map((e) => SnWebFeed.fromJson(e))
|
||||
.cast<SnWebFeed>()
|
||||
.toList();
|
||||
|
||||
final hasMore = offset + feeds.length < total;
|
||||
final nextCursor = hasMore ? (offset + feeds.length).toString() : null;
|
||||
|
||||
return CursorPagingData(
|
||||
items: feeds,
|
||||
hasMore: hasMore,
|
||||
nextCursor: nextCursor,
|
||||
);
|
||||
return feeds;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,83 +79,70 @@ class MarketplaceWebFeedsScreen extends HookConsumerWidget {
|
||||
title: const Text('webFeeds').tr(),
|
||||
actions: const [Gap(8)],
|
||||
),
|
||||
body: PagingHelperView(
|
||||
provider: marketplaceWebFeedsNotifierProvider(query: query.value),
|
||||
futureRefreshable:
|
||||
marketplaceWebFeedsNotifierProvider(query: query.value).future,
|
||||
notifierRefreshable:
|
||||
marketplaceWebFeedsNotifierProvider(query: query.value).notifier,
|
||||
contentBuilder:
|
||||
(data, widgetCount, endItemView) => Column(
|
||||
children: [
|
||||
// Search bar above the list
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: SearchBar(
|
||||
elevation: WidgetStateProperty.all(4),
|
||||
controller: searchController,
|
||||
focusNode: focusNode,
|
||||
hintText: 'search'.tr(),
|
||||
leading: const Icon(Symbols.search),
|
||||
padding: WidgetStateProperty.all(
|
||||
const EdgeInsets.symmetric(horizontal: 24),
|
||||
),
|
||||
onTapOutside:
|
||||
(_) => FocusManager.instance.primaryFocus?.unfocus(),
|
||||
trailing: [
|
||||
if (query.value != null && query.value!.isNotEmpty)
|
||||
IconButton(
|
||||
icon: const Icon(Symbols.close),
|
||||
onPressed: () {
|
||||
query.value = null;
|
||||
searchController.clear();
|
||||
focusNode.unfocus();
|
||||
},
|
||||
),
|
||||
],
|
||||
onChanged: (value) {
|
||||
// Debounce search to avoid excessive API calls
|
||||
debounceTimer.value?.cancel();
|
||||
debounceTimer.value = Timer(
|
||||
const Duration(milliseconds: 500),
|
||||
() {
|
||||
query.value = value.isEmpty ? null : value;
|
||||
},
|
||||
);
|
||||
},
|
||||
onSubmitted: (value) {
|
||||
query.value = value.isEmpty ? null : value;
|
||||
body: Column(
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: SearchBar(
|
||||
elevation: WidgetStateProperty.all(4),
|
||||
controller: searchController,
|
||||
focusNode: focusNode,
|
||||
hintText: 'search'.tr(),
|
||||
leading: const Icon(Symbols.search),
|
||||
padding: WidgetStateProperty.all(
|
||||
const EdgeInsets.symmetric(horizontal: 24),
|
||||
),
|
||||
onTapOutside:
|
||||
(_) => FocusManager.instance.primaryFocus?.unfocus(),
|
||||
trailing: [
|
||||
if (query.value != null && query.value!.isNotEmpty)
|
||||
IconButton(
|
||||
icon: const Icon(Symbols.close),
|
||||
onPressed: () {
|
||||
query.value = null;
|
||||
searchController.clear();
|
||||
focusNode.unfocus();
|
||||
},
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: ListView.builder(
|
||||
padding: EdgeInsets.zero,
|
||||
itemCount: widgetCount,
|
||||
itemBuilder: (context, index) {
|
||||
if (index == widgetCount - 1) {
|
||||
return endItemView;
|
||||
}
|
||||
|
||||
final feed = data.items[index];
|
||||
return ListTile(
|
||||
title: Text(feed.title),
|
||||
subtitle: Text(feed.description ?? ''),
|
||||
trailing: const Icon(Symbols.chevron_right),
|
||||
onTap: () {
|
||||
// Navigate to web feed detail page
|
||||
context.pushNamed(
|
||||
'webFeedDetail',
|
||||
pathParameters: {'feedId': feed.id},
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
onChanged: (value) {
|
||||
// Debounce search to avoid excessive API calls
|
||||
debounceTimer.value?.cancel();
|
||||
debounceTimer.value = Timer(
|
||||
const Duration(milliseconds: 500),
|
||||
() {
|
||||
query.value = value.isEmpty ? null : value;
|
||||
},
|
||||
);
|
||||
},
|
||||
onSubmitted: (value) {
|
||||
query.value = value.isEmpty ? null : value;
|
||||
focusNode.unfocus();
|
||||
},
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: PaginationList(
|
||||
provider: marketplaceWebFeedsNotifierProvider,
|
||||
notifier: marketplaceWebFeedsNotifierProvider.notifier,
|
||||
padding: EdgeInsets.zero,
|
||||
itemBuilder: (context, index, feed) {
|
||||
return ListTile(
|
||||
title: Text(feed.title),
|
||||
subtitle: Text(feed.description ?? ''),
|
||||
trailing: const Icon(Symbols.chevron_right),
|
||||
onTap: () {
|
||||
// Navigate to web feed detail page
|
||||
context.pushNamed(
|
||||
'webFeedDetail',
|
||||
pathParameters: {'feedId': feed.id},
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user