refactor: add translation option and builder for no chats case

This commit is contained in:
Vick Top 2024-04-16 15:50:09 +02:00
parent 89edfdd18c
commit f2e6875630
6 changed files with 117 additions and 74 deletions

View file

@ -24,7 +24,12 @@ class Home extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: chatNavigatorUserStory(context),
);
child: chatNavigatorUserStory(context,
configuration: ChatUserStoryConfiguration(
chatService: LocalChatService(),
chatOptionsBuilder: (ctx) => ChatOptions(
noChatsPlaceholderBuilder: (translations) =>
Text(translations.noUsersFound),
))));
}
}

View file

@ -18,6 +18,7 @@ class ChatOptions {
this.userAvatarBuilder = _createUserAvatar,
this.groupAvatarBuilder = _createGroupAvatar,
this.noChatsPlaceholderBuilder = _createNoChatsPlaceholder,
this.noUsersPlaceholderBuilder = _createNoUsersPlaceholder,
});
/// Builder function for the new chat button.
@ -43,6 +44,9 @@ class ChatOptions {
/// Builder function for the placeholder shown when no chats are available.
final NoChatsPlaceholderBuilder noChatsPlaceholderBuilder;
/// Builder function for the placeholder shown when no users are available.
final NoUsersPlaceholderBuilder noUsersPlaceholderBuilder;
}
Widget _createNewChatButton(
@ -179,6 +183,20 @@ Widget _createGroupAvatar(
Widget _createNoChatsPlaceholder(
ChatTranslations translations,
) =>
Center(
child: Text(
translations.noChatsFound,
textAlign: TextAlign.center,
style: const TextStyle(
color: Colors.white,
fontSize: 18,
),
),
);
Widget _createNoUsersPlaceholder(
ChatTranslations translations,
) =>
Center(
child: Text(
@ -231,3 +249,7 @@ typedef GroupAvatarBuilder = Widget Function(
typedef NoChatsPlaceholderBuilder = Widget Function(
ChatTranslations translations,
);
typedef NoUsersPlaceholderBuilder = Widget Function(
ChatTranslations translations,
);

View file

@ -20,7 +20,8 @@ class ChatTranslations {
'Are you sure you want to delete this chat?',
this.deleteChatModalCancel = 'Cancel',
this.deleteChatModalConfirm = 'Delete',
this.noUsersFound = 'No users found',
this.noUsersFound = 'No users were found to start a chat with.',
this.noChatsFound = 'Click on \'Start a chat\' to create a new chat.',
this.anonymousUser = 'Anonymous user',
this.chatCantBeDeleted = 'This chat can\'t be deleted',
this.chatProfileUsers = 'Users:',
@ -42,6 +43,7 @@ class ChatTranslations {
final String deleteChatModalCancel;
final String deleteChatModalConfirm;
final String noUsersFound;
final String noChatsFound;
final String chatCantBeDeleted;
final String chatProfileUsers;

View file

@ -196,28 +196,38 @@ class _ChatDetailScreenState extends State<ChatDetailScreen> {
36.0,
),
] else if (chatModel is PersonalChatModel) ...[
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
widget.options.userAvatarBuilder(
chatModel.user,
36.0,
),
const SizedBox(width: 8),
Text(
chatModel.user.firstName ??
widget.translations.anonymousUser,
style: theme.appBarTheme.titleTextStyle ??
const TextStyle(
fontSize: 18,
fontWeight: FontWeight.w800,
color: Colors.white,
),
),
],
widget.options.userAvatarBuilder(
chatModel.user,
36.0,
),
] else
...[],
Padding(
padding: const EdgeInsets.only(left: 15.5),
child: widget.chatTitleBuilder != null
? widget.chatTitleBuilder!.call(
(chatModel is GroupChatModel)
? chatModel.title
: (chatModel is PersonalChatModel)
? chatModel.user.fullName ??
widget.translations.anonymousUser
: '',
)
: Text(
(chatModel is GroupChatModel)
? chatModel.title
: (chatModel is PersonalChatModel)
? chatModel.user.fullName ??
widget.translations.anonymousUser
: '',
style: theme.appBarTheme.titleTextStyle ??
const TextStyle(
fontWeight: FontWeight.w800,
fontSize: 18.0,
color: Colors.white,
),
),
),
],
),
),

View file

@ -287,57 +287,59 @@ class ChatListItem extends StatelessWidget {
@override
Widget build(BuildContext context) => Column(
children: [
GestureDetector(
onTap: () => widget.onPressChat(chat),
child: Container(
color: Colors.transparent,
child: widget.options.chatRowContainerBuilder(
(chat is PersonalChatModel)
? ChatRow(
unreadMessages: chat.unreadMessages ?? 0,
avatar: widget.options.userAvatarBuilder(
(chat as PersonalChatModel).user,
40.0,
children: [
GestureDetector(
onTap: () => widget.onPressChat(chat),
child: Container(
color: Colors.transparent,
child: widget.options.chatRowContainerBuilder(
(chat is PersonalChatModel)
? ChatRow(
unreadMessages: chat.unreadMessages ?? 0,
avatar: widget.options.userAvatarBuilder(
(chat as PersonalChatModel).user,
40.0,
),
title: (chat as PersonalChatModel).user.fullName ??
translations.anonymousUser,
subTitle: chat.lastMessage != null
? chat.lastMessage is ChatTextMessageModel
? (chat.lastMessage! as ChatTextMessageModel)
.text
: '📷 '
'${translations.image}'
: '',
lastUsed: chat.lastUsed != null
? _dateFormatter.format(
date: chat.lastUsed!,
)
: null,
)
: ChatRow(
title: (chat as GroupChatModel).title,
unreadMessages: chat.unreadMessages ?? 0,
subTitle: chat.lastMessage != null
? chat.lastMessage is ChatTextMessageModel
? (chat.lastMessage! as ChatTextMessageModel)
.text
: '📷 '
'${translations.image}'
: '',
avatar: widget.options.groupAvatarBuilder(
(chat as GroupChatModel).title,
(chat as GroupChatModel).imageUrl,
40.0,
),
lastUsed: chat.lastUsed != null
? _dateFormatter.format(
date: chat.lastUsed!,
)
: null,
),
),
title: (chat as PersonalChatModel).user.fullName ??
translations.anonymousUser,
subTitle: chat.lastMessage != null
? chat.lastMessage is ChatTextMessageModel
? (chat.lastMessage! as ChatTextMessageModel).text
: '📷 '
'${translations.image}'
: '',
lastUsed: chat.lastUsed != null
? _dateFormatter.format(
date: chat.lastUsed!,
)
: null,
)
: ChatRow(
title: (chat as GroupChatModel).title,
unreadMessages: chat.unreadMessages ?? 0,
subTitle: chat.lastMessage != null
? chat.lastMessage is ChatTextMessageModel
? (chat.lastMessage! as ChatTextMessageModel).text
: '📷 '
'${translations.image}'
: '',
avatar: widget.options.groupAvatarBuilder(
(chat as GroupChatModel).title,
(chat as GroupChatModel).imageUrl,
40.0,
),
lastUsed: chat.lastUsed != null
? _dateFormatter.format(
date: chat.lastUsed!,
)
: null,
),
),
),
),
const Divider(),
],
);
const Divider(),
],
);
}

View file

@ -106,8 +106,10 @@ class _NewChatScreenState extends State<NewChatScreen> {
} else if (snapshot.hasData) {
return _buildUserList(snapshot.data!);
} else {
return widget.options
.noChatsPlaceholderBuilder(widget.translations);
return Padding(
padding: const EdgeInsets.only(top: 20.0),
child: Text(widget.translations.noUsersFound),
);
}
},
),