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 json
import json, sets
import ../../status/chat as status_chat
import view
import messages
@ -17,17 +17,19 @@ var sendMessage = proc (view: ChatsView, chatId: string, msg: string): string =
chatMessage.timestamp = $parsedMessage["timestamp"]
chatMessage.identicon = parsedMessage["identicon"].str
chatMessage.isCurrentUser = true
view.pushMessage(chatMessage)
view.pushMessage(chatId, chatMessage)
sentMessage
type ChatController* = ref object of SignalSubscriber
view*: ChatsView
variant*: QVariant
channels: HashSet[string]
proc newController*(): ChatController =
result = ChatController()
result.channels = initHashSet[string]()
result.view = newChatsView(sendMessage)
result.view.names = @[]
result.variant = newQVariant(result.view)
proc delete*(self: ChatController) =
@ -38,7 +40,13 @@ proc init*(self: ChatController) =
discard
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
echo "Joining chat: ", chatId
let oneToOne = isOneToOneChat(chatId)
@ -62,5 +70,5 @@ method onSignal(self: ChatController, data: Signal) =
chatMessage.message = message.text
chatMessage.timestamp = message.timestamp #TODO convert to date/time?
chatMessage.identicon = message.identicon
chatMessage.isCurrentUser = message.isCurrentUser #TODO: Determine who originated the message
self.view.pushMessage(chatMessage)
chatMessage.isCurrentUser = message.isCurrentUser
self.view.pushMessage(message.chatId, chatMessage)

View File

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

View File

@ -24,7 +24,7 @@ proc fromEvent*(event: JsonNode): Signal =
text: jsonMsg["text"].getStr,
timestamp: $jsonMsg["timestamp"].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)

View File

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