fix(@desktop/app-search): loading messages improvements

This commit is contained in:
Sale Djenic 2022-01-27 13:44:33 +01:00
parent 25f7544b41
commit 0a9aad7b03
8 changed files with 65 additions and 13 deletions

View File

@ -38,7 +38,7 @@ type
searchLocation: string searchLocation: string
searchSubLocation: string searchSubLocation: string
searchTerm: 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, proc newController*(delegate: io_interface.AccessInterface, events: EventEmitter, contactsService: contact_service.Service,
chatService: chat_service.Service, communityService: community_service.Service, chatService: chat_service.Service, communityService: community_service.Service,

View File

@ -23,6 +23,7 @@ type
chatId: string chatId: string
belongsToCommunity: bool belongsToCommunity: bool
searchedMessageId: string searchedMessageId: string
loadingMessagesPerPageFactor: int
contactService: contact_service.Service contactService: contact_service.Service
communityService: community_service.Service communityService: community_service.Service
chatService: chat_service.Service chatService: chat_service.Service
@ -37,6 +38,7 @@ proc newController*(delegate: io_interface.AccessInterface, events: EventEmitter
result.events = events result.events = events
result.sectionId = sectionId result.sectionId = sectionId
result.chatId = chatId result.chatId = chatId
result.loadingMessagesPerPageFactor = 1
result.belongsToCommunity = belongsToCommunity result.belongsToCommunity = belongsToCommunity
result.contactService = contactService result.contactService = contactService
result.communityService = communityService result.communityService = communityService
@ -145,7 +147,7 @@ method init*(self: Controller) =
var args = ActiveSectionChatArgs(e) var args = ActiveSectionChatArgs(e)
if(self.sectionId != args.sectionId or self.chatId != args.chatId): if(self.sectionId != args.sectionId or self.chatId != args.chatId):
return return
self.delegate.switchToMessage(args.messageId) self.delegate.scrollToMessage(args.messageId)
method getMySectionId*(self: Controller): string = method getMySectionId*(self: Controller): string =
return self.sectionId return self.sectionId
@ -166,7 +168,8 @@ method belongsToCommunity*(self: Controller): bool =
return self.belongsToCommunity return self.belongsToCommunity
method loadMoreMessages*(self: Controller) = 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) = method addReaction*(self: Controller, messageId: string, emojiId: int) =
self.messageService.addReaction(self.chatId, messageId, emojiId) self.messageService.addReaction(self.chatId, messageId, emojiId)
@ -212,4 +215,13 @@ method setSearchedMessageId*(self: Controller, searchedMessageId: string) =
self.searchedMessageId = searchedMessageId self.searchedMessageId = searchedMessageId
method clearSearchedMessageId*(self: Controller) = method clearSearchedMessageId*(self: Controller) =
self.setSearchedMessageId("") 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

View File

@ -79,4 +79,13 @@ method setSearchedMessageId*(self: AccessInterface, searchedMessageId: string) {
raise newException(ValueError, "No implementation available") raise newException(ValueError, "No implementation available")
method clearSearchedMessageId*(self: AccessInterface) {.base.} = 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") raise newException(ValueError, "No implementation available")

View File

@ -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], method newMessagesLoaded*(self: Module, messages: seq[MessageDto], reactions: seq[ReactionDto],
pinnedMessages: seq[PinnedMessageDto]) = pinnedMessages: seq[PinnedMessageDto]) =
var viewItems: seq[Item] var viewItems: seq[Item]
@ -157,10 +170,10 @@ method newMessagesLoaded*(self: Module, messages: seq[MessageDto], reactions: se
if(not self.view.getInitialMessagesLoaded()): if(not self.view.getInitialMessagesLoaded()):
self.view.initialMessagesAreLoaded() self.view.initialMessagesAreLoaded()
self.setLoadingHistoryMessagesInProgress(false)
# check if this loading was caused by the click on a messages from the app search result # check if this loading was caused by the click on a messages from the app search result
let searchedMessageId = self.controller.getSearchedMessageId() discard self.checkIfMessageLoadedAndScrollToItIfItIs()
if(searchedMessageId.len > 0):
self.switchToMessage(searchedMessageId)
method messageAdded*(self: Module, message: MessageDto) = method messageAdded*(self: Module, message: MessageDto) =
let sender = self.controller.getContactDetails(message.`from`) let sender = self.controller.getContactDetails(message.`from`)
@ -198,6 +211,7 @@ method onSendingMessageError*(self: Module) =
self.view.emitSendingMessageErrorSignal() self.view.emitSendingMessageErrorSignal()
method loadMoreMessages*(self: Module) = method loadMoreMessages*(self: Module) =
self.setLoadingHistoryMessagesInProgress(true)
self.controller.loadMoreMessages() self.controller.loadMoreMessages()
method toggleReaction*(self: Module, messageId: string, emojiId: int) = method toggleReaction*(self: Module, messageId: string, emojiId: int) =
@ -330,4 +344,8 @@ method switchToMessage*(self: Module, messageId: string) =
self.view.emitSwitchToMessageSignal(index) self.view.emitSwitchToMessageSignal(index)
else: else:
self.controller.setSearchedMessageId(messageId) self.controller.setSearchedMessageId(messageId)
method scrollToMessage*(self: Module, messageId: string) =
self.controller.setSearchedMessageId(messageId)
if(not self.checkIfMessageLoadedAndScrollToItIfItIs()):
self.loadMoreMessages() self.loadMoreMessages()

View File

@ -38,5 +38,5 @@ method onMessageEdited*(self: AccessInterface, message: MessageDto) {.base.} =
method setLoadingHistoryMessagesInProgress*(self: AccessInterface, isLoading: bool) {.base.} = method setLoadingHistoryMessagesInProgress*(self: AccessInterface, isLoading: bool) {.base.} =
raise newException(ValueError, "No implementation available") 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") raise newException(ValueError, "No implementation available")

View File

@ -133,4 +133,12 @@ QtObject:
proc switchToMessage(self: View, messageIndex: int) {.signal.} proc switchToMessage(self: View, messageIndex: int) {.signal.}
proc emitSwitchToMessageSignal*(self: View, messageIndex: int) = proc emitSwitchToMessageSignal*(self: View, messageIndex: int) =
self.switchToMessage(messageIndex) 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()

View File

@ -20,7 +20,8 @@ logScope:
topics = "messages-service" topics = "messages-service"
let NEW_LINE = re"\n|\r" #must be defined as let, not const 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" const CURSOR_VALUE_IGNORE = "ignore"
# Signals which may be emitted by this service: # Signals which may be emitted by this service:
@ -271,7 +272,7 @@ QtObject:
self.events.emit(SIGNAL_MESSAGES_LOADED, data) 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): if (chatId.len == 0):
error "empty chat id", methodName="asyncLoadMoreMessagesForChat" error "empty chat id", methodName="asyncLoadMoreMessagesForChat"
return return
@ -297,7 +298,7 @@ QtObject:
chatId: chatId, chatId: chatId,
msgCursor: msgCursor, msgCursor: msgCursor,
pinnedMsgCursor: pinnedMsgCursor, pinnedMsgCursor: pinnedMsgCursor,
limit: MESSAGES_PER_PAGE limit: if(limit <= MESSAGES_PER_PAGE_MAX): limit else: MESSAGES_PER_PAGE_MAX
) )
self.threadpool.start(arg) self.threadpool.start(arg)

View File

@ -48,7 +48,11 @@ Item {
sendingMsgFailedPopup.open(); sendingMsgFailedPopup.open();
} }
onSwitchToMessage: { onScrollMessagesUp: {
chatLogView.positionViewAtEnd()
}
onScrollToMessage: {
chatLogView.positionViewAtIndex(messageIndex, ListView.Center); chatLogView.positionViewAtIndex(messageIndex, ListView.Center);
chatLogView.itemAtIndex(messageIndex).startMessageFoundAnimation(); chatLogView.itemAtIndex(messageIndex).startMessageFoundAnimation();
} }