feat: support multiple channels on the UI

This commit is contained in:
Richard Ramos 2020-05-21 11:08:04 -04:00 committed by Iuri Matias
parent 50fc70adfb
commit 0e2131d499
4 changed files with 53 additions and 33 deletions

View File

@ -1,5 +1,5 @@
import NimQml import NimQml
import json import json, sets
import ../../status/chat as status_chat import ../../status/chat as status_chat
import view import view
import messages import messages
@ -17,17 +17,19 @@ var sendMessage = proc (view: ChatsView, chatId: string, msg: string): string =
chatMessage.timestamp = $parsedMessage["timestamp"] chatMessage.timestamp = $parsedMessage["timestamp"]
chatMessage.identicon = parsedMessage["identicon"].str chatMessage.identicon = parsedMessage["identicon"].str
chatMessage.isCurrentUser = true chatMessage.isCurrentUser = true
view.pushMessage(chatMessage)
view.pushMessage(chatId, chatMessage)
sentMessage sentMessage
type ChatController* = ref object of SignalSubscriber type ChatController* = ref object of SignalSubscriber
view*: ChatsView view*: ChatsView
variant*: QVariant variant*: QVariant
channels: HashSet[string]
proc newController*(): ChatController = proc newController*(): ChatController =
result = ChatController() result = ChatController()
result.channels = initHashSet[string]()
result.view = newChatsView(sendMessage) result.view = newChatsView(sendMessage)
result.view.names = @[]
result.variant = newQVariant(result.view) result.variant = newQVariant(result.view)
proc delete*(self: ChatController) = proc delete*(self: ChatController) =
@ -38,7 +40,13 @@ proc init*(self: ChatController) =
discard discard
proc join*(self: ChatController, chatId: string) = proc join*(self: ChatController, chatId: string) =
# TODO: check whether we have joined a chat already or not if self.channels.contains(chatId):
# TODO:
return
self.channels.incl chatId
# TODO: save chat list in the db # TODO: save chat list in the db
echo "Joining chat: ", chatId echo "Joining chat: ", chatId
let oneToOne = isOneToOneChat(chatId) let oneToOne = isOneToOneChat(chatId)
@ -62,5 +70,5 @@ method onSignal(self: ChatController, data: Signal) =
chatMessage.message = message.text chatMessage.message = message.text
chatMessage.timestamp = message.timestamp #TODO convert to date/time? chatMessage.timestamp = message.timestamp #TODO convert to date/time?
chatMessage.identicon = message.identicon chatMessage.identicon = message.identicon
chatMessage.isCurrentUser = message.isCurrentUser #TODO: Determine who originated the message chatMessage.isCurrentUser = message.isCurrentUser
self.view.pushMessage(chatMessage) self.view.pushMessage(message.chatId, chatMessage)

View File

@ -14,31 +14,34 @@ QtObject:
ChatsView* = ref object of QAbstractListModel ChatsView* = ref object of QAbstractListModel
names*: seq[string] names*: seq[string]
callResult: string callResult: string
messageList: ChatMessageList messageList: Table[string, ChatMessageList]
sendMessage: proc (view: ChatsView, chatId: string, msg: string): string activeChannel: string
sendMessage: proc (view: ChatsView, channel: string, msg: string): string
proc delete(self: ChatsView) = proc delete(self: ChatsView) = self.QAbstractListModel.delete
self.QAbstractListModel.delete
proc setup(self: ChatsView) = proc setup(self: ChatsView) = self.QAbstractListModel.setup
self.QAbstractListModel.setup
proc newChatsView*(sendMessage: proc): ChatsView = proc newChatsView*(sendMessage: proc): ChatsView =
new(result, delete) new(result, delete)
result.sendMessage = sendMessage result.sendMessage = sendMessage
result.names = @[] result.names = @[]
result.messageList = newChatMessageList() result.activeChannel = ""
result.messageList = initTable[string, ChatMessageList]() # newChatMessageList()
result.setup result.setup
proc addNameTolist*(self: ChatsView, chatId: string) {.slot.} = proc upsertChannel(self: ChatsView, channel: string) =
self.beginInsertRows(newQModelIndex(), self.names.len, self.names.len) if not self.messageList.hasKey(channel):
self.names.add(chatId) self.messageList[channel] = newChatMessageList()
self.endInsertRows()
proc get*(self: ChatsView, index: int): string {.slot.} = proc addNameTolist*(self: ChatsView, channel: string) {.slot.} =
if index < 0 or index >= self.names.len: if(self.activeChannel == ""):
return self.activeChannel = channel
return self.names[index]
self.beginInsertRows(newQModelIndex(), self.names.len, self.names.len)
self.names.add(channel)
self.upsertChannel(channel)
self.endInsertRows()
method rowCount(self: ChatsView, index: QModelIndex = nil): int = method rowCount(self: ChatsView, index: QModelIndex = nil): int =
return self.names.len return self.names.len
@ -66,26 +69,32 @@ QtObject:
proc `callResult=`*(self: ChatsView, callResult: string) = self.setCallResult(callResult) proc `callResult=`*(self: ChatsView, callResult: string) = self.setCallResult(callResult)
# Binding between a QML variable and accesors is done here
QtProperty[string] callResult: QtProperty[string] callResult:
read = callResult read = callResult
write = setCallResult write = setCallResult
notify = callResultChanged notify = callResultChanged
proc onSend*(self: ChatsView, inputJSON: string) {.slot.} = proc onSend*(self: ChatsView, inputJSON: string) {.slot.} =
# TODO unhardcode chatId self.setCallResult(self.sendMessage(self, self.activeChannel, inputJSON))
self.setCallResult(self.sendMessage(self, "test", inputJSON))
echo "Done!: ", self.callResult
proc onMessage*(self: ChatsView, message: string) {.slot.} = proc pushMessage*(self:ChatsView, channel: string, message: ChatMessage) =
self.setCallResult(message) self.upsertChannel(channel)
echo "Received message: ", message self.messageList[channel].add(message)
proc pushMessage*(self:ChatsView, message: ChatMessage) = proc activeChannelChanged*(self: ChatsView) {.signal.}
self.messageList.add(message)
proc setActiveChannelByIndex(self: ChatsView, index: int) {.slot.} =
if self.activeChannel == self.names[index]: return
self.activeChannel = self.names[index]
self.activeChannelChanged()
QtProperty[string] activeChannel:
write = setActiveChannel
notify = activeChannelChanged
proc getMessageList(self: ChatsView): QVariant {.slot.} = proc getMessageList(self: ChatsView): QVariant {.slot.} =
return newQVariant(self.messageList) return newQVariant(self.messageList[self.activeChannel])
QtProperty[QVariant] messageList: QtProperty[QVariant] messageList:
read = getMessageList read = getMessageList
notify = activeChannelChanged

View File

@ -24,7 +24,7 @@ proc fromEvent*(event: JsonNode): Signal =
text: jsonMsg["text"].getStr, text: jsonMsg["text"].getStr,
timestamp: $jsonMsg["timestamp"].getInt, timestamp: $jsonMsg["timestamp"].getInt,
whisperTimestamp: $jsonMsg["whisperTimestamp"].getInt, whisperTimestamp: $jsonMsg["whisperTimestamp"].getInt,
isCurrentUser: false isCurrentUser: false # TODO: this must compare the fromAuthor against current user because the messages received from the mailserver will arrive as signals too, and those include the current user messages
) )
signal.messages.add(msg) signal.messages.add(msg)

View File

@ -145,7 +145,10 @@ SplitView {
MouseArea { MouseArea {
anchors.fill: parent anchors.fill: parent
onClicked: chatGroupsListView.currentIndex = index onClicked: {
chatsModel.setActiveChannelByIndex(index)
chatGroupsListView.currentIndex = index
}
} }
Rectangle { Rectangle {