Make the view react to chat updates

This commit is contained in:
Richard Ramos 2020-06-13 17:47:26 -04:00 committed by Iuri Matias
parent f46eed86d7
commit 6a22c0275f
3 changed files with 18 additions and 19 deletions

View File

@ -33,10 +33,9 @@ proc handleChatEvents(self: ChatController) =
self.status.events.on("messagesLoaded") do(e:Args):
self.view.pushMessages(MsgsLoadedArgs(e).messages)
self.status.events.on("pushMessage") do(e: Args):
var evArgs = PushMessageArgs(e)
for chat in evArgs.chats:
self.view.updateChat(chat)
self.status.events.on("chatUpdate") do(e: Args):
var evArgs = ChatUpdateArgs(e)
self.view.updateChats(evArgs.chats)
self.view.pushMessages(evArgs.messages)
self.status.events.on("channelJoined") do(e: Args):
@ -67,10 +66,7 @@ proc init*(self: ChatController) =
self.status.chat.init()
proc handleMessage(self: ChatController, data: MessageSignal) =
for chat in data.chats:
self.status.chat.update(chat) # TODO: possible code smell. Try to unify this, by having the view react to the model
self.view.updateChat(chat)
self.view.pushMessages(data.messages)
self.status.chat.update(data.chats, data.messages)
proc handleDiscoverySummary(self: ChatController, data: DiscoverySummarySignal) =
## Handle mailserver peers being added and removed

View File

@ -126,12 +126,13 @@ QtObject:
proc leaveActiveChat*(self: ChatsView) {.slot.} =
self.status.chat.leave(self.activeChannel.id)
proc updateChat*(self: ChatsView, chat: Chat) =
self.upsertChannel(chat.id)
self.chats.updateChat(chat)
if(self.activeChannel.id == chat.id):
self.activeChannel.setChatItem(chat)
self.activeChannelChanged()
proc updateChats*(self: ChatsView, chats: seq[Chat]) =
for chat in chats:
self.upsertChannel(chat.id)
self.chats.updateChat(chat)
if(self.activeChannel.id == chat.id):
self.activeChannel.setChatItem(chat)
self.activeChannelChanged()
proc blockContact*(self: ChatsView, id: string): string {.slot.} =
return self.status.chat.blockContact(id)

View File

@ -9,7 +9,7 @@ import ../signals/messages
import tables
type
PushMessageArgs* = ref object of Args
ChatUpdateArgs* = ref object of Args
chats*: seq[Chat]
messages*: seq[Message]
@ -44,8 +44,10 @@ proc newChatModel*(events: EventEmitter): ChatModel =
proc delete*(self: ChatModel) =
discard
proc update*(self: ChatModel, chat: Chat) =
self.channels[chat.id] = chat
proc update*(self: ChatModel, chats: seq[Chat], messages: seq[Message]) =
for chat in chats:
self.channels[chat.id] = chat
self.events.emit("chatUpdate", ChatUpdateArgs(messages: messages, chats: chats))
proc hasChannel*(self: ChatModel, chatId: string): bool =
self.channels.hasKey(chatId)
@ -137,7 +139,7 @@ proc formatChatUpdate(response: JsonNode): (seq[Chat], seq[Message]) =
proc sendMessage*(self: ChatModel, chatId: string, msg: string): string =
var sentMessage = status_chat.sendChatMessage(chatId, msg)
var (chats, messages) = formatChatUpdate(parseJson(sentMessage))
self.events.emit("pushMessage", PushMessageArgs(messages: messages, chats: chats))
self.events.emit("chatUpdate", ChatUpdateArgs(messages: messages, chats: chats))
sentMessage
proc chatMessages*(self: ChatModel, chatId: string, initialLoad:bool = true) =
@ -159,7 +161,7 @@ proc markAllChannelMessagesRead*(self: ChatModel, chatId: string): JsonNode =
proc confirmJoiningGroup*(self: ChatModel, chatId: string) =
var response = parseJson(status_chat.confirmJoiningGroup(chatId))
var (chats, messages) = formatChatUpdate(response)
self.events.emit("pushMessage", PushMessageArgs(messages: messages, chats: chats))
self.events.emit("chatUpdate", ChatUpdateArgs(messages: messages, chats: chats))
proc blockContact*(self: ChatModel, id: string): string =
var contact = status_profile.getContactByID(id)