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.status.events.on("messagesLoaded") do(e:Args):
self.view.pushMessages(MsgsLoadedArgs(e).messages) self.view.pushMessages(MsgsLoadedArgs(e).messages)
self.status.events.on("pushMessage") do(e: Args): self.status.events.on("chatUpdate") do(e: Args):
var evArgs = PushMessageArgs(e) var evArgs = ChatUpdateArgs(e)
for chat in evArgs.chats: self.view.updateChats(evArgs.chats)
self.view.updateChat(chat)
self.view.pushMessages(evArgs.messages) self.view.pushMessages(evArgs.messages)
self.status.events.on("channelJoined") do(e: Args): self.status.events.on("channelJoined") do(e: Args):
@ -67,10 +66,7 @@ proc init*(self: ChatController) =
self.status.chat.init() self.status.chat.init()
proc handleMessage(self: ChatController, data: MessageSignal) = proc handleMessage(self: ChatController, data: MessageSignal) =
for chat in data.chats: self.status.chat.update(data.chats, data.messages)
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)
proc handleDiscoverySummary(self: ChatController, data: DiscoverySummarySignal) = proc handleDiscoverySummary(self: ChatController, data: DiscoverySummarySignal) =
## Handle mailserver peers being added and removed ## Handle mailserver peers being added and removed

View File

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

View File

@ -9,7 +9,7 @@ import ../signals/messages
import tables import tables
type type
PushMessageArgs* = ref object of Args ChatUpdateArgs* = ref object of Args
chats*: seq[Chat] chats*: seq[Chat]
messages*: seq[Message] messages*: seq[Message]
@ -44,8 +44,10 @@ proc newChatModel*(events: EventEmitter): ChatModel =
proc delete*(self: ChatModel) = proc delete*(self: ChatModel) =
discard discard
proc update*(self: ChatModel, chat: Chat) = proc update*(self: ChatModel, chats: seq[Chat], messages: seq[Message]) =
self.channels[chat.id] = chat 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 = proc hasChannel*(self: ChatModel, chatId: string): bool =
self.channels.hasKey(chatId) 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 = proc sendMessage*(self: ChatModel, chatId: string, msg: string): string =
var sentMessage = status_chat.sendChatMessage(chatId, msg) var sentMessage = status_chat.sendChatMessage(chatId, msg)
var (chats, messages) = formatChatUpdate(parseJson(sentMessage)) 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 sentMessage
proc chatMessages*(self: ChatModel, chatId: string, initialLoad:bool = true) = 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) = proc confirmJoiningGroup*(self: ChatModel, chatId: string) =
var response = parseJson(status_chat.confirmJoiningGroup(chatId)) var response = parseJson(status_chat.confirmJoiningGroup(chatId))
var (chats, messages) = formatChatUpdate(response) 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 = proc blockContact*(self: ChatModel, id: string): string =
var contact = status_profile.getContactByID(id) var contact = status_profile.getContactByID(id)