diff --git a/lib/src/core/network/lemmy_api.dart b/lib/src/core/network/lemmy_api.dart index 7b976b9a9..2a45031bf 100644 --- a/lib/src/core/network/lemmy_api.dart +++ b/lib/src/core/network/lemmy_api.dart @@ -205,6 +205,7 @@ class LemmyApi { /// Fetches a list of posts from the Lemmy API Future> getPosts({ + int? page, String? cursor, int? limit, FeedListType? feedListType, @@ -214,17 +215,19 @@ class LemmyApi { bool? showHidden, bool? showSaved, }) async { - final queryParams = { + Map queryParams = { 'type_': feedListType?.value, 'sort': postSortType?.value, 'page_cursor': cursor, + 'page': page, 'limit': limit, 'community_name': communityName, 'community_id': communityId, - 'saved_only': showSaved, - 'show_hidden': showHidden, }; + if (showSaved == true) queryParams['saved_only'] = showSaved; + if (showHidden == true) queryParams['show_hidden'] = showHidden; + final json = await _request(HttpMethod.get, '/api/v3/post/list', queryParams); return { 'posts': json['posts'].map((pv) => ThunderPost.fromLemmyPostView(pv)).toList(), diff --git a/lib/src/features/feed/presentation/bloc/feed_bloc.dart b/lib/src/features/feed/presentation/bloc/feed_bloc.dart index 3b490d93e..00516e275 100644 --- a/lib/src/features/feed/presentation/bloc/feed_bloc.dart +++ b/lib/src/features/feed/presentation/bloc/feed_bloc.dart @@ -4,6 +4,7 @@ import 'package:bloc/bloc.dart'; import 'package:bloc_concurrency/bloc_concurrency.dart'; import 'package:equatable/equatable.dart'; import 'package:stream_transform/stream_transform.dart'; +import 'package:thunder/src/core/network/lemmy_api.dart'; import 'package:thunder/src/features/account/account.dart'; import 'package:thunder/src/features/comment/comment.dart'; @@ -655,7 +656,7 @@ class FeedBloc extends Bloc { )); } catch (e) { debugPrint('Error fetching feed: $e'); - return emit(state.copyWith(status: FeedStatus.failure, message: e.toString())); + return emit(state.copyWith(status: FeedStatus.failure, message: e is LemmyApiException ? e.message : e.toString())); } } diff --git a/lib/src/features/post/data/repositories/post_repository.dart b/lib/src/features/post/data/repositories/post_repository.dart index c318de41b..c1f16dbdf 100644 --- a/lib/src/features/post/data/repositories/post_repository.dart +++ b/lib/src/features/post/data/repositories/post_repository.dart @@ -166,8 +166,13 @@ class PostRepositoryImpl implements PostRepository { }) async { switch (account.platform) { case ThreadiversePlatform.lemmy: - return await lemmy.getPosts( - cursor: cursor, + // Use page-based pagination for Lemmy for 0.19.x instances as there are some performance issues with cursor-based pagination. + // See https://github.com/LemmyNet/lemmy/issues/6171, https://lemmy.world/post/40266465/21176898 + // TODO: Once 1.x.x is released, we can switch back to cursor-based pagination. + final page = cursor != null ? int.tryParse(cursor) ?? 1 : 1; + + final response = await lemmy.getPosts( + page: page, limit: limit, postSortType: postSortType, feedListType: feedListType, @@ -176,6 +181,13 @@ class PostRepositoryImpl implements PostRepository { showHidden: showHidden, showSaved: showSaved, ); + + // Return next page as string cursor for Lemmy + final nextPage = response['posts'].isNotEmpty ? (page + 1).toString() : null; + return { + 'posts': response['posts'], + 'next_page': nextPage, + }; case ThreadiversePlatform.piefed: // PieFed uses integer page-based pagination. The cursor in this case is the page number. final page = cursor != null ? int.tryParse(cursor) ?? 1 : 1;