♻️ Replaced all list with own pagination list
This commit is contained in:
@@ -3,53 +3,36 @@ import 'package:gap/gap.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:island/models/realm.dart';
|
||||
import 'package:island/pods/network.dart';
|
||||
import 'package:island/pods/paging.dart';
|
||||
import 'package:island/widgets/paging/pagination_list.dart';
|
||||
import 'package:island/widgets/realm/realm_card.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:riverpod_paging_utils/riverpod_paging_utils.dart';
|
||||
import 'package:styled_widget/styled_widget.dart';
|
||||
|
||||
part 'realm_list.g.dart';
|
||||
final realmListNotifierProvider = AsyncNotifierProvider.autoDispose
|
||||
.family<RealmListNotifier, List<SnRealm>, String?>(RealmListNotifier.new);
|
||||
|
||||
@riverpod
|
||||
class RealmListNotifier extends _$RealmListNotifier
|
||||
with CursorPagingNotifierMixin<SnRealm> {
|
||||
class RealmListNotifier
|
||||
extends AutoDisposeFamilyAsyncNotifier<List<SnRealm>, String?>
|
||||
with FamilyAsyncPaginationController<SnRealm, String?> {
|
||||
static const int _pageSize = 20;
|
||||
|
||||
@override
|
||||
Future<CursorPagingData<SnRealm>> build(String? query) {
|
||||
return fetch(cursor: null, query: query);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<CursorPagingData<SnRealm>> fetch({
|
||||
required String? cursor,
|
||||
String? query,
|
||||
}) async {
|
||||
Future<List<SnRealm>> fetch() async {
|
||||
final client = ref.read(apiClientProvider);
|
||||
final offset = cursor == null ? 0 : int.parse(cursor);
|
||||
|
||||
final queryParams = {
|
||||
'offset': offset,
|
||||
'offset': fetchedCount,
|
||||
'take': _pageSize,
|
||||
if (query != null && query.isNotEmpty) 'query': query,
|
||||
if (arg != null && arg!.isNotEmpty) 'query': arg,
|
||||
};
|
||||
|
||||
final response = await client.get(
|
||||
'/sphere/discovery/realms',
|
||||
queryParameters: queryParams,
|
||||
);
|
||||
final total = int.parse(response.headers.value('X-Total') ?? '0');
|
||||
totalCount = int.parse(response.headers.value('X-Total') ?? '0');
|
||||
final List<dynamic> data = response.data;
|
||||
final realms = data.map((json) => SnRealm.fromJson(json)).toList();
|
||||
|
||||
final hasMore = offset + realms.length < total;
|
||||
final nextCursor = hasMore ? (offset + realms.length).toString() : null;
|
||||
|
||||
return CursorPagingData(
|
||||
items: realms,
|
||||
hasMore: hasMore,
|
||||
nextCursor: nextCursor,
|
||||
);
|
||||
return data.map((json) => SnRealm.fromJson(json)).toList();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,34 +43,32 @@ class SliverRealmList extends HookConsumerWidget {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return PagingHelperSliverView(
|
||||
provider: realmListNotifierProvider(query),
|
||||
futureRefreshable: realmListNotifierProvider(query).future,
|
||||
notifierRefreshable: realmListNotifierProvider(query).notifier,
|
||||
contentBuilder:
|
||||
(data, widgetCount, endItemView) => SliverList.separated(
|
||||
itemCount: widgetCount,
|
||||
itemBuilder: (context, index) {
|
||||
if (index == widgetCount - 1) {
|
||||
return endItemView;
|
||||
}
|
||||
|
||||
final realm = data.items[index];
|
||||
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 16,
|
||||
vertical: 8,
|
||||
),
|
||||
child:
|
||||
ConstrainedBox(
|
||||
constraints: const BoxConstraints(maxWidth: 540),
|
||||
child: RealmCard(realm: realm),
|
||||
).center(),
|
||||
);
|
||||
},
|
||||
separatorBuilder: (_, _) => const Gap(8),
|
||||
),
|
||||
final provider = realmListNotifierProvider(query);
|
||||
return PaginationList(
|
||||
provider: provider,
|
||||
notifier: provider.notifier,
|
||||
isSliver: true,
|
||||
isRefreshable: false,
|
||||
itemBuilder: (context, index, realm) {
|
||||
return Column(
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
||||
child:
|
||||
ConstrainedBox(
|
||||
constraints: const BoxConstraints(maxWidth: 540),
|
||||
child: RealmCard(realm: realm),
|
||||
).center(),
|
||||
),
|
||||
if (index <
|
||||
(ref.read(provider).valueOrNull?.length ?? 0) -
|
||||
1) // Add gap except for last item? Actually PaginationList handles loading indicator which might look like last item.
|
||||
// Wait, ref.read(provider).valueOrNull?.length might change.
|
||||
// Simpler to just add bottom padding to all, or Gap.
|
||||
const Gap(8),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,179 +0,0 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'realm_list.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// RiverpodGenerator
|
||||
// **************************************************************************
|
||||
|
||||
String _$realmListNotifierHash() => r'8ae5c3ae2837acae4c7bf5e44578518afc9ea1a6';
|
||||
|
||||
/// Copied from Dart SDK
|
||||
class _SystemHash {
|
||||
_SystemHash._();
|
||||
|
||||
static int combine(int hash, int value) {
|
||||
// ignore: parameter_assignments
|
||||
hash = 0x1fffffff & (hash + value);
|
||||
// ignore: parameter_assignments
|
||||
hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10));
|
||||
return hash ^ (hash >> 6);
|
||||
}
|
||||
|
||||
static int finish(int hash) {
|
||||
// ignore: parameter_assignments
|
||||
hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3));
|
||||
// ignore: parameter_assignments
|
||||
hash = hash ^ (hash >> 11);
|
||||
return 0x1fffffff & (hash + ((0x00003fff & hash) << 15));
|
||||
}
|
||||
}
|
||||
|
||||
abstract class _$RealmListNotifier
|
||||
extends BuildlessAutoDisposeAsyncNotifier<CursorPagingData<SnRealm>> {
|
||||
late final String? query;
|
||||
|
||||
FutureOr<CursorPagingData<SnRealm>> build(String? query);
|
||||
}
|
||||
|
||||
/// See also [RealmListNotifier].
|
||||
@ProviderFor(RealmListNotifier)
|
||||
const realmListNotifierProvider = RealmListNotifierFamily();
|
||||
|
||||
/// See also [RealmListNotifier].
|
||||
class RealmListNotifierFamily
|
||||
extends Family<AsyncValue<CursorPagingData<SnRealm>>> {
|
||||
/// See also [RealmListNotifier].
|
||||
const RealmListNotifierFamily();
|
||||
|
||||
/// See also [RealmListNotifier].
|
||||
RealmListNotifierProvider call(String? query) {
|
||||
return RealmListNotifierProvider(query);
|
||||
}
|
||||
|
||||
@override
|
||||
RealmListNotifierProvider getProviderOverride(
|
||||
covariant RealmListNotifierProvider provider,
|
||||
) {
|
||||
return call(provider.query);
|
||||
}
|
||||
|
||||
static const Iterable<ProviderOrFamily>? _dependencies = null;
|
||||
|
||||
@override
|
||||
Iterable<ProviderOrFamily>? get dependencies => _dependencies;
|
||||
|
||||
static const Iterable<ProviderOrFamily>? _allTransitiveDependencies = null;
|
||||
|
||||
@override
|
||||
Iterable<ProviderOrFamily>? get allTransitiveDependencies =>
|
||||
_allTransitiveDependencies;
|
||||
|
||||
@override
|
||||
String? get name => r'realmListNotifierProvider';
|
||||
}
|
||||
|
||||
/// See also [RealmListNotifier].
|
||||
class RealmListNotifierProvider
|
||||
extends
|
||||
AutoDisposeAsyncNotifierProviderImpl<
|
||||
RealmListNotifier,
|
||||
CursorPagingData<SnRealm>
|
||||
> {
|
||||
/// See also [RealmListNotifier].
|
||||
RealmListNotifierProvider(String? query)
|
||||
: this._internal(
|
||||
() => RealmListNotifier()..query = query,
|
||||
from: realmListNotifierProvider,
|
||||
name: r'realmListNotifierProvider',
|
||||
debugGetCreateSourceHash:
|
||||
const bool.fromEnvironment('dart.vm.product')
|
||||
? null
|
||||
: _$realmListNotifierHash,
|
||||
dependencies: RealmListNotifierFamily._dependencies,
|
||||
allTransitiveDependencies:
|
||||
RealmListNotifierFamily._allTransitiveDependencies,
|
||||
query: query,
|
||||
);
|
||||
|
||||
RealmListNotifierProvider._internal(
|
||||
super._createNotifier, {
|
||||
required super.name,
|
||||
required super.dependencies,
|
||||
required super.allTransitiveDependencies,
|
||||
required super.debugGetCreateSourceHash,
|
||||
required super.from,
|
||||
required this.query,
|
||||
}) : super.internal();
|
||||
|
||||
final String? query;
|
||||
|
||||
@override
|
||||
FutureOr<CursorPagingData<SnRealm>> runNotifierBuild(
|
||||
covariant RealmListNotifier notifier,
|
||||
) {
|
||||
return notifier.build(query);
|
||||
}
|
||||
|
||||
@override
|
||||
Override overrideWith(RealmListNotifier Function() create) {
|
||||
return ProviderOverride(
|
||||
origin: this,
|
||||
override: RealmListNotifierProvider._internal(
|
||||
() => create()..query = query,
|
||||
from: from,
|
||||
name: null,
|
||||
dependencies: null,
|
||||
allTransitiveDependencies: null,
|
||||
debugGetCreateSourceHash: null,
|
||||
query: query,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
AutoDisposeAsyncNotifierProviderElement<
|
||||
RealmListNotifier,
|
||||
CursorPagingData<SnRealm>
|
||||
>
|
||||
createElement() {
|
||||
return _RealmListNotifierProviderElement(this);
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
return other is RealmListNotifierProvider && other.query == query;
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode {
|
||||
var hash = _SystemHash.combine(0, runtimeType.hashCode);
|
||||
hash = _SystemHash.combine(hash, query.hashCode);
|
||||
|
||||
return _SystemHash.finish(hash);
|
||||
}
|
||||
}
|
||||
|
||||
@Deprecated('Will be removed in 3.0. Use Ref instead')
|
||||
// ignore: unused_element
|
||||
mixin RealmListNotifierRef
|
||||
on AutoDisposeAsyncNotifierProviderRef<CursorPagingData<SnRealm>> {
|
||||
/// The parameter `query` of this provider.
|
||||
String? get query;
|
||||
}
|
||||
|
||||
class _RealmListNotifierProviderElement
|
||||
extends
|
||||
AutoDisposeAsyncNotifierProviderElement<
|
||||
RealmListNotifier,
|
||||
CursorPagingData<SnRealm>
|
||||
>
|
||||
with RealmListNotifierRef {
|
||||
_RealmListNotifierProviderElement(super.provider);
|
||||
|
||||
@override
|
||||
String? get query => (origin as RealmListNotifierProvider).query;
|
||||
}
|
||||
|
||||
// ignore_for_file: type=lint
|
||||
// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member, deprecated_member_use_from_same_package
|
||||
Reference in New Issue
Block a user