🐛 Fixes and optimzation on post
♻️ Replace freezed abstract classes with sealed
This commit is contained in:
@ -1,13 +1,59 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:island/models/post.dart';
|
||||
import 'package:island/pods/network.dart';
|
||||
import 'package:island/services/responsive.dart';
|
||||
import 'package:island/widgets/content/paging_helper_ext.dart';
|
||||
import 'package:island/widgets/post/post_item.dart';
|
||||
import 'package:island/widgets/response.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:riverpod_paging_utils/riverpod_paging_utils.dart';
|
||||
import 'package:styled_widget/styled_widget.dart';
|
||||
import 'package:very_good_infinite_list/very_good_infinite_list.dart';
|
||||
|
||||
part 'post_replies.g.dart';
|
||||
|
||||
@riverpod
|
||||
class PostRepliesNotifier extends _$PostRepliesNotifier
|
||||
with CursorPagingNotifierMixin<SnPost> {
|
||||
static const int _pageSize = 20;
|
||||
|
||||
PostRepliesNotifier();
|
||||
|
||||
String? _postId;
|
||||
|
||||
@override
|
||||
Future<CursorPagingData<SnPost>> build(String postId) {
|
||||
_postId = postId;
|
||||
return fetch(cursor: null);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<CursorPagingData<SnPost>> fetch({required String? cursor}) async {
|
||||
if (_postId == null) {
|
||||
throw StateError('PostRepliesNotifier must be initialized with postId');
|
||||
}
|
||||
|
||||
final client = ref.read(apiClientProvider);
|
||||
final offset = cursor == null ? 0 : int.parse(cursor);
|
||||
|
||||
final response = await client.get(
|
||||
'/posts/$_postId/replies',
|
||||
queryParameters: {'offset': offset, 'take': _pageSize},
|
||||
);
|
||||
|
||||
final total = int.parse(response.headers.value('X-Total') ?? '0');
|
||||
final List<dynamic> data = response.data;
|
||||
final posts = data.map((json) => SnPost.fromJson(json)).toList();
|
||||
|
||||
final hasMore = offset + posts.length < total;
|
||||
final nextCursor = hasMore ? (offset + posts.length).toString() : null;
|
||||
|
||||
return CursorPagingData(
|
||||
items: posts,
|
||||
hasMore: hasMore,
|
||||
nextCursor: nextCursor,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class PostRepliesList extends HookConsumerWidget {
|
||||
final String postId;
|
||||
@ -15,96 +61,46 @@ class PostRepliesList extends HookConsumerWidget {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final postAsync = ref.watch(postRepliesProvider(postId));
|
||||
final isWide = isWideScreen(context);
|
||||
|
||||
return postAsync.when(
|
||||
data:
|
||||
(controller) => SliverInfiniteList(
|
||||
itemCount: controller.posts.length,
|
||||
isLoading: controller.isLoading,
|
||||
hasReachedMax: controller.hasReachedMax,
|
||||
onFetchData: controller.fetchMore,
|
||||
itemBuilder: (context, index) {
|
||||
final post = controller.posts[index];
|
||||
return PostItem(
|
||||
item: post,
|
||||
backgroundColor: isWide ? Colors.transparent : null,
|
||||
);
|
||||
},
|
||||
separatorBuilder: (_, __) => const Divider(height: 1),
|
||||
emptyBuilder: (context) {
|
||||
return SliverToBoxAdapter(
|
||||
child: Column(
|
||||
children: [
|
||||
Text(
|
||||
'No replies',
|
||||
textAlign: TextAlign.center,
|
||||
).fontSize(18).bold(),
|
||||
Text('Why not start a discussion?'),
|
||||
],
|
||||
).padding(vertical: 16),
|
||||
);
|
||||
},
|
||||
),
|
||||
loading:
|
||||
() => SliverFillRemaining(
|
||||
child: const Center(child: CircularProgressIndicator()),
|
||||
),
|
||||
error:
|
||||
(e, _) => SliverFillRemaining(
|
||||
child: ResponseErrorWidget(
|
||||
error: e,
|
||||
onRetry: () {
|
||||
ref.invalidate(postRepliesProvider(postId));
|
||||
},
|
||||
),
|
||||
),
|
||||
return PagingHelperSliverView(
|
||||
provider: postRepliesNotifierProvider(postId),
|
||||
futureRefreshable: postRepliesNotifierProvider(postId).future,
|
||||
notifierRefreshable: postRepliesNotifierProvider(postId).notifier,
|
||||
contentBuilder: (data, widgetCount, endItemView) {
|
||||
if (data.items.isEmpty) {
|
||||
return SliverToBoxAdapter(
|
||||
child: Column(
|
||||
children: [
|
||||
Text(
|
||||
'No replies',
|
||||
textAlign: TextAlign.center,
|
||||
).fontSize(18).bold(),
|
||||
const Text('Why not start a discussion?'),
|
||||
],
|
||||
).padding(vertical: 16),
|
||||
);
|
||||
}
|
||||
|
||||
return SliverList.builder(
|
||||
itemCount: widgetCount,
|
||||
itemBuilder: (context, index) {
|
||||
if (index == widgetCount - 1) {
|
||||
return endItemView;
|
||||
}
|
||||
|
||||
return Column(
|
||||
children: [
|
||||
PostItem(
|
||||
item: data.items[index],
|
||||
backgroundColor: isWide ? Colors.transparent : null,
|
||||
),
|
||||
const Divider(height: 1),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final postRepliesProvider =
|
||||
FutureProviderFamily<_PostRepliesController, String>((ref, postId) async {
|
||||
final client = ref.watch(apiClientProvider);
|
||||
final controller = _PostRepliesController(client, postId);
|
||||
await controller.fetchMore();
|
||||
return controller;
|
||||
});
|
||||
|
||||
class _PostRepliesController {
|
||||
_PostRepliesController(this._dio, this.parentId);
|
||||
|
||||
final Dio _dio;
|
||||
final String parentId;
|
||||
final List<SnPost> posts = [];
|
||||
bool isLoading = false;
|
||||
bool hasReachedMax = false;
|
||||
int offset = 0;
|
||||
final int take = 20;
|
||||
int total = 0;
|
||||
|
||||
Future<void> fetchMore() async {
|
||||
if (isLoading || hasReachedMax) return;
|
||||
isLoading = true;
|
||||
|
||||
final response = await _dio.get(
|
||||
'/posts/$parentId/replies',
|
||||
queryParameters: {'offset': offset, 'take': take},
|
||||
);
|
||||
|
||||
final List<SnPost> fetched =
|
||||
(response.data as List)
|
||||
.map((e) => SnPost.fromJson(e as Map<String, dynamic>))
|
||||
.toList();
|
||||
|
||||
final headerTotal = int.tryParse(response.headers['x-total']?.first ?? '');
|
||||
if (headerTotal != null) total = headerTotal;
|
||||
|
||||
posts.addAll(fetched);
|
||||
offset += fetched.length;
|
||||
if (posts.length >= total) hasReachedMax = true;
|
||||
|
||||
isLoading = false;
|
||||
}
|
||||
}
|
||||
|
180
lib/widgets/post/post_replies.g.dart
Normal file
180
lib/widgets/post/post_replies.g.dart
Normal file
@ -0,0 +1,180 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'post_replies.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// RiverpodGenerator
|
||||
// **************************************************************************
|
||||
|
||||
String _$postRepliesNotifierHash() =>
|
||||
r'49c178102ec0a4136974a0e9a8f090f511abd542';
|
||||
|
||||
/// 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 _$PostRepliesNotifier
|
||||
extends BuildlessAutoDisposeAsyncNotifier<CursorPagingData<SnPost>> {
|
||||
late final String postId;
|
||||
|
||||
FutureOr<CursorPagingData<SnPost>> build(String postId);
|
||||
}
|
||||
|
||||
/// See also [PostRepliesNotifier].
|
||||
@ProviderFor(PostRepliesNotifier)
|
||||
const postRepliesNotifierProvider = PostRepliesNotifierFamily();
|
||||
|
||||
/// See also [PostRepliesNotifier].
|
||||
class PostRepliesNotifierFamily
|
||||
extends Family<AsyncValue<CursorPagingData<SnPost>>> {
|
||||
/// See also [PostRepliesNotifier].
|
||||
const PostRepliesNotifierFamily();
|
||||
|
||||
/// See also [PostRepliesNotifier].
|
||||
PostRepliesNotifierProvider call(String postId) {
|
||||
return PostRepliesNotifierProvider(postId);
|
||||
}
|
||||
|
||||
@override
|
||||
PostRepliesNotifierProvider getProviderOverride(
|
||||
covariant PostRepliesNotifierProvider provider,
|
||||
) {
|
||||
return call(provider.postId);
|
||||
}
|
||||
|
||||
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'postRepliesNotifierProvider';
|
||||
}
|
||||
|
||||
/// See also [PostRepliesNotifier].
|
||||
class PostRepliesNotifierProvider
|
||||
extends
|
||||
AutoDisposeAsyncNotifierProviderImpl<
|
||||
PostRepliesNotifier,
|
||||
CursorPagingData<SnPost>
|
||||
> {
|
||||
/// See also [PostRepliesNotifier].
|
||||
PostRepliesNotifierProvider(String postId)
|
||||
: this._internal(
|
||||
() => PostRepliesNotifier()..postId = postId,
|
||||
from: postRepliesNotifierProvider,
|
||||
name: r'postRepliesNotifierProvider',
|
||||
debugGetCreateSourceHash:
|
||||
const bool.fromEnvironment('dart.vm.product')
|
||||
? null
|
||||
: _$postRepliesNotifierHash,
|
||||
dependencies: PostRepliesNotifierFamily._dependencies,
|
||||
allTransitiveDependencies:
|
||||
PostRepliesNotifierFamily._allTransitiveDependencies,
|
||||
postId: postId,
|
||||
);
|
||||
|
||||
PostRepliesNotifierProvider._internal(
|
||||
super._createNotifier, {
|
||||
required super.name,
|
||||
required super.dependencies,
|
||||
required super.allTransitiveDependencies,
|
||||
required super.debugGetCreateSourceHash,
|
||||
required super.from,
|
||||
required this.postId,
|
||||
}) : super.internal();
|
||||
|
||||
final String postId;
|
||||
|
||||
@override
|
||||
FutureOr<CursorPagingData<SnPost>> runNotifierBuild(
|
||||
covariant PostRepliesNotifier notifier,
|
||||
) {
|
||||
return notifier.build(postId);
|
||||
}
|
||||
|
||||
@override
|
||||
Override overrideWith(PostRepliesNotifier Function() create) {
|
||||
return ProviderOverride(
|
||||
origin: this,
|
||||
override: PostRepliesNotifierProvider._internal(
|
||||
() => create()..postId = postId,
|
||||
from: from,
|
||||
name: null,
|
||||
dependencies: null,
|
||||
allTransitiveDependencies: null,
|
||||
debugGetCreateSourceHash: null,
|
||||
postId: postId,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
AutoDisposeAsyncNotifierProviderElement<
|
||||
PostRepliesNotifier,
|
||||
CursorPagingData<SnPost>
|
||||
>
|
||||
createElement() {
|
||||
return _PostRepliesNotifierProviderElement(this);
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
return other is PostRepliesNotifierProvider && other.postId == postId;
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode {
|
||||
var hash = _SystemHash.combine(0, runtimeType.hashCode);
|
||||
hash = _SystemHash.combine(hash, postId.hashCode);
|
||||
|
||||
return _SystemHash.finish(hash);
|
||||
}
|
||||
}
|
||||
|
||||
@Deprecated('Will be removed in 3.0. Use Ref instead')
|
||||
// ignore: unused_element
|
||||
mixin PostRepliesNotifierRef
|
||||
on AutoDisposeAsyncNotifierProviderRef<CursorPagingData<SnPost>> {
|
||||
/// The parameter `postId` of this provider.
|
||||
String get postId;
|
||||
}
|
||||
|
||||
class _PostRepliesNotifierProviderElement
|
||||
extends
|
||||
AutoDisposeAsyncNotifierProviderElement<
|
||||
PostRepliesNotifier,
|
||||
CursorPagingData<SnPost>
|
||||
>
|
||||
with PostRepliesNotifierRef {
|
||||
_PostRepliesNotifierProviderElement(super.provider);
|
||||
|
||||
@override
|
||||
String get postId => (origin as PostRepliesNotifierProvider).postId;
|
||||
}
|
||||
|
||||
// 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