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) discard self.view.chats.addChatItemToList(chatItem)
self.status.events.on("channelLeft") do(e: Args): 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.status.events.on("activeChannelChanged") do(e: Args):
self.view.setActiveChannel(ChannelArgs(e).channel) self.view.setActiveChannel(ChannelArgs(e).channel)

View File

@ -7,6 +7,7 @@ import ../../status/status
import views/channels_list import views/channels_list
import views/message_list import views/message_list
import views/chat_item
QtObject: QtObject:
type type
@ -15,7 +16,7 @@ QtObject:
chats*: ChannelsList chats*: ChannelsList
callResult: string callResult: string
messageList: Table[string, ChatMessageList] messageList: Table[string, ChatMessageList]
activeChannel: string activeChannel*: ChatItemView
proc setup(self: ChatsView) = self.QAbstractListModel.setup proc setup(self: ChatsView) = self.QAbstractListModel.setup
@ -25,7 +26,7 @@ QtObject:
new(result, delete) new(result, delete)
result.status = status result.status = status
result.chats = newChannelsList() result.chats = newChannelsList()
result.activeChannel = "" result.activeChannel = newChatItemView()
result.messageList = initTable[string, ChatMessageList]() result.messageList = initTable[string, ChatMessageList]()
result.setup() result.setup()
@ -35,8 +36,6 @@ QtObject:
QtProperty[QVariant] chats: QtProperty[QVariant] chats:
read = getChatsList read = getChatsList
proc activeChannel*(self: ChatsView): string {.slot.} = self.activeChannel
proc getChannelColor*(self: ChatsView, channel: string): string {.slot.} = proc getChannelColor*(self: ChatsView, channel: string): string {.slot.} =
self.chats.getChannelColor(channel) self.chats.getChannelColor(channel)
@ -46,12 +45,12 @@ QtObject:
if(self.chats.chats.len == 0): return if(self.chats.chats.len == 0): return
let selectedChannel = self.chats.getChannel(index) let selectedChannel = self.chats.getChannel(index)
if self.activeChannel == selectedChannel.id: return if self.activeChannel.id == selectedChannel.id: return
self.activeChannel = selectedChannel.id self.activeChannel.setChatItem(selectedChannel)
self.activeChannelChanged() self.activeChannelChanged()
proc getActiveChannelIdx(self: ChatsView): QVariant {.slot.} = 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: QtProperty[QVariant] activeChannelIndex:
read = getActiveChannelIdx read = getActiveChannelIdx
@ -59,11 +58,14 @@ QtObject:
notify = activeChannelChanged notify = activeChannelChanged
proc setActiveChannel*(self: ChatsView, channel: string) = proc setActiveChannel*(self: ChatsView, channel: string) =
self.activeChannel = channel self.activeChannel.setChatItem(self.chats.getChannel(self.chats.chats.findById(channel)))
self.activeChannelChanged() self.activeChannelChanged()
QtProperty[string] activeChannel: proc getActiveChannel*(self: ChatsView): QVariant {.slot.} =
read = activeChannel result = newQVariant(self.activeChannel)
QtProperty[QVariant] activeChannel:
read = getActiveChannel
write = setActiveChannel write = setActiveChannel
notify = activeChannelChanged notify = activeChannelChanged
@ -76,8 +78,8 @@ QtObject:
self.messageList[channel].add(message) self.messageList[channel].add(message)
proc getMessageList(self: ChatsView): QVariant {.slot.} = proc getMessageList(self: ChatsView): QVariant {.slot.} =
self.upsertChannel(self.activeChannel) self.upsertChannel(self.activeChannel.id)
return newQVariant(self.messageList[self.activeChannel]) return newQVariant(self.messageList[self.activeChannel.id])
QtProperty[QVariant] messageList: QtProperty[QVariant] messageList:
read = getMessageList read = getMessageList
@ -87,13 +89,13 @@ QtObject:
discard self.chats.addChatItemToList(chatItem) discard self.chats.addChatItemToList(chatItem)
proc sendMessage*(self: ChatsView, message: string) {.slot.} = 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.} = proc joinChat*(self: ChatsView, channel: string, chatTypeInt: int): int {.slot.} =
self.status.chat.join(channel, ChatType(chatTypeInt)) self.status.chat.join(channel, ChatType(chatTypeInt))
proc leaveActiveChat*(self: ChatsView) {.slot.} = proc leaveActiveChat*(self: ChatsView) {.slot.} =
self.status.chat.leave(self.activeChannel) self.status.chat.leave(self.activeChannel.id)
proc updateChat*(self: ChatsView, chat: ChatItem) = proc updateChat*(self: ChatsView, chat: ChatItem) =
self.chats.updateChat(chat) 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" import "../components"
Rectangle { Rectangle {
property string channelNameStr: "#" + chatsModel.activeChannel property string channelNameStr: "#" + chatsModel.activeChannel.id
id: chatTopBarContent id: chatTopBarContent
color: "white" color: "white"
@ -20,16 +20,16 @@ Rectangle {
ChannelIcon { ChannelIcon {
id: channelIcon id: channelIcon
channelName: chatsModel.activeChannel channelName: chatsModel.activeChannel.id
/* TODO(pascal): expose chatType from active channel to set it here */ channelType: chatsModel.activeChannel.chatType
channelType: Constants.chatTypeOneToOne channelIdenticon: chatsModel.activeChannel.identicon
} }
TextEdit { TextEdit {
id: channelName id: channelName
width: 80 width: 80
height: 20 height: 20
text: chatTopBarContent.channelNameStr text: chatsModel.activeChannel.chatType == Constants.chatTypeOneToOne ? chatsModel.activeChannel.name : channelNameStr
anchors.left: channelIcon.right anchors.left: channelIcon.right
anchors.leftMargin: Theme.smallPadding anchors.leftMargin: Theme.smallPadding
anchors.top: parent.top anchors.top: parent.top

View File

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