ui: Prevent opening the same conversation twice

This commit is contained in:
PapaTutuWawa 2021-12-23 20:54:12 +01:00
parent 75dc8e081d
commit 00c326cc2d

View File

@ -15,12 +15,17 @@ typedef AddConversationFunction = void Function(Conversation conversation);
class _NewConversationViewModel { class _NewConversationViewModel {
final AddConversationFunction addConversation; final AddConversationFunction addConversation;
final List<Conversation> conversations;
_NewConversationViewModel({ required this.addConversation }); _NewConversationViewModel({ required this.addConversation, required this.conversations });
} }
class NewConversationPage extends StatelessWidget { class NewConversationPage extends StatelessWidget {
void _addNewContact(_NewConversationViewModel viewModel, BuildContext context, String jid) { void _addNewContact(_NewConversationViewModel viewModel, BuildContext context, String jid) {
bool hasConversation = viewModel.conversations.length > 0 && viewModel.conversations.firstWhere((item) => item.jid == jid, orElse: null) != null;
// Prevent adding the same conversation twice to the list of open conversations
if (!hasConversation) {
Conversation? conversation = GetIt.I.get<ConversationRepository>().getConversation(jid); Conversation? conversation = GetIt.I.get<ConversationRepository>().getConversation(jid);
if (conversation == null) { if (conversation == null) {
@ -37,10 +42,14 @@ class NewConversationPage extends StatelessWidget {
GetIt.I.get<ConversationRepository>().setConversation(conversation); GetIt.I.get<ConversationRepository>().setConversation(conversation);
} }
viewModel.addConversation(conversation); viewModel.addConversation(conversation);
}
// TODO: Pass arguments
// TODO: Make sure that no conversation can be added twice // TODO: Make sure that no conversation can be added twice
Navigator.pushNamedAndRemoveUntil(context, "/conversation", ModalRoute.withName("/conversations"), arguments: ConversationPageArguments(jid: jid)); Navigator.pushNamedAndRemoveUntil(
context,
"/conversation",
ModalRoute.withName("/conversations"),
arguments: ConversationPageArguments(jid: jid));
} }
@override @override
@ -70,7 +79,8 @@ class NewConversationPage extends StatelessWidget {
lastMessageBody: c.lastMessageBody, lastMessageBody: c.lastMessageBody,
jid: c.jid jid: c.jid
) )
) ),
conversations: store.state.conversations
), ),
builder: (context, viewModel) => ListView.builder( builder: (context, viewModel) => ListView.builder(
itemCount: conversations.length + 2, itemCount: conversations.length + 2,