Commit 0d2b088b authored by Wilko Manger's avatar Wilko Manger

Use extension methods for Room utils

parent 4b643393
......@@ -172,7 +172,7 @@ class NotificationsBloc extends Bloc<NotificationsEvent, NotificationsState> {
await plugin.show(
event.id.hashCode,
nameOf(room),
room.getDisplayName(),
message.text,
NotificationDetails(
AndroidNotificationDetails(
......@@ -186,7 +186,8 @@ class NotificationsBloc extends Bloc<NotificationsEvent, NotificationsState> {
style: AndroidNotificationStyle.Messaging,
styleInformation: MessagingStyleInformation(
senderPerson,
conversationTitle: !room.isDirect ? await nameOf(room) : null,
conversationTitle:
!room.isDirect ? await room.getDisplayName() : null,
groupConversation: room.isDirect,
messages: [message],
),
......
......@@ -48,8 +48,6 @@ class ChatBloc extends Bloc<ChatEvent, ChatState> {
});
}
List<Type> get _ignoredEvents => ignoredEventsOf(room, isOverview: false);
LocalUser get me => matrix.user;
// TODO: Move to separate bloc
......@@ -165,7 +163,7 @@ class ChatBloc extends Bloc<ChatEvent, ChatState> {
// Don't show creation events in rooms that are replacements
shouldIgnore |= event is RoomCreationEvent && room.isReplacement;
if (_ignoredEvents.contains(event.runtimeType) || shouldIgnore) {
if (room.ignoredEvents.contains(event.runtimeType) || shouldIgnore) {
continue;
}
......
......@@ -87,7 +87,7 @@ class _ChatPageState extends State<ChatPage> {
@override
Widget build(BuildContext context) {
Widget avatar = Container();
final avatarUrl = avatarUrlOf(_room);
final avatarUrl = widget.room.displayAvatarUrl;
if (avatarUrl != null) {
avatar = Hero(
tag: _room.id,
......
......@@ -67,7 +67,7 @@ class _ChatSettingsPageState extends State<ChatSettingsPage> {
),
body: NestedScrollView(
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
final url = avatarUrlOf(room)?.toDownloadString(context);
final url = widget.room.displayAvatarUrl?.toDownloadString(context);
return <Widget>[
SliverAppBar(
expandedHeight: 128.0,
......
......@@ -48,12 +48,10 @@ class ChatsBloc extends Bloc<ChatsEvent, ChatsState> {
continue;
}
final ignoredEvents = ignoredEventsOf(room, isOverview: true);
// TODO: Add optional filter argument to up to call
final latestEvent =
(await room.timeline.get(upTo: 10, allowRemote: false)).firstWhere(
(event) => !ignoredEvents.contains(event.runtimeType),
(event) => !room.ignoredEvents.contains(event.runtimeType),
orElse: () => null,
);
......
......@@ -33,7 +33,7 @@ class ChatAvatar extends StatelessWidget {
@override
Widget build(BuildContext context) {
final avatarUrl = avatarUrlOf(room);
final avatarUrl = room.displayAvatarUrl;
if (avatarUrl != null) {
return Container(
width: 48,
......
......@@ -39,7 +39,7 @@ class ChatName extends StatelessWidget {
@override
Widget build(BuildContext context) {
return FutureOrBuilder<String>(
futureOr: nameOf(room, context),
futureOr: room.getDisplayName(context),
builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
if (snapshot.hasData) {
final name = snapshot.data;
......
......@@ -24,74 +24,66 @@ import 'package:pattle/src/resources/localizations.dart';
import '../matrix.dart';
import '../util/user.dart';
Uri avatarUrlOf(Room room) =>
room.avatarUrl ??
(room.isDirect ? room.directUser.avatarUrl : room.avatarUrl);
extension RoomExtension on Room {
Uri get displayAvatarUrl =>
avatarUrl ?? (isDirect ? directUser.avatarUrl : avatarUrl);
FutureOr<String> nameOf(Room room, [BuildContext context]) {
if (room.name != null) {
return room.name;
}
if (room.isDirect) {
return room.directUser.getDisplayName(context);
}
String calculateName(Iterable<User> members) {
var name = '';
if (members != null) {
if (members.length == 1 && context != null) {
return l(context).you;
// TODO: Check for aliases (public chats)
} else {
final nonMeMembers = members
.where((user) => context != null && user != Matrix.of(context).user)
.toList(growable: false);
var i = 0;
for (User member in nonMeMembers) {
if (i > 4) {
name += ' ${l(context).andOthers}';
break;
}
FutureOr<String> getDisplayName([BuildContext context]) {
if (name != null) {
return name;
}
name += member.getDisplayName(context);
if (isDirect) {
return directUser.getDisplayName(context);
}
if (i != nonMeMembers.length - 1) {
name += ', ';
String calculateName(Iterable<User> members) {
var name = '';
if (members != null) {
if (members.length == 1 && context != null) {
return l(context).you;
// TODO: Check for aliases (public chats)
} else {
final nonMeMembers = members
.where(
(user) => context != null && user != Matrix.of(context).user)
.toList(growable: false);
var i = 0;
for (User member in nonMeMembers) {
if (i > 4) {
name += ' ${l(context).andOthers}';
break;
}
name += member.getDisplayName(context);
if (i != nonMeMembers.length - 1) {
name += ', ';
}
i++;
}
i++;
}
} else {
return id.toString();
}
} else {
return room.id.toString();
}
return name.isNotEmpty ? name : room.id.toString();
}
final futureOrMembers = room.members.get(upTo: 6);
if (futureOrMembers is Future<Iterable<User>>) {
return futureOrMembers.then(calculateName);
} else {
return calculateName(futureOrMembers);
}
}
List<Type> ignoredEventsOf(Room room, {@required bool isOverview}) {
List<Type> ignored = [
RedactionEvent,
AvatarChangeEvent,
];
if (isOverview) {
ignored.add(DisplayNameChangeEvent);
}
return name.isNotEmpty ? name : id.toString();
}
if (room.isDirect) {
ignored.add(RoomCreationEvent);
final futureOrMembers = members.get(upTo: 6);
if (futureOrMembers is Future<Iterable<User>>) {
return futureOrMembers.then(calculateName);
} else {
return calculateName(futureOrMembers);
}
}
return ignored;
List<Type> get ignoredEvents => [
RedactionEvent,
AvatarChangeEvent,
DisplayNameChangeEvent,
if (isDirect) RoomCreationEvent,
];
}
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