12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- 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;
- 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) return;
-
- final previousPosts = state.valueOrNull ?? [];
- state = AsyncValue<List<Post>>.loading().copyWithPrevious(state);
-
- try {
- final newPosts = await client.getPosts(
- creatorId,
- service,
- _currentPage * 50,
- query
- );
- state = AsyncValue.data([...previousPosts, ...newPosts]);
- _currentPage++;
- } catch (e) {
- state = AsyncValue<List<Post>>.error(e, StackTrace.current)
- .copyWithPrevious(AsyncValue.data(previousPosts));
- }
- }
- }
|