Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User feed page refactor #1144

Merged
merged 12 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/account/models/account.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Account {
});

Map<String, dynamic> toMap() {
return {'accountId': id, 'username': username, 'jwt': jwt, 'instance': instance, 'userId': userId};
return {'accountId': id.toString(), 'username': username, 'jwt': jwt, 'instance': instance, 'userId': userId};
}

@override
Expand Down Expand Up @@ -46,7 +46,7 @@ class Account {

return List.generate(maps.length, (i) {
return Account(
id: maps[i]['accountId'],
id: maps[i]['accountId'].toString(),
username: maps[i]['username'],
jwt: maps[i]['jwt'],
instance: maps[i]['instance'],
Expand All @@ -65,7 +65,7 @@ class Account {
if (maps == null || maps.isEmpty) return null;

return Account(
id: maps.first['accountId'],
id: maps.first['accountId'].toString(),
username: maps.first['username'],
jwt: maps.first['jwt'],
instance: maps.first['instance'],
Expand Down
6 changes: 3 additions & 3 deletions lib/community/utils/post_card_action_helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ import 'package:thunder/shared/picker_item.dart';
import 'package:thunder/shared/snackbar.dart';
import 'package:thunder/thunder/bloc/thunder_bloc.dart';
import 'package:thunder/user/bloc/user_bloc.dart';
import 'package:thunder/user/enums/user_action.dart';
import 'package:thunder/utils/instance.dart';
import 'package:thunder/instance/utils/navigate_instance.dart';
import 'package:thunder/user/utils/navigate_user.dart';
import 'package:lemmy_api_client/v3.dart';

import 'package:thunder/core/auth/bloc/auth_bloc.dart';
Expand Down Expand Up @@ -347,7 +347,7 @@ void onSelected(BuildContext context, PostCardAction postCardAction, PostViewMed
onTapCommunityName(context, postViewMedia.postView.community.id);
break;
case PostCardAction.visitProfile:
navigateToUserPage(context, userId: postViewMedia.postView.post.creatorId);
navigateToFeedPage(context, feedType: FeedType.user, userId: postViewMedia.postView.post.creatorId);
break;
case PostCardAction.visitInstance:
navigateToInstancePage(context, instanceHost: fetchInstanceNameFromUrl(postViewMedia.postView.community.actorId)!, instanceId: postViewMedia.postView.community.instanceId);
Expand Down Expand Up @@ -417,7 +417,7 @@ void onSelected(BuildContext context, PostCardAction postCardAction, PostViewMed
);
break;
case PostCardAction.blockUser:
context.read<UserBloc>().add(BlockUserEvent(personId: postViewMedia.postView.creator.id, blocked: true));
context.read<UserBloc>().add(UserActionEvent(userAction: UserAction.block, userId: postViewMedia.postView.creator.id, value: true));
break;
case PostCardAction.subscribeToCommunity:
context.read<CommunityBloc>().add(CommunityActionEvent(communityAction: CommunityAction.follow, communityId: postViewMedia.postView.community.id, value: true));
Expand Down
5 changes: 3 additions & 2 deletions lib/community/widgets/community_sidebar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ import 'package:thunder/core/auth/bloc/auth_bloc.dart';
import 'package:thunder/core/enums/full_name_separator.dart';
import 'package:thunder/core/singletons/lemmy_client.dart';
import 'package:thunder/feed/bloc/feed_bloc.dart';
import 'package:thunder/feed/utils/utils.dart';
import 'package:thunder/feed/view/feed_page.dart';
import 'package:thunder/instance/widgets/instance_view.dart';
import 'package:thunder/post/utils/navigate_create_post.dart';
import 'package:thunder/shared/common_markdown_body.dart';
import 'package:thunder/shared/avatars/user_avatar.dart';
import 'package:thunder/utils/date_time.dart';
import 'package:thunder/utils/instance.dart';
import 'package:thunder/user/utils/navigate_user.dart';

const kSidebarWidthFactor = 0.8;

Expand Down Expand Up @@ -215,7 +216,7 @@ class CommunityModeratorList extends StatelessWidget {
for (CommunityModeratorView mods in getCommunityResponse.moderators)
Material(
child: InkWell(
onTap: () => navigateToUserPage(context, userId: mods.moderator.id),
onTap: () => navigateToFeedPage(context, feedType: FeedType.user, userId: mods.moderator.id),
borderRadius: BorderRadius.circular(50),
child: Padding(
padding: const EdgeInsets.all(8.0),
Expand Down
2 changes: 1 addition & 1 deletion lib/community/widgets/post_card_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import 'package:thunder/community/widgets/post_card.dart';
import 'package:thunder/core/models/post_view_media.dart';
import 'package:thunder/shared/text/scalable_text.dart';
import 'package:thunder/thunder/bloc/thunder_bloc.dart';
import 'package:thunder/user/bloc/user_bloc.dart';
import 'package:thunder/user/bloc/user_bloc_old.dart';

class PostCardList extends StatefulWidget {
final List<PostViewMedia>? postViews;
Expand Down
3 changes: 1 addition & 2 deletions lib/community/widgets/post_card_metadata.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import 'package:thunder/shared/text/scalable_text.dart';
import 'package:thunder/thunder/bloc/thunder_bloc.dart';
import 'package:thunder/utils/date_time.dart';
import 'package:thunder/utils/instance.dart';
import 'package:thunder/user/utils/navigate_user.dart';
import 'package:thunder/utils/numbers.dart';

const Color upVoteColor = Colors.orange;
Expand Down Expand Up @@ -561,7 +560,7 @@ class PostCommunityAndAuthor extends StatelessWidget {
children: [
InkWell(
borderRadius: BorderRadius.circular(6),
onTap: (compactMode && !state.tappableAuthorCommunity) ? null : () => navigateToUserPage(context, userId: postView.creator.id),
onTap: (compactMode && !state.tappableAuthorCommunity) ? null : () => navigateToFeedPage(context, feedType: FeedType.user, userId: postView.creator.id),
child: ScalableText(
'$creatorName',
fontScale: state.metadataFontSizeScale,
Expand Down
68 changes: 54 additions & 14 deletions lib/feed/bloc/feed_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import 'package:equatable/equatable.dart';
import 'package:lemmy_api_client/v3.dart';
import 'package:stream_transform/stream_transform.dart';

import 'package:thunder/account/models/account.dart';
import 'package:thunder/core/auth/helpers/fetch_account.dart';
import 'package:thunder/core/models/post_view_media.dart';
import 'package:thunder/core/singletons/lemmy_client.dart';
import 'package:thunder/feed/enums/feed_type_subview.dart';
import 'package:thunder/feed/utils/community.dart';
import 'package:thunder/feed/utils/post.dart';
import 'package:thunder/feed/view/feed_page.dart';
Expand Down Expand Up @@ -367,12 +370,15 @@ class FeedBloc extends Bloc<FeedEvent, FeedState> {
emit(state.copyWith(status: FeedStatus.failure, message: 'Action is not supported'));
break;
}

// TODO: Add support for comment actions (for user profile)
}

/// Handles updating a given item within the feed
Future<void> _onFeedItemUpdated(FeedItemUpdatedEvent event, Emitter<FeedState> emit) async {
emit(state.copyWith(status: FeedStatus.fetching));

// TODO: Add support for updating comments (for user profile)
for (final (index, postViewMedia) in state.postViewMedias.indexed) {
if (postViewMedia.postView.post.id == event.postViewMedia.postView.post.id) {
state.postViewMedias[index] = event.postViewMedia;
Expand All @@ -396,11 +402,14 @@ class FeedBloc extends Bloc<FeedEvent, FeedState> {
emit(const FeedState(
status: FeedStatus.initial,
postViewMedias: <PostViewMedia>[],
hasReachedEnd: false,
commentViews: <CommentView>[],
hasReachedPostsEnd: false,
hasReachedCommentsEnd: false,
feedType: FeedType.general,
postListingType: null,
sortType: null,
fullCommunityView: null,
fullPersonView: null,
communityId: null,
communityName: null,
userId: null,
Expand Down Expand Up @@ -428,14 +437,15 @@ class FeedBloc extends Bloc<FeedEvent, FeedState> {
// Assert any requirements
if (event.reset) assert(event.feedType != null);
if (event.reset && event.feedType == FeedType.community) assert(!(event.communityId == null && event.communityName == null));
if (event.reset && event.feedType == FeedType.user) assert(event.userId != null && event.username != null);
if (event.reset && event.feedType == FeedType.user) assert(!(event.userId != null && event.username != null));
if (event.reset && event.feedType == FeedType.general) assert(event.postListingType != null);

// Handle the initial fetch or reload of a feed
if (event.reset) {
if (state.status != FeedStatus.initial) add(ResetFeedEvent());

GetCommunityResponse? fullCommunityView;
GetPersonDetailsResponse? fullPersonView;

switch (event.feedType) {
case FeedType.community:
Expand All @@ -453,39 +463,62 @@ class FeedBloc extends Bloc<FeedEvent, FeedState> {
break;
case FeedType.user:
// Fetch user information
try {
Account? account = await fetchActiveProfileAccount();
LemmyApiV3 lemmy = LemmyClient.instance.lemmyApiV3;

fullPersonView = await lemmy.run(GetPersonDetails(
auth: account?.jwt,
personId: event.userId,
username: event.username,
));
} catch (e) {
// If we are given a user feed, but we can't load the user, that's a problem! Emit an error.
return emit(state.copyWith(
status: FeedStatus.failureLoadingUser,
message: getExceptionErrorMessage(e, additionalInfo: event.username),
feedType: event.feedType,
));
}
break;
case FeedType.general:
break;
default:
break;
}

Map<String, dynamic> postViewMediaResult = await fetchPosts(
Map<String, dynamic> feedItemResult = await fetchFeedItems(
page: 1,
postListingType: event.postListingType,
sortType: event.sortType,
communityId: event.communityId,
communityName: event.communityName,
userId: event.userId,
userId: event.userId ?? fullPersonView?.personView.person.id,
username: event.username,
feedTypeSubview: event.feedTypeSubview,
);

// Extract information from the response
List<PostViewMedia> postViewMedias = postViewMediaResult['postViewMedias'];
bool hasReachedEnd = postViewMediaResult['hasReachedEnd'];
int currentPage = postViewMediaResult['currentPage'];
List<PostViewMedia> postViewMedias = feedItemResult['postViewMedias'];
List<CommentView> commentViews = feedItemResult['commentViews'];
bool hasReachedPostsEnd = feedItemResult['hasReachedPostsEnd'];
bool hasReachedCommentsEnd = feedItemResult['hasReachedCommentsEnd'];
int currentPage = feedItemResult['currentPage'];

return emit(state.copyWith(
status: FeedStatus.success,
postViewMedias: postViewMedias,
hasReachedEnd: hasReachedEnd,
commentViews: commentViews,
hasReachedPostsEnd: hasReachedPostsEnd,
hasReachedCommentsEnd: hasReachedCommentsEnd,
feedType: event.feedType,
postListingType: event.postListingType,
sortType: event.sortType,
fullCommunityView: fullCommunityView,
fullPersonView: fullPersonView,
communityId: event.communityId,
communityName: event.communityName,
userId: event.userId,
userId: event.userId ?? fullPersonView?.personView.person.id,
username: event.username,
currentPage: currentPage,
));
Expand All @@ -498,21 +531,25 @@ class FeedBloc extends Bloc<FeedEvent, FeedState> {
emit(state.copyWith(status: FeedStatus.fetching));

List<PostViewMedia> postViewMedias = List.from(state.postViewMedias);
List<CommentView> commentViews = List.from(state.commentViews);

Map<String, dynamic> postViewMediaResult = await fetchPosts(
Map<String, dynamic> feedItemResult = await fetchFeedItems(
page: state.currentPage,
postListingType: state.postListingType,
sortType: state.sortType,
communityId: state.communityId,
communityName: state.communityName,
userId: state.userId,
username: state.username,
feedTypeSubview: event.feedTypeSubview,
);

// Extract information from the response
List<PostViewMedia> newPostViewMedias = postViewMediaResult['postViewMedias'];
bool hasReachedEnd = postViewMediaResult['hasReachedEnd'];
int currentPage = postViewMediaResult['currentPage'];
List<PostViewMedia> newPostViewMedias = feedItemResult['postViewMedias'];
List<CommentView> newCommentViews = feedItemResult['commentViews'];
bool hasReachedPostsEnd = feedItemResult['hasReachedPostsEnd'];
bool hasReachedCommentsEnd = feedItemResult['hasReachedCommentsEnd'];
int currentPage = feedItemResult['currentPage'];

Set<int> newInsertedPostIds = Set.from(state.insertedPostIds);
List<PostViewMedia> filteredPostViewMedias = [];
Expand All @@ -527,12 +564,15 @@ class FeedBloc extends Bloc<FeedEvent, FeedState> {
}

postViewMedias.addAll(filteredPostViewMedias);
commentViews.addAll(newCommentViews);

return emit(state.copyWith(
status: FeedStatus.success,
insertedPostIds: newInsertedPostIds.toList(),
postViewMedias: postViewMedias,
hasReachedEnd: hasReachedEnd,
commentViews: commentViews,
hasReachedPostsEnd: hasReachedPostsEnd,
hasReachedCommentsEnd: hasReachedCommentsEnd,
currentPage: currentPage,
));
}
Expand Down
4 changes: 4 additions & 0 deletions lib/feed/bloc/feed_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ final class FeedFetchedEvent extends FeedEvent {
/// The type of feed to display.
final FeedType? feedType;

/// The subtype of feed to display (if applicable). This is only used when [feedType] is [FeedType.user]
final FeedTypeSubview feedTypeSubview;

/// The type of general feed to display: all, local, subscribed.
final ListingType? postListingType;

Expand All @@ -34,6 +37,7 @@ final class FeedFetchedEvent extends FeedEvent {

const FeedFetchedEvent({
this.feedType,
this.feedTypeSubview = FeedTypeSubview.post,
this.postListingType,
this.sortType,
this.communityId,
Expand Down
Loading
Loading