Commit 1cef6788 authored by Wilko Manger's avatar Wilko Manger

Use extension methods for user utility functions

parent 53c2b36a
......@@ -65,7 +65,7 @@ Future<void> _showNotification(Map<String, dynamic> message) async {
final room = await user.rooms[roomId];
final event = await room.timeline[eventId];
final senderName = displayNameOf(event.sender);
final senderName = event.sender.displayName;
final icon = await DefaultCacheManager().getSingleFile(
event.sender.avatarUrl.toThumbnailStringWith(user.homeserver),
......@@ -115,7 +115,7 @@ Future<void> _showNotification(Map<String, dynamic> message) async {
Message fromEvent(RoomEvent event, Person person) {
if (event is EmoteMessageEvent) {
return Message(
'${displayNameOf(event.sender)} ${event.content.body}',
'${event.sender.displayName} ${event.content.body}',
event.time,
person,
);
......
......@@ -33,7 +33,7 @@ class ImagePageState extends State<ImagePage> {
ImageBloc bloc;
final ImageMessageEvent message;
var _messageSender;
User _messageSender;
var _date;
ImagePageState(ChatEvent<ImageMessageEvent> chatEvent)
......@@ -72,7 +72,7 @@ class ImagePageState extends State<ImagePage> {
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(displayNameOf(_messageSender)),
Text(_messageSender.displayName),
SizedBox(height: 2),
Text(
_date,
......
......@@ -26,23 +26,23 @@ List<TextSpan> spanFor(
TextStyle style = const TextStyle(fontWeight: FontWeight.bold),
}) {
final sender = TextSpan(
text: displayNameOf(event.sender),
text: event.sender.displayName,
style: style,
);
final subject = TextSpan(
text: displayNameOf(event.content.subject),
text: event.content.subject.displayName,
style: style,
);
var text;
if (event is DisplayNameChangeEvent) {
final oldName = TextSpan(
text: displayNameOrId(event.content.subject.id, event.oldSubjectName),
text: event.content.subject.nameOrDisplayId(name: event.oldSubjectName),
style: style,
);
final newName = TextSpan(
text: displayNameOf(event.content.subject),
text: event.content.subject.displayName,
style: style,
);
......
......@@ -28,7 +28,7 @@ List<TextSpan> typingSpan(BuildContext context, Room room) {
if (room.typingUsers.length == 1) {
return l(context).isTyping(
TextSpan(
text: displayNameOf(room.typingUsers.first),
text: room.typingUsers.first.displayName,
),
);
}
......@@ -36,20 +36,20 @@ List<TextSpan> typingSpan(BuildContext context, Room room) {
if (room.typingUsers.length == 2) {
return l(context).areTyping(
TextSpan(
text: displayNameOf(room.typingUsers.first),
text: room.typingUsers.first.displayName,
),
TextSpan(
text: displayNameOf(room.typingUsers[1]),
text: room.typingUsers[1].displayName,
),
);
}
return l(context).andMoreAreTyping(
TextSpan(
text: displayNameOf(room.typingUsers.first),
text: room.typingUsers.first.displayName,
),
TextSpan(
text: displayNameOf(room.typingUsers[1]),
text: room.typingUsers[1].displayName,
),
);
}
......@@ -90,7 +90,7 @@ abstract class MessageBubbleState<T extends MessageBubble>
TextStyle senderTextStyle(BuildContext context, {Color color}) {
if (color == null) {
color = colorOf(context, widget.event.sender);
color = widget.event.sender.getColor(context);
}
return textStyle(context, color: color).copyWith(
......@@ -122,7 +122,7 @@ abstract class MessageBubbleState<T extends MessageBubble>
if ((isStartOfGroup || (widget.isRepliedTo && !widget.isMine)) &&
!widget.item.room.isDirect) {
return Text(
displayNameOf(widget.event.sender, context),
widget.event.sender.displayName,
style: senderTextStyle(context, color: color),
);
} else {
......@@ -149,8 +149,7 @@ abstract class MessageBubbleState<T extends MessageBubble>
final previousEvent = (widget.previousItem as ChatEvent).event;
final previousHasSameSender = previousEvent != null &&
displayNameOf(previousEvent.sender) ==
displayNameOf(widget.event.sender) &&
previousEvent.sender.displayName == widget.event.sender.displayName &&
previousEvent.sender == widget.event.sender;
if (!previousHasSameSender) {
......@@ -194,8 +193,7 @@ abstract class MessageBubbleState<T extends MessageBubble>
final nextEvent = (widget.nextItem as ChatEvent).event;
final nextHasSameSender = nextEvent != null &&
displayNameOf(nextEvent.sender) ==
displayNameOf(widget.event.sender) &&
nextEvent.sender.displayName == widget.event.sender.displayName &&
nextEvent.sender == widget.event.sender;
if (!nextHasSameSender) {
......
......@@ -62,7 +62,7 @@ class CreationBubbleState extends StateBubbleState<CreationBubble> {
List<TextSpan> buildContentSpans(BuildContext context) =>
l(context).createdThisGroup(
TextSpan(
text: displayNameOf(widget.item.room.creator),
text: widget.item.room.creator.displayName,
style: defaultEmphasisTextStyle,
),
);
......
......@@ -60,7 +60,7 @@ class TopicBubbleState extends StateBubbleState<TopicBubble> {
List<TextSpan> buildContentSpans(BuildContext context) =>
l(context).changedDescriptionTapToView(
TextSpan(
text: displayNameOf(widget.event.sender),
text: widget.event.sender.displayName,
style: defaultEmphasisTextStyle,
),
);
......
......@@ -51,7 +51,7 @@ class UpgradeBubbleState extends StateBubbleState<UpgradeBubble> {
List<TextSpan> buildContentSpans(BuildContext context) =>
l(context).upgradedThisGroup(
TextSpan(
text: displayNameOf(widget.event.sender),
text: widget.event.sender.displayName,
style: defaultEmphasisTextStyle,
),
);
......
......@@ -135,7 +135,7 @@ class TextBubbleState extends MessageBubbleState<TextBubble> {
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
displayNameOf(widget.event.sender, context) + ' ',
widget.event.sender.getDisplayName(context) + ' ',
style: senderTextStyle(
context,
color: widget.isMine ? Colors.white : null,
......@@ -191,7 +191,7 @@ class TextBubbleState extends MessageBubbleState<TextBubble> {
child: CustomPaint(
painter: needsBorder
? ReplyBorderPainter(
color: colorOf(context, widget.event.sender),
color: widget.event.sender.getColor(context),
)
: null,
child: Padding(
......
......@@ -52,7 +52,7 @@ class CreateGroupBloc {
}
_userSubj.add(users.toList(growable: false)
..sort((User a, User b) => displayNameOf(a).compareTo(displayNameOf(b))));
..sort((User a, User b) => a.displayName.compareTo(b.displayName)));
}
Future<void> createRoom() async {
......
......@@ -124,7 +124,7 @@ class CreateGroupDetailsPageState extends State<CreateGroupDetailsPage> {
),
),
Text(
displayNameOf(user, context),
user.getDisplayName(context),
maxLines: 1,
overflow: TextOverflow.ellipsis,
textAlign: TextAlign.center,
......
......@@ -55,7 +55,7 @@ class ChatAvatar extends StatelessWidget {
return CircleAvatar(
foregroundColor: Colors.white,
backgroundColor: room.isDirect
? colorOf(context, room.directUser)
? room.directUser.getColor(context)
: LightColors.red[500],
radius: 24,
child: _icon(),
......
......@@ -45,7 +45,7 @@ abstract class Subtitle extends StatelessWidget {
: isMine = event?.sender == di.getLocalUser(),
senderName =
event != null && event.sender != di.getLocalUser() && !room.isDirect
? '${displayNameOf(event.sender)}: '
? '${event.sender.displayName}: '
: '';
factory Subtitle.forChat(ChatOverview chat) {
......
......@@ -32,7 +32,7 @@ class TextSubtitle extends Subtitle {
final sender = senderSpan(
context,
sender: event is EmoteMessageEvent
? displayNameOf(event.sender, context) + ' '
? event.sender.getDisplayName(context) + ' '
: null,
);
if (event.content.inReplyToId == null) {
......
......@@ -40,7 +40,7 @@ class TopicSubtitle extends Subtitle {
style: textStyle(context),
children: l(context).changedDescription(
TextSpan(
text: displayNameOf(event.sender),
text: event.sender.displayName,
style: TextStyle(
fontWeight: FontWeight.bold,
),
......
......@@ -33,7 +33,7 @@ class NamePageState extends State<NamePage> {
void initState() {
super.initState();
final name = displayNameOf(bloc.me);
final name = bloc.me.displayName;
textController.value = TextEditingValue(
text: name,
......
......@@ -82,7 +82,7 @@ class ProfilePageState extends State<ProfilePage> {
color: redOnBackground(context),
),
title: Text(l(context).name),
subtitle: Text(displayNameOf(bloc.me)),
subtitle: Text(bloc.me.displayName),
trailing: Icon(Icons.edit),
onTap: () => Navigator.pushNamed(
context,
......
......@@ -69,7 +69,7 @@ class SettingsPageState extends State<SettingsPage> {
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
displayNameOf(bloc.me),
bloc.me.displayName,
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.w600,
......
......@@ -42,7 +42,7 @@ class Redacted extends StatelessWidget {
text = [TextSpan(text: ' ${l(context).youDeletedThisMessage}')];
} else {
text = l(context).hasDeletedThisMessage(
TextSpan(text: ' ${displayNameOf(event.redaction.sender)}'),
TextSpan(text: ' ${event.redaction.sender.displayName}'),
);
}
......
......@@ -40,7 +40,7 @@ class UserAvatar extends StatelessWidget {
);
} else {
return CircleAvatar(
backgroundColor: colorOf(context, user),
backgroundColor: user.getColor(context),
radius: radius,
child: Icon(
Icons.person,
......
......@@ -91,7 +91,7 @@ class UserItemState extends State<UserItem> {
return ListTile(
leading: avatar,
title: Text(
displayNameOf(widget.user, context),
widget.user.getDisplayName(context),
style: TextStyle(
fontWeight: FontWeight.w600,
),
......
......@@ -34,7 +34,7 @@ FutureOr<String> nameOf(Room room, [BuildContext context]) {
}
if (room.isDirect) {
return displayNameOf(room.directUser, context);
return room.directUser.getDisplayName(context);
}
String calculateName(Iterable<User> members) {
......@@ -55,7 +55,7 @@ FutureOr<String> nameOf(Room room, [BuildContext context]) {
break;
}
name += displayNameOf(member, context);
name += member.getDisplayName(context);
if (i != nonMeMembers.length - 1) {
name += ', ';
......
......@@ -32,20 +32,26 @@ String _limited(String name) {
}
}
/// Get the proper display name for [user].
///
/// If [context] is provided, the local user will be 'You' instead
/// of their actual display name.
///
/// This is however not always desired, mostly only when showing the
/// display name to the end user.
String displayNameOf(User user, [BuildContext context]) =>
context != null && user == di.getLocalUser()
? l(context).you
: _limited(user?.name) ?? user.id.toString().split(':')[0];
String displayNameOrId(UserId id, String name) =>
_limited(name) ?? id.toString().split(':')[0];
Color colorOf(BuildContext context, User user) =>
userColor(context, user.id.hashCode % LightColors.userColors.length);
extension UserExtensions on User {
String get _displayId => id.toString().split(':')[0];
String get displayName => getDisplayName();
/// Get the proper display name for [user].
///
/// If [context] is provided, the local user will be 'You' instead
/// of their actual display name.
///
/// This is however not always desired, mostly only when showing the
/// display name to the end user.
String getDisplayName([BuildContext context]) =>
context != null && this == di.getLocalUser()
? l(context).you
: _limited(name) ?? _displayId;
String nameOrDisplayId({@required String name}) =>
_limited(name) ?? _displayId;
Color getColor(BuildContext context) =>
userColor(context, id.hashCode % LightColors.userColors.length);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment