From 717710c5defe5f5087d5adeeaf66d81fc2d71971 Mon Sep 17 00:00:00 2001 From: Eduard Kuzmenko Date: Sat, 21 Nov 2020 08:37:04 +0200 Subject: [PATCH] Fix reordering chat list Fix dialogFolder on messages.getPinnedDialogs --- src/lib/appManagers/appDialogsManager.ts | 40 ++++++---- src/lib/appManagers/appMessagesManager.ts | 94 +++++++++++++---------- 2 files changed, 81 insertions(+), 53 deletions(-) diff --git a/src/lib/appManagers/appDialogsManager.ts b/src/lib/appManagers/appDialogsManager.ts index 079f22c5..bc699376 100644 --- a/src/lib/appManagers/appDialogsManager.ts +++ b/src/lib/appManagers/appDialogsManager.ts @@ -511,7 +511,7 @@ export class AppDialogsManager { if(this.getDialogDom(dialog.peerID)) { this.setLastMessage(dialog); - this.setDialogPosition(dialog); + this.reorderDialogs(); } } @@ -763,8 +763,8 @@ export class AppDialogsManager { const rect = this.scroll.container.getBoundingClientRect(); const children = Array.from(this.scroll.splitUp.children) as HTMLElement[]; - const firstElement = findUpTag(document.elementFromPoint(rect.x, rect.y + 1), 'LI') as HTMLElement; - const lastElement = findUpTag(document.elementFromPoint(rect.x, rect.y + rect.height - 1), 'LI') as HTMLElement; + const firstElement = findUpTag(document.elementFromPoint(Math.ceil(rect.x), Math.ceil(rect.y + 1)), 'LI') as HTMLElement; + const lastElement = findUpTag(document.elementFromPoint(Math.ceil(rect.x), Math.floor(rect.y + rect.height - 1)), 'LI') as HTMLElement; //alert('got element:' + rect.y); @@ -889,25 +889,39 @@ export class AppDialogsManager { } } - private setDialogPosition(dialog: Dialog) { - const dom = this.getDialogDom(dialog.peerID); - if(!dom) { - return; - } + private reorderDialogs() { + //const perf = performance.now(); - let pos = appMessagesManager.dialogsStorage.getDialog(dialog.peerID, this.filterID)[1]; + let offset = 0; if(this.topOffsetIndex) { const element = this.chatList.firstElementChild; if(element) { const peerID = +element.getAttribute('data-peerID'); const firstDialog = appMessagesManager.getDialogByPeerID(peerID); - pos -= firstDialog[1]; + offset = firstDialog[1]; } } - if(positionElementByIndex(dom.listEl, this.chatList, pos)) { - this.log.debug('setDialogPosition:', dialog, dom, pos); - } + const dialogs = appMessagesManager.dialogsStorage.getFolder(this.filterID); + const currentOrder = (Array.from(this.chatList.children) as HTMLElement[]).map(el => +el.getAttribute('data-peerID')); + + dialogs.forEach((dialog, index) => { + const dom = this.getDialogDom(dialog.peerID); + if(!dom) { + return; + } + + const currentIndex = currentOrder[dialog.peerID]; + const needIndex = index - offset; + + if(currentIndex != needIndex) { + if(positionElementByIndex(dom.listEl, this.chatList, needIndex)) { + this.log.debug('setDialogPosition:', dialog, dom, needIndex); + } + } + }); + + //this.log('Reorder time:', performance.now() - perf); } public setLastMessage(dialog: any, lastMessage?: any, dom?: DialogDom, highlightWord?: string) { diff --git a/src/lib/appManagers/appMessagesManager.ts b/src/lib/appManagers/appMessagesManager.ts index 966ff3a0..e0f1b07c 100644 --- a/src/lib/appManagers/appMessagesManager.ts +++ b/src/lib/appManagers/appMessagesManager.ts @@ -2405,6 +2405,13 @@ export class AppMessagesManager { public applyConversations(dialogsResult: MessagesPeerDialogs.messagesPeerDialogs) { // * В эту функцию попадут только те диалоги, в которых есть read_inbox_max_id и read_outbox_max_id, в отличие от тех, что будут в getTopMessages + // ! fix 'dialogFolder', maybe there is better way to do it, this only can happen by 'messages.getPinnedDialogs' by folder_id: 0 + dialogsResult.dialogs.forEachReverse((dialog, idx) => { + if(dialog._ == 'dialogFolder') { + dialogsResult.dialogs.splice(idx, 1); + } + }); + appUsersManager.saveApiUsers(dialogsResult.users); appChatsManager.saveApiChats(dialogsResult.chats); this.saveMessages(dialogsResult.messages); @@ -2412,7 +2419,6 @@ export class AppMessagesManager { //this.log('applyConversation', dialogsResult); const updatedDialogs: {[peerID: number]: Dialog} = {}; - let hasUpdated = false; (dialogsResult.dialogs as Dialog[]).forEach((dialog) => { const peerID = appPeersManager.getPeerID(dialog.peer); let topMessage = dialog.top_message; @@ -2444,8 +2450,9 @@ export class AppMessagesManager { /* if(wasDialogBefore) { rootScope.$broadcast('dialog_top', dialog); } else { */ - updatedDialogs[peerID] = dialog; - hasUpdated = true; + //if(wasDialogBefore?.top_message != topMessage) { + updatedDialogs[peerID] = dialog; + //} //} } else { const dropped = this.dialogsStorage.dropDialog(peerID); @@ -2464,7 +2471,7 @@ export class AppMessagesManager { } }); - if(hasUpdated) { + if(Object.keys(updatedDialogs).length) { rootScope.broadcast('dialogs_multiupdate', updatedDialogs); } } @@ -3327,6 +3334,41 @@ export class AppMessagesManager { case 'updatePinnedDialogs': { const folderID = update.folder_id ?? 0; + + const handleOrder = (order: number[]) => { + this.dialogsStorage.pinnedOrders[folderID].length = 0; + let willHandle = false; + order.reverse(); // index must be higher + order.forEach((peerID) => { + newPinned[peerID] = true; + + const foundDialog = this.getDialogByPeerID(peerID); + if(!foundDialog.length) { + this.newDialogsToHandle[peerID] = {reload: true}; + willHandle = true; + return; + } + + const dialog = foundDialog[0]; + dialog.pFlags.pinned = true; + this.dialogsStorage.generateIndexForDialog(dialog); + + this.newDialogsToHandle[peerID] = dialog; + willHandle = true; + }); + + this.dialogsStorage.getFolder(folderID).forEach(dialog => { + const peerID = dialog.peerID; + if(dialog.pFlags.pinned && !newPinned[peerID]) { + this.newDialogsToHandle[peerID] = {reload: true}; + willHandle = true; + } + }); + + if(willHandle) { + this.scheduleHandleNewDialogs(); + } + }; this.log('updatePinnedDialogs', update); const newPinned: {[peerID: number]: true} = {}; @@ -3334,10 +3376,14 @@ export class AppMessagesManager { apiManager.invokeApi('messages.getPinnedDialogs', { folder_id: folderID }).then((dialogsResult) => { - dialogsResult.dialogs.reverse(); + // * for test reordering and rendering + // dialogsResult.dialogs.reverse(); + this.applyConversations(dialogsResult); - dialogsResult.dialogs.forEach((dialog) => { + handleOrder(dialogsResult.dialogs.map(d => d.peerID)); + + /* dialogsResult.dialogs.forEach((dialog) => { newPinned[dialog.peerID] = true; }); @@ -3347,7 +3393,7 @@ export class AppMessagesManager { this.newDialogsToHandle[peerID] = {reload: true}; this.scheduleHandleNewDialogs(); } - }); + }); */ }); break; @@ -3355,39 +3401,7 @@ export class AppMessagesManager { //this.log('before order:', this.dialogsStorage[0].map(d => d.peerID)); - this.dialogsStorage.pinnedOrders[folderID].length = 0; - let willHandle = false; - update.order.reverse(); // index must be higher - update.order.forEach((peer: any) => { - const peerID = appPeersManager.getPeerID(peer.peer); - newPinned[peerID] = true; - - const foundDialog = this.getDialogByPeerID(peerID); - if(!foundDialog.length) { - this.newDialogsToHandle[peerID] = {reload: true}; - willHandle = true; - return; - } - - const dialog = foundDialog[0]; - dialog.pFlags.pinned = true; - this.dialogsStorage.generateIndexForDialog(dialog); - - this.newDialogsToHandle[peerID] = dialog; - willHandle = true; - }); - - this.dialogsStorage.getFolder(folderID).forEach(dialog => { - const peerID = dialog.peerID; - if(dialog.pFlags.pinned && !newPinned[peerID]) { - this.newDialogsToHandle[peerID] = {reload: true}; - willHandle = true; - } - }); - - if(willHandle) { - this.scheduleHandleNewDialogs(); - } + handleOrder(update.order.map(peer => appPeersManager.getPeerID((peer as DialogPeer.dialogPeer).peer))); break; }