ui: Prevent opening the same conversation twice
This commit is contained in:
parent
75dc8e081d
commit
00c326cc2d
@ -15,32 +15,41 @@ 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) {
|
||||||
Conversation? conversation = GetIt.I.get<ConversationRepository>().getConversation(jid);
|
bool hasConversation = viewModel.conversations.length > 0 && viewModel.conversations.firstWhere((item) => item.jid == jid, orElse: null) != null;
|
||||||
|
|
||||||
if (conversation == null) {
|
// Prevent adding the same conversation twice to the list of open conversations
|
||||||
// TODO
|
if (!hasConversation) {
|
||||||
// TODO: Install a middleware to make sure that the conversation gets added to the
|
Conversation? conversation = GetIt.I.get<ConversationRepository>().getConversation(jid);
|
||||||
// repository. Also handle updates
|
|
||||||
conversation = Conversation(
|
if (conversation == null) {
|
||||||
title: jid,
|
// TODO
|
||||||
jid: jid,
|
// TODO: Install a middleware to make sure that the conversation gets added to the
|
||||||
lastMessageBody: "",
|
// repository. Also handle updates
|
||||||
avatarUrl: "",
|
conversation = Conversation(
|
||||||
unreadCounter: 0
|
title: jid,
|
||||||
);
|
jid: jid,
|
||||||
GetIt.I.get<ConversationRepository>().setConversation(conversation);
|
lastMessageBody: "",
|
||||||
|
avatarUrl: "",
|
||||||
|
unreadCounter: 0
|
||||||
|
);
|
||||||
|
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,
|
||||||
|
Loading…
Reference in New Issue
Block a user