1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- 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<AsyncValue<List<Post>>> {
- 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<void> 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<void> loadNext() async {
- if (!mounted ||state.isLoading || _reachedEnd) return;
- final previousPosts = state.valueOrNull ?? [];
- state = AsyncValue<List<Post>>.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<List<Post>>.error(e, StackTrace.current)
- .copyWithPrevious(AsyncValue.data(previousPosts));
- }
- }
- }
|