feat: show unread messages badge

This commit is contained in:
Richard Ramos 2020-07-22 15:05:13 -04:00 committed by Iuri Matias
parent a1585c8499
commit 4b558c8c72
4 changed files with 44 additions and 0 deletions

View File

@ -24,6 +24,7 @@ proc handleChatEvents(self: ChatController) =
self.status.chat.chatMessages(channel.chat.id) self.status.chat.chatMessages(channel.chat.id)
self.status.events.on("chatsLoaded") do(e:Args): self.status.events.on("chatsLoaded") do(e:Args):
self.view.calculateUnreadMessages()
self.view.setActiveChannelByIndex(0) self.view.setActiveChannelByIndex(0)
self.view.appReady() self.view.appReady()

View File

@ -32,6 +32,7 @@ QtObject:
replyTo: string replyTo: string
channelOpenTime*: Table[string, int64] channelOpenTime*: Table[string, int64]
connected: bool connected: bool
unreadMessageCnt: int
proc setup(self: ChatsView) = self.QAbstractListModel.setup proc setup(self: ChatsView) = self.QAbstractListModel.setup
@ -55,6 +56,7 @@ QtObject:
result.messageList = initTable[string, ChatMessageList]() result.messageList = initTable[string, ChatMessageList]()
result.stickerPacks = newStickerPackList() result.stickerPacks = newStickerPackList()
result.recentStickers = newStickerList() result.recentStickers = newStickerList()
result.unreadMessageCnt = 0
result.setup() result.setup()
proc addStickerPackToList*(self: ChatsView, stickerPack: StickerPack, isInstalled, isBought: bool) = proc addStickerPackToList*(self: ChatsView, stickerPack: StickerPack, isInstalled, isBought: bool) =
@ -268,6 +270,23 @@ QtObject:
proc clearChatHistory*(self: ChatsView, id: string) {.slot.} = proc clearChatHistory*(self: ChatsView, id: string) {.slot.} =
self.status.chat.clearHistory(id) self.status.chat.clearHistory(id)
proc unreadMessages*(self: ChatsView): int {.slot.} =
result = self.unreadMessageCnt
proc unreadMessagesCntChanged*(self: ChatsView) {.signal.}
QtProperty[int] unreadMessagesCount:
read = unreadMessages
notify = unreadMessagesCntChanged
proc calculateUnreadMessages*(self: ChatsView) =
var unreadTotal = 0
for chatItem in self.chats.chats:
unreadTotal = unreadTotal + chatItem.unviewedMessagesCount
if unreadTotal != self.unreadMessageCnt:
self.unreadMessageCnt = unreadTotal
self.unreadMessagesCntChanged()
proc updateChats*(self: ChatsView, chats: seq[Chat]) = proc updateChats*(self: ChatsView, chats: seq[Chat]) =
for chat in chats: for chat in chats:
self.upsertChannel(chat.id) self.upsertChannel(chat.id)
@ -276,6 +295,7 @@ QtObject:
self.activeChannel.setChatItem(chat) self.activeChannel.setChatItem(chat)
self.currentSuggestions.setNewData(self.status.contacts.getContacts()) self.currentSuggestions.setNewData(self.status.contacts.getContacts())
self.activeChannelChanged() self.activeChannelChanged()
self.calculateUnreadMessages()
proc renameGroup*(self: ChatsView, newName: string) {.slot.} = proc renameGroup*(self: ChatsView, newName: string) {.slot.} =
self.status.chat.renameGroup(self.activeChannel.id, newName) self.status.chat.renameGroup(self.activeChannel.id, newName)

View File

@ -255,6 +255,10 @@ proc chatMessages*(self: ChatModel, chatId: string, initialLoad:bool = true) =
proc markAllChannelMessagesRead*(self: ChatModel, chatId: string): JsonNode = proc markAllChannelMessagesRead*(self: ChatModel, chatId: string): JsonNode =
var response = status_chat.markAllRead(chatId) var response = status_chat.markAllRead(chatId)
result = parseJson(response) result = parseJson(response)
if self.channels.hasKey(chatId):
self.channels[chatId].unviewedMessagesCount = 0
self.events.emit("chatUpdate", ChatUpdateArgs(messages: @[], chats: @[self.channels[chatId]], contacts: @[]))
proc confirmJoiningGroup*(self: ChatModel, chatId: string) = proc confirmJoiningGroup*(self: ChatModel, chatId: string) =
var response = status_chat.confirmJoiningGroup(chatId) var response = status_chat.confirmJoiningGroup(chatId)

View File

@ -55,6 +55,25 @@ RowLayout {
fillMode: Image.PreserveAspectFit fillMode: Image.PreserveAspectFit
source: parent.checked ? "img/messageActive.svg" : "img/message.svg" source: parent.checked ? "img/messageActive.svg" : "img/message.svg"
} }
Rectangle {
visible: !parent.checked && chatsModel.unreadMessagesCount > 0
anchors.top: image.top
anchors.left: image.right
anchors.leftMargin: -10
anchors.topMargin: -5
radius: 9
color: Style.current.blue
width: childrenRect.width + 10
height: childrenRect.height + 5
Text {
font.pixelSize: 12
color: Style.current.white
anchors.centerIn: parent
text: chatsModel.unreadMessagesCount
}
}
} }
TabButton { TabButton {