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

Refactor post metadata and user/community chips #1281

Merged
merged 6 commits into from
Apr 18, 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
157 changes: 0 additions & 157 deletions lib/community/widgets/post_card_metadata.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,105 +20,6 @@ import 'package:thunder/utils/numbers.dart';
const Color upVoteColor = Colors.orange;
const Color downVoteColor = Colors.blue;

@Deprecated("Use [PostViewMetaData] instead")
class PostCardMetaData extends StatelessWidget {
final int score;
final int voteType;
final int unreadComments;
final int comments;
final bool hasBeenEdited;
final DateTime published;
final String? hostURL;
final Color? readColor;

const PostCardMetaData({
super.key,
required this.score,
required this.voteType,
required this.unreadComments,
required this.comments,
required this.hasBeenEdited,
required this.published,
this.hostURL,
this.readColor,
});

final MaterialColor upVoteColor = Colors.orange;
final MaterialColor downVoteColor = Colors.blue;

@override
Widget build(BuildContext context) {
final AuthState authState = context.watch<AuthBloc>().state;
final showScores = authState.getSiteResponse?.myUser?.localUserView.localUser.showScores ?? true;
final theme = Theme.of(context);

return BlocBuilder<ThunderBloc, ThunderState>(
builder: (context, state) {
return Wrap(
children: [
IconText(
fontScale: state.metadataFontSizeScale,
text: showScores ? formatNumberToK(score) : null,
textColor: voteType == 1
? upVoteColor
: voteType == -1
? downVoteColor
: readColor,
icon: Icon(voteType == 1 ? Icons.arrow_upward : (voteType == -1 ? Icons.arrow_downward : (score < 0 ? Icons.arrow_downward : Icons.arrow_upward)),
size: 20.0,
color: voteType == 1
? upVoteColor
: voteType == -1
? downVoteColor
: readColor),
padding: 2.0,
),
const SizedBox(width: 8.0),
IconText(
fontScale: state.metadataFontSizeScale,
icon: Icon(
unreadComments > 0 && unreadComments != comments ? Icons.mark_unread_chat_alt_rounded : Icons.chat,
size: 18.0,
color: unreadComments > 0 && unreadComments != comments ? theme.primaryColor : readColor,
),
text: unreadComments > 0 && unreadComments != comments ? '+${formatNumberToK(unreadComments)}' : formatNumberToK(comments),
textColor: unreadComments > 0 && unreadComments != comments ? theme.primaryColor : readColor,
padding: 5.0,
),
const SizedBox(width: 8.0),
IconText(
fontScale: state.metadataFontSizeScale,
icon: Icon(
hasBeenEdited ? Icons.edit : Icons.history_rounded,
size: 18.0,
color: readColor,
),
text: formatTimeToString(dateTime: published.toIso8601String()),
textColor: readColor,
),
const SizedBox(width: 8.0),
if (hostURL != null)
Tooltip(
message: hostURL,
preferBelow: false,
child: IconText(
fontScale: state.metadataFontSizeScale,
icon: Icon(
Icons.public,
size: 17.0,
color: readColor,
),
text: Uri.parse(hostURL!).host.replaceFirst('www.', ''),
textColor: readColor,
),
),
],
);
},
);
}
}

/// Contains metadata related to a given post. This is generally displayed as part of the post card.
///
/// This information is customizable, and can be changed by the user in the settings.
Expand Down Expand Up @@ -452,64 +353,6 @@ class UrlPostCardMetaData extends StatelessWidget {
}
}

class PostViewMetaData extends StatelessWidget {
final int unreadComments;
final int comments;
final bool hasBeenEdited;
final DateTime published;
final bool saved;

const PostViewMetaData({
super.key,
required this.unreadComments,
required this.comments,
required this.hasBeenEdited,
required this.published,
required this.saved,
});

final MaterialColor upVoteColor = Colors.orange;
final MaterialColor downVoteColor = Colors.blue;
final MaterialColor savedColor = Colors.purple;

@override
Widget build(BuildContext context) {
final theme = Theme.of(context);

return BlocBuilder<ThunderBloc, ThunderState>(
builder: (context, state) {
return Row(
mainAxisSize: MainAxisSize.min,
children: [
IconText(
fontScale: state.metadataFontSizeScale,
icon: Icon(
Icons.chat,
size: 17.0,
color: theme.textTheme.titleSmall?.color?.withOpacity(0.75),
),
text: formatNumberToK(comments),
textColor: theme.textTheme.titleSmall?.color?.withOpacity(0.9),
padding: 5.0,
),
const SizedBox(width: 10.0),
IconText(
fontScale: state.metadataFontSizeScale,
icon: Icon(
hasBeenEdited ? Icons.refresh_rounded : Icons.history_rounded,
size: 19.0,
color: theme.textTheme.titleSmall?.color?.withOpacity(0.75),
),
text: formatTimeToString(dateTime: published.toIso8601String()),
textColor: theme.textTheme.titleSmall?.color?.withOpacity(0.9),
),
],
);
},
);
}
}

class PostCommunityAndAuthor extends StatelessWidget {
const PostCommunityAndAuthor({
super.key,
Expand Down
24 changes: 24 additions & 0 deletions lib/core/enums/user_type.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import 'package:flutter/material.dart';

enum UserType {
moderator,
admin,
self,
bot,
op;

get color {
switch (this) {
case UserType.moderator:
return Colors.orange;
case UserType.admin:
return Colors.red;
case UserType.self:
return Colors.green;
case UserType.bot:
return Colors.purple;
case UserType.op:
return Colors.blue;
}
}
}
12 changes: 12 additions & 0 deletions lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@
"@boldUserName": {
"description": "Setting for bolding the username"
},
"bot": "Bot",
"@bot": {
"description": "Label for a bot user."
},
"browserMode": "Link handling",
"@browserMode": {
"description": "Title for link handling setting (called browserMode internally)"
Expand Down Expand Up @@ -897,6 +901,10 @@
"@markPostAsReadOnScroll": {
"description": "Toggle to mark posts as read as you scroll past them in the feed."
},
"me": "Me",
"@me": {
"description": "Label for the current user."
},
"medium": "Medium",
"@medium": {
"description": "Description for medium font scale"
Expand Down Expand Up @@ -1115,6 +1123,10 @@
"@openSettings": {
"description": "Prompt for the user to open system settings"
},
"originalPoster": "Original Poster",
"@originalPoster": {
"description": "Label for the original poster."
},
"overview": "Overview",
"@overview": {},
"password": "Password",
Expand Down
61 changes: 61 additions & 0 deletions lib/post/widgets/post_metadata.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import 'package:flutter/material.dart';

import 'package:thunder/community/widgets/post_card_metadata.dart';
import 'package:thunder/post/enums/post_card_metadata_item.dart';

/// Contains metadata related to a given post. This is generally displayed as part of the post view.
///
/// The order in which the items are displayed depends on the order in the [postCardMetadataItems] list
class PostMetadata extends StatelessWidget {
/// The number of comments on the post. If null, no comment count will be displayed.
final int? commentCount;

/// The number of unread comments on the post. If null, no unread comment count will be displayed.
final int? unreadCommentCount;

/// The date/time the post was created or updated. This string should conform to ISO-8601 format.
final String? dateTime;

/// Whether or not the post has been edited. This determines the icon for the [dateTime] field.
final bool? hasBeenEdited;

/// The URL to display in the metadata. If null, no URL will be displayed.
final String? url;

const PostMetadata({
super.key,
this.commentCount,
this.unreadCommentCount,
this.dateTime,
this.hasBeenEdited = false,
this.url,
});

@override
Widget build(BuildContext context) {
List<PostCardMetadataItem> postCardMetadataItems = [
PostCardMetadataItem.commentCount,
PostCardMetadataItem.dateTime,
];

return Column(
children: [
Wrap(
spacing: 8.0,
runSpacing: 4.0,
crossAxisAlignment: WrapCrossAlignment.center,
children: postCardMetadataItems.map(
(PostCardMetadataItem postCardMetadataItem) {
return switch (postCardMetadataItem) {
PostCardMetadataItem.commentCount => CommentCountPostCardMetaData(commentCount: commentCount, unreadCommentCount: unreadCommentCount ?? 0, hasBeenRead: false),
PostCardMetadataItem.dateTime => DateTimePostCardMetaData(dateTime: dateTime!, hasBeenRead: false, hasBeenEdited: hasBeenEdited ?? false),
PostCardMetadataItem.url => UrlPostCardMetaData(url: url, hasBeenRead: false),
_ => Container(),
};
},
).toList(),
),
],
);
}
}
Loading
Loading