Skip to content

Commit

Permalink
Refactor: community/user avatars (#1070)
Browse files Browse the repository at this point in the history
* refactored community and user avatars

* updated comments for avatars

* fixed rename widget issue

* removed redundant nullable values for avatars

* added null checks

* made community and person nullable

* removed null assertion operator
  • Loading branch information
hjiangsu authored Jan 19, 2024
1 parent 8738abe commit 37ee89b
Show file tree
Hide file tree
Showing 15 changed files with 127 additions and 113 deletions.
4 changes: 2 additions & 2 deletions lib/community/pages/create_post_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import 'package:thunder/core/singletons/lemmy_client.dart';
import 'package:thunder/core/singletons/preferences.dart';
import 'package:thunder/post/cubit/create_post_cubit.dart';
import 'package:thunder/shared/common_markdown_body.dart';
import 'package:thunder/shared/community_icon.dart';
import 'package:thunder/shared/avatars/community_avatar.dart';
import 'package:thunder/shared/cross_posts.dart';
import 'package:thunder/shared/input_dialogs.dart';
import 'package:thunder/shared/link_preview_card.dart';
Expand Down Expand Up @@ -789,7 +789,7 @@ class _CommunitySelectorState extends State<CommunitySelector> {
padding: const EdgeInsets.only(left: 8, top: 12, bottom: 12),
child: Row(
children: [
CommunityIcon(community: _communityView?.community, radius: 16),
CommunityAvatar(community: _communityView?.community, radius: 16),
const SizedBox(width: 12),
_communityId != null
? Column(
Expand Down
6 changes: 3 additions & 3 deletions lib/community/widgets/community_drawer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import 'package:thunder/account/utils/profiles.dart';
import 'package:thunder/community/bloc/anonymous_subscriptions_bloc.dart';
import 'package:thunder/core/auth/bloc/auth_bloc.dart';
import 'package:thunder/feed/feed.dart';
import 'package:thunder/shared/community_icon.dart';
import 'package:thunder/shared/user_avatar.dart';
import 'package:thunder/shared/avatars/community_avatar.dart';
import 'package:thunder/shared/avatars/user_avatar.dart';
import 'package:thunder/thunder/bloc/thunder_bloc.dart';
import 'package:thunder/utils/instance.dart';
import 'package:thunder/utils/global_context.dart';
Expand Down Expand Up @@ -475,7 +475,7 @@ class CommunityItem extends StatelessWidget {

return Row(
children: [
CommunityIcon(community: community, radius: 16),
CommunityAvatar(community: community, radius: 16),
const SizedBox(width: 16.0),
Expanded(
child: Tooltip(
Expand Down
4 changes: 2 additions & 2 deletions lib/community/widgets/community_header.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:cached_network_image/cached_network_image.dart';
import 'package:lemmy_api_client/v3.dart';

import 'package:thunder/core/enums/full_name_separator.dart';
import 'package:thunder/shared/community_icon.dart';
import 'package:thunder/shared/avatars/community_avatar.dart';
import 'package:thunder/shared/icon_text.dart';
import 'package:thunder/utils/instance.dart';
import 'package:thunder/utils/numbers.dart';
Expand Down Expand Up @@ -91,7 +91,7 @@ class _CommunityHeaderState extends State<CommunityHeader> {
children: [
Row(
children: [
CommunityIcon(
CommunityAvatar(
community: widget.getCommunityResponse.communityView.community,
radius: 45.0,
),
Expand Down
2 changes: 1 addition & 1 deletion lib/community/widgets/community_sidebar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import 'package:thunder/feed/bloc/feed_bloc.dart';
import 'package:thunder/instance/instance_view.dart';
import 'package:thunder/shared/common_markdown_body.dart';
import 'package:thunder/shared/snackbar.dart';
import 'package:thunder/shared/user_avatar.dart';
import 'package:thunder/shared/avatars/user_avatar.dart';
import 'package:thunder/thunder/bloc/thunder_bloc.dart';
import 'package:thunder/utils/date_time.dart';
import 'package:thunder/utils/instance.dart';
Expand Down
4 changes: 2 additions & 2 deletions lib/community/widgets/post_card_metadata.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:lemmy_api_client/v3.dart';
import 'package:thunder/core/auth/bloc/auth_bloc.dart';
import 'package:thunder/core/enums/full_name_separator.dart';
import 'package:thunder/feed/feed.dart';
import 'package:thunder/shared/community_icon.dart';
import 'package:thunder/shared/avatars/community_avatar.dart';
import 'package:thunder/shared/icon_text.dart';
import 'package:thunder/shared/text/scalable_text.dart';
import 'package:thunder/thunder/bloc/thunder_bloc.dart';
Expand Down Expand Up @@ -203,7 +203,7 @@ class PostCommunityAndAuthor extends StatelessWidget {
GestureDetector(
child: Padding(
padding: const EdgeInsets.only(right: 8.0),
child: CommunityIcon(community: postView.community, radius: 14),
child: CommunityAvatar(community: postView.community, radius: 14),
),
onTap: () => navigateToFeedPage(context, communityId: postView.community.id, feedType: FeedType.community),
),
Expand Down
6 changes: 3 additions & 3 deletions lib/search/pages/search_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ import 'package:thunder/shared/error_message.dart';
import 'package:thunder/shared/input_dialogs.dart';
import 'package:thunder/shared/snackbar.dart';
import 'package:thunder/shared/sort_picker.dart';
import 'package:thunder/shared/community_icon.dart';
import 'package:thunder/shared/user_avatar.dart';
import 'package:thunder/shared/avatars/community_avatar.dart';
import 'package:thunder/shared/avatars/user_avatar.dart';
import 'package:thunder/thunder/bloc/thunder_bloc.dart';
import 'package:thunder/utils/bottom_sheet_list_picker.dart';
import 'package:thunder/utils/constants.dart';
Expand Down Expand Up @@ -713,7 +713,7 @@ class _SearchPageState extends State<SearchPage> with AutomaticKeepAliveClientMi
message: '${communityView.community.title}\n${generateCommunityFullName(context, communityView.community.name, fetchInstanceNameFromUrl(communityView.community.actorId))}',
preferBelow: false,
child: ListTile(
leading: CommunityIcon(community: communityView.community, radius: 25),
leading: CommunityAvatar(community: communityView.community, radius: 25),
title: Text(
communityView.community.title,
overflow: TextOverflow.ellipsis,
Expand Down
52 changes: 52 additions & 0 deletions lib/shared/avatars/community_avatar.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import 'package:flutter/material.dart';

import 'package:lemmy_api_client/v3.dart';
import 'package:cached_network_image/cached_network_image.dart';

/// A community avatar. Displays the associated community icon if available.
///
/// Otherwise, displays the first letter of the community title (display name).
/// If no title is available, displays the first letter of the community name.
class CommunityAvatar extends StatelessWidget {
/// The community information to display
final Community? community;

/// The radius of the avatar. Defaults to 12
final double radius;

const CommunityAvatar({super.key, this.community, this.radius = 12.0});

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

CircleAvatar placeholderIcon = CircleAvatar(
backgroundColor: theme.colorScheme.secondaryContainer,
maxRadius: radius,
child: Text(
community?.title.isNotEmpty == true
? community!.title[0].toUpperCase()
: community?.name.isNotEmpty == true
? community!.name[0].toUpperCase()
: '',
semanticsLabel: '',
style: TextStyle(fontWeight: FontWeight.bold, fontSize: radius),
),
);

if (community?.icon?.isNotEmpty != true) return placeholderIcon;

return CachedNetworkImage(
imageUrl: community!.icon!,
imageBuilder: (context, imageProvider) {
return CircleAvatar(
backgroundColor: Colors.transparent,
foregroundImage: imageProvider,
maxRadius: radius,
);
},
placeholder: (context, url) => placeholderIcon,
errorWidget: (context, url, error) => placeholderIcon,
);
}
}
52 changes: 52 additions & 0 deletions lib/shared/avatars/user_avatar.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import 'package:flutter/material.dart';

import 'package:lemmy_api_client/v3.dart';
import 'package:cached_network_image/cached_network_image.dart';

/// A user avatar. Displays the associated user icon if available.
///
/// Otherwise, displays the first letter of the user's display name.
/// If no display name is available, displays the first letter of the user's username.
class UserAvatar extends StatelessWidget {
/// The user information to display
final Person? person;

/// The radius of the avatar. Defaults to 16
final double radius;

const UserAvatar({super.key, this.person, this.radius = 16.0});

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

CircleAvatar placeholderIcon = CircleAvatar(
backgroundColor: theme.colorScheme.secondaryContainer,
maxRadius: radius,
child: Text(
person?.displayName?.isNotEmpty == true
? person!.displayName![0].toUpperCase()
: person?.name.isNotEmpty == true
? person!.name[0].toUpperCase()
: '',
semanticsLabel: '',
style: TextStyle(fontWeight: FontWeight.bold, fontSize: radius),
),
);

if (person?.avatar?.isNotEmpty != true) return placeholderIcon;

return CachedNetworkImage(
imageUrl: person!.avatar!,
imageBuilder: (context, imageProvider) {
return CircleAvatar(
backgroundColor: Colors.transparent,
foregroundImage: imageProvider,
maxRadius: radius,
);
},
placeholder: (context, url) => placeholderIcon,
errorWidget: (context, url, error) => placeholderIcon,
);
}
}
44 changes: 0 additions & 44 deletions lib/shared/community_icon.dart

This file was deleted.

6 changes: 3 additions & 3 deletions lib/shared/input_dialogs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import 'package:thunder/core/auth/helpers/fetch_account.dart';
import 'package:thunder/core/enums/full_name_separator.dart';
import 'package:thunder/core/singletons/lemmy_client.dart';
import 'package:thunder/feed/utils/community.dart';
import 'package:thunder/shared/community_icon.dart';
import 'package:thunder/shared/avatars/community_avatar.dart';
import 'package:thunder/shared/dialogs.dart';
import 'package:thunder/shared/user_avatar.dart';
import 'package:thunder/shared/avatars/user_avatar.dart';
import 'package:thunder/utils/global_context.dart';
import 'package:thunder/utils/instance.dart';
import 'package:thunder/utils/numbers.dart';
Expand Down Expand Up @@ -185,7 +185,7 @@ Widget buildCommunitySuggestionWidget(BuildContext context, CommunityView payloa
child: InkWell(
onTap: onSelected == null ? null : () => onSelected(payload),
child: ListTile(
leading: CommunityIcon(community: payload.community),
leading: CommunityAvatar(community: payload.community),
title: Text(
payload.community.title,
maxLines: 1,
Expand Down
46 changes: 0 additions & 46 deletions lib/shared/user_avatar.dart

This file was deleted.

6 changes: 3 additions & 3 deletions lib/user/pages/user_settings_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import 'package:thunder/core/singletons/lemmy_client.dart';
import 'package:thunder/feed/feed.dart';
import 'package:thunder/settings/widgets/settings_list_tile.dart';
import 'package:thunder/settings/widgets/toggle_option.dart';
import 'package:thunder/shared/community_icon.dart';
import 'package:thunder/shared/avatars/community_avatar.dart';
import 'package:thunder/shared/dialogs.dart';
import 'package:thunder/shared/input_dialogs.dart';
import 'package:thunder/shared/snackbar.dart';
import 'package:thunder/shared/user_avatar.dart';
import 'package:thunder/shared/avatars/user_avatar.dart';
import 'package:thunder/thunder/thunder_icons.dart';
import 'package:thunder/user/bloc/user_settings_bloc.dart';
import 'package:thunder/user/widgets/user_indicator.dart';
Expand Down Expand Up @@ -372,7 +372,7 @@ class _UserSettingsPageState extends State<UserSettingsPage> {
navigateToFeedPage(context, feedType: FeedType.community, communityName: '${community.name}@${fetchInstanceNameFromUrl(community.actorId)}');
},
child: ListTile(
leading: CommunityIcon(community: community, radius: 16.0),
leading: CommunityAvatar(community: community, radius: 16.0),
visualDensity: const VisualDensity(vertical: -2),
title: Text(
community.title,
Expand Down
2 changes: 1 addition & 1 deletion lib/user/widgets/user_header.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:lemmy_api_client/v3.dart';
import 'package:thunder/core/enums/full_name_separator.dart';

import 'package:thunder/shared/icon_text.dart';
import 'package:thunder/shared/user_avatar.dart';
import 'package:thunder/shared/avatars/user_avatar.dart';
import 'package:thunder/utils/instance.dart';
import 'package:thunder/utils/numbers.dart';

Expand Down
2 changes: 1 addition & 1 deletion lib/user/widgets/user_indicator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:lemmy_api_client/v3.dart';

import 'package:thunder/account/bloc/account_bloc.dart';
import 'package:thunder/core/enums/full_name_separator.dart';
import 'package:thunder/shared/user_avatar.dart';
import 'package:thunder/shared/avatars/user_avatar.dart';
import 'package:thunder/utils/instance.dart';

class UserIndicator extends StatefulWidget {
Expand Down
4 changes: 2 additions & 2 deletions lib/user/widgets/user_sidebar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import 'package:thunder/core/auth/bloc/auth_bloc.dart';
import 'package:thunder/core/enums/full_name_separator.dart';
import 'package:thunder/feed/utils/utils.dart';
import 'package:thunder/feed/view/feed_page.dart';
import 'package:thunder/shared/community_icon.dart';
import 'package:thunder/shared/avatars/community_avatar.dart';
import 'package:thunder/shared/snackbar.dart';
import 'package:thunder/user/widgets/user_sidebar_activity.dart';
import 'package:thunder/user/widgets/user_sidebar_stats.dart';
Expand Down Expand Up @@ -362,7 +362,7 @@ class _UserSidebarState extends State<UserSidebar> {
padding: const EdgeInsets.all(8.0),
child: Row(
children: [
CommunityIcon(community: mods.community, radius: 20.0),
CommunityAvatar(community: mods.community, radius: 20.0),
const SizedBox(width: 16.0),
Expanded(
child: Column(
Expand Down

0 comments on commit 37ee89b

Please sign in to comment.