From 0a9aad7b034ed40059f9182365b8c5fb82eb15a5 Mon Sep 17 00:00:00 2001 From: Sale Djenic Date: Thu, 27 Jan 2022 13:44:33 +0100 Subject: [PATCH] fix(@desktop/app-search): loading messages improvements --- .../modules/main/app_search/controller.nim | 2 +- .../chat_content/messages/controller.nim | 18 +++++++++++--- .../messages/controller_interface.nim | 9 +++++++ .../chat_content/messages/module.nim | 24 ++++++++++++++++--- .../module_controller_delegate_interface.nim | 2 +- .../chat_content/messages/view.nim | 10 +++++++- src/app_service/service/message/service.nim | 7 +++--- .../Chat/views/ChatMessagesView.qml | 6 ++++- 8 files changed, 65 insertions(+), 13 deletions(-) diff --git a/src/app/modules/main/app_search/controller.nim b/src/app/modules/main/app_search/controller.nim index ca8c3b1e5d..9634ae2d99 100644 --- a/src/app/modules/main/app_search/controller.nim +++ b/src/app/modules/main/app_search/controller.nim @@ -38,7 +38,7 @@ type searchLocation: string searchSubLocation: string searchTerm: string - resultItems: Table[string, ResultItemDetails] # [resuiltItemId, ResultItemDetails] + resultItems: Table[string, ResultItemDetails] # [resultItemId, ResultItemDetails] proc newController*(delegate: io_interface.AccessInterface, events: EventEmitter, contactsService: contact_service.Service, chatService: chat_service.Service, communityService: community_service.Service, diff --git a/src/app/modules/main/chat_section/chat_content/messages/controller.nim b/src/app/modules/main/chat_section/chat_content/messages/controller.nim index 16c133ec64..39fd67e91b 100644 --- a/src/app/modules/main/chat_section/chat_content/messages/controller.nim +++ b/src/app/modules/main/chat_section/chat_content/messages/controller.nim @@ -23,6 +23,7 @@ type chatId: string belongsToCommunity: bool searchedMessageId: string + loadingMessagesPerPageFactor: int contactService: contact_service.Service communityService: community_service.Service chatService: chat_service.Service @@ -37,6 +38,7 @@ proc newController*(delegate: io_interface.AccessInterface, events: EventEmitter result.events = events result.sectionId = sectionId result.chatId = chatId + result.loadingMessagesPerPageFactor = 1 result.belongsToCommunity = belongsToCommunity result.contactService = contactService result.communityService = communityService @@ -145,7 +147,7 @@ method init*(self: Controller) = var args = ActiveSectionChatArgs(e) if(self.sectionId != args.sectionId or self.chatId != args.chatId): return - self.delegate.switchToMessage(args.messageId) + self.delegate.scrollToMessage(args.messageId) method getMySectionId*(self: Controller): string = return self.sectionId @@ -166,7 +168,8 @@ method belongsToCommunity*(self: Controller): bool = return self.belongsToCommunity method loadMoreMessages*(self: Controller) = - self.messageService.asyncLoadMoreMessagesForChat(self.chatId) + let limit = self.loadingMessagesPerPageFactor * MESSAGES_PER_PAGE + self.messageService.asyncLoadMoreMessagesForChat(self.chatId, limit) method addReaction*(self: Controller, messageId: string, emojiId: int) = self.messageService.addReaction(self.chatId, messageId, emojiId) @@ -212,4 +215,13 @@ method setSearchedMessageId*(self: Controller, searchedMessageId: string) = self.searchedMessageId = searchedMessageId method clearSearchedMessageId*(self: Controller) = - self.setSearchedMessageId("") \ No newline at end of file + self.setSearchedMessageId("") + +method getLoadingMessagesPerPageFactor*(self: Controller): int = + return self.loadingMessagesPerPageFactor + +method increaseLoadingMessagesPerPageFactor*(self: Controller) = + self.loadingMessagesPerPageFactor = self.loadingMessagesPerPageFactor + 1 + +method resetLoadingMessagesPerPageFactor*(self: Controller) = + self.loadingMessagesPerPageFactor = 1 \ No newline at end of file diff --git a/src/app/modules/main/chat_section/chat_content/messages/controller_interface.nim b/src/app/modules/main/chat_section/chat_content/messages/controller_interface.nim index 184a5366fb..42f7a5c260 100644 --- a/src/app/modules/main/chat_section/chat_content/messages/controller_interface.nim +++ b/src/app/modules/main/chat_section/chat_content/messages/controller_interface.nim @@ -79,4 +79,13 @@ method setSearchedMessageId*(self: AccessInterface, searchedMessageId: string) { raise newException(ValueError, "No implementation available") method clearSearchedMessageId*(self: AccessInterface) {.base.} = + raise newException(ValueError, "No implementation available") + +method getLoadingMessagesPerPageFactor*(self: AccessInterface): int {.base.} = + raise newException(ValueError, "No implementation available") + +method increaseLoadingMessagesPerPageFactor*(self: AccessInterface) {.base.} = + raise newException(ValueError, "No implementation available") + +method resetLoadingMessagesPerPageFactor*(self: AccessInterface) {.base.} = raise newException(ValueError, "No implementation available") \ No newline at end of file diff --git a/src/app/modules/main/chat_section/chat_content/messages/module.nim b/src/app/modules/main/chat_section/chat_content/messages/module.nim index 962067227c..59c6969feb 100644 --- a/src/app/modules/main/chat_section/chat_content/messages/module.nim +++ b/src/app/modules/main/chat_section/chat_content/messages/module.nim @@ -94,6 +94,19 @@ proc createChatIdentifierItem(self: Module): Item = @[], ) +proc checkIfMessageLoadedAndScrollToItIfItIs(self: Module): bool = + let searchedMessageId = self.controller.getSearchedMessageId() + if(searchedMessageId.len > 0): + self.view.emitScrollMessagesUpSignal() + let index = self.view.model().findIndexForMessageId(searchedMessageId) + self.controller.increaseLoadingMessagesPerPageFactor() + if(index != -1): + self.controller.clearSearchedMessageId() + self.controller.resetLoadingMessagesPerPageFactor() + self.view.emitScrollToMessageSignal(index) + return true + return false + method newMessagesLoaded*(self: Module, messages: seq[MessageDto], reactions: seq[ReactionDto], pinnedMessages: seq[PinnedMessageDto]) = var viewItems: seq[Item] @@ -157,10 +170,10 @@ method newMessagesLoaded*(self: Module, messages: seq[MessageDto], reactions: se if(not self.view.getInitialMessagesLoaded()): self.view.initialMessagesAreLoaded() + self.setLoadingHistoryMessagesInProgress(false) + # check if this loading was caused by the click on a messages from the app search result - let searchedMessageId = self.controller.getSearchedMessageId() - if(searchedMessageId.len > 0): - self.switchToMessage(searchedMessageId) + discard self.checkIfMessageLoadedAndScrollToItIfItIs() method messageAdded*(self: Module, message: MessageDto) = let sender = self.controller.getContactDetails(message.`from`) @@ -198,6 +211,7 @@ method onSendingMessageError*(self: Module) = self.view.emitSendingMessageErrorSignal() method loadMoreMessages*(self: Module) = + self.setLoadingHistoryMessagesInProgress(true) self.controller.loadMoreMessages() method toggleReaction*(self: Module, messageId: string, emojiId: int) = @@ -330,4 +344,8 @@ method switchToMessage*(self: Module, messageId: string) = self.view.emitSwitchToMessageSignal(index) else: self.controller.setSearchedMessageId(messageId) + +method scrollToMessage*(self: Module, messageId: string) = + self.controller.setSearchedMessageId(messageId) + if(not self.checkIfMessageLoadedAndScrollToItIfItIs()): self.loadMoreMessages() \ No newline at end of file diff --git a/src/app/modules/main/chat_section/chat_content/messages/private_interfaces/module_controller_delegate_interface.nim b/src/app/modules/main/chat_section/chat_content/messages/private_interfaces/module_controller_delegate_interface.nim index 36943a8958..20c4735cf5 100644 --- a/src/app/modules/main/chat_section/chat_content/messages/private_interfaces/module_controller_delegate_interface.nim +++ b/src/app/modules/main/chat_section/chat_content/messages/private_interfaces/module_controller_delegate_interface.nim @@ -38,5 +38,5 @@ method onMessageEdited*(self: AccessInterface, message: MessageDto) {.base.} = method setLoadingHistoryMessagesInProgress*(self: AccessInterface, isLoading: bool) {.base.} = raise newException(ValueError, "No implementation available") -method switchToMessage*(self: AccessInterface, messageId: string) {.base.} = +method scrollToMessage*(self: AccessInterface, messageId: string) {.base.} = raise newException(ValueError, "No implementation available") \ No newline at end of file diff --git a/src/app/modules/main/chat_section/chat_content/messages/view.nim b/src/app/modules/main/chat_section/chat_content/messages/view.nim index 11e918e1ab..46f541c953 100644 --- a/src/app/modules/main/chat_section/chat_content/messages/view.nim +++ b/src/app/modules/main/chat_section/chat_content/messages/view.nim @@ -133,4 +133,12 @@ QtObject: proc switchToMessage(self: View, messageIndex: int) {.signal.} proc emitSwitchToMessageSignal*(self: View, messageIndex: int) = - self.switchToMessage(messageIndex) \ No newline at end of file + self.switchToMessage(messageIndex) + + proc scrollToMessage(self: View, messageIndex: int) {.signal.} + proc emitScrollToMessageSignal*(self: View, messageIndex: int) = + self.scrollToMessage(messageIndex) + + proc scrollMessagesUp(self: View) {.signal.} + proc emitScrollMessagesUpSignal*(self: View) = + self.scrollMessagesUp() \ No newline at end of file diff --git a/src/app_service/service/message/service.nim b/src/app_service/service/message/service.nim index ede4e57de8..79f53572f4 100644 --- a/src/app_service/service/message/service.nim +++ b/src/app_service/service/message/service.nim @@ -20,7 +20,8 @@ logScope: topics = "messages-service" let NEW_LINE = re"\n|\r" #must be defined as let, not const -const MESSAGES_PER_PAGE = 20 +const MESSAGES_PER_PAGE* = 20 +const MESSAGES_PER_PAGE_MAX* = 300 const CURSOR_VALUE_IGNORE = "ignore" # Signals which may be emitted by this service: @@ -271,7 +272,7 @@ QtObject: self.events.emit(SIGNAL_MESSAGES_LOADED, data) - proc asyncLoadMoreMessagesForChat*(self: Service, chatId: string) = + proc asyncLoadMoreMessagesForChat*(self: Service, chatId: string, limit = MESSAGES_PER_PAGE) = if (chatId.len == 0): error "empty chat id", methodName="asyncLoadMoreMessagesForChat" return @@ -297,7 +298,7 @@ QtObject: chatId: chatId, msgCursor: msgCursor, pinnedMsgCursor: pinnedMsgCursor, - limit: MESSAGES_PER_PAGE + limit: if(limit <= MESSAGES_PER_PAGE_MAX): limit else: MESSAGES_PER_PAGE_MAX ) self.threadpool.start(arg) diff --git a/ui/app/AppLayouts/Chat/views/ChatMessagesView.qml b/ui/app/AppLayouts/Chat/views/ChatMessagesView.qml index f41ae67cd7..150cf9c454 100644 --- a/ui/app/AppLayouts/Chat/views/ChatMessagesView.qml +++ b/ui/app/AppLayouts/Chat/views/ChatMessagesView.qml @@ -48,7 +48,11 @@ Item { sendingMsgFailedPopup.open(); } - onSwitchToMessage: { + onScrollMessagesUp: { + chatLogView.positionViewAtEnd() + } + + onScrollToMessage: { chatLogView.positionViewAtIndex(messageIndex, ListView.Center); chatLogView.itemAtIndex(messageIndex).startMessageFoundAnimation(); }