import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:veloe_kemono_party_flutter/kemono_client.dart'; import 'package:veloe_kemono_party_flutter/models/post.dart'; class PostsNotifier extends StateNotifier>> { final String creatorId; final String service; final String query; final KemonoClient client; int _currentPage = 0; bool _reachedEnd = false; PostsNotifier({ required this.creatorId, required this.service, required this.query, required this.client, }) : super(const AsyncValue.loading()) { loadInitial(); } Future loadInitial() async { if (!mounted) return; state = const AsyncValue.loading(); try { final posts = await client.getPosts( creatorId, service, _currentPage * 50, query ); state = AsyncValue.data(posts); _currentPage = 1; } catch (e) { state = AsyncValue.error(e, StackTrace.current); } } Future loadNext() async { if (!mounted ||state.isLoading || _reachedEnd) return; final previousPosts = state.valueOrNull ?? []; state = AsyncValue>.loading().copyWithPrevious(state); try { final newPosts = await client.getPosts( creatorId, service, _currentPage * 50, query ); if (newPosts.length == 0){ _reachedEnd = true; return; } state = AsyncValue.data([...previousPosts, ...newPosts]); _currentPage++; } catch (e) { state = AsyncValue>.error(e, StackTrace.current) .copyWithPrevious(AsyncValue.data(previousPosts)); } } }