Skip to content
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
4 changes: 4 additions & 0 deletions lib/src/app/utils/navigation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,15 @@ Future<void> navigateToComment(BuildContext context, ThunderComment comment) asy
canSwipe: !kIsWeb && Platform.isIOS || gestureCubit.state.enableFullScreenSwipeNavigationGesture,
canOnlySwipeFromEdge: disableFullPageSwipe(isUserLoggedIn: profileBloc.state.isLoggedIn, state: gestureCubit.state, isPostPage: true) || !gestureCubit.state.enableFullScreenSwipeNavigationGesture,
builder: (context) {
final postNavigationCubit = PostNavigationCubit();
postNavigationCubit.setHighlightedCommentId(comment.id);

return MultiBlocProvider(
providers: [
BlocProvider.value(value: profileBloc),
BlocProvider.value(value: thunderBloc),
BlocProvider(create: (context) => PostBloc(account: account)),
BlocProvider.value(value: postNavigationCubit),
],
child: FutureBuilder<ThunderPost>(
future: getPostFromComment(comment, account),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,9 @@ class _ProfileSelectState extends State<ProfileSelect> {
count: 1,
timeout: 5,
).stream.first;
setState(() => account.latency = pingData.response?.time);
if (mounted) {
setState(() => account.latency = pingData.response?.time);
}
}
}
}
Expand Down Expand Up @@ -820,7 +822,9 @@ class _ProfileSelectState extends State<ProfileSelect> {
count: 1,
timeout: 5,
).stream.first;
setState(() => anonymousInstanceExtended.latency = pingData.response?.time);
if (mounted) {
setState(() => anonymousInstanceExtended.latency = pingData.response?.time);
}
}
}

Expand Down
34 changes: 23 additions & 11 deletions lib/src/features/post/presentation/pages/post_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -191,19 +191,26 @@ class _PostPageState extends State<PostPage> {
// If it is not visible, scroll to it.
final navigationState = context.read<PostNavigationCubit>().state;
final highlightedCommentId = navigationState.highlightedCommentId;
final highlightedCommentIndex = state.comments.indexWhere((element) => element.comment!.id == highlightedCommentId);
final highlightedCommentIndex = state.comments.indexWhere((element) => element.comment?.id == highlightedCommentId);

if (widget.highlightedCommentId != null && listController.isAttached && highlightedCommentIndex != -1) {
final visibleRange = listController.visibleRange;

if (visibleRange != null && (highlightedCommentIndex < (visibleRange.$1 + 3) || highlightedCommentIndex > (visibleRange.$2 - 3))) {
listController.animateToItem(
index: highlightedCommentIndex,
scrollController: scrollController,
alignment: 0,
duration: (estimatedDistance) => const Duration(milliseconds: 250),
curve: (estimatedDistance) => Curves.easeInOutCubicEmphasized,
);
// Add 1 to account for the placeholder widget at index 0
final adjustedIndex = highlightedCommentIndex + 1;

if (visibleRange != null && (adjustedIndex < (visibleRange.$1 + 3) || adjustedIndex > (visibleRange.$2 - 3))) {
// Defer animation to after the frame is rendered so that list extents are available
WidgetsBinding.instance.addPostFrameCallback((_) {
if (listController.isAttached) {
listController.animateToItem(
index: adjustedIndex,
scrollController: scrollController,
alignment: 0,
duration: (estimatedDistance) => const Duration(milliseconds: 250),
curve: (estimatedDistance) => Curves.easeInOutCubicEmphasized,
);
}
});
}
}

Expand Down Expand Up @@ -318,7 +325,12 @@ class _PostPageState extends State<PostPage> {
}

final commentNode = state.comments[index - 1];
final comment = commentNode.comment!;
final comment = commentNode.comment;

// Skip rendering if comment is null
if (comment == null) {
return const SizedBox.shrink();
}

final collapsedComments = context.read<PostBloc>().state.collapsedComments;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class _PostBodyState extends State<PostBody> with SingleTickerProviderStateMixin
final contentFontSizeScale = context.select<ThemePreferencesCubit, FontScale>((cubit) => cubit.state.contentFontSizeScale);

final post = widget.post;
final media = post.media.first;
final media = post.media.firstOrNull;

List<Widget> children = [
PostBodyTitle(
Expand All @@ -146,7 +146,7 @@ class _PostBodyState extends State<PostBody> with SingleTickerProviderStateMixin
),
];

if (postBodyViewType != PostBodyViewType.condensed && media.mediaType != MediaType.text) {
if (postBodyViewType != PostBodyViewType.condensed && media != null && media.mediaType != MediaType.text) {
children.add(
Expandable(
controller: expandableController,
Expand Down Expand Up @@ -218,7 +218,7 @@ class _PostBodyState extends State<PostBody> with SingleTickerProviderStateMixin
unreadCommentCount: post.unreadComments,
dateTime: post.updated?.toIso8601String() ?? post.published.toIso8601String(),
hasBeenEdited: post.updated != null,
url: media.mediaType != MediaType.image ? post.url : null,
url: media?.mediaType != MediaType.image ? post.url : null,
),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ class PostBodyTitle extends StatelessWidget {
Widget _buildCondensedTitle(BuildContext context) {
final showThumbnailPreviewOnRight = context.select<FeedPreferencesCubit, bool>((cubit) => cubit.state.showThumbnailPreviewOnRight);

final media = post.media.first;
final media = post.media.firstOrNull;

return Padding(
padding: EdgeInsets.symmetric(horizontal: media.mediaType == MediaType.text || showThumbnailPreviewOnRight ? 12.0 : 0.0).copyWith(top: 8.0, right: 8.0),
padding: EdgeInsets.symmetric(horizontal: media?.mediaType == MediaType.text || media == null || showThumbnailPreviewOnRight ? 12.0 : 0.0).copyWith(top: 8.0, right: 8.0),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (media.mediaType != MediaType.text && !showThumbnailPreviewOnRight) CompactThumbnailPreview(media: media, postId: post.id),
if (media != null && media.mediaType != MediaType.text && !showThumbnailPreviewOnRight) CompactThumbnailPreview(media: media, postId: post.id),
Expanded(
child: Column(
mainAxisSize: MainAxisSize.min,
Expand All @@ -76,7 +76,7 @@ class PostBodyTitle extends StatelessWidget {
],
),
),
if (media.mediaType != MediaType.text && showThumbnailPreviewOnRight) CompactThumbnailPreview(media: media, postId: post.id),
if (media != null && media.mediaType != MediaType.text && showThumbnailPreviewOnRight) CompactThumbnailPreview(media: media, postId: post.id),
_buildExpandButton(context, postBodyViewType),
],
),
Expand Down Expand Up @@ -125,10 +125,11 @@ class PostBodyTitle extends StatelessWidget {
Widget _buildExpandButton(BuildContext context, PostBodyViewType postBodyViewType) {
final l10n = GlobalContext.l10n;

final mediaType = post.media.first.mediaType;
final mediaType = post.media.firstOrNull?.mediaType;
final hasPostBody = post.body?.isNotEmpty == true;

if (mediaType == MediaType.text && !hasPostBody) return const SizedBox.shrink();
if (mediaType == null && !hasPostBody) return const SizedBox.shrink();
if (postBodyViewType == PostBodyViewType.condensed && !hasPostBody) return const SizedBox.shrink();

return IconButton(
Expand Down Expand Up @@ -189,6 +190,11 @@ class _PostBodyAuthorCommunityMetadataState extends State<PostBodyAuthorCommunit
final creator = widget.post.creator;
final community = widget.post.community;

// Return empty widget if creator or community is null
if (creator == null || community == null) {
return const SizedBox.shrink();
}

final postBodyShowUserInstance = context.select<FeedPreferencesCubit, bool>((cubit) => cubit.state.postBodyShowUserInstance);
final postBodyShowCommunityInstance = context.select<FeedPreferencesCubit, bool>((cubit) => cubit.state.postBodyShowCommunityInstance);

Expand All @@ -197,7 +203,7 @@ class _PostBodyAuthorCommunityMetadataState extends State<PostBodyAuthorCommunit
crossAxisAlignment: WrapCrossAlignment.center,
children: [
UserChip(
user: creator!,
user: creator,
personAvatar: UserAvatar(user: creator, radius: 8, thumbnailSize: 20, format: 'png'),
userGroups: userGroups,
includeInstance: postBodyShowUserInstance,
Expand All @@ -208,7 +214,7 @@ class _PostBodyAuthorCommunityMetadataState extends State<PostBodyAuthorCommunit
color: theme.textTheme.bodyMedium?.color?.withValues(alpha: 0.6),
),
CommunityChip(
communityId: community!.id,
communityId: community.id,
communityAvatar: CommunityAvatar(community: community, radius: 8, thumbnailSize: 20, format: 'png'),
communityName: community.name,
communityTitle: community.title,
Expand Down