🐛 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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user