simply chat views by reacting to model events
simply chat views by reacting to model events abstract join event from join event call; react to join channel event remove unneded if else move left channel to event refactor getChannelColor refactor getChannelColor rename sendMessage method cleanup move random color to channels list remove toChatType remove toChatType
This commit is contained in:
parent
6f3b987346
commit
ebd29d9ffd
|
@ -3,6 +3,7 @@ import json, eventemitter
|
|||
import ../../models/chat as chat_model
|
||||
import ../../signals/types
|
||||
import ../../status/types as status_types
|
||||
import views/channels_list
|
||||
import view
|
||||
import chronicles
|
||||
|
||||
|
@ -39,6 +40,17 @@ proc init*(self: ChatController) =
|
|||
chatMessage.isCurrentUser = true
|
||||
self.view.pushMessage(sentMessage.chatId, chatMessage)
|
||||
|
||||
self.model.events.on("channelJoined") do(e: Args):
|
||||
var channelMessage = ChannelArgs(e)
|
||||
let chatItem = newChatItem(id = channelMessage.channel, channelMessage.chatTypeInt)
|
||||
discard self.view.chats.addChatItemToList(chatItem)
|
||||
|
||||
self.model.events.on("channelLeft") do(e: Args):
|
||||
discard self.view.chats.removeChatItemFromList(self.view.activeChannel)
|
||||
|
||||
self.model.events.on("activeChannelChanged") do(e: Args):
|
||||
self.view.setActiveChannel(ChannelArgs(e).channel)
|
||||
|
||||
self.model.load()
|
||||
self.view.setActiveChannelByIndex(0)
|
||||
|
||||
|
|
|
@ -4,13 +4,12 @@ import views/channels_list
|
|||
import views/message_list
|
||||
import ../../signals/types
|
||||
import ../../models/chat
|
||||
import random
|
||||
|
||||
QtObject:
|
||||
type
|
||||
ChatsView* = ref object of QAbstractListModel
|
||||
model: ChatModel
|
||||
chats: ChannelsList
|
||||
chats*: ChannelsList
|
||||
callResult: string
|
||||
messageList: Table[string, ChatMessageList]
|
||||
activeChannel: string
|
||||
|
@ -33,19 +32,10 @@ QtObject:
|
|||
QtProperty[QVariant] chats:
|
||||
read = getChatsList
|
||||
|
||||
proc onSend*(self: ChatsView, inputJSON: string) {.slot.} =
|
||||
discard self.model.sendMessage(self.activeChannel, inputJSON)
|
||||
|
||||
proc activeChannel*(self: ChatsView): string {.slot.} = self.activeChannel
|
||||
|
||||
proc getChannelColor*(self: ChatsView, channel:string): string {.slot.} =
|
||||
var selectedChannel: ChatItem
|
||||
try:
|
||||
selectedChannel = self.chats.getChannelByName(channel)
|
||||
except:
|
||||
return channelColors[0]
|
||||
|
||||
result = selectedChannel.color
|
||||
proc getChannelColor*(self: ChatsView, channel: string): string {.slot.} =
|
||||
self.chats.getChannelColor(channel)
|
||||
|
||||
proc activeChannelChanged*(self: ChatsView) {.signal.}
|
||||
|
||||
|
@ -93,28 +83,14 @@ QtObject:
|
|||
proc pushChatItem*(self: ChatsView, chatItem: ChatItem) =
|
||||
discard self.chats.addChatItemToList(chatItem)
|
||||
|
||||
proc joinChat*(self: ChatsView, channel: string, chatTypeInt: int): int {.slot.} =
|
||||
let chatType = ChatType(chatTypeInt)
|
||||
|
||||
if self.model.hasChannel(channel):
|
||||
result = self.chats.chats.findById(channel)
|
||||
else:
|
||||
self.model.join(channel)
|
||||
randomize()
|
||||
let randomColorIndex = rand(channelColors.len - 1)
|
||||
let chatItem = newChatItem(id = channel, chatType, color = channelColors[randomColorIndex])
|
||||
result = self.chats.addChatItemToList(chatItem)
|
||||
proc sendMessage*(self: ChatsView, message: string) {.slot.} =
|
||||
discard self.model.sendMessage(self.activeChannel, message)
|
||||
|
||||
self.setActiveChannel(channel)
|
||||
proc joinChat*(self: ChatsView, channel: string, chatTypeInt: int): int {.slot.} =
|
||||
self.model.join(channel, ChatType(chatTypeInt))
|
||||
|
||||
proc leaveActiveChat*(self: ChatsView) {.slot.} =
|
||||
self.model.leave(self.activeChannel)
|
||||
let channelCount = self.chats.removeChatItemFromList(self.activeChannel)
|
||||
if channelCount == 0:
|
||||
self.setActiveChannel("")
|
||||
else:
|
||||
let nextChannel = self.chats.chats[self.chats.chats.len - 1]
|
||||
self.setActiveChannel(nextChannel.name)
|
||||
|
||||
proc updateChat*(self: ChatsView, chat: ChatItem) =
|
||||
self.chats.updateChat(chat)
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import NimQml
|
||||
import Tables
|
||||
import strformat
|
||||
import random
|
||||
|
||||
import ../../../models/chat
|
||||
|
||||
|
@ -77,6 +78,9 @@ QtObject:
|
|||
}.toTable
|
||||
|
||||
proc addChatItemToList*(self: ChannelsList, channel: ChatItem): int =
|
||||
if channel.color == "":
|
||||
randomize()
|
||||
channel.color = channelColors[rand(channelColors.len - 1)]
|
||||
self.beginInsertRows(newQModelIndex(), 0, 0)
|
||||
self.chats.insert(channel, 0)
|
||||
self.endInsertRows()
|
||||
|
@ -99,11 +103,11 @@ QtObject:
|
|||
else:
|
||||
result = idx
|
||||
|
||||
proc getChannelByName*(self: ChannelsList, name: string): ChatItem =
|
||||
proc getChannelColor*(self: ChannelsList, name: string): string =
|
||||
for chat in self.chats:
|
||||
if chat.name == name:
|
||||
return chat
|
||||
raise newException(OSError, fmt"No chat found with the name {name}")
|
||||
return chat.color
|
||||
return channelColors[0]
|
||||
|
||||
proc updateChat*(self: ChannelsList, channel: ChatItem) =
|
||||
let idx = self.upsertChannel(channel)
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import eventemitter, sets, json, strutils
|
||||
import sequtils
|
||||
import ../status/utils
|
||||
import ../status/chat as status_chat
|
||||
import chronicles
|
||||
|
@ -14,6 +15,10 @@ type MsgArgs* = ref object of Args
|
|||
chatId*: string
|
||||
payload*: JsonNode
|
||||
|
||||
type ChannelArgs* = ref object of Args
|
||||
channel*: string
|
||||
chatTypeInt*: ChatType
|
||||
|
||||
type ChatArgs* = ref object of Args
|
||||
chats*: seq[Chat]
|
||||
|
||||
|
@ -35,7 +40,10 @@ proc delete*(self: ChatModel) =
|
|||
proc hasChannel*(self: ChatModel, chatId: string): bool =
|
||||
result = self.channels.contains(chatId)
|
||||
|
||||
proc join*(self: ChatModel, chatId: string, isNewChat: bool = true) =
|
||||
proc getActiveChannel*(self: ChatModel): string =
|
||||
if (self.channels.len == 0): "" else: self.channels.toSeq[self.channels.len - 1]
|
||||
|
||||
proc join*(self: ChatModel, chatId: string, chatTypeInt: ChatType, isNewChat: bool = true) =
|
||||
if self.hasChannel(chatId): return
|
||||
|
||||
self.channels.incl chatId
|
||||
|
@ -54,7 +62,7 @@ proc join*(self: ChatModel, chatId: string, isNewChat: bool = true) =
|
|||
status_chat.chatMessages(chatId)
|
||||
|
||||
let parsedResult = parseJson(filterResult)["result"]
|
||||
|
||||
|
||||
var topics = newSeq[string](0)
|
||||
for topicObj in parsedResult:
|
||||
if (($topicObj["chatId"]).strip(chars = {'"'}) == chatId):
|
||||
|
@ -67,10 +75,14 @@ proc join*(self: ChatModel, chatId: string, isNewChat: bool = true) =
|
|||
else:
|
||||
status_chat.requestMessages(topics, generatedSymKey, peer, 20)
|
||||
|
||||
self.events.emit("channelJoined", ChannelArgs(channel: chatId, chatTypeInt: chatTypeInt))
|
||||
self.events.emit("activeChannelChanged", ChannelArgs(channel: self.getActiveChannel()))
|
||||
|
||||
proc load*(self: ChatModel) =
|
||||
let chatList = status_chat.loadChats()
|
||||
for chat in chatList:
|
||||
self.join(chat.id, false)
|
||||
# TODO: use correct type of chat instead of hardcoded 2 (assumes it's only public chats)
|
||||
self.join(chat.id, ChatType.Public, false)
|
||||
self.events.emit("chatsLoaded", ChatArgs(chats: chatList))
|
||||
|
||||
proc leave*(self: ChatModel, chatId: string) =
|
||||
|
@ -81,6 +93,8 @@ proc leave*(self: ChatModel, chatId: string) =
|
|||
|
||||
self.filters.del(chatId)
|
||||
self.channels.excl(chatId)
|
||||
self.events.emit("channelLeft", ChannelArgs(channel: chatId))
|
||||
self.events.emit("activeChannelChanged", ChannelArgs(channel: self.getActiveChannel()))
|
||||
|
||||
proc sendMessage*(self: ChatModel, chatId: string, msg: string): string =
|
||||
var sentMessage = status_chat.sendChatMessage(chatId, msg)
|
||||
|
|
|
@ -32,7 +32,7 @@ Rectangle {
|
|||
anchors.right: parent.right
|
||||
anchors.rightMargin: 16
|
||||
onClicked: {
|
||||
chatsModel.onSend(txtData.text)
|
||||
chatsModel.sendMessage(txtData.text)
|
||||
txtData.text = ""
|
||||
}
|
||||
background: Rectangle {
|
||||
|
@ -62,11 +62,11 @@ Rectangle {
|
|||
anchors.left: parent.left
|
||||
anchors.leftMargin: 24
|
||||
Keys.onEnterPressed: {
|
||||
chatsModel.onSend(txtData.text)
|
||||
chatsModel.sendMessage(txtData.text)
|
||||
txtData.text = ""
|
||||
}
|
||||
Keys.onReturnPressed: {
|
||||
chatsModel.onSend(txtData.text)
|
||||
chatsModel.sendMessage(txtData.text)
|
||||
txtData.text = ""
|
||||
}
|
||||
background: Rectangle {
|
||||
|
|
Loading…
Reference in New Issue