refactor(chat): make activeChannel a ChatItem to expose relevant information to the view

This commit is contained in:
Pascal Precht 2020-06-02 14:00:38 +02:00 committed by Iuri Matias
parent cc85a42b5c
commit dc6793a0f0
5 changed files with 64 additions and 20 deletions

View File

@ -42,7 +42,7 @@ proc handleChatEvents(self: ChatController) =
discard self.view.chats.addChatItemToList(chatItem)
self.status.events.on("channelLeft") do(e: Args):
discard self.view.chats.removeChatItemFromList(self.view.activeChannel)
discard self.view.chats.removeChatItemFromList(self.view.activeChannel.chatItem.id)
self.status.events.on("activeChannelChanged") do(e: Args):
self.view.setActiveChannel(ChannelArgs(e).channel)

View File

@ -7,6 +7,7 @@ import ../../status/status
import views/channels_list
import views/message_list
import views/chat_item
QtObject:
type
@ -15,7 +16,7 @@ QtObject:
chats*: ChannelsList
callResult: string
messageList: Table[string, ChatMessageList]
activeChannel: string
activeChannel*: ChatItemView
proc setup(self: ChatsView) = self.QAbstractListModel.setup
@ -25,7 +26,7 @@ QtObject:
new(result, delete)
result.status = status
result.chats = newChannelsList()
result.activeChannel = ""
result.activeChannel = newChatItemView()
result.messageList = initTable[string, ChatMessageList]()
result.setup()
@ -35,8 +36,6 @@ QtObject:
QtProperty[QVariant] chats:
read = getChatsList
proc activeChannel*(self: ChatsView): string {.slot.} = self.activeChannel
proc getChannelColor*(self: ChatsView, channel: string): string {.slot.} =
self.chats.getChannelColor(channel)
@ -46,12 +45,12 @@ QtObject:
if(self.chats.chats.len == 0): return
let selectedChannel = self.chats.getChannel(index)
if self.activeChannel == selectedChannel.id: return
self.activeChannel = selectedChannel.id
if self.activeChannel.id == selectedChannel.id: return
self.activeChannel.setChatItem(selectedChannel)
self.activeChannelChanged()
proc getActiveChannelIdx(self: ChatsView): QVariant {.slot.} =
return newQVariant(self.chats.chats.findById(self.activeChannel))
return newQVariant(self.chats.chats.findById(self.activeChannel.id))
QtProperty[QVariant] activeChannelIndex:
read = getActiveChannelIdx
@ -59,11 +58,14 @@ QtObject:
notify = activeChannelChanged
proc setActiveChannel*(self: ChatsView, channel: string) =
self.activeChannel = channel
self.activeChannel.setChatItem(self.chats.getChannel(self.chats.chats.findById(channel)))
self.activeChannelChanged()
QtProperty[string] activeChannel:
read = activeChannel
proc getActiveChannel*(self: ChatsView): QVariant {.slot.} =
result = newQVariant(self.activeChannel)
QtProperty[QVariant] activeChannel:
read = getActiveChannel
write = setActiveChannel
notify = activeChannelChanged
@ -76,8 +78,8 @@ QtObject:
self.messageList[channel].add(message)
proc getMessageList(self: ChatsView): QVariant {.slot.} =
self.upsertChannel(self.activeChannel)
return newQVariant(self.messageList[self.activeChannel])
self.upsertChannel(self.activeChannel.id)
return newQVariant(self.messageList[self.activeChannel.id])
QtProperty[QVariant] messageList:
read = getMessageList
@ -87,13 +89,13 @@ QtObject:
discard self.chats.addChatItemToList(chatItem)
proc sendMessage*(self: ChatsView, message: string) {.slot.} =
discard self.status.chat.sendMessage(self.activeChannel, message)
discard self.status.chat.sendMessage(self.activeChannel.id, message)
proc joinChat*(self: ChatsView, channel: string, chatTypeInt: int): int {.slot.} =
self.status.chat.join(channel, ChatType(chatTypeInt))
proc leaveActiveChat*(self: ChatsView) {.slot.} =
self.status.chat.leave(self.activeChannel)
self.status.chat.leave(self.activeChannel.id)
proc updateChat*(self: ChatsView, chat: ChatItem) =
self.chats.updateChat(chat)

View File

@ -0,0 +1,41 @@
import NimQml
import std/wrapnils
import ../../../status/chat
QtObject:
type ChatItemView* = ref object of QObject
chatItem*: ChatItem
proc setup(self: ChatItemView) =
self.QObject.setup
proc delete*(self: ChatItemView) =
self.QObject.delete
proc newChatItemView*(): ChatItemView =
new(result, delete)
result = ChatItemView()
result.setup
proc setChatItem*(self: ChatItemView, chatItem: ChatItem) =
self.chatItem = chatItem
proc id*(self: ChatItemView): string {.slot.} = result = ?.self.chatItem.id
QtProperty[string] id:
read = id
proc name*(self: ChatItemView): string {.slot.} = result = ?.self.chatItem.name
QtProperty[string] name:
read = name
proc identicon*(self: ChatItemView): string {.slot.} = result = ?.self.chatItem.identicon
QtProperty[string] identicon:
read = identicon
proc chatType*(self: ChatItemView): int {.slot.} =
if self.chatItem != nil:
result = self.chatItem.chatType.int
else:
result = 0
QtProperty[int] chatType:
read = chatType

View File

@ -8,7 +8,7 @@ import "../../../../imports"
import "../components"
Rectangle {
property string channelNameStr: "#" + chatsModel.activeChannel
property string channelNameStr: "#" + chatsModel.activeChannel.id
id: chatTopBarContent
color: "white"
@ -20,16 +20,16 @@ Rectangle {
ChannelIcon {
id: channelIcon
channelName: chatsModel.activeChannel
/* TODO(pascal): expose chatType from active channel to set it here */
channelType: Constants.chatTypeOneToOne
channelName: chatsModel.activeChannel.id
channelType: chatsModel.activeChannel.chatType
channelIdenticon: chatsModel.activeChannel.identicon
}
TextEdit {
id: channelName
width: 80
height: 20
text: chatTopBarContent.channelNameStr
text: chatsModel.activeChannel.chatType == Constants.chatTypeOneToOne ? chatsModel.activeChannel.name : channelNameStr
anchors.left: channelIcon.right
anchors.leftMargin: Theme.smallPadding
anchors.top: parent.top

View File

@ -38,6 +38,7 @@ Rectangle {
bottomMargin: 12
channelName: name
channelType: chatType
channelIdenticon: identicon
}
Text {